001
014
015 package com.liferay.portal.tools;
016
017 import com.liferay.portal.kernel.util.ArrayUtil;
018 import com.liferay.portal.kernel.util.StringBundler;
019 import com.liferay.portal.kernel.util.StringPool;
020 import com.liferay.portal.kernel.util.StringUtil;
021 import com.liferay.portal.kernel.xml.Document;
022 import com.liferay.portal.kernel.xml.Element;
023 import com.liferay.portal.kernel.xml.UnsecureSAXReaderUtil;
024
025 import com.thoughtworks.qdox.JavaDocBuilder;
026 import com.thoughtworks.qdox.model.DocletTag;
027 import com.thoughtworks.qdox.model.JavaClass;
028 import com.thoughtworks.qdox.model.JavaMethod;
029 import com.thoughtworks.qdox.model.JavaParameter;
030 import com.thoughtworks.qdox.model.Type;
031 import com.thoughtworks.qdox.model.TypeVariable;
032
033 import java.io.File;
034 import java.io.IOException;
035
036 import java.util.LinkedHashSet;
037 import java.util.List;
038 import java.util.Set;
039
040
043 public class InstanceWrapperBuilder {
044
045 public static void main(String[] args) {
046 ToolDependencies.wireBasic();
047
048 if (args.length == 1) {
049 new InstanceWrapperBuilder(args[0]);
050 }
051 else {
052 throw new IllegalArgumentException();
053 }
054 }
055
056 public InstanceWrapperBuilder(String xml) {
057 try {
058 File file = new File(xml);
059
060 Document document = UnsecureSAXReaderUtil.read(file);
061
062 Element rootElement = document.getRootElement();
063
064 List<Element> instanceWrapperElements = rootElement.elements(
065 "instance-wrapper");
066
067 for (Element instanceWrapperElement : instanceWrapperElements) {
068 String parentDir = instanceWrapperElement.attributeValue(
069 "parent-dir");
070 String srcFile = instanceWrapperElement.attributeValue(
071 "src-file");
072
073 _createIW(parentDir, srcFile);
074 }
075 }
076 catch (Exception e) {
077 e.printStackTrace();
078 }
079 }
080
081 private void _createIW(String parentDir, String srcFile)
082 throws IOException {
083
084 JavaClass javaClass = _getJavaClass(parentDir, srcFile);
085
086 JavaMethod[] javaMethods = javaClass.getMethods();
087
088 StringBundler sb = new StringBundler();
089
090
091
092 sb.append("package ");
093 sb.append(javaClass.getPackage().getName());
094 sb.append(";");
095
096
097
098 sb.append("public class ");
099 sb.append(javaClass.getName());
100 sb.append("_IW {");
101
102
103
104 sb.append("public static ");
105 sb.append(javaClass.getName());
106 sb.append("_IW getInstance() {");
107 sb.append("return _instance;");
108 sb.append("}\n");
109
110 for (JavaMethod javaMethod : javaMethods) {
111 String methodName = javaMethod.getName();
112
113 if (!javaMethod.isPublic() || !javaMethod.isStatic()) {
114 continue;
115 }
116
117 if (methodName.equals("getInstance")) {
118 methodName = "getWrappedInstance";
119 }
120
121 DocletTag[] docletTags = javaMethod.getTagsByName("deprecated");
122
123 if (ArrayUtil.isNotEmpty(docletTags)) {
124 sb.append("\t\n");
127 sb.append("\t@Deprecated\n");
128 }
129
130 sb.append("public ");
131
132 TypeVariable[] typeParameters = javaMethod.getTypeParameters();
133
134 if (typeParameters.length > 0) {
135 sb.append(" <");
136
137 for (int i = 0; i < typeParameters.length; i++) {
138 TypeVariable typeParameter = typeParameters[i];
139
140 sb.append(typeParameter.getName());
141 sb.append(", ");
142 }
143
144 sb.setIndex(sb.index() - 1);
145
146 sb.append("> ");
147 }
148
149 sb.append(_getTypeGenericsName(javaMethod.getReturnType()));
150 sb.append(" ");
151 sb.append(methodName);
152 sb.append(StringPool.OPEN_PARENTHESIS);
153
154 JavaParameter[] javaParameters = javaMethod.getParameters();
155
156 for (int i = 0; i < javaParameters.length; i++) {
157 JavaParameter javaParameter = javaParameters[i];
158
159 sb.append(_getTypeGenericsName(javaParameter.getType()));
160
161 if (javaParameter.isVarArgs()) {
162 sb.append("...");
163 }
164
165 sb.append(" ");
166 sb.append(javaParameter.getName());
167 sb.append(", ");
168 }
169
170 if (javaParameters.length > 0) {
171 sb.setIndex(sb.index() - 1);
172 }
173
174 sb.append(StringPool.CLOSE_PARENTHESIS);
175
176 Type[] thrownExceptions = javaMethod.getExceptions();
177
178 Set<String> newExceptions = new LinkedHashSet<>();
179
180 for (int j = 0; j < thrownExceptions.length; j++) {
181 Type thrownException = thrownExceptions[j];
182
183 newExceptions.add(thrownException.getValue());
184 }
185
186 if (!newExceptions.isEmpty()) {
187 sb.append(" throws ");
188
189 for (String newException : newExceptions) {
190 sb.append(newException);
191 sb.append(", ");
192 }
193
194 sb.setIndex(sb.index() - 1);
195 }
196
197 sb.append("{\n");
198
199 if (!javaMethod.getReturnType().getValue().equals("void")) {
200 sb.append("return ");
201 }
202
203 sb.append(javaClass.getName());
204 sb.append(".");
205 sb.append(javaMethod.getName());
206 sb.append("(");
207
208 for (int j = 0; j < javaParameters.length; j++) {
209 JavaParameter javaParameter = javaParameters[j];
210
211 sb.append(javaParameter.getName());
212 sb.append(", ");
213 }
214
215 if (javaParameters.length > 0) {
216 sb.setIndex(sb.index() - 1);
217 }
218
219 sb.append(");");
220 sb.append("}\n");
221 }
222
223
224
225 sb.append("private ");
226 sb.append(javaClass.getName());
227 sb.append("_IW() {");
228 sb.append("}");
229
230
231
232 sb.append("private static ");
233 sb.append(javaClass.getName());
234 sb.append("_IW _instance = new ");
235 sb.append(javaClass.getName());
236 sb.append("_IW();");
237
238
239
240 sb.append("}");
241
242
243
244 File file = new File(
245 parentDir + "/" +
246 StringUtil.replace(javaClass.getPackage().getName(), ".", "/") +
247 "/" + javaClass.getName() + "_IW.java");
248
249 ToolsUtil.writeFile(file, sb.toString(), null);
250 }
251
252 private String _getDimensions(Type type) {
253 String dimensions = "";
254
255 for (int i = 0; i < type.getDimensions(); i++) {
256 dimensions += "[]";
257 }
258
259 return dimensions;
260 }
261
262 private JavaClass _getJavaClass(String parentDir, String srcFile)
263 throws IOException {
264
265 String className = StringUtil.replace(
266 srcFile.substring(0, srcFile.length() - 5), "/", ".");
267
268 JavaDocBuilder builder = new JavaDocBuilder();
269
270 builder.addSource(new File(parentDir + "/" + srcFile));
271
272 return builder.getClassByName(className);
273 }
274
275 private String _getTypeGenericsName(Type type) {
276 Type[] actualTypeArguments = type.getActualTypeArguments();
277
278 if (actualTypeArguments == null) {
279 String value = type.getValue();
280
281 return value.concat(_getDimensions(type));
282 }
283
284 StringBundler sb = new StringBundler(
285 actualTypeArguments.length * 2 + 3);
286
287 sb.append(type.getValue());
288 sb.append("<");
289
290 for (int i = 0; i < actualTypeArguments.length; i++) {
291 sb.append(_getTypeGenericsName(actualTypeArguments[i]));
292 sb.append(", ");
293 }
294
295 if (actualTypeArguments.length > 0) {
296 sb.setIndex(sb.index() - 1);
297 }
298
299 sb.append(">");
300 sb.append(_getDimensions(type));
301
302 return sb.toString();
303 }
304
305 }