KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > annotation > factory > duplicate > javassist > ProxyMapCreator


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 javassist.bytecode.annotation.AnnotationMemberValue;
25 import javassist.bytecode.annotation.ArrayMemberValue;
26 import javassist.bytecode.annotation.BooleanMemberValue;
27 import javassist.bytecode.annotation.ByteMemberValue;
28 import javassist.bytecode.annotation.CharMemberValue;
29 import javassist.bytecode.annotation.ClassMemberValue;
30 import javassist.bytecode.annotation.DoubleMemberValue;
31 import javassist.bytecode.annotation.EnumMemberValue;
32 import javassist.bytecode.annotation.FloatMemberValue;
33 import javassist.bytecode.annotation.IntegerMemberValue;
34 import javassist.bytecode.annotation.LongMemberValue;
35 import javassist.bytecode.annotation.MemberValue;
36 import javassist.bytecode.annotation.MemberValueVisitor;
37 import javassist.bytecode.annotation.ShortMemberValue;
38 import javassist.bytecode.annotation.StringMemberValue;
39
40 import java.lang.reflect.Array JavaDoc;
41 import java.lang.reflect.Field JavaDoc;
42 import java.lang.reflect.Method JavaDoc;
43 import java.util.HashMap JavaDoc;
44 import java.util.Iterator JavaDoc;
45 import java.util.Map JavaDoc;
46 import java.util.Set JavaDoc;
47
48 /**
49  * Comment
50  *
51  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
52  * @version $Revision: 57050 $
53  */

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