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