1
22
23 package com.liferay.portal.tools;
24
25 import com.liferay.portal.kernel.util.StringUtil;
26 import com.liferay.portal.tools.comparator.JavaMethodComparator;
27 import com.liferay.portal.tools.servicebuilder.ServiceBuilder;
28 import com.liferay.portal.util.InitUtil;
29 import com.liferay.util.TextFormatter;
30
31 import com.thoughtworks.qdox.JavaDocBuilder;
32 import com.thoughtworks.qdox.model.JavaClass;
33 import com.thoughtworks.qdox.model.JavaMethod;
34 import com.thoughtworks.qdox.model.JavaParameter;
35 import com.thoughtworks.qdox.model.Type;
36
37 import java.io.File;
38 import java.io.IOException;
39
40 import java.util.Arrays;
41 import java.util.Iterator;
42 import java.util.LinkedHashSet;
43 import java.util.Set;
44 import java.util.TreeSet;
45
46
52 public class CopyInterfaceBuilder {
53
54 public static void main(String[] args) {
55 InitUtil.initWithSpring();
56
57 if (args.length == 2) {
58 new CopyInterfaceBuilder(args[0], args[1]);
59 }
60 else {
61 throw new IllegalArgumentException();
62 }
63 }
64
65 public CopyInterfaceBuilder(String parentDir, String srcFile) {
66 try {
67 _copyInterface(parentDir, srcFile);
68 }
69 catch (Exception e) {
70 e.printStackTrace();
71 }
72 }
73
74 private void _copyInterface(String parentDir, String srcFile)
75 throws IOException {
76
77 JavaClass javaClass = _getJavaClass(parentDir, srcFile);
78
79 JavaMethod[] methods = javaClass.getMethods();
80
81 Arrays.sort(methods, new JavaMethodComparator());
82
83 StringBuilder sb = new StringBuilder();
84
85
87 sb.append("package " + javaClass.getPackage() + ";");
88
89
91 sb.append("[$IMPORTS$]");
92
93
95 sb.append("public class Copy" + javaClass.getName() + " implements " + javaClass.getName() + " {");
96
97 String varName = "_" + TextFormatter.format(javaClass.getName(), TextFormatter.I);
98
99
101 Set<String> imports = new TreeSet<String>();
102
103 for (int i = 0; i < methods.length; i++) {
104 JavaMethod javaMethod = methods[i];
105
106 String methodName = javaMethod.getName();
107
108 if (javaMethod.isPublic()) {
109 String returnValueName = javaMethod.getReturns().getValue();
110
111 imports.add(returnValueName);
112
113 sb.append("public " + javaMethod.getReturns().getJavaClass().getName() + _getDimensions(javaMethod.getReturns()) + " " + methodName + "(");
114
115 JavaParameter[] parameters = javaMethod.getParameters();
116
117 for (int j = 0; j < parameters.length; j++) {
118 JavaParameter javaParameter = parameters[j];
119
120 sb.append(javaParameter.getType().getJavaClass().getName() + _getDimensions(javaParameter.getType()) + " " + javaParameter.getName());
121
122 imports.add(javaParameter.getType().getValue());
123
124 if ((j + 1) != parameters.length) {
125 sb.append(", ");
126 }
127 }
128
129 sb.append(")");
130
131 Type[] thrownExceptions = javaMethod.getExceptions();
132
133 Set<String> newExceptions = new LinkedHashSet<String>();
134
135 for (int j = 0; j < thrownExceptions.length; j++) {
136 Type thrownException = thrownExceptions[j];
137
138 newExceptions.add(thrownException.getJavaClass().getName());
139
140 imports.add(thrownException.getValue());
141 }
142
143 if (newExceptions.size() > 0) {
144 sb.append(" throws ");
145
146 Iterator<String> itr = newExceptions.iterator();
147
148 while (itr.hasNext()) {
149 sb.append(itr.next());
150
151 if (itr.hasNext()) {
152 sb.append(", ");
153 }
154 }
155 }
156
157 sb.append("{");
158
159 if (!returnValueName.equals("void")) {
160 sb.append("return ");
161 }
162
163 sb.append(varName + "." + methodName + "(");
164
165 for (int j = 0; j < parameters.length; j++) {
166 JavaParameter javaParameter = parameters[j];
167
168 sb.append(javaParameter.getName());
169
170 if ((j + 1) != parameters.length) {
171 sb.append(", ");
172 }
173 }
174
175 sb.append(");");
176 sb.append("}");
177 }
178 }
179
180
182 sb.append("private " + javaClass.getName() + " " + varName + ";");
183
184
186 sb.append("}");
187
188
190 String content = sb.toString();
191
192 sb = new StringBuilder();
193
194 Iterator<String> itr = imports.iterator();
195
196 while (itr.hasNext()) {
197 String importClass = itr.next();
198
199 if (!importClass.equals("boolean") && !importClass.equals("double") && !importClass.equals("int") && !importClass.equals("long") && !importClass.equals("short") && !importClass.equals("void")) {
200 sb.append("import " + importClass + ";");
201 }
202 }
203
204 content = StringUtil.replace(content, "[$IMPORTS$]", sb.toString());
205
206
208 File file = new File(parentDir + "/" + StringUtil.replace(javaClass.getPackage(), ".", "/") + "/Copy" + javaClass.getName() + ".java");
209
210 ServiceBuilder.writeFile(file, content);
211 }
212
213 private String _getDimensions(Type type) {
214 String dimensions = "";
215
216 for (int i = 0; i < type.getDimensions(); i++) {
217 dimensions += "[]";
218 }
219
220 return dimensions;
221 }
222
223 private JavaClass _getJavaClass(String parentDir, String srcFile)
224 throws IOException {
225
226 String className = StringUtil.replace(
227 srcFile.substring(0, srcFile.length() - 5), "/", ".");
228
229 JavaDocBuilder builder = new JavaDocBuilder();
230
231 builder.addSource(new File(parentDir + "/" + srcFile));
232
233 return builder.getClassByName(className);
234 }
235
236 }