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.runtime.dynamic.sql.elements;
17  
18  import org.mybatis.generator.api.IntrospectedColumn;
19  import org.mybatis.generator.api.IntrospectedTable;
20  import org.mybatis.generator.api.dom.java.Interface;
21  import org.mybatis.generator.api.dom.java.Method;
22  import org.mybatis.generator.api.dom.java.Parameter;
23  import org.mybatis.generator.config.Context;
24  
25  public abstract class AbstractMethodGenerator {
26      protected final Context context;
27      protected final IntrospectedTable introspectedTable;
28      protected final String tableFieldName;
29  
30      protected AbstractMethodGenerator(BaseBuilder<?> builder) {
31          context = builder.context;
32          introspectedTable = builder.introspectedTable;
33          tableFieldName = builder.tableFieldName;
34      }
35  
36      protected String calculateFieldName(IntrospectedColumn column) {
37          return calculateFieldName(tableFieldName, column);
38      }
39  
40      public static String calculateFieldName(String tableFieldName, IntrospectedColumn column) {
41          String fieldName = column.getJavaProperty();
42          if (fieldName.equals(tableFieldName)) {
43              // name collision, no shortcut generated
44              fieldName = tableFieldName + "." + fieldName; //$NON-NLS-1$
45          }
46          return fieldName;
47      }
48  
49      protected void acceptParts(MethodAndImports.Builder builder, Method method, MethodParts methodParts) {
50          for (Parameter parameter : methodParts.getParameters()) {
51              method.addParameter(parameter);
52          }
53  
54          for (String annotation : methodParts.getAnnotations()) {
55              method.addAnnotation(annotation);
56          }
57  
58          method.addBodyLines(methodParts.getBodyLines());
59          builder.withImports(methodParts.getImports());
60      }
61  
62      public abstract MethodAndImports generateMethodAndImports();
63  
64      public abstract boolean callPlugins(Method method, Interface interfaze);
65  
66      public abstract static class BaseBuilder<T extends BaseBuilder<T>> {
67          private Context context;
68          private IntrospectedTable introspectedTable;
69          private String tableFieldName;
70  
71          public T withContext(Context context) {
72              this.context = context;
73              return getThis();
74          }
75  
76          public T withIntrospectedTable(IntrospectedTable introspectedTable) {
77              this.introspectedTable = introspectedTable;
78              return getThis();
79          }
80  
81          public T withTableFieldName(String tableFieldName) {
82              this.tableFieldName = tableFieldName;
83              return getThis();
84          }
85  
86          public abstract T getThis();
87      }
88  }