001    /**
002     * Copyright (c) 2000-2012 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.xml.Document;
019    import com.liferay.portal.kernel.xml.Element;
020    import com.liferay.portal.kernel.xml.SAXReaderUtil;
021    import com.liferay.portal.tools.servicebuilder.ServiceBuilder;
022    import com.liferay.portal.util.InitUtil;
023    
024    import com.thoughtworks.qdox.JavaDocBuilder;
025    import com.thoughtworks.qdox.model.DocletTag;
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    import com.thoughtworks.qdox.model.TypeVariable;
031    
032    import java.io.File;
033    import java.io.IOException;
034    
035    import java.util.Iterator;
036    import java.util.LinkedHashSet;
037    import java.util.List;
038    import java.util.Set;
039    
040    /**
041     * @author Brian Wing Shun Chan
042     */
043    public class InstanceWrapperBuilder {
044    
045            public static void main(String[] args) {
046                    InitUtil.initWithSpring();
047    
048                    if (args.length == 1) {
049                            new InstanceWrapperBuilder(args[0]);
050                    }
051                    else {
052                            throw new IllegalArgumentException();
053                    }
054            }
055    
056            public InstanceWrapperBuilder(String xml) {
057                    try {
058                            File file = new File(xml);
059    
060                            Document document = SAXReaderUtil.read(file);
061    
062                            Element rootElement = document.getRootElement();
063    
064                            List<Element> instanceWrapperElements = rootElement.elements(
065                                    "instance-wrapper");
066    
067                            for (Element instanceWrapperElement : instanceWrapperElements) {
068                                    String parentDir = instanceWrapperElement.attributeValue(
069                                            "parent-dir");
070                                    String srcFile = instanceWrapperElement.attributeValue(
071                                            "src-file");
072    
073                                    _createIW(parentDir, srcFile);
074                            }
075                    }
076                    catch (Exception e) {
077                            e.printStackTrace();
078                    }
079            }
080    
081            private void _createIW(String parentDir, String srcFile)
082                    throws IOException {
083    
084                    JavaClass javaClass = _getJavaClass(parentDir, srcFile);
085    
086                    JavaMethod[] javaMethods = javaClass.getMethods();
087    
088                    StringBuilder sb = new StringBuilder();
089    
090                    // Package
091    
092                    sb.append("package " + javaClass.getPackage().getName() + ";");
093    
094                    // Class declaration
095    
096                    sb.append("public class " + javaClass.getName() + "_IW {");
097    
098                    // Methods
099    
100                    sb.append("public static " + javaClass.getName() + "_IW getInstance() {");
101                    sb.append("return _instance;");
102                    sb.append("}\n");
103    
104                    for (JavaMethod javaMethod : javaMethods) {
105                            String methodName = javaMethod.getName();
106    
107                            if (!javaMethod.isPublic() || !javaMethod.isStatic()) {
108                                    continue;
109                            }
110    
111                            if (methodName.equals("getInstance")) {
112                                    methodName = "getWrappedInstance";
113                            }
114    
115                            DocletTag[] docletTags = javaMethod.getTagsByName("deprecated");
116    
117                            if ((docletTags != null) && (docletTags.length > 0)) {
118                                    sb.append("\t/**\n");
119                                    sb.append("\t * @deprecated\n");
120                                    sb.append("\t */\n");
121                            }
122    
123                            sb.append("public ");
124    
125                            TypeVariable[] typeParameters = javaMethod.getTypeParameters();
126    
127                            if (typeParameters.length > 0) {
128                                    sb.append(" <");
129    
130                                    for (int i = 0; i < typeParameters.length; i++) {
131                                            TypeVariable typeParameter = typeParameters[i];
132    
133                                            sb.append(typeParameter.getName());
134    
135                                            if ((i + 1) != typeParameters.length) {
136                                                    sb.append(", ");
137                                            }
138                                    }
139                                    
140                                    sb.append("> ");
141                            }
142    
143                            sb.append(_getTypeGenericsName(javaMethod.getReturns()) + " " + methodName + "(");
144    
145                            JavaParameter[] javaParameters = javaMethod.getParameters();
146    
147                            for (int i = 0; i < javaParameters.length; i++) {
148                                    JavaParameter javaParameter = javaParameters[i];
149    
150                                    sb.append(_getTypeGenericsName(javaParameter.getType()));
151    
152                                    if (javaParameter.isVarArgs()) {
153                                            sb.append("...");
154                                    }
155    
156                                    sb.append(" " + javaParameter.getName());
157    
158                                    if ((i + 1) != javaParameters.length) {
159                                            sb.append(", ");
160                                    }
161                            }
162    
163                            sb.append(")");
164    
165                            Type[] thrownExceptions = javaMethod.getExceptions();
166    
167                            Set<String> newExceptions = new LinkedHashSet<String>();
168    
169                            for (int j = 0; j < thrownExceptions.length; j++) {
170                                    Type thrownException = thrownExceptions[j];
171    
172                                    newExceptions.add(thrownException.getValue());
173                            }
174    
175                            if (newExceptions.size() > 0) {
176                                    sb.append(" throws ");
177    
178                                    Iterator<String> itr = newExceptions.iterator();
179    
180                                    while (itr.hasNext()) {
181                                            sb.append(itr.next());
182    
183                                            if (itr.hasNext()) {
184                                                    sb.append(", ");
185                                            }
186                                    }
187                            }
188    
189                            sb.append("{\n");
190    
191                            if (!javaMethod.getReturns().getValue().equals("void")) {
192                                    sb.append("return ");
193                            }
194    
195                            sb.append(javaClass.getName() + "." + javaMethod.getName() + "(");
196    
197                            for (int j = 0; j < javaParameters.length; j++) {
198                                    JavaParameter javaParameter = javaParameters[j];
199    
200                                    sb.append(javaParameter.getName());
201    
202                                    if ((j + 1) != javaParameters.length) {
203                                            sb.append(", ");
204                                    }
205                            }
206    
207                            sb.append(");");
208                            sb.append("}\n");
209                    }
210    
211                    // Private constructor
212    
213                    sb.append("private " + javaClass.getName() + "_IW() {");
214                    sb.append("}");
215    
216                    // Fields
217    
218                    sb.append("private static " + javaClass.getName() + "_IW _instance = new " + javaClass.getName() + "_IW();");
219    
220                    // Class close brace
221    
222                    sb.append("}");
223    
224                    // Write file
225    
226                    File file = new File(parentDir + "/" + StringUtil.replace(javaClass.getPackage().getName(), ".", "/") + "/" + javaClass.getName() + "_IW.java");
227    
228                    ServiceBuilder.writeFile(file, sb.toString());
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            private String _getTypeGenericsName(Type type) {
255                    StringBuilder sb = new StringBuilder();
256    
257                    sb.append(type.getValue());
258    
259                    Type[] actualTypeArguments = type.getActualTypeArguments();
260    
261                    if (actualTypeArguments != null) {
262                            sb.append("<");
263    
264                            for (int i = 0; i < actualTypeArguments.length; i++) {
265                                    if (i > 0) {
266                                            sb.append(", ");
267                                    }
268    
269                                    sb.append(_getTypeGenericsName(actualTypeArguments[i]));
270                            }
271    
272                            sb.append(">");
273                    }
274    
275                    sb.append(_getDimensions(type));
276    
277                    return sb.toString();
278            }
279    
280    }