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