View Javadoc

    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(
    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     /***
    111      * Returns all classes.
    112      *
    113      * @return a collections with all classes
    114      */
    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     /***
    130      * Parses a specific class A returns an array with the methods.
    131      *
    132      * @return an array with the methods
    133      */
    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     /***
    149      * Parses a specific class A returns an array with the methods.
    150      *
    151      * @return an array with the methods
    152      */
    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 }