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