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.FieldInfo;
13  import org.codehaus.aspectwerkz.reflect.ReflectHelper;
14  
15  import java.lang.reflect.Field;
16  import java.util.List;
17  
18  /***
19   * Implementation of the FieldInfo interface for java.lang.reflect.*.
20   *
21   * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
22   */
23  public class JavaFieldInfo extends JavaMemberInfo implements FieldInfo {
24      /***
25       * The field type.
26       */
27      private ClassInfo m_type = null;
28  
29      /***
30       * The signature of the field.
31       */
32      private String m_signature;
33  
34      /***
35       * Creates a new field java instance.
36       *
37       * @param field
38       * @param declaringType
39       */
40      JavaFieldInfo(final Field field, final JavaClassInfo declaringType) {
41          super(field, declaringType);
42          m_signature = ReflectHelper.getFieldSignature(field);
43      }
44  
45      /***
46       * Returns the field info for the field specified.
47       *
48       * @param field the field
49       * @return the field info
50       */
51      public static FieldInfo getFieldInfo(final Field field) {
52          Class declaringClass = field.getDeclaringClass();
53          JavaClassInfoRepository repository = JavaClassInfoRepository.getRepository(declaringClass.getClassLoader());
54          ClassInfo classInfo = repository.getClassInfo(declaringClass.getName());
55          if (classInfo == null) {
56              classInfo = JavaClassInfo.getClassInfo(declaringClass);
57          }
58          return classInfo.getField(ReflectHelper.calculateHash(field));
59      }
60  
61      /***
62       * Returns the signature for the element.
63       *
64       * @return the signature for the element
65       */
66      public String getSignature() {
67          return m_signature;
68      }
69  
70      /***
71       * Returns the annotations.
72       *
73       * @return the annotations
74       */
75      public List getAnnotations() {
76          if (m_annotations == null) {
77              m_annotations = Annotations.getAnnotationInfos((Field) m_member);
78          }
79          return m_annotations;
80      }
81  
82      /***
83       * Returns the type.
84       *
85       * @return the type
86       */
87      public ClassInfo getType() {
88          if (m_type == null) {
89              Class type = ((Field) m_member).getType();
90              if (m_classInfoRepository.hasClassInfo(type.getName())) {
91                  m_type = m_classInfoRepository.getClassInfo(type.getName());
92              } else {
93                  m_type = JavaClassInfo.getClassInfo(type);
94                  m_classInfoRepository.addClassInfo(m_type);
95              }
96          }
97          return m_type;
98      }
99  
100     public boolean equals(Object o) {
101         if (this == o) {
102             return true;
103         }
104         if (!(o instanceof FieldInfo)) {
105             return false;
106         }
107         FieldInfo fieldInfo = (FieldInfo) o;
108         if (!m_declaringType.getName().equals(fieldInfo.getDeclaringType().getName())) {
109             return false;
110         }
111         if (!m_member.getName().equals(fieldInfo.getName())) {
112             return false;
113         }
114         ClassInfo fieldType = fieldInfo.getType();
115         if (!m_type.getName().equals(fieldType.getName())) {
116             return false;
117         }
118         return true;
119     }
120 
121     public int hashCode() {
122         int result = 29;
123         if (m_type == null) {
124             getType();
125         }
126         result = (29 * result) + m_declaringType.getName().hashCode();
127         result = (29 * result) + m_member.getName().hashCode();
128         result = (29 * result) + getType().getName().hashCode();
129         return result;
130     }
131 }