001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.aspectwerkz.reflect.impl.asm;
005:
006: import com.tc.backport175.bytecode.AnnotationElement.Annotation;
007:
008: import com.tc.asm.Type;
009:
010: import com.tc.aspectwerkz.transform.inlining.AsmHelper;
011: import com.tc.aspectwerkz.reflect.ClassInfo;
012: import com.tc.aspectwerkz.reflect.FieldInfo;
013:
014: /**
015: * ASM implementation of the FieldInfo interface.
016: *
017: * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
018: */
019: public class AsmFieldInfo extends AsmMemberInfo implements FieldInfo {
020:
021: /**
022: * The field type name.
023: */
024: private String m_typeName;
025:
026: /**
027: * The field type.
028: */
029: private ClassInfo m_type = null;
030:
031: /**
032: * Creates a new field java instance.
033: *
034: * @param field
035: * @param declaringType
036: * @param loader
037: */
038: AsmFieldInfo(final FieldStruct field, final String declaringType,
039: final ClassLoader loader) {
040: super (field, declaringType, loader);
041: m_typeName = Type.getType(field.desc).getClassName();
042: }
043:
044: /**
045: * Returns the field info for the field specified.
046: *
047: * @param fieldName
048: * @param fieldDesc
049: * @param bytecode
050: * @param loader
051: * @return the field info
052: */
053: public static FieldInfo getFieldInfo(final String fieldName,
054: final String fieldDesc, final byte[] bytecode,
055: final ClassLoader loader) {
056: String className = AsmClassInfo
057: .retrieveClassNameFromBytecode(bytecode);
058: AsmClassInfoRepository repository = AsmClassInfoRepository
059: .getRepository(loader);
060: ClassInfo classInfo = repository.getClassInfo(className);
061: if (classInfo == null) {
062: classInfo = AsmClassInfo.getClassInfo(bytecode, loader);
063: }
064: return classInfo.getField(AsmHelper.calculateFieldHash(
065: fieldName, fieldDesc));
066: }
067:
068: /**
069: * Returns the signature for the element.
070: *
071: * @return the signature for the element
072: */
073: public String getSignature() {
074: return AsmHelper.getFieldDescriptor(this );
075: }
076:
077: public String getGenericsSignature() {
078: return m_member.signature;
079: }
080:
081: /**
082: * Returns the type.
083: *
084: * @return the type
085: */
086: public ClassInfo getType() {
087: if (m_type == null) {
088: m_type = AsmClassInfo.getClassInfo(m_typeName,
089: (ClassLoader) m_loaderRef.get());
090: }
091: return m_type;
092: }
093:
094: /**
095: * Returns the annotations.
096: *
097: * @return the annotations
098: */
099: public Annotation[] getAnnotations() {
100: return getDeclaringType().getAnnotationReader()
101: .getFieldAnnotationElements(m_member.name,
102: m_member.desc);
103: }
104:
105: public boolean equals(Object o) {
106: if (this == o) {
107: return true;
108: }
109: if (!(o instanceof FieldInfo)) {
110: return false;
111: }
112: FieldInfo fieldInfo = (FieldInfo) o;
113: if (!m_declaringTypeName.equals(fieldInfo.getDeclaringType()
114: .getName())) {
115: return false;
116: }
117: if (!m_member.name.equals(fieldInfo.getName())) {
118: return false;
119: }
120: if (!m_typeName.equals(fieldInfo.getType().getName())) {
121: return false;
122: }
123: return true;
124: }
125:
126: public int hashCode() {
127: int result = 29;
128: result = (29 * result) + m_declaringTypeName.hashCode();
129: result = (29 * result) + m_member.name.hashCode();
130: result = (29 * result) + m_typeName.hashCode();
131: return result;
132: }
133:
134: public String toString() {
135: StringBuffer sb = new StringBuffer(m_declaringTypeName);
136: sb.append('.').append(m_member.name).append(' ');
137: sb.append(m_member.desc);
138: return sb.toString();
139: }
140: }
|