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