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.reflect.impl.java;
    9
    10  import org.codehaus.aspectwerkz.annotation.Annotations;
    11  import org.codehaus.aspectwerkz.reflect.ClassInfo;
    12  import org.codehaus.aspectwerkz.reflect.ConstructorInfo;
    13  import org.codehaus.aspectwerkz.reflect.ReflectHelper;
    14  import org.codehaus.aspectwerkz.reflect.ReflectHelper;
    15
    16  import java.lang.reflect.Constructor;
    17  import java.util.List;
    18
    19  /***
    20   * Implementation of the ConstructorInfo interface for java.lang.reflect.*.
    21   *
    22   * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
    23   */
    24  public class JavaConstructorInfo extends JavaMemberInfo implements ConstructorInfo {
    25      /***
    26       * A list with the parameter types.
    27       */
    28      private ClassInfo[] m_parameterTypes = null;
    29
    30      /***
    31       * A list with the exception types.
    32       */
    33      private ClassInfo[] m_exceptionTypes = null;
    34
    35      /***
    36       * The signature of the class.
    37       */
    38      private String m_signature;
    39
    40      /***
    41       * Creates a new method meta data instance.
    42       *
    43       * @param constructor
    44       * @param declaringType
    45       */
    46      JavaConstructorInfo(final Constructor constructor, final JavaClassInfo declaringType) {
    47          super(constructor, declaringType);
    48          m_signature = ReflectHelper.getConstructorSignature(constructor);
    49      }
    50
    51      /***
    52       * Returns the constructor info for the constructor specified.
    53       *
    54       * @param constructor the constructor
    55       * @return the constructor info
    56       */
    57      public static ConstructorInfo getConstructorInfo(final Constructor constructor) {
    58          Class declaringClass = constructor.getDeclaringClass();
    59          JavaClassInfoRepository repository = JavaClassInfoRepository.getRepository(declaringClass.getClassLoader());
    60          ClassInfo classInfo = repository.getClassInfo(declaringClass.getName());
    61          if (classInfo == null) {
    62              classInfo = JavaClassInfo.getClassInfo(declaringClass);
    63          }
    64          return classInfo.getConstructor(ReflectHelper.calculateHash(constructor));
    65      }
    66
    67      /***
    68       * Returns the signature for the element.
    69       *
    70       * @return the signature for the element
    71       */
    72      public String getSignature() {
    73          return m_signature;
    74      }
    75
    76      /***
    77       * Returns the attributes.
    78       *
    79       * @return the attributes
    80       * @TODO: fix constructor annotations
    81       */
    82      public List getAnnotations() {
    83          if (m_annotations == null) {
    84              m_annotations = Annotations.getAnnotationInfos((Constructor) m_member);
    85          }
    86          return m_annotations;
    87      }
    88
    89      /***
    90       * Returns the parameter types.
    91       *
    92       * @return the parameter types
    93       */
    94      public ClassInfo[] getParameterTypes() {
    95          if (m_parameterTypes == null) {
    96              Class[] parameterTypes = ((Constructor) m_member).getParameterTypes();
    97              m_parameterTypes = new ClassInfo[parameterTypes.length];
    98              for (int i = 0; i < parameterTypes.length; i++) {
    99                  Class parameterType = parameterTypes[i];
    100                 ClassInfo metaData;
    101                 if (m_classInfoRepository.hasClassInfo(parameterType.getName())) {
    102                     metaData = m_classInfoRepository.getClassInfo(parameterType.getName());
    103                 } else {
    104                     metaData = JavaClassInfo.getClassInfo(parameterType);
    105                     m_classInfoRepository.addClassInfo(metaData);
    106                 }
    107                 m_parameterTypes[i] = metaData;
    108             }
    109         }
    110         return m_parameterTypes;
    111     }
    112
    113     /***
    114      * Returns the exception types.
    115      *
    116      * @return the exception types
    117      */
    118     public ClassInfo[] getExceptionTypes() {
    119         if (m_exceptionTypes == null) {
    120             Class[] exceptionTypes = ((Constructor) m_member).getExceptionTypes();
    121             m_exceptionTypes = new ClassInfo[exceptionTypes.length];
    122             for (int i = 0; i < exceptionTypes.length; i++) {
    123                 Class exceptionType = exceptionTypes[i];
    124                 ClassInfo metaData;
    125                 if (m_classInfoRepository.hasClassInfo(exceptionType.getName())) {
    126                     metaData = m_classInfoRepository.getClassInfo(exceptionType.getName());
    127                 } else {
    128                     metaData = JavaClassInfo.getClassInfo(exceptionType);
    129                     m_classInfoRepository.addClassInfo(metaData);
    130                 }
    131                 m_exceptionTypes[i] = metaData;
    132             }
    133         }
    134         return m_exceptionTypes;
    135     }
    136
    137     public boolean equals(Object o) {
    138         if (this == o) {
    139             return true;
    140         }
    141         if (!(o instanceof ConstructorInfo)) {
    142             return false;
    143         }
    144         ConstructorInfo constructorInfo = (ConstructorInfo) o;
    145         if (!m_declaringType.getName().equals(constructorInfo.getDeclaringType().getName())) {
    146             return false;
    147         }
    148         if (!m_member.getName().equals(constructorInfo.getName())) {
    149             return false;
    150         }
    151         Class[] parameterTypes1 = ((Constructor) m_member).getParameterTypes();
    152         ClassInfo[] parameterTypes2 = constructorInfo.getParameterTypes();
    153         if (parameterTypes1.length != parameterTypes2.length) {
    154             return false;
    155         }
    156         for (int i = 0; i < parameterTypes1.length; i++) {
    157             if (!parameterTypes1[i].getName().equals(parameterTypes2[i].getName())) {
    158                 return false;
    159             }
    160         }
    161         return true;
    162     }
    163
    164     public int hashCode() {
    165         int result = 29;
    166         result = (29 * result) + m_declaringType.getName().hashCode();
    167         result = (29 * result) + m_member.getName().hashCode();
    168         Class[] parameterTypes = ((Constructor) m_member).getParameterTypes();
    169         for (int i = 0; i < parameterTypes.length; i++) {
    170             result = (29 * result) + parameterTypes[i].getName().hashCode();
    171         }
    172         return result;
    173     }
    174 }