1 /*************************************************************************************** 2 * Copyright (c) Jonas Bonér, Alexandre Vasseur. All rights reserved. * 3 * http://aspectwerkz.codehaus.org * 4 * ---------------------------------------------------------------------------------- * 5 * The software in this package is published under the terms of the LGPL license * 6 * a copy of which has been included with this distribution in the license.txt file. * 7 **************************************************************************************/ 8 package org.codehaus.aspectwerkz.expression; 9 10 import com.thoughtworks.qdox.JavaDocBuilder; 11 import com.thoughtworks.qdox.model.JavaClass; 12 import com.thoughtworks.qdox.model.JavaField; 13 import com.thoughtworks.qdox.model.JavaMethod; 14 import com.thoughtworks.qdox.model.JavaParameter; 15 import com.thoughtworks.qdox.model.Type; 16 17 import org.codehaus.aspectwerkz.exception.DefinitionException; 18 19 import java.io.File; 20 import java.util.ArrayList; 21 import java.util.Collection; 22 import java.util.Iterator; 23 24 /*** 25 * Parses a src tree with <code>QDox</code>. 26 * 27 * @author <a href="mailto:jboner@codehaus.org">Jonas Bonér </a> 28 */ 29 public class QDoxParser { 30 /*** 31 * The QDox builder. 32 */ 33 private JavaDocBuilder m_builder = new JavaDocBuilder(); 34 35 /*** 36 * The parsed java class. 37 */ 38 private JavaClass m_class; 39 40 /*** 41 * The name of the class. 42 */ 43 private String m_className; 44 45 /*** 46 * Transforms the QDox JavaMethod parameters to a String array with the types represented as strings. 47 * 48 * @param method the JavaMethod 49 * @return an array with the types as strings 50 */ 51 public static String[] getJavaMethodParametersAsStringArray(final JavaMethod method) { 52 JavaParameter[] javaParameters = method.getParameters(); 53 String[] parameters = new String[javaParameters.length]; 54 for (int i = 0; i < javaParameters.length; i++) { 55 Type type = javaParameters[i].getType(); 56 int dimensions = type.getDimensions(); 57 StringBuffer parameter = new StringBuffer(type.getValue()); 58 for (int j = 0; j < dimensions; j++) { 59 parameter.append("[]"); 60 } 61 parameters[i] = parameter.toString(); 62 } 63 return parameters; 64 } 65 66 /*** 67 * Adds a source tree to the builder. 68 * 69 * @param srcDir the source tree 70 */ 71 public QDoxParser(final String srcDir) { 72 m_builder.addSourceTree(new File(srcDir)); 73 } 74 75 /*** 76 * Parses a specific class. 77 * 78 * @param className the name of the class to compile 79 * @return true if class was found and false otherwise 80 * @todo QDox seems to have a problem retrieving inner classes => null 81 */ 82 public boolean parse(final String className) { 83 m_class = m_builder.getClassByName(className); 84 if (m_class == null) { 85 return false; 86 } 87 m_className = m_class.getFullyQualifiedName(); 88 return true; 89 } 90 91 /*** 92 * Returns the QDox JavaClass. 93 * 94 * @return the QDox JavaClass 95 */ 96 public JavaClass getJavaClass() { 97 if ((m_class == null) && (m_className == null)) { 98 throw new DefinitionException("no class has been parsed, call parse(..) first"); 99 } 100 if (m_class == null) { 101 throw new DefinitionException("could not find source file for " 102 + m_className 103 + " (have you specified the correct srcDir)"); 104 } 105 return m_class; 106 } 107 108 /*** 109 * Returns all classes. 110 * 111 * @return a collections with all classes 112 */ 113 public String[] getAllClassNames() { 114 Collection classes = m_builder.getClassLibrary().all(); 115 Collection classNames = new ArrayList(); 116 String className = null; 117 for (Iterator it = classes.iterator(); it.hasNext();) { 118 className = (String) it.next(); 119 if ("java.lang.Object".equals(className)) { 120 continue; 121 } 122 classNames.add(className); 123 } 124 return (String[]) classNames.toArray(new String[] {}); 125 } 126 127 /*** 128 * Parses a specific class A returns an array with the methods. 129 * 130 * @return an array with the methods 131 */ 132 public JavaMethod[] getJavaMethods() { 133 if ((m_class == null) && (m_className == null)) { 134 throw new DefinitionException("no class has been parsed, call parse(..) first"); 135 } 136 if (m_class == null) { 137 throw new DefinitionException("could not find source file for " 138 + m_className 139 + " (have you specified the correct srcDir)"); 140 } 141 return m_class.getMethods(); 142 } 143 144 /*** 145 * Parses a specific class A returns an array with the methods. 146 * 147 * @return an array with the methods 148 */ 149 public JavaField[] getJavaFields() { 150 if ((m_class == null) && (m_className == null)) { 151 throw new DefinitionException("no class has been parsed, call parse(..) first"); 152 } 153 if (m_class == null) { 154 throw new DefinitionException("could not find source file for " 155 + m_className 156 + " (have you specified the correct srcDir)"); 157 } 158 return m_class.getFields(); 159 } 160 }