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.StringBundler;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.kernel.util.StringUtil;
020    import com.liferay.portal.kernel.util.TextFormatter;
021    import com.liferay.portal.tools.comparator.JavaMethodComparator;
022    import com.liferay.portal.tools.servicebuilder.ServiceBuilder;
023    import com.liferay.portal.util.InitUtil;
024    
025    import com.thoughtworks.qdox.JavaDocBuilder;
026    import com.thoughtworks.qdox.model.JavaClass;
027    import com.thoughtworks.qdox.model.JavaMethod;
028    import com.thoughtworks.qdox.model.JavaParameter;
029    import com.thoughtworks.qdox.model.Type;
030    
031    import java.io.File;
032    import java.io.IOException;
033    
034    import java.util.Arrays;
035    import java.util.LinkedHashSet;
036    import java.util.Set;
037    import java.util.TreeSet;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     */
042    public class CopyInterfaceBuilder {
043    
044            public static void main(String[] args) {
045                    InitUtil.initWithSpring();
046    
047                    if (args.length == 2) {
048                            new CopyInterfaceBuilder(args[0], args[1]);
049                    }
050                    else {
051                            throw new IllegalArgumentException();
052                    }
053            }
054    
055            public CopyInterfaceBuilder(String parentDir, String srcFile) {
056                    try {
057                            _copyInterface(parentDir, srcFile);
058                    }
059                    catch (Exception e) {
060                            e.printStackTrace();
061                    }
062            }
063    
064            private void _copyInterface(String parentDir, String srcFile)
065                    throws IOException {
066    
067                    JavaClass javaClass = _getJavaClass(parentDir, srcFile);
068    
069                    JavaMethod[] methods = javaClass.getMethods();
070    
071                    Arrays.sort(methods, new JavaMethodComparator());
072    
073                    StringBundler sb = new StringBundler();
074    
075                    // Package
076    
077                    sb.append("package ");
078                    sb.append(javaClass.getPackage().getName());
079                    sb.append(";");
080    
081                    // Imports
082    
083                    sb.append("[$IMPORTS$]");
084    
085                    // Class declaration
086    
087                    sb.append("public class Copy");
088                    sb.append(javaClass.getName());
089                    sb.append(" implements ");
090                    sb.append(javaClass.getName());
091                    sb.append(" {");
092    
093                    String varName = "_" + TextFormatter.format(
094                            javaClass.getName(), TextFormatter.I);
095    
096                    // Methods
097    
098                    Set<String> imports = new TreeSet<String>();
099    
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 ");
111                                    sb.append(javaMethod.getReturns().getJavaClass().getName());
112                                    sb.append(_getDimensions(javaMethod.getReturns()));
113                                    sb.append(" ");
114                                    sb.append(methodName);
115                                    sb.append(StringPool.OPEN_PARENTHESIS);
116    
117                                    JavaParameter[] parameters = javaMethod.getParameters();
118    
119                                    for (int j = 0; j < parameters.length; j++) {
120                                            JavaParameter javaParameter = parameters[j];
121    
122                                            sb.append(javaParameter.getType().getJavaClass().getName());
123                                            sb.append(_getDimensions(javaParameter.getType()));
124                                            sb.append(" ");
125                                            sb.append(javaParameter.getName());
126                                            sb.append(", ");
127    
128                                            imports.add(javaParameter.getType().getValue());
129                                    }
130    
131                                    if (parameters.length > 0) {
132                                            sb.setIndex(sb.index() - 1);
133                                    }
134    
135                                    sb.append(StringPool.CLOSE_PARENTHESIS);
136    
137                                    Type[] thrownExceptions = javaMethod.getExceptions();
138    
139                                    Set<String> newExceptions = new LinkedHashSet<String>();
140    
141                                    for (int j = 0; j < thrownExceptions.length; j++) {
142                                            Type thrownException = thrownExceptions[j];
143    
144                                            newExceptions.add(thrownException.getJavaClass().getName());
145    
146                                            imports.add(thrownException.getValue());
147                                    }
148    
149                                    if (newExceptions.size() > 0) {
150                                            sb.append(" throws ");
151    
152                                            for (String newException : newExceptions) {
153                                                    sb.append(newException);
154                                                    sb.append(", ");
155                                            }
156    
157                                            sb.setIndex(sb.index() - 1);
158                                    }
159    
160                                    sb.append("{");
161    
162                                    if (!returnValueName.equals("void")) {
163                                            sb.append("return ");
164                                    }
165    
166                                    sb.append(varName);
167                                    sb.append(".");
168                                    sb.append(methodName);
169                                    sb.append(StringPool.OPEN_PARENTHESIS);
170    
171                                    for (int j = 0; j < parameters.length; j++) {
172                                            JavaParameter javaParameter = parameters[j];
173    
174                                            sb.append(javaParameter.getName());
175                                            sb.append(", ");
176                                    }
177    
178                                    if (parameters.length > 0) {
179                                            sb.setIndex(sb.index() - 1);
180                                    }
181    
182                                    sb.append(");");
183                                    sb.append("}");
184                            }
185                    }
186    
187                    // Fields
188    
189                    sb.append("private ");
190                    sb.append(javaClass.getName());
191                    sb.append(" ");
192                    sb.append(varName);
193                    sb.append(";");
194    
195                    // Class close brace
196    
197                    sb.append("}");
198    
199                    // Imports
200    
201                    String content = sb.toString();
202    
203                    sb = new StringBundler(imports.size() * 3);
204    
205                    for (String importClass : imports) {
206                            if (importClass.equals("boolean") || importClass.equals("byte") ||
207                                    importClass.equals("double") || importClass.equals("float") ||
208                                    importClass.equals("int") || importClass.equals("long") ||
209                                    importClass.equals("short") || importClass.equals("void")) {
210    
211                                    continue;
212                            }
213    
214                            sb.append("import ");
215                            sb.append(importClass);
216                            sb.append(";");
217                    }
218    
219                    content = StringUtil.replace(content, "[$IMPORTS$]", sb.toString());
220    
221                    // Write file
222    
223                    File file = new File(
224                            parentDir + "/" +
225                                    StringUtil.replace(javaClass.getPackage().getName(), ".", "/") +
226                                            "/Copy" + javaClass.getName() + ".java");
227    
228                    ServiceBuilder.writeFile(file, content);
229            }
230    
231            private String _getDimensions(Type type) {
232                    String dimensions = "";
233    
234                    for (int i = 0; i < type.getDimensions(); i++) {
235                            dimensions += "[]";
236                    }
237    
238                    return dimensions;
239            }
240    
241            private JavaClass _getJavaClass(String parentDir, String srcFile)
242                    throws IOException {
243    
244                    String className = StringUtil.replace(
245                            srcFile.substring(0, srcFile.length() - 5), "/", ".");
246    
247                    JavaDocBuilder builder = new JavaDocBuilder();
248    
249                    builder.addSource(new File(parentDir + "/" + srcFile));
250    
251                    return builder.getClassByName(className);
252            }
253    
254    }