KickJava   Java API By Example, From Geeks To Geeks.

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