1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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  /**
46   * <a href="CopyInterfaceBuilder.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   *
50   */
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          // Package
83  
84          sb.append("package " + javaClass.getPackage() + ";");
85  
86          // Imports
87  
88          sb.append("[$IMPORTS$]");
89  
90          // Class declaration
91  
92          sb.append("public class Copy" + javaClass.getName() + " implements " + javaClass.getName() + " {");
93  
94          String varName = "_" + TextFormatter.format(javaClass.getName(), TextFormatter.I);
95  
96          // Methods
97  
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         // Fields
178 
179         sb.append("private " + javaClass.getName() + " " + varName + ";");
180 
181         // Class close brace
182 
183         sb.append("}");
184 
185         // Imports
186 
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         // Write file
204 
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 }