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