View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.audit.generator;
18  
19  import java.lang.reflect.Method;
20  import java.util.ArrayList;
21  import java.util.Iterator;
22  import java.util.List;
23  
24  import org.apache.logging.log4j.audit.util.NamingUtils;
25  import org.apache.logging.log4j.audit.util.StringUtil;
26  import static org.apache.logging.log4j.audit.generator.Constants.PUBLIC;
27  
28  public class MethodDefinition implements Comparable<MethodDefinition> {
29  
30      private String visibility = PUBLIC;
31  
32      private String name;
33  
34      private String returnType;
35  
36      private String annotation = null;
37  
38      private boolean isStatic = false;
39  
40      private boolean isFinal = false;
41  
42      private boolean isAbstract = false;
43  
44      private boolean isInterface = false;
45  
46      private List<Parameter> parameters = new ArrayList<>();
47  
48      private List<String> exceptions = new ArrayList<>();
49  
50      private String content;
51  
52      private String javadocComments = null;
53  
54      public class StandardSingleton extends MethodDefinition {
55          /**
56           * this must be used with the local variable
57           */
58          public StandardSingleton(String returnType, String name, List<String> parameters) {
59              super(returnType, name);
60              setStatic(true);
61              String prefix = "get";
62              setName(prefix + name.substring(0, 1).toUpperCase()
63                      + name.substring(1));
64              StringBuilder sb = new StringBuilder();
65              sb.append("\tif (").append(name).append(" == null) {\n");
66              sb.append("\t\t").append(name).append(" = new ").append(returnType)
67                      .append("(");
68              boolean first = true;
69              if (parameters != null) {
70                  for (Iterator<String> iter = parameters.iterator(); iter.hasNext(); ) {
71                      String element = iter.next();
72                      if (!first) {
73                          sb.append(", ");
74                      }
75                      sb.append(element);
76                      first = false;
77                  }
78              }
79              sb.append(");\n\t}\n\treturn ").append(name).append(";");
80              setContent(sb.toString());
81          }
82      }
83  
84      private static MethodDefinition definition = new MethodDefinition("dumb",
85              "dumb");
86  
87      public static MethodDefinition getStandardSingleton(String returnType,
88                                                          String name, List<String> parameters) {
89          return definition.new StandardSingleton(returnType, name, parameters);
90      }
91  
92      public MethodDefinition(String returnType, String name, String content) {
93          this.returnType = returnType;
94          this.name = name;
95          if (content != null) {
96              this.content = StringUtil.filterContent(content, name, returnType);
97          } else {
98              createStubContent();
99          }
100     }
101 
102     public MethodDefinition(String returnType, String name) {
103         this(returnType, name, null);
104     }
105 
106     private void createStubContent() {
107         String content = "// default stub - please modify\n";
108         setContent(content);
109         if (!returnType.equals("void")) {
110 
111             if (returnType.equals("int")) {
112                 setContent(content + "return 0;");
113             } else if (returnType.equals("boolean")) {
114                 setContent(content + "return false;");
115             } else if (returnType.equals("double")) {
116                 setContent(content + "return 0.0;");
117             } else if (returnType.equals("long")) {
118                 setContent(content + "return 0;");
119             } else if (returnType.equals("float")) {
120                 setContent(content + "return 0.0;");
121             } else if (returnType.equals("float")) {
122                 setContent(content + "return 0.0;");
123             } else if (returnType.equals("char")) {
124                 setContent(content + "return ' ';");
125             } else if (returnType.equals("short")) {
126                 setContent(content + "return 0;");
127             } else {
128                 setContent(content + "return null;");
129             }
130         }
131 
132     }
133 
134     public MethodDefinition(Method method) {
135         this(method, null);
136     }
137 
138     public MethodDefinition(Method method, String content) {
139         this.returnType = method.getReturnType().getName();
140         this.name = method.getName();
141 
142         if (content == null) {
143             createStubContent();
144         } else {
145             this.content = content;
146         }
147         int pName = 'a';
148         for (Class<?> param : method.getParameterTypes()) {
149             addParameter(new Parameter(Character.toString((char) pName++),
150                     param.getName(), ""));
151         }
152 
153         for (Class<?> param : method.getExceptionTypes()) {
154             exceptions.add(param.getName());
155         }
156     }
157 
158     public void addParameter(Parameter paramater) {
159         parameters.add(paramater);
160     }
161 
162     public String getAnnotation() {
163         return annotation;
164     }
165 
166     public String getContent() {
167         return content;
168     }
169 
170     public List<String> getExceptions() {
171         return exceptions;
172     }
173 
174     public String getName() {
175         return name;
176     }
177 
178     public List<Parameter> getParameters() {
179         return parameters;
180     }
181 
182     public String getReturnType() {
183         return returnType;
184     }
185 
186     public String getVisability() {
187         return visibility;
188     }
189 
190     public boolean isAbstract() {
191         return isAbstract;
192     }
193 
194     public boolean isFinal() {
195         return isFinal;
196     }
197 
198     public boolean isInterface() {
199         return isInterface;
200     }
201 
202     public boolean isStatic() {
203         return isStatic;
204     }
205 
206     public void setAbstract(boolean isAbstract) {
207         this.isAbstract = isAbstract;
208     }
209 
210     public void setAnnotation(String annotation) {
211         this.annotation = annotation;
212     }
213 
214     public void setContent(String content) {
215         this.content = content;
216     }
217 
218     public void setExceptions(List<String> exceptions) {
219         this.exceptions = exceptions;
220     }
221 
222     public void setFinal(boolean isFinal) {
223         this.isFinal = isFinal;
224     }
225 
226     public void setInterface(boolean isInterface) {
227         this.isInterface = isInterface;
228     }
229 
230     public void setName(String name) {
231         this.name = name;
232     }
233 
234     public void setReturnType(String returnType) {
235         this.returnType = returnType;
236     }
237 
238     public void setStatic(boolean isStatic) {
239         this.isStatic = isStatic;
240     }
241 
242     public void setVisability(String visability) {
243         this.visibility = visability;
244     }
245 
246     @Override
247     public String toString() {
248         StringBuilder sb = new StringBuilder();
249 
250         sb.append("    /**\n");
251         if (getJavadocComments() != null) {
252             sb.append("     * ").append(getJavadocComments());
253         }
254 
255         if (getParameters() != null) {
256             for (Parameter param : getParameters()) {
257                 sb.append("\n     * @param ").append(param.getName())
258                         .append(" ").append(param.getDescription());
259             }
260         }
261 
262         sb.append("\n     */\n");
263         sb.append("    ");
264         if (getAnnotation() != null) {
265             sb.append(getAnnotation());
266             sb.append("\n    ");
267         }
268 
269         if (getVisability() != null) {
270             sb.append(getVisability()).append(" ");
271         }
272         if (isFinal() && !isInterface()) {
273             sb.append("final ");
274         }
275         if (isStatic() && !isInterface()) {
276             sb.append("static ");
277         }
278         if (isAbstract() && !isInterface()) {
279             sb.append("abstract ");
280         }
281         sb.append(returnType).append(" ");
282         sb.append(getName()).append("(");
283         if (getParameters() != null) {
284             boolean first = true;
285             for (Parameter element : getParameters()) {
286                 if (!first) {
287                     sb.append(", ");
288                 }
289                 sb.append(element);
290                 first = false;
291             }
292         }
293         sb.append(")");
294         if (getExceptions() != null && getExceptions().size() > 0) {
295             sb.append(" throws ");
296             boolean first = true;
297             for (String element : getExceptions()) {
298                 if (!first) {
299                     sb.append(", ");
300                 }
301                 sb.append(element);
302                 first = false;
303             }
304         }
305 
306         if (isAbstract() || isInterface()) {
307             sb.append(";");
308             return sb.toString();
309         }
310         sb.append(" {\n");
311         sb.append(getContent());
312         sb.append("\n}");
313         return sb.toString();
314     }
315 
316     @Override
317     public int compareTo(MethodDefinition arg0) {
318         int res = NamingUtils.getMethodShortName(getName()).compareTo(
319                 NamingUtils.getMethodShortName(arg0.getName()));
320         if (res == 0) {
321             return getName().compareTo(arg0.getName());
322         }
323         return res;
324     }
325 
326     public String getJavadocComments() {
327         return javadocComments;
328     }
329 
330     public void setJavadocComments(String javadocComments) {
331         this.javadocComments = javadocComments;
332     }
333 
334 }