1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.tools;
16  
17  import com.liferay.portal.kernel.util.StringUtil;
18  import com.liferay.portal.tools.comparator.JavaMethodComparator;
19  import com.liferay.portal.tools.servicebuilder.ServiceBuilder;
20  import com.liferay.portal.util.InitUtil;
21  import com.liferay.util.TextFormatter;
22  
23  import com.thoughtworks.qdox.JavaDocBuilder;
24  import com.thoughtworks.qdox.model.JavaClass;
25  import com.thoughtworks.qdox.model.JavaMethod;
26  import com.thoughtworks.qdox.model.JavaParameter;
27  import com.thoughtworks.qdox.model.Type;
28  
29  import java.io.File;
30  import java.io.IOException;
31  
32  import java.util.Arrays;
33  import java.util.Iterator;
34  import java.util.LinkedHashSet;
35  import java.util.Set;
36  import java.util.TreeSet;
37  
38  /**
39   * <a href="CopyInterfaceBuilder.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   */
43  public class CopyInterfaceBuilder {
44  
45      public static void main(String[] args) {
46          InitUtil.initWithSpring();
47  
48          if (args.length == 2) {
49              new CopyInterfaceBuilder(args[0], args[1]);
50          }
51          else {
52              throw new IllegalArgumentException();
53          }
54      }
55  
56      public CopyInterfaceBuilder(String parentDir, String srcFile) {
57          try {
58              _copyInterface(parentDir, srcFile);
59          }
60          catch (Exception e) {
61              e.printStackTrace();
62          }
63      }
64  
65      private void _copyInterface(String parentDir, String srcFile)
66          throws IOException {
67  
68          JavaClass javaClass = _getJavaClass(parentDir, srcFile);
69  
70          JavaMethod[] methods = javaClass.getMethods();
71  
72          Arrays.sort(methods, new JavaMethodComparator());
73  
74          StringBuilder sb = new StringBuilder();
75  
76          // Package
77  
78          sb.append("package " + javaClass.getPackage().getName() + ";");
79  
80          // Imports
81  
82          sb.append("[$IMPORTS$]");
83  
84          // Class declaration
85  
86          sb.append("public class Copy" + javaClass.getName() + " implements " + javaClass.getName() + " {");
87  
88          String varName = "_" + TextFormatter.format(javaClass.getName(), TextFormatter.I);
89  
90          // Methods
91  
92          Set<String> imports = new TreeSet<String>();
93  
94          for (int i = 0; i < methods.length; i++) {
95              JavaMethod javaMethod = methods[i];
96  
97              String methodName = javaMethod.getName();
98  
99              if (javaMethod.isPublic()) {
100                 String returnValueName = javaMethod.getReturns().getValue();
101 
102                 imports.add(returnValueName);
103 
104                 sb.append("public " + javaMethod.getReturns().getJavaClass().getName() + _getDimensions(javaMethod.getReturns()) + " " + methodName + "(");
105 
106                 JavaParameter[] parameters = javaMethod.getParameters();
107 
108                 for (int j = 0; j < parameters.length; j++) {
109                     JavaParameter javaParameter = parameters[j];
110 
111                     sb.append(javaParameter.getType().getJavaClass().getName() + _getDimensions(javaParameter.getType()) + " " + javaParameter.getName());
112 
113                     imports.add(javaParameter.getType().getValue());
114 
115                     if ((j + 1) != parameters.length) {
116                         sb.append(", ");
117                     }
118                 }
119 
120                 sb.append(")");
121 
122                 Type[] thrownExceptions = javaMethod.getExceptions();
123 
124                 Set<String> newExceptions = new LinkedHashSet<String>();
125 
126                 for (int j = 0; j < thrownExceptions.length; j++) {
127                     Type thrownException = thrownExceptions[j];
128 
129                     newExceptions.add(thrownException.getJavaClass().getName());
130 
131                     imports.add(thrownException.getValue());
132                 }
133 
134                 if (newExceptions.size() > 0) {
135                     sb.append(" throws ");
136 
137                     Iterator<String> itr = newExceptions.iterator();
138 
139                     while (itr.hasNext()) {
140                         sb.append(itr.next());
141 
142                         if (itr.hasNext()) {
143                             sb.append(", ");
144                         }
145                     }
146                 }
147 
148                 sb.append("{");
149 
150                 if (!returnValueName.equals("void")) {
151                     sb.append("return ");
152                 }
153 
154                 sb.append(varName + "." + methodName + "(");
155 
156                 for (int j = 0; j < parameters.length; j++) {
157                     JavaParameter javaParameter = parameters[j];
158 
159                     sb.append(javaParameter.getName());
160 
161                     if ((j + 1) != parameters.length) {
162                         sb.append(", ");
163                     }
164                 }
165 
166                 sb.append(");");
167                 sb.append("}");
168             }
169         }
170 
171         // Fields
172 
173         sb.append("private " + javaClass.getName() + " " + varName + ";");
174 
175         // Class close brace
176 
177         sb.append("}");
178 
179         // Imports
180 
181         String content = sb.toString();
182 
183         sb = new StringBuilder();
184 
185         Iterator<String> itr = imports.iterator();
186 
187         while (itr.hasNext()) {
188             String importClass = itr.next();
189 
190             if (!importClass.equals("boolean") && !importClass.equals("double") && !importClass.equals("int") && !importClass.equals("long") && !importClass.equals("short") && !importClass.equals("void")) {
191                 sb.append("import " + importClass + ";");
192             }
193         }
194 
195         content = StringUtil.replace(content, "[$IMPORTS$]", sb.toString());
196 
197         // Write file
198 
199         File file = new File(parentDir + "/" + StringUtil.replace(javaClass.getPackage().getName(), ".", "/") + "/Copy" + javaClass.getName() + ".java");
200 
201         ServiceBuilder.writeFile(file, content);
202     }
203 
204     private String _getDimensions(Type type) {
205         String dimensions = "";
206 
207         for (int i = 0; i < type.getDimensions(); i++) {
208             dimensions += "[]";
209         }
210 
211         return dimensions;
212     }
213 
214     private JavaClass _getJavaClass(String parentDir, String srcFile)
215         throws IOException {
216 
217         String className = StringUtil.replace(
218             srcFile.substring(0, srcFile.length() - 5), "/", ".");
219 
220         JavaDocBuilder builder = new JavaDocBuilder();
221 
222         builder.addSource(new File(parentDir + "/" + srcFile));
223 
224         return builder.getClassByName(className);
225     }
226 
227 }