1 /***************************************************************************************
2 * Copyright (c) Jonas Bonér, Alexandre Vasseur. All rights reserved. *
3 * http://aspectwerkz.codehaus.org *
4 * ---------------------------------------------------------------------------------- *
5 * The software in this package is published under the terms of the LGPL license *
6 * a copy of which has been included with this distribution in the license.txt file. *
7 **************************************************************************************/
8 package org.codehaus.aspectwerkz.reflect.impl.java;
9
10 import org.codehaus.aspectwerkz.annotation.Annotations;
11 import org.codehaus.aspectwerkz.reflect.ClassInfo;
12 import org.codehaus.aspectwerkz.reflect.FieldInfo;
13 import org.codehaus.aspectwerkz.transform.ReflectHelper;
14
15 import java.lang.reflect.Field;
16 import java.util.List;
17
18 /***
19 * Implementation of the FieldInfo interface for java.lang.reflect.*.
20 *
21 * @author <a href="mailto:jboner@codehaus.org">Jonas Bonér </a>
22 */
23 public class JavaFieldInfo extends JavaMemberInfo implements FieldInfo {
24 /***
25 * The field type.
26 */
27 private ClassInfo m_type = null;
28
29 /***
30 * Creates a new field java instance.
31 *
32 * @param field
33 * @param declaringType
34 */
35 JavaFieldInfo(final Field field, final JavaClassInfo declaringType) {
36 super(field, declaringType);
37 }
38
39 /***
40 * Returns the field info for the field specified.
41 *
42 * @param field the field
43 * @return the field info
44 */
45 public static FieldInfo getFieldInfo(final Field field) {
46 Class declaringClass = field.getDeclaringClass();
47 JavaClassInfoRepository repository = JavaClassInfoRepository.getRepository(declaringClass.getClassLoader());
48 ClassInfo classInfo = repository.getClassInfo(declaringClass.getName());
49 if (classInfo == null) {
50 classInfo = JavaClassInfo.getClassInfo(declaringClass);
51 }
52 return classInfo.getField(ReflectHelper.calculateHash(field));
53 }
54
55 /***
56 * Returns the annotations.
57 *
58 * @return the annotations
59 */
60 public List getAnnotations() {
61 if (m_annotations == null) {
62 m_annotations = Annotations.getAnnotationInfos((Field) m_member);
63 }
64 return m_annotations;
65 }
66
67 /***
68 * Returns the type.
69 *
70 * @return the type
71 */
72 public ClassInfo getType() {
73 if (m_type == null) {
74 Class type = ((Field) m_member).getType();
75 if (m_classInfoRepository.hasClassInfo(type.getName())) {
76 m_type = m_classInfoRepository.getClassInfo(type.getName());
77 } else {
78 m_type = JavaClassInfo.getClassInfo(type);
79 m_classInfoRepository.addClassInfo(m_type);
80 }
81 }
82 return m_type;
83 }
84
85 public boolean equals(Object o) {
86 if (this == o) {
87 return true;
88 }
89 if (!(o instanceof FieldInfo)) {
90 return false;
91 }
92 FieldInfo fieldInfo = (FieldInfo) o;
93 if (!m_declaringType.getName().equals(fieldInfo.getDeclaringType().getName())) {
94 return false;
95 }
96 if (!m_member.getName().equals(fieldInfo.getName())) {
97 return false;
98 }
99 ClassInfo fieldType = fieldInfo.getType();
100 if (!m_type.getName().equals(fieldType.getName())) {
101 return false;
102 }
103 return true;
104 }
105
106 public int hashCode() {
107 int result = 29;
108 if (m_type == null) {
109 getType();
110 }
111 result = (29 * result) + m_declaringType.getName().hashCode();
112 result = (29 * result) + m_member.getName().hashCode();
113 result = (29 * result) + getType().getName().hashCode();
114 return result;
115 }
116 }