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