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