KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > aspectwerkz > reflect > impl > asm > AsmFieldInfo


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.asm;
5
6 import com.tc.backport175.bytecode.AnnotationElement.Annotation;
7
8 import com.tc.asm.Type;
9
10 import com.tc.aspectwerkz.transform.inlining.AsmHelper;
11 import com.tc.aspectwerkz.reflect.ClassInfo;
12 import com.tc.aspectwerkz.reflect.FieldInfo;
13
14 /**
15  * ASM implementation of the FieldInfo interface.
16  *
17  * @author <a HREF="mailto:jboner@codehaus.org">Jonas BonŽr </a>
18  */

19 public class AsmFieldInfo extends AsmMemberInfo implements FieldInfo {
20
21   /**
22    * The field type name.
23    */

24   private String JavaDoc m_typeName;
25
26   /**
27    * The field type.
28    */

29   private ClassInfo m_type = null;
30
31   /**
32    * Creates a new field java instance.
33    *
34    * @param field
35    * @param declaringType
36    * @param loader
37    */

38   AsmFieldInfo(final FieldStruct field, final String JavaDoc declaringType, final ClassLoader JavaDoc loader) {
39     super(field, declaringType, loader);
40     m_typeName = Type.getType(field.desc).getClassName();
41   }
42
43   /**
44    * Returns the field info for the field specified.
45    *
46    * @param fieldName
47    * @param fieldDesc
48    * @param bytecode
49    * @param loader
50    * @return the field info
51    */

52   public static FieldInfo getFieldInfo(final String JavaDoc fieldName,
53                                        final String JavaDoc fieldDesc,
54                                        final byte[] bytecode,
55                                        final ClassLoader JavaDoc loader) {
56     String JavaDoc className = AsmClassInfo.retrieveClassNameFromBytecode(bytecode);
57     AsmClassInfoRepository repository = AsmClassInfoRepository.getRepository(loader);
58     ClassInfo classInfo = repository.getClassInfo(className);
59     if (classInfo == null) {
60       classInfo = AsmClassInfo.getClassInfo(bytecode, loader);
61     }
62     return classInfo.getField(AsmHelper.calculateFieldHash(fieldName, fieldDesc));
63   }
64
65   /**
66    * Returns the signature for the element.
67    *
68    * @return the signature for the element
69    */

70   public String JavaDoc getSignature() {
71     return AsmHelper.getFieldDescriptor(this);
72   }
73   
74   public String JavaDoc getGenericsSignature() {
75     return m_member.signature;
76   }
77
78   /**
79    * Returns the type.
80    *
81    * @return the type
82    */

83   public ClassInfo getType() {
84     if (m_type == null) {
85       m_type = AsmClassInfo.getClassInfo(m_typeName, (ClassLoader JavaDoc) m_loaderRef.get());
86     }
87     return m_type;
88   }
89
90   /**
91    * Returns the annotations.
92    *
93    * @return the annotations
94    */

95   public Annotation[] getAnnotations() {
96     return getDeclaringType().getAnnotationReader().getFieldAnnotationElements(m_member.name, m_member.desc);
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_declaringTypeName.equals(fieldInfo.getDeclaringType().getName())) {
108       return false;
109     }
110     if (!m_member.name.equals(fieldInfo.getName())) {
111       return false;
112     }
113     if (!m_typeName.equals(fieldInfo.getType().getName())) {
114       return false;
115     }
116     return true;
117   }
118
119   public int hashCode() {
120     int result = 29;
121     result = (29 * result) + m_declaringTypeName.hashCode();
122     result = (29 * result) + m_member.name.hashCode();
123     result = (29 * result) + m_typeName.hashCode();
124     return result;
125   }
126
127   public String JavaDoc toString() {
128     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(m_declaringTypeName);
129     sb.append('.').append(m_member.name).append(' ');
130     sb.append(m_member.desc);
131     return sb.toString();
132   }
133 }
Popular Tags