View Javadoc
1   /*
2    *    Copyright 2006-2023 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.mybatis.generator.api;
17  
18  import org.mybatis.generator.api.dom.java.CompilationUnit;
19  
20  public class GeneratedJavaFile extends GeneratedFile {
21  
22      private final CompilationUnit compilationUnit;
23  
24      private final String fileEncoding;
25  
26      private final JavaFormatter javaFormatter;
27  
28      public GeneratedJavaFile(CompilationUnit compilationUnit,
29              String targetProject,
30              String fileEncoding,
31              JavaFormatter javaFormatter) {
32          super(targetProject);
33          this.compilationUnit = compilationUnit;
34          this.fileEncoding = fileEncoding;
35          this.javaFormatter = javaFormatter;
36      }
37  
38      public GeneratedJavaFile(CompilationUnit compilationUnit,
39              String targetProject,
40              JavaFormatter javaFormatter) {
41          this(compilationUnit, targetProject, null, javaFormatter);
42      }
43  
44      @Override
45      public String getFormattedContent() {
46          return javaFormatter.getFormattedContent(compilationUnit);
47      }
48  
49      @Override
50      public String getFileName() {
51          return compilationUnit.getType().getShortNameWithoutTypeArguments() + ".java"; //$NON-NLS-1$
52      }
53  
54      @Override
55      public String getTargetPackage() {
56          return compilationUnit.getType().getPackageName();
57      }
58  
59      /**
60       * This method is required by the Eclipse Java merger. If you are not
61       * running in Eclipse, or some other system that implements the Java merge
62       * function, you may return null from this method.
63       *
64       * @return the CompilationUnit associated with this file, or null if the
65       *         file is not mergeable.
66       */
67      public CompilationUnit getCompilationUnit() {
68          return compilationUnit;
69      }
70  
71      /**
72       * A Java file is mergeable if the getCompilationUnit() method returns a valid compilation unit.
73       *
74       * @return true, if is mergeable
75       */
76      @Override
77      public boolean isMergeable() {
78          return true;
79      }
80  
81      @Override
82      public String getFileEncoding() {
83          return fileEncoding;
84      }
85  }