1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.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  /**
47   * <a href="CopyInterfaceBuilder.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Brian Wing Shun Chan
50   */
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          // Package
85  
86          sb.append("package " + javaClass.getPackage().getName() + ";");
87  
88          // Imports
89  
90          sb.append("[$IMPORTS$]");
91  
92          // Class declaration
93  
94          sb.append("public class Copy" + javaClass.getName() + " implements " + javaClass.getName() + " {");
95  
96          String varName = "_" + TextFormatter.format(javaClass.getName(), TextFormatter.I);
97  
98          // Methods
99  
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         // Fields
180 
181         sb.append("private " + javaClass.getName() + " " + varName + ";");
182 
183         // Class close brace
184 
185         sb.append("}");
186 
187         // Imports
188 
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         // Write file
206 
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 }