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.java;
005:
006: import com.tc.backport175.bytecode.AnnotationElement;
007:
008: import java.lang.reflect.Field;
009:
010: import com.tc.aspectwerkz.reflect.ClassInfo;
011: import com.tc.aspectwerkz.reflect.ReflectHelper;
012: import com.tc.aspectwerkz.reflect.FieldInfo;
013:
014: /**
015: * Implementation of the FieldInfo interface for java.lang.reflect.*.
016: *
017: * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
018: */
019: public class JavaFieldInfo extends JavaMemberInfo implements FieldInfo {
020: /**
021: * The field type.
022: */
023: private ClassInfo m_type = null;
024:
025: /**
026: * The signature of the field.
027: */
028: private String m_signature;
029:
030: /**
031: * Creates a new field java instance.
032: *
033: * @param field
034: * @param declaringType
035: */
036: JavaFieldInfo(final Field field, final JavaClassInfo declaringType) {
037: super (field, declaringType);
038: m_signature = ReflectHelper.getFieldSignature(field);
039: }
040:
041: /**
042: * Returns the field info for the field specified.
043: *
044: * @param field the field
045: * @return the field info
046: */
047: public static FieldInfo getFieldInfo(final Field field) {
048: Class declaringClass = field.getDeclaringClass();
049: JavaClassInfoRepository repository = JavaClassInfoRepository
050: .getRepository(declaringClass.getClassLoader());
051: ClassInfo classInfo = repository.getClassInfo(declaringClass
052: .getName());
053: if (classInfo == null) {
054: classInfo = JavaClassInfo.getClassInfo(declaringClass);
055: }
056: return classInfo.getField(ReflectHelper.calculateHash(field));
057: }
058:
059: /**
060: * Returns the signature for the element.
061: *
062: * @return the signature for the element
063: */
064: public String getSignature() {
065: return m_signature;
066: }
067:
068: public String getGenericsSignature() {
069: // XXX implement
070: throw new RuntimeException();
071: }
072:
073: /**
074: * Returns the annotations.
075: *
076: * @return the annotations
077: */
078: public AnnotationElement.Annotation[] getAnnotations() {
079: return getDeclaringType().getAnnotationReader()
080: .getFieldAnnotationElements(getName(), m_signature);
081: }
082:
083: /**
084: * Returns the type.
085: *
086: * @return the type
087: */
088: public ClassInfo getType() {
089: if (m_type == null) {
090: Class type = ((Field) m_member).getType();
091: if (m_classInfoRepository.hasClassInfo(type.getName())) {
092: m_type = m_classInfoRepository.getClassInfo(type
093: .getName());
094: } else {
095: m_type = JavaClassInfo.getClassInfo(type);
096: m_classInfoRepository.addClassInfo(m_type);
097: }
098: }
099: return m_type;
100: }
101:
102: public boolean equals(Object o) {
103: if (this == o) {
104: return true;
105: }
106: if (!(o instanceof FieldInfo)) {
107: return false;
108: }
109: FieldInfo fieldInfo = (FieldInfo) o;
110: if (!m_declaringType.getName().equals(
111: fieldInfo.getDeclaringType().getName())) {
112: return false;
113: }
114: if (!m_member.getName().equals(fieldInfo.getName())) {
115: return false;
116: }
117: ClassInfo fieldType = fieldInfo.getType();
118: if (!m_type.getName().equals(fieldType.getName())) {
119: return false;
120: }
121: return true;
122: }
123:
124: public int hashCode() {
125: int result = 29;
126: if (m_type == null) {
127: getType();
128: }
129: result = (29 * result) + m_declaringType.getName().hashCode();
130: result = (29 * result) + m_member.getName().hashCode();
131: result = (29 * result) + getType().getName().hashCode();
132: return result;
133: }
134: }
|