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