KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > annotation > ProxyMapCreator


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.aop.annotation;
8
9 import javassist.bytecode.annotation.AnnotationMemberValue;
10 import javassist.bytecode.annotation.ArrayMemberValue;
11 import javassist.bytecode.annotation.BooleanMemberValue;
12 import javassist.bytecode.annotation.ByteMemberValue;
13 import javassist.bytecode.annotation.CharMemberValue;
14 import javassist.bytecode.annotation.ClassMemberValue;
15 import javassist.bytecode.annotation.DoubleMemberValue;
16 import javassist.bytecode.annotation.EnumMemberValue;
17 import javassist.bytecode.annotation.FloatMemberValue;
18 import javassist.bytecode.annotation.IntegerMemberValue;
19 import javassist.bytecode.annotation.LongMemberValue;
20 import javassist.bytecode.annotation.MemberValue;
21 import javassist.bytecode.annotation.MemberValueVisitor;
22 import javassist.bytecode.annotation.ShortMemberValue;
23 import javassist.bytecode.annotation.StringMemberValue;
24
25 import java.lang.reflect.Array JavaDoc;
26 import java.lang.reflect.Field JavaDoc;
27 import java.lang.reflect.Method JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.Set JavaDoc;
32
33 /**
34  * Comment
35  *
36  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
37  * @version $Revision: 1.8 $
38  */

39 public class ProxyMapCreator implements MemberValueVisitor
40 {
41    public Object JavaDoc value;
42    private Class JavaDoc type;
43
44
45    public ProxyMapCreator(Class JavaDoc type)
46    {
47       this.type = type;
48    }
49
50    public void visitAnnotationMemberValue(AnnotationMemberValue annotationMemberValue)
51    {
52       try
53       {
54          value = AnnotationProxy.createProxy(annotationMemberValue.getValue());
55       }
56       catch (Exception JavaDoc e)
57       {
58          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
59
}
60    }
61
62    public void visitArrayMemberValue(ArrayMemberValue arrayMemberValue)
63    {
64       Class JavaDoc baseType = type.getComponentType();
65       int size = 0;
66       if (arrayMemberValue.getValue() == null || arrayMemberValue.getValue().length == 0)
67       {
68          size = 0;
69       }
70       else
71       {
72          size = arrayMemberValue.getValue().length;
73       }
74       value = Array.newInstance(baseType, size);
75       MemberValue[] elements = arrayMemberValue.getValue();
76       for (int i = 0; i < size; i++)
77       {
78          ProxyMapCreator creator = new ProxyMapCreator(baseType);
79          elements[i].accept(creator);
80          Array.set(value, i, creator.value);
81       }
82    }
83
84    public void visitBooleanMemberValue(BooleanMemberValue booleanMemberValue)
85    {
86       value = new Boolean JavaDoc(booleanMemberValue.getValue());
87    }
88
89    public void visitByteMemberValue(ByteMemberValue byteMemberValue)
90    {
91       value = new Byte JavaDoc(byteMemberValue.getValue());
92    }
93
94    public void visitCharMemberValue(CharMemberValue charMemberValue)
95    {
96       value = new Character JavaDoc(charMemberValue.getValue());
97    }
98
99    public void visitDoubleMemberValue(DoubleMemberValue doubleMemberValue)
100    {
101       value = new Double JavaDoc(doubleMemberValue.getValue());
102    }
103
104    public void visitEnumMemberValue(EnumMemberValue enumMemberValue)
105    {
106       try
107       {
108          Field JavaDoc enumVal = type.getField(enumMemberValue.getValue());
109          value = enumVal.get(null);
110       }
111       catch (NoSuchFieldException JavaDoc e)
112       {
113          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
114
}
115       catch (SecurityException JavaDoc e)
116       {
117          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
118
}
119       catch (IllegalArgumentException JavaDoc e)
120       {
121          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
122
}
123       catch (IllegalAccessException JavaDoc e)
124       {
125          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
126
}
127    }
128
129    public void visitFloatMemberValue(FloatMemberValue floatMemberValue)
130    {
131       value = new Float JavaDoc(floatMemberValue.getValue());
132    }
133
134    public void visitIntegerMemberValue(IntegerMemberValue integerMemberValue)
135    {
136       value = new Integer JavaDoc(integerMemberValue.getValue());
137    }
138
139    public void visitLongMemberValue(LongMemberValue longMemberValue)
140    {
141       value = new Long JavaDoc(longMemberValue.getValue());
142    }
143
144    public void visitShortMemberValue(ShortMemberValue shortMemberValue)
145    {
146       value = new Short JavaDoc(shortMemberValue.getValue());
147    }
148
149    public void visitStringMemberValue(StringMemberValue stringMemberValue)
150    {
151       value = stringMemberValue.getValue();
152    }
153
154    public void visitClassMemberValue(ClassMemberValue classMemberValue)
155    {
156       try
157       {
158          String JavaDoc classname = classMemberValue.getValue();
159          if (classname.equals("void"))
160          {
161             value = void.class;
162          }
163          else if (classname.equals("int"))
164          {
165             value = int.class;
166          }
167          else if (classname.equals("byte"))
168          {
169             value = byte.class;
170          }
171          else if (classname.equals("long"))
172          {
173             value = long.class;
174          }
175          else if (classname.equals("double"))
176          {
177             value = double.class;
178          }
179          else if (classname.equals("float"))
180          {
181             value = float.class;
182          }
183          else if (classname.equals("char"))
184          {
185             value = char.class;
186          }
187          else if (classname.equals("short"))
188          {
189             value = short.class;
190          }
191          else if (classname.equals("boolean"))
192          {
193             value = boolean.class;
194          }
195          else
196          {
197             value = Thread.currentThread().getContextClassLoader().loadClass(classMemberValue.getValue());
198          }
199       }
200       catch (ClassNotFoundException JavaDoc e)
201       {
202          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
203
}
204
205    }
206
207    public static Class JavaDoc getMemberType(Class JavaDoc annotation, String JavaDoc member)
208    {
209       Method JavaDoc[] methods = annotation.getMethods();
210       for (int i = 0; i < methods.length; i++)
211       {
212          if (methods[i].getName().equals(member))
213          {
214             return methods[i].getReturnType();
215          }
216       }
217       throw new RuntimeException JavaDoc("unable to determine member type for annotation: " + annotation.getName() + "." + member);
218    }
219
220    public static Map JavaDoc createProxyMap(Class JavaDoc annotation, javassist.bytecode.annotation.Annotation info)
221    {
222       //TODO: Need to handle default values for annotations in jdk 1.5
223
Map JavaDoc map = new HashMap JavaDoc();
224
225       if (info.getMemberNames() == null) return map;
226       Set JavaDoc members = info.getMemberNames();
227       Iterator JavaDoc it = members.iterator();
228       while (it.hasNext())
229       {
230          String JavaDoc name = (String JavaDoc) it.next();
231          MemberValue mv = info.getMemberValue(name);
232          ProxyMapCreator creator = new ProxyMapCreator(getMemberType(annotation, name));
233          mv.accept(creator);
234          map.put(name, creator.value);
235       }
236       return map;
237    }
238 }
239
Popular Tags