KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > annotation > factory > javassist > MemberValueGetter


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, 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.annotation.factory.javassist;
23
24 import java.lang.reflect.Array JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26
27 import org.jboss.reflect.plugins.introspection.IntrospectionTypeInfoFactoryImpl;
28
29 import javassist.bytecode.annotation.Annotation;
30 import javassist.bytecode.annotation.AnnotationMemberValue;
31 import javassist.bytecode.annotation.ArrayMemberValue;
32 import javassist.bytecode.annotation.BooleanMemberValue;
33 import javassist.bytecode.annotation.ByteMemberValue;
34 import javassist.bytecode.annotation.CharMemberValue;
35 import javassist.bytecode.annotation.ClassMemberValue;
36 import javassist.bytecode.annotation.DoubleMemberValue;
37 import javassist.bytecode.annotation.EnumMemberValue;
38 import javassist.bytecode.annotation.FloatMemberValue;
39 import javassist.bytecode.annotation.IntegerMemberValue;
40 import javassist.bytecode.annotation.LongMemberValue;
41 import javassist.bytecode.annotation.MemberValue;
42 import javassist.bytecode.annotation.MemberValueVisitor;
43 import javassist.bytecode.annotation.ShortMemberValue;
44 import javassist.bytecode.annotation.StringMemberValue;
45
46 /**
47  *
48  * @author <a HREF="kabir.khan@jboss.com">Kabir Khan</a>
49  * @version $Revision: 57133 $
50  */

51 public class MemberValueGetter implements MemberValueVisitor
52 {
53    Object JavaDoc value;
54    Method JavaDoc method;
55    static IntrospectionTypeInfoFactoryImpl typeFactory = new IntrospectionTypeInfoFactoryImpl();
56
57    public MemberValueGetter(Method JavaDoc method)
58    {
59       this.method = method;
60    }
61    
62    public Object JavaDoc getValue()
63    {
64       return value;
65    }
66    
67    public void visitAnnotationMemberValue(AnnotationMemberValue node)
68    {
69       try
70       {
71          Annotation ann = node.getValue();
72          value = AnnotationProxy.createProxy(ann);
73       }
74       catch (Exception JavaDoc e)
75       {
76          throw new RuntimeException JavaDoc(e);
77       }
78    }
79
80    public void visitArrayMemberValue(ArrayMemberValue node)
81    {
82       MemberValue[] values = node.getValue();
83       value = node.getValue();
84       Object JavaDoc vals = Array.newInstance(getAttributeType(), values.length);
85       
86       for (int i = 0 ; i < values.length ; i++)
87       {
88          values[i].accept(this);
89          Array.set(vals, i, value);
90       }
91
92       value = vals;
93    }
94
95    public void visitBooleanMemberValue(BooleanMemberValue node)
96    {
97       value = new Boolean JavaDoc(node.getValue());
98    }
99
100    public void visitByteMemberValue(ByteMemberValue node)
101    {
102       value = new Byte JavaDoc(node.getValue());
103    }
104
105    public void visitCharMemberValue(CharMemberValue node)
106    {
107       value = new Character JavaDoc(node.getValue());
108    }
109
110    public void visitDoubleMemberValue(DoubleMemberValue node)
111    {
112       value = new Double JavaDoc(node.getValue());
113    }
114
115    @SuppressWarnings JavaDoc("unchecked")
116    public void visitEnumMemberValue(EnumMemberValue node)
117    {
118       value = Enum.valueOf(getAttributeType(), node.getValue());
119    }
120
121    public void visitFloatMemberValue(FloatMemberValue node)
122    {
123       value = new Float JavaDoc(node.getValue());
124    }
125
126    public void visitIntegerMemberValue(IntegerMemberValue node)
127    {
128       value = new Integer JavaDoc(node.getValue());
129    }
130
131    public void visitLongMemberValue(LongMemberValue node)
132    {
133       value = new Long JavaDoc(node.getValue());
134    }
135
136    public void visitShortMemberValue(ShortMemberValue node)
137    {
138       value = new Short JavaDoc(node.getValue());
139    }
140
141    public void visitStringMemberValue(StringMemberValue node)
142    {
143       value = node.getValue();
144    }
145
146    public void visitClassMemberValue(ClassMemberValue node)
147    {
148       try
149       {
150          value = Class.forName(node.getValue());
151       }
152       catch (ClassNotFoundException JavaDoc e)
153       {
154          throw new RuntimeException JavaDoc(e);
155       }
156    }
157
158    private Class JavaDoc getAttributeType()
159    {
160       Class JavaDoc rtn = method.getReturnType();
161       
162       while (rtn.isArray())
163       {
164          rtn = rtn.getComponentType();
165       }
166       
167       return rtn;
168    }
169    
170 }
171
Popular Tags