1
14
15 package com.liferay.portal.tools;
16
17 import com.liferay.portal.kernel.util.StringUtil;
18 import com.liferay.portal.kernel.xml.Document;
19 import com.liferay.portal.kernel.xml.DocumentException;
20 import com.liferay.portal.kernel.xml.Element;
21 import com.liferay.portal.kernel.xml.SAXReaderUtil;
22 import com.liferay.portal.tools.servicebuilder.ServiceBuilder;
23 import com.liferay.portal.util.InitUtil;
24
25 import com.thoughtworks.qdox.JavaDocBuilder;
26 import com.thoughtworks.qdox.model.DocletTag;
27 import com.thoughtworks.qdox.model.JavaClass;
28 import com.thoughtworks.qdox.model.JavaMethod;
29 import com.thoughtworks.qdox.model.JavaParameter;
30 import com.thoughtworks.qdox.model.Type;
31 import com.thoughtworks.qdox.model.TypeVariable;
32
33 import java.io.File;
34 import java.io.IOException;
35
36 import java.util.Iterator;
37 import java.util.LinkedHashSet;
38 import java.util.Set;
39
40
45 public class InstanceWrapperBuilder {
46
47 public static void main(String[] args) {
48 InitUtil.initWithSpring();
49
50 if (args.length == 1) {
51 new InstanceWrapperBuilder(args[0]);
52 }
53 else {
54 throw new IllegalArgumentException();
55 }
56 }
57
58 public InstanceWrapperBuilder(String xml) {
59 try {
60 File file = new File(xml);
61
62 Document doc = null;
63
64 try {
65 doc = SAXReaderUtil.read(file);
66 }
67 catch (DocumentException de) {
68 de.printStackTrace();
69 }
70
71 Element root = doc.getRootElement();
72
73 Iterator<Element> itr = root.elements(
74 "instance-wrapper").iterator();
75
76 while (itr.hasNext()) {
77 Element instanceWrapper = itr.next();
78
79 String parentDir = instanceWrapper.attributeValue("parent-dir");
80 String srcFile = instanceWrapper.attributeValue("src-file");
81
82 _createIW(parentDir, srcFile);
83 }
84 }
85 catch (Exception e) {
86 e.printStackTrace();
87 }
88 }
89
90 private void _createIW(String parentDir, String srcFile)
91 throws IOException {
92
93 JavaClass javaClass = _getJavaClass(parentDir, srcFile);
94
95 JavaMethod[] methods = javaClass.getMethods();
96
97 StringBuilder sb = new StringBuilder();
98
99
101 sb.append("package " + javaClass.getPackage().getName() + ";");
102
103
105 sb.append("public class " + javaClass.getName() + "_IW {");
106
107
109 sb.append("public static " + javaClass.getName() + "_IW getInstance() {");
110 sb.append("return _instance;");
111 sb.append("}\n");
112
113 for (int i = 0; i < methods.length; i++) {
114 JavaMethod javaMethod = methods[i];
115
116 String methodName = javaMethod.getName();
117
118 if (!javaMethod.isPublic() || !javaMethod.isStatic()) {
119 continue;
120 }
121
122 if (methodName.equals("getInstance")) {
123 methodName = "getWrappedInstance";
124 }
125
126 DocletTag[] docletTags = javaMethod.getTagsByName("deprecated");
127
128 if ((docletTags != null) && (docletTags.length > 0)) {
129 sb.append("\t/**\n");
130 sb.append("\t * @deprecated\n");
131 sb.append("\t */\n");
132 }
133
134 sb.append("public ");
135
136 TypeVariable[] typeParameters = javaMethod.getTypeParameters();
137
138 if (typeParameters.length > 0) {
139 sb.append(" " + typeParameters[0].getGenericValue() + " ");
140 }
141
142 sb.append(_getTypeGenericsName(javaMethod.getReturns()) + " " + methodName + "(");
143
144 JavaParameter[] parameters = javaMethod.getParameters();
145
146 for (int j = 0; j < parameters.length; j++) {
147 JavaParameter javaParameter = parameters[j];
148
149 sb.append(_getTypeGenericsName(javaParameter.getType()));
150
151 if (javaParameter.isVarArgs()) {
152 sb.append("...");
153 }
154
155 sb.append(" " + javaParameter.getName());
156
157 if ((j + 1) != parameters.length) {
158 sb.append(", ");
159 }
160 }
161
162 sb.append(")");
163
164 Type[] thrownExceptions = javaMethod.getExceptions();
165
166 Set<String> newExceptions = new LinkedHashSet<String>();
167
168 for (int j = 0; j < thrownExceptions.length; j++) {
169 Type thrownException = thrownExceptions[j];
170
171 newExceptions.add(thrownException.getValue());
172 }
173
174 if (newExceptions.size() > 0) {
175 sb.append(" throws ");
176
177 Iterator<String> itr = newExceptions.iterator();
178
179 while (itr.hasNext()) {
180 sb.append(itr.next());
181
182 if (itr.hasNext()) {
183 sb.append(", ");
184 }
185 }
186 }
187
188 sb.append("{\n");
189
190 if (!javaMethod.getReturns().getValue().equals("void")) {
191 sb.append("return ");
192 }
193
194 sb.append(javaClass.getName() + "." + javaMethod.getName() + "(");
195
196 for (int j = 0; j < parameters.length; j++) {
197 JavaParameter javaParameter = parameters[j];
198
199 sb.append(javaParameter.getName());
200
201 if ((j + 1) != parameters.length) {
202 sb.append(", ");
203 }
204 }
205
206 sb.append(");");
207 sb.append("}\n");
208 }
209
210
212 sb.append("private " + javaClass.getName() + "_IW() {");
213 sb.append("}");
214
215
217 sb.append("private static " + javaClass.getName() + "_IW _instance = new " + javaClass.getName() + "_IW();");
218
219
221 sb.append("}");
222
223
225 File file = new File(parentDir + "/" + StringUtil.replace(javaClass.getPackage().getName(), ".", "/") + "/" + javaClass.getName() + "_IW.java");
226
227 ServiceBuilder.writeFile(file, sb.toString());
228 }
229
230 private String _getDimensions(Type type) {
231 String dimensions = "";
232
233 for (int i = 0; i < type.getDimensions(); i++) {
234 dimensions += "[]";
235 }
236
237 return dimensions;
238 }
239
240 private JavaClass _getJavaClass(String parentDir, String srcFile)
241 throws IOException {
242
243 String className = StringUtil.replace(
244 srcFile.substring(0, srcFile.length() - 5), "/", ".");
245
246 JavaDocBuilder builder = new JavaDocBuilder();
247
248 builder.addSource(new File(parentDir + "/" + srcFile));
249
250 return builder.getClassByName(className);
251 }
252
253 private String _getTypeGenericsName(Type type) {
254 StringBuilder sb = new StringBuilder();
255
256 sb.append(type.getValue());
257
258 Type[] actualTypeArguments = type.getActualTypeArguments();
259
260 if (actualTypeArguments != null) {
261 sb.append("<");
262
263 for (int i = 0; i < actualTypeArguments.length; i++) {
264 if (i > 0) {
265 sb.append(", ");
266 }
267
268 sb.append(_getTypeGenericsName(actualTypeArguments[i]));
269 }
270
271 sb.append(">");
272 }
273
274 sb.append(_getDimensions(type));
275
276 return sb.toString();
277 }
278
279 }