KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > aspectwerkz > reflect > impl > java > JavaFieldInfo


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.aspectwerkz.reflect.impl.java;
5
6 import com.tc.backport175.bytecode.AnnotationElement;
7
8
9 import java.lang.reflect.Field JavaDoc;
10
11 import com.tc.aspectwerkz.reflect.ClassInfo;
12 import com.tc.aspectwerkz.reflect.ReflectHelper;
13 import com.tc.aspectwerkz.reflect.FieldInfo;
14
15 /**
16  * Implementation of the FieldInfo interface for java.lang.reflect.*.
17  *
18  * @author <a HREF="mailto:jboner@codehaus.org">Jonas BonŽr </a>
19  */

20 public class JavaFieldInfo extends JavaMemberInfo implements FieldInfo {
21   /**
22    * The field type.
23    */

24   private ClassInfo m_type = null;
25
26   /**
27    * The signature of the field.
28    */

29   private String JavaDoc m_signature;
30
31   /**
32    * Creates a new field java instance.
33    *
34    * @param field
35    * @param declaringType
36    */

37   JavaFieldInfo(final Field JavaDoc field, final JavaClassInfo declaringType) {
38     super(field, declaringType);
39     m_signature = ReflectHelper.getFieldSignature(field);
40   }
41
42   /**
43    * Returns the field info for the field specified.
44    *
45    * @param field the field
46    * @return the field info
47    */

48   public static FieldInfo getFieldInfo(final Field JavaDoc field) {
49     Class JavaDoc declaringClass = field.getDeclaringClass();
50     JavaClassInfoRepository repository = JavaClassInfoRepository.getRepository(declaringClass.getClassLoader());
51     ClassInfo classInfo = repository.getClassInfo(declaringClass.getName());
52     if (classInfo == null) {
53       classInfo = JavaClassInfo.getClassInfo(declaringClass);
54     }
55     return classInfo.getField(ReflectHelper.calculateHash(field));
56   }
57
58   /**
59    * Returns the signature for the element.
60    *
61    * @return the signature for the element
62    */

63   public String JavaDoc getSignature() {
64     return m_signature;
65   }
66   
67   public String JavaDoc getGenericsSignature() {
68     // XXX implement
69
throw new RuntimeException JavaDoc();
70   }
71
72   /**
73    * Returns the annotations.
74    *
75    * @return the annotations
76    */

77   public AnnotationElement.Annotation[] getAnnotations() {
78     return getDeclaringType().getAnnotationReader().getFieldAnnotationElements(getName(), m_signature);
79   }
80
81   /**
82    * Returns the type.
83    *
84    * @return the type
85    */

86   public ClassInfo getType() {
87     if (m_type == null) {
88       Class JavaDoc type = ((Field JavaDoc) m_member).getType();
89       if (m_classInfoRepository.hasClassInfo(type.getName())) {
90         m_type = m_classInfoRepository.getClassInfo(type.getName());
91       } else {
92         m_type = JavaClassInfo.getClassInfo(type);
93         m_classInfoRepository.addClassInfo(m_type);
94       }
95     }
96     return m_type;
97   }
98
99   public boolean equals(Object JavaDoc o) {
100     if (this == o) {
101       return true;
102     }
103     if (!(o instanceof FieldInfo)) {
104       return false;
105     }
106     FieldInfo fieldInfo = (FieldInfo) o;
107     if (!m_declaringType.getName().equals(fieldInfo.getDeclaringType().getName())) {
108       return false;
109     }
110     if (!m_member.getName().equals(fieldInfo.getName())) {
111       return false;
112     }
113     ClassInfo fieldType = fieldInfo.getType();
114     if (!m_type.getName().equals(fieldType.getName())) {
115       return false;
116     }
117     return true;
118   }
119
120   public int hashCode() {
121     int result = 29;
122     if (m_type == null) {
123       getType();
124     }
125     result = (29 * result) + m_declaringType.getName().hashCode();
126     result = (29 * result) + m_member.getName().hashCode();
127     result = (29 * result) + getType().getName().hashCode();
128     return result;
129   }
130 }
Popular Tags