001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.tools;
016    
017    import com.liferay.portal.kernel.util.StringUtil;
018    import com.liferay.portal.kernel.util.TextFormatter;
019    import com.liferay.portal.tools.comparator.JavaMethodComparator;
020    import com.liferay.portal.tools.servicebuilder.ServiceBuilder;
021    import com.liferay.portal.util.InitUtil;
022    
023    import com.thoughtworks.qdox.JavaDocBuilder;
024    import com.thoughtworks.qdox.model.JavaClass;
025    import com.thoughtworks.qdox.model.JavaMethod;
026    import com.thoughtworks.qdox.model.JavaParameter;
027    import com.thoughtworks.qdox.model.Type;
028    
029    import java.io.File;
030    import java.io.IOException;
031    
032    import java.util.Arrays;
033    import java.util.Iterator;
034    import java.util.LinkedHashSet;
035    import java.util.Set;
036    import java.util.TreeSet;
037    
038    /**
039     * @author Brian Wing Shun Chan
040     */
041    public class CopyInterfaceBuilder {
042    
043            public static void main(String[] args) {
044                    InitUtil.initWithSpring();
045    
046                    if (args.length == 2) {
047                            new CopyInterfaceBuilder(args[0], args[1]);
048                    }
049                    else {
050                            throw new IllegalArgumentException();
051                    }
052            }
053    
054            public CopyInterfaceBuilder(String parentDir, String srcFile) {
055                    try {
056                            _copyInterface(parentDir, srcFile);
057                    }
058                    catch (Exception e) {
059                            e.printStackTrace();
060                    }
061            }
062    
063            private void _copyInterface(String parentDir, String srcFile)
064                    throws IOException {
065    
066                    JavaClass javaClass = _getJavaClass(parentDir, srcFile);
067    
068                    JavaMethod[] methods = javaClass.getMethods();
069    
070                    Arrays.sort(methods, new JavaMethodComparator());
071    
072                    StringBuilder sb = new StringBuilder();
073    
074                    // Package
075    
076                    sb.append("package " + javaClass.getPackage().getName() + ";");
077    
078                    // Imports
079    
080                    sb.append("[$IMPORTS$]");
081    
082                    // Class declaration
083    
084                    sb.append("public class Copy" + javaClass.getName() + " implements " + javaClass.getName() + " {");
085    
086                    String varName = "_" + TextFormatter.format(javaClass.getName(), TextFormatter.I);
087    
088                    // Methods
089    
090                    Set<String> imports = new TreeSet<String>();
091    
092                    for (int i = 0; i < methods.length; i++) {
093                            JavaMethod javaMethod = methods[i];
094    
095                            String methodName = javaMethod.getName();
096    
097                            if (javaMethod.isPublic()) {
098                                    String returnValueName = javaMethod.getReturns().getValue();
099    
100                                    imports.add(returnValueName);
101    
102                                    sb.append("public " + javaMethod.getReturns().getJavaClass().getName() + _getDimensions(javaMethod.getReturns()) + " " + methodName + "(");
103    
104                                    JavaParameter[] parameters = javaMethod.getParameters();
105    
106                                    for (int j = 0; j < parameters.length; j++) {
107                                            JavaParameter javaParameter = parameters[j];
108    
109                                            sb.append(javaParameter.getType().getJavaClass().getName() + _getDimensions(javaParameter.getType()) + " " + javaParameter.getName());
110    
111                                            imports.add(javaParameter.getType().getValue());
112    
113                                            if ((j + 1) != parameters.length) {
114                                                    sb.append(", ");
115                                            }
116                                    }
117    
118                                    sb.append(")");
119    
120                                    Type[] thrownExceptions = javaMethod.getExceptions();
121    
122                                    Set<String> newExceptions = new LinkedHashSet<String>();
123    
124                                    for (int j = 0; j < thrownExceptions.length; j++) {
125                                            Type thrownException = thrownExceptions[j];
126    
127                                            newExceptions.add(thrownException.getJavaClass().getName());
128    
129                                            imports.add(thrownException.getValue());
130                                    }
131    
132                                    if (newExceptions.size() > 0) {
133                                            sb.append(" throws ");
134    
135                                            Iterator<String> itr = newExceptions.iterator();
136    
137                                            while (itr.hasNext()) {
138                                                    sb.append(itr.next());
139    
140                                                    if (itr.hasNext()) {
141                                                            sb.append(", ");
142                                                    }
143                                            }
144                                    }
145    
146                                    sb.append("{");
147    
148                                    if (!returnValueName.equals("void")) {
149                                            sb.append("return ");
150                                    }
151    
152                                    sb.append(varName + "." + methodName + "(");
153    
154                                    for (int j = 0; j < parameters.length; j++) {
155                                            JavaParameter javaParameter = parameters[j];
156    
157                                            sb.append(javaParameter.getName());
158    
159                                            if ((j + 1) != parameters.length) {
160                                                    sb.append(", ");
161                                            }
162                                    }
163    
164                                    sb.append(");");
165                                    sb.append("}");
166                            }
167                    }
168    
169                    // Fields
170    
171                    sb.append("private " + javaClass.getName() + " " + varName + ";");
172    
173                    // Class close brace
174    
175                    sb.append("}");
176    
177                    // Imports
178    
179                    String content = sb.toString();
180    
181                    sb = new StringBuilder();
182    
183                    Iterator<String> itr = imports.iterator();
184    
185                    while (itr.hasNext()) {
186                            String importClass = itr.next();
187    
188                            if (!importClass.equals("boolean") && !importClass.equals("double") && !importClass.equals("int") && !importClass.equals("long") && !importClass.equals("short") && !importClass.equals("void")) {
189                                    sb.append("import " + importClass + ";");
190                            }
191                    }
192    
193                    content = StringUtil.replace(content, "[$IMPORTS$]", sb.toString());
194    
195                    // Write file
196    
197                    File file = new File(parentDir + "/" + StringUtil.replace(javaClass.getPackage().getName(), ".", "/") + "/Copy" + javaClass.getName() + ".java");
198    
199                    ServiceBuilder.writeFile(file, content);
200            }
201    
202            private String _getDimensions(Type type) {
203                    String dimensions = "";
204    
205                    for (int i = 0; i < type.getDimensions(); i++) {
206                            dimensions += "[]";
207                    }
208    
209                    return dimensions;
210            }
211    
212            private JavaClass _getJavaClass(String parentDir, String srcFile)
213                    throws IOException {
214    
215                    String className = StringUtil.replace(
216                            srcFile.substring(0, srcFile.length() - 5), "/", ".");
217    
218                    JavaDocBuilder builder = new JavaDocBuilder();
219    
220                    builder.addSource(new File(parentDir + "/" + srcFile));
221    
222                    return builder.getClassByName(className);
223            }
224    
225    }