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.StringBundler;
018    import com.liferay.portal.kernel.util.StringUtil;
019    import com.liferay.portal.kernel.xml.Document;
020    import com.liferay.portal.kernel.xml.Element;
021    import com.liferay.portal.kernel.xml.SAXReaderUtil;
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.DocletTag;
027    import com.thoughtworks.qdox.model.JavaClass;
028    import com.thoughtworks.qdox.model.JavaMethod;
029    import com.thoughtworks.qdox.model.JavaParameter;
030    import com.thoughtworks.qdox.model.Type;
031    import com.thoughtworks.qdox.model.TypeVariable;
032    
033    import java.io.File;
034    import java.io.IOException;
035    
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                    StringBundler sb = new StringBundler();
089    
090                    // Package
091    
092                    sb.append("package ");
093                    sb.append(javaClass.getPackage().getName());
094                    sb.append(";");
095    
096                    // Class declaration
097    
098                    sb.append("public class ");
099                    sb.append(javaClass.getName());
100                    sb.append("_IW {");
101    
102                    // Methods
103    
104                    sb.append("public static ");
105                    sb.append(javaClass.getName());
106                    sb.append("_IW getInstance() {");
107                    sb.append("return _instance;");
108                    sb.append("}\n");
109    
110                    for (JavaMethod javaMethod : javaMethods) {
111                            String methodName = javaMethod.getName();
112    
113                            if (!javaMethod.isPublic() || !javaMethod.isStatic()) {
114                                    continue;
115                            }
116    
117                            if (methodName.equals("getInstance")) {
118                                    methodName = "getWrappedInstance";
119                            }
120    
121                            DocletTag[] docletTags = javaMethod.getTagsByName("deprecated");
122    
123                            if ((docletTags != null) && (docletTags.length > 0)) {
124                                    sb.append("\t/**\n");
125                                    sb.append("\t * @deprecated\n");
126                                    sb.append("\t */\n");
127                            }
128    
129                            sb.append("public ");
130    
131                            TypeVariable[] typeParameters = javaMethod.getTypeParameters();
132    
133                            if (typeParameters.length > 0) {
134                                    sb.append(" <");
135    
136                                    for (int i = 0; i < typeParameters.length; i++) {
137                                            TypeVariable typeParameter = typeParameters[i];
138    
139                                            sb.append(typeParameter.getName());
140                                            sb.append(", ");
141                                    }
142    
143                                    sb.setIndex(sb.index() - 1);
144    
145                                    sb.append("> ");
146                            }
147    
148                            sb.append(_getTypeGenericsName(javaMethod.getReturns()));
149                            sb.append(" ");
150                            sb.append(methodName);
151                            sb.append("(");
152    
153                            JavaParameter[] javaParameters = javaMethod.getParameters();
154    
155                            for (int i = 0; i < javaParameters.length; i++) {
156                                    JavaParameter javaParameter = javaParameters[i];
157    
158                                    sb.append(_getTypeGenericsName(javaParameter.getType()));
159    
160                                    if (javaParameter.isVarArgs()) {
161                                            sb.append("...");
162                                    }
163    
164                                    sb.append(" ");
165                                    sb.append(javaParameter.getName());
166                                    sb.append(", ");
167                            }
168    
169                            if (javaParameters.length > 0) {
170                                    sb.setIndex(sb.index() - 1);
171                            }
172    
173                            sb.append(")");
174    
175                            Type[] thrownExceptions = javaMethod.getExceptions();
176    
177                            Set<String> newExceptions = new LinkedHashSet<String>();
178    
179                            for (int j = 0; j < thrownExceptions.length; j++) {
180                                    Type thrownException = thrownExceptions[j];
181    
182                                    newExceptions.add(thrownException.getValue());
183                            }
184    
185                            if (newExceptions.size() > 0) {
186                                    sb.append(" throws ");
187    
188                                    for (String newException : newExceptions) {
189                                            sb.append(newException);
190                                            sb.append(", ");
191                                    }
192    
193                                    sb.setIndex(sb.index() - 1);
194                            }
195    
196                            sb.append("{\n");
197    
198                            if (!javaMethod.getReturns().getValue().equals("void")) {
199                                    sb.append("return ");
200                            }
201    
202                            sb.append(javaClass.getName() + "." + javaMethod.getName() + "(");
203    
204                            for (int j = 0; j < javaParameters.length; j++) {
205                                    JavaParameter javaParameter = javaParameters[j];
206    
207                                    sb.append(javaParameter.getName());
208                                    sb.append(", ");
209                            }
210    
211                            if (javaParameters.length > 0) {
212                                    sb.setIndex(sb.index() - 1);
213                            }
214    
215                            sb.append(");");
216                            sb.append("}\n");
217                    }
218    
219                    // Private constructor
220    
221                    sb.append("private ");
222                    sb.append(javaClass.getName());
223                    sb.append("_IW() {");
224                    sb.append("}");
225    
226                    // Fields
227    
228                    sb.append("private static ");
229                    sb.append(javaClass.getName());
230                    sb.append("_IW _instance = new ");
231                    sb.append(javaClass.getName());
232                    sb.append("_IW();");
233    
234                    // Class close brace
235    
236                    sb.append("}");
237    
238                    // Write file
239    
240                    File file = new File(
241                            parentDir + "/" +
242                                    StringUtil.replace(javaClass.getPackage().getName(), ".", "/") +
243                                            "/" + javaClass.getName() + "_IW.java");
244    
245                    ServiceBuilder.writeFile(file, sb.toString());
246            }
247    
248            private String _getDimensions(Type type) {
249                    String dimensions = "";
250    
251                    for (int i = 0; i < type.getDimensions(); i++) {
252                            dimensions += "[]";
253                    }
254    
255                    return dimensions;
256            }
257    
258            private JavaClass _getJavaClass(String parentDir, String srcFile)
259                    throws IOException {
260    
261                    String className = StringUtil.replace(
262                            srcFile.substring(0, srcFile.length() - 5), "/", ".");
263    
264                    JavaDocBuilder builder = new JavaDocBuilder();
265    
266                    builder.addSource(new File(parentDir + "/" + srcFile));
267    
268                    return builder.getClassByName(className);
269            }
270    
271            private String _getTypeGenericsName(Type type) {
272                    Type[] actualTypeArguments = type.getActualTypeArguments();
273    
274                    if (actualTypeArguments == null) {
275                            String value = type.getValue();
276    
277                            return value.concat(_getDimensions(type));
278                    }
279                    else {
280                            StringBundler sb = new StringBundler(
281                                    actualTypeArguments.length * 2 + 3);
282    
283                            sb.append(type.getValue());
284                            sb.append("<");
285    
286                            for (int i = 0; i < actualTypeArguments.length; i++) {
287                                    sb.append(_getTypeGenericsName(actualTypeArguments[i]));
288                                    sb.append(", ");
289                            }
290    
291                            if (actualTypeArguments.length > 0) {
292                                    sb.setIndex(sb.index() - 1);
293                            }
294    
295                            sb.append(">");
296                            sb.append(_getDimensions(type));
297    
298                            return sb.toString();
299                    }
300            }
301    
302    }