1 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 29 public class QDoxParser { 30 33 private JavaDocBuilder m_builder = new JavaDocBuilder(); 34 35 38 private JavaClass m_class; 39 40 43 private String m_className; 44 45 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 71 public QDoxParser(final String srcDir) { 72 m_builder.addSourceTree(new File (srcDir)); 73 } 74 75 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 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( 102 "could not find source file for " 103 + m_className 104 + " (have you specified the correct srcDir)" 105 ); 106 } 107 return m_class; 108 } 109 110 115 public String [] getAllClassNames() { 116 Collection classes = m_builder.getClassLibrary().all(); 117 Collection classNames = new ArrayList (); 118 String className = null; 119 for (Iterator it = classes.iterator(); it.hasNext();) { 120 className = (String ) it.next(); 121 if ("java.lang.Object".equals(className)) { 122 continue; 123 } 124 classNames.add(className); 125 } 126 return (String []) classNames.toArray(new String []{}); 127 } 128 129 134 public JavaMethod[] getJavaMethods() { 135 if ((m_class == null) && (m_className == null)) { 136 throw new DefinitionException("no class has been parsed, call parse(..) first"); 137 } 138 if (m_class == null) { 139 throw new DefinitionException( 140 "could not find source file for " 141 + m_className 142 + " (have you specified the correct srcDir)" 143 ); 144 } 145 return m_class.getMethods(); 146 } 147 148 153 public JavaField[] getJavaFields() { 154 if ((m_class == null) && (m_className == null)) { 155 throw new DefinitionException("no class has been parsed, call parse(..) first"); 156 } 157 if (m_class == null) { 158 throw new DefinitionException( 159 "could not find source file for " 160 + m_className 161 + " (have you specified the correct srcDir)" 162 ); 163 } 164 return m_class.getFields(); 165 } 166 }
| Popular Tags
|