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 org.apache.logging.log4j.audit.util.NamingUtils;
20  import org.apache.logging.log4j.audit.util.StringUtil;
21  
22  public class AccessorDefinition {
23  
24  	public class StandardGetter extends MethodDefinition {
25  		public StandardGetter(AccessorDefinition beanDefinition) {
26  			super(beanDefinition.getType(), NamingUtils.getAccessorName(
27  					beanDefinition.getType(), beanDefinition.getName()));
28  			if (getterContent != null) {
29  				setContent("\t" + StringUtil.filterContent(getterContent, name, type));
30  			} else {
31  				StringBuilder sb = new StringBuilder();
32  				sb.append("\treturn ").append(beanDefinition.getName()).append(";");
33  				setContent(sb.toString());
34  			}
35  		}
36  	}
37  
38  	public class StandardSetter extends MethodDefinition {
39  		public StandardSetter(AccessorDefinition beanDefinition) {
40  			super(beanDefinition.getType(), NamingUtils
41  					.getMutatorName(beanDefinition.getName()));
42  			setReturnType("void");
43  			if (setterContent != null) {
44  				setContent("\t"
45  						+ StringUtil.filterContent(setterContent, name, type));
46  			} else {
47  				StringBuilder sb = new StringBuilder();
48  				sb.append("\tthis.").append(beanDefinition.getName())
49  						.append(" = ").append(beanDefinition.getName()).append(";");
50  				setContent(sb.toString());
51  			}
52  			getParameters().add(new Parameter(beanDefinition.getName(), beanDefinition.getType(), ""));
53  		}
54  	}
55  
56  	public static String variableCaseName(String variable) {
57  		return variable.substring(0, 1).toLowerCase() + variable.substring(1);
58  	}
59  
60  	private String name;
61  	private String type;
62  	private String packageName = null;
63  
64  	private String annotation = null;
65  
66  	private String setterContent;
67  	private String getterContent;
68  
69  	public void setSetterContent(String setterContent) {
70  		this.setterContent = setterContent;
71  	}
72  
73  	public void setGetterContent(String getterContent) {
74  		this.getterContent = getterContent;
75  	}
76  
77  	public AccessorDefinition(String name, String type) {
78  		this(name, type, null, null);
79  	}
80  
81  	public AccessorDefinition(String name, String type, String setterContent, String getterContent) {
82  
83  		setName(NamingUtils.getFieldName(name));
84  		setType(type);
85  		setSetterContent(setterContent);
86  		setGetterContent(getterContent);
87  	}
88  
89  	public void addBean(ClassGenerator generator) {
90  		addBean(generator, true, true, true);
91  	}
92  
93  	public void addBean(ClassGenerator generator, boolean addLocalVariable, boolean addGetter, boolean addSetter) {
94  
95  		if (generator.isClass() && addLocalVariable) {
96  			generator.addLocalVariable(new VariableDefinition("private",
97  					getType(), getName(), null, getAnnotation()));
98  		}
99  
100 		if (packageName != null) {
101 			generator.getImports().add(packageName);
102 		}
103 		if (addGetter) {
104 			MethodDefinition methodDefinition = new StandardGetter(this);
105 			methodDefinition.setInterface(!generator.isClass());
106 			generator.addBeanMethods(this);
107 			generator.addMethod(methodDefinition);
108 		}
109 		if (addSetter) {
110 			MethodDefinition methodDefinition = new StandardSetter(this);
111 			methodDefinition.setInterface(!generator.isClass());
112 			generator.addMethod(methodDefinition);
113 		}
114 	}
115 
116 	private String extractPackageName(String variable) {
117 		int lastDot = variable.lastIndexOf('.');
118 		if (lastDot < 0) {
119 			return null;
120 		}
121 		return variable;
122 	}
123 
124 	public String getAnnotation() {
125 		return annotation;
126 	}
127 
128 	public String getName() {
129 		return name;
130 	}
131 
132 	public String getType() {
133 		return type;
134 	}
135 
136 	public void setAnnotation(String annotation) {
137 		this.annotation = annotation;
138 	}
139 
140 	public void setName(String name) {
141 		this.name = variableCaseName(name);
142 	}
143 
144 	public void setType(String type) {
145 		this.packageName = extractPackageName(type);
146 		int lastDot = type.lastIndexOf('.');
147 		this.type = type;
148 		if (lastDot >= 0) {
149 			this.type = type.substring(lastDot + 1);
150 		}
151 	}
152 
153 }