View Javadoc
1   /*
2    *    Copyright 2006-2026 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.dom.java;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.Collections;
21  import java.util.List;
22  import java.util.Optional;
23  
24  import org.jspecify.annotations.Nullable;
25  
26  public class Method extends JavaElement {
27  
28      private final List<String> bodyLines = new ArrayList<>();
29  
30      private boolean constructor;
31  
32      private @Nullable FullyQualifiedJavaType returnType;
33  
34      private String name;
35  
36      private final List<TypeParameter> typeParameters = new ArrayList<>();
37  
38      private final List<Parameter> parameters = new ArrayList<>();
39  
40      private final List<FullyQualifiedJavaType> exceptions = new ArrayList<>();
41  
42      private boolean isSynchronized;
43  
44      private boolean isNative;
45  
46      private boolean isDefault;
47  
48      private boolean isAbstract;
49  
50      private boolean isFinal;
51  
52      private final List<String> returnTypeAnnotations = new ArrayList<>();
53  
54      public Method(String name) {
55          this.name = name;
56      }
57  
58      /**
59       * Copy constructor. Not a truly deep copy, but close enough for most purposes.
60       *
61       * @param original
62       *            the original
63       */
64      public Method(Method original) {
65          super(original);
66          this.bodyLines.addAll(original.bodyLines);
67          this.constructor = original.constructor;
68          this.exceptions.addAll(original.exceptions);
69          this.name = original.name;
70          this.typeParameters.addAll(original.typeParameters);
71          this.parameters.addAll(original.parameters);
72          this.returnType = original.returnType;
73          this.isNative = original.isNative;
74          this.isSynchronized = original.isSynchronized;
75          this.isDefault = original.isDefault;
76          this.isAbstract = original.isAbstract;
77          this.isFinal = original.isFinal;
78      }
79  
80      public List<String> getBodyLines() {
81          return bodyLines;
82      }
83  
84      public void addBodyLine(String line) {
85          bodyLines.add(line);
86      }
87  
88      public void addBodyLine(int index, String line) {
89          bodyLines.add(index, line);
90      }
91  
92      public void addBodyLines(Collection<String> lines) {
93          bodyLines.addAll(lines);
94      }
95  
96      public void addBodyLines(int index, Collection<String> lines) {
97          bodyLines.addAll(index, lines);
98      }
99  
100     public boolean isConstructor() {
101         return constructor;
102     }
103 
104     public void setConstructor(boolean constructor) {
105         this.constructor = constructor;
106     }
107 
108     public String getName() {
109         return name;
110     }
111 
112     public void setName(String name) {
113         this.name = name;
114     }
115 
116     public List<TypeParameter> getTypeParameters() {
117         return typeParameters;
118     }
119 
120     public void addTypeParameter(TypeParameter typeParameter) {
121         typeParameters.add(typeParameter);
122     }
123 
124     public void addTypeParameter(int index, TypeParameter typeParameter) {
125         typeParameters.add(index, typeParameter);
126     }
127 
128     public List<Parameter> getParameters() {
129         return Collections.unmodifiableList(parameters);
130     }
131 
132     public void addParameter(Parameter parameter) {
133         parameters.add(parameter);
134     }
135 
136     public void addParameter(int index, Parameter parameter) {
137         parameters.add(index, parameter);
138     }
139 
140     public Optional<FullyQualifiedJavaType> getReturnType() {
141         return Optional.ofNullable(returnType);
142     }
143 
144     public void setReturnType(FullyQualifiedJavaType returnType) {
145         this.returnType = returnType;
146     }
147 
148     public List<FullyQualifiedJavaType> getExceptions() {
149         return Collections.unmodifiableList(exceptions);
150     }
151 
152     public void addException(FullyQualifiedJavaType exception) {
153         exceptions.add(exception);
154     }
155 
156     public boolean isSynchronized() {
157         return isSynchronized;
158     }
159 
160     public void setSynchronized(boolean isSynchronized) {
161         this.isSynchronized = isSynchronized;
162     }
163 
164     public boolean isNative() {
165         return isNative;
166     }
167 
168     public void setNative(boolean isNative) {
169         this.isNative = isNative;
170     }
171 
172     public boolean isDefault() {
173         return isDefault;
174     }
175 
176     public void setDefault(boolean isDefault) {
177         this.isDefault = isDefault;
178     }
179 
180     public boolean isAbstract() {
181         return isAbstract;
182     }
183 
184     public void setAbstract(boolean isAbstract) {
185         this.isAbstract = isAbstract;
186     }
187 
188     public boolean isFinal() {
189         return isFinal;
190     }
191 
192     public void setFinal(boolean isFinal) {
193         this.isFinal = isFinal;
194     }
195 
196     public List<String> getReturnTypeAnnotations() {
197         return Collections.unmodifiableList(returnTypeAnnotations);
198     }
199 
200     public void addReturnTypeAnnotation(String annotation) {
201         returnTypeAnnotations.add(annotation);
202     }
203 }