KickJava   Java API By Example, From Geeks To Geeks.

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

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