KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > reflect > plugins > javassist > JavassistFieldInfo


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.reflect.plugins.javassist;
23
24 import org.jboss.reflect.plugins.AnnotationHelper;
25 import org.jboss.reflect.spi.AnnotationValue;
26 import org.jboss.reflect.spi.ClassInfo;
27 import org.jboss.reflect.spi.FieldInfo;
28 import org.jboss.reflect.spi.TypeInfo;
29 import org.jboss.util.JBossStringBuilder;
30
31 import javassist.CtClass;
32 import javassist.CtField;
33 import javassist.Modifier;
34 import javassist.NotFoundException;
35
36 /**
37  * JavassistFieldInfo.
38  *
39  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
40  * @version $Revision: 57133 $
41  */

42 public class JavassistFieldInfo extends JavassistAnnotatedInfo implements FieldInfo
43 {
44    /** The reflection factory */
45    private static final JavassistReflectionFactory reflectionFactory = new JavassistReflectionFactory(true);
46
47    /** The field */
48    private CtField ctField;
49    
50    /** The field implementation */
51    private transient JavassistField field;
52
53    /** The type */
54    private transient TypeInfo fieldType;
55    
56    /** The type info */
57    protected JavassistTypeInfo typeInfo;
58
59    /**
60     * Create a new JavassistFieldInfo.
61     *
62     * @param annotationHelper the annotation helper
63     * @param typeInfo the type info
64     * @param ctField the field
65     */

66    public JavassistFieldInfo(AnnotationHelper annotationHelper, JavassistTypeInfo typeInfo, CtField ctField)
67    {
68       super(annotationHelper);
69       this.typeInfo = typeInfo;
70       this.ctField = ctField;
71    }
72
73    public String JavaDoc getName()
74    {
75       return ctField.getName();
76    }
77
78    public int getModifiers()
79    {
80       return ctField.getModifiers();
81    }
82
83    public boolean isPublic()
84    {
85       return Modifier.isPublic(getModifiers());
86    }
87
88    public boolean isStatic()
89    {
90       return Modifier.isStatic(getModifiers());
91    }
92
93    public ClassInfo getDeclaringClass()
94    {
95       return typeInfo;
96    }
97
98    public TypeInfo getType()
99    {
100       if (fieldType != null)
101          return fieldType;
102       try
103       {
104          CtClass clazz = ctField.getType();
105          fieldType = typeInfo.getFactory().getTypeInfo(clazz);
106          return fieldType;
107       }
108       catch (NotFoundException e)
109       {
110          throw JavassistTypeInfoFactoryImpl.raiseFieldNotFound(getName(), e);
111       }
112    }
113
114    public Object JavaDoc get(Object JavaDoc target) throws Throwable JavaDoc
115    {
116       if (field == null)
117          field = reflectionFactory.createField(ctField);
118       return field.get(target);
119    }
120
121    public Object JavaDoc set(Object JavaDoc target, Object JavaDoc value) throws Throwable JavaDoc
122    {
123       if (field == null)
124          field = reflectionFactory.createField(ctField);
125       field.set(target, value);
126       return null;
127    }
128
129    protected int getHashCode()
130    {
131       return getName().hashCode();
132    }
133
134    public boolean equals(Object JavaDoc obj)
135    {
136       if (obj == this)
137          return true;
138       if (obj == null || obj instanceof FieldInfo == false)
139          return false;
140       
141       FieldInfo other = (FieldInfo) obj;
142       if (getName().equals(other.getName()) == false)
143          return false;
144       return getDeclaringClass().equals(other.getDeclaringClass());
145    }
146
147    public void toShortString(JBossStringBuilder buffer)
148    {
149       buffer.append(getName());
150    }
151
152    protected void toString(JBossStringBuilder buffer)
153    {
154       buffer.append("name=").append(getName());
155       super.toString(buffer);
156    }
157    
158    public AnnotationValue[] getAnnotations()
159    {
160       return getAnnotations(ctField);
161    }
162 }
163
Popular Tags