KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > reflect > plugins > AnnotationValueFactory


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.reflect.plugins;
23
24 import java.lang.annotation.Annotation JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26 import java.security.AccessController JavaDoc;
27 import java.security.PrivilegedAction JavaDoc;
28 import java.util.HashMap JavaDoc;
29
30 import org.jboss.reflect.spi.AnnotationInfo;
31 import org.jboss.reflect.spi.AnnotationValue;
32 import org.jboss.reflect.spi.ArrayInfo;
33 import org.jboss.reflect.spi.ClassInfo;
34 import org.jboss.reflect.spi.EnumInfo;
35 import org.jboss.reflect.spi.PrimitiveInfo;
36 import org.jboss.reflect.spi.PrimitiveValue;
37 import org.jboss.reflect.spi.TypeInfo;
38 import org.jboss.reflect.spi.TypeInfoFactory;
39 import org.jboss.reflect.spi.Value;
40
41 /**
42  *
43  * @author <a HREF="kabir.khan@jboss.com">Kabir Khan</a>
44  * @version $Revision: 57306 $
45  */

46 public class AnnotationValueFactory
47 {
48
49    public static Value createValue(AnnotationHelper annotationHelper, TypeInfo type, Object JavaDoc value)
50    {
51       Value rtnValue = null;
52       if (type instanceof ArrayInfo)
53       {
54          Object JavaDoc[] objects = getArray((ArrayInfo)type, value);
55          Value[] values = new Value[objects.length];
56          for (int i = 0 ; i < objects.length ; i++)
57          {
58             values[i] = createValue(annotationHelper, ((ArrayInfo)type).getComponentType(), objects[i]);
59          }
60          rtnValue = new ArrayValueImpl(type, values);
61       }
62       else if (type instanceof PrimitiveInfo)
63       {
64          rtnValue = new PrimitiveValue(value.toString(), (PrimitiveInfo)type);
65       }
66       else if (type.getName().equals("java.lang.String"))
67       {
68          rtnValue = new StringValueImpl((String JavaDoc)value, type);
69       }
70       else if (type instanceof AnnotationInfo)
71       {
72          rtnValue = annotationHelper.createAnnotationValue((AnnotationInfo)type, value);
73       }
74       else if (type instanceof EnumInfo)
75       {
76          rtnValue = new EnumValueImpl(type, value.toString());
77       }
78       else if (type instanceof ClassInfo)
79       {
80          rtnValue = new ClassValueImpl(((Class JavaDoc)value).getName(), type); //FixMe - do not depend on Class
81
}
82       
83       return rtnValue;
84    }
85
86    public static AnnotationValue createAnnotationValue(TypeInfoFactory typeInfoFactory, AnnotationHelper annotationHelper, AnnotationInfo info, Object JavaDoc ann)
87    {
88       Annotation JavaDoc annotation = (Annotation JavaDoc)ann;
89       Class JavaDoc clazz = annotation.annotationType();
90       
91       Method JavaDoc[] methods = getDeclaredMethods(clazz);
92       
93       HashMap JavaDoc<String JavaDoc, Value> attributes = new HashMap JavaDoc<String JavaDoc, Value>();
94       
95       for (int j = 0 ; j < methods.length ; j++)
96       {
97          try
98          {
99             Class JavaDoc typeClass = methods[j].getReturnType();
100             Object JavaDoc val = methods[j].invoke(annotation, new Object JavaDoc[0]);
101
102             TypeInfo typeInfo = typeInfoFactory.getTypeInfo(typeClass);
103
104             Value value = createValue(annotationHelper, typeInfo, val);
105             
106             attributes.put(methods[j].getName(), value);
107          }
108          catch (Throwable JavaDoc e)
109          {
110             throw new RuntimeException JavaDoc(e);
111          }
112       }
113       return new AnnotationValueImpl(info, attributes);
114    }
115
116    
117    private static Object JavaDoc[] getArray(ArrayInfo arrayInfo, Object JavaDoc value)
118    {
119       TypeInfo componentType = arrayInfo.getComponentType();
120       if (!(componentType instanceof PrimitiveInfo))
121       {
122          return (Object JavaDoc[])value;
123       }
124       else
125       {
126          Object JavaDoc[] ret = null;
127          String JavaDoc typeName = componentType.getName();
128
129          if (typeName.equals("boolean"))
130          {
131             boolean[] input = (boolean[])value;
132             ret = new Boolean JavaDoc[input.length];
133             for (int i = 0 ; i < ret.length ; i++)
134             {
135                ret[i] = new Boolean JavaDoc(input[i]);
136             }
137          }
138          else if (typeName.equals("char"))
139          {
140             char[] input = (char[])value;
141             ret = new Character JavaDoc[input.length];
142             for (int i = 0 ; i < ret.length ; i++)
143             {
144                ret[i] = new Character JavaDoc(input[i]);
145             }
146          }
147          else if (typeName.equals("double"))
148          {
149             double[] input = (double[])value;
150             ret = new Double JavaDoc[input.length];
151             for (int i = 0 ; i < ret.length ; i++)
152             {
153                ret[i] = new Double JavaDoc(input[i]);
154             }
155          }
156          else if (typeName.equals("float"))
157          {
158             float[] input = (float[])value;
159             ret = new Float JavaDoc[input.length];
160             for (int i = 0 ; i < ret.length ; i++)
161             {
162                ret[i] = new Float JavaDoc(input[i]);
163             }
164
165          }
166          else if (typeName.equals("int"))
167          {
168             int[] input = (int[])value;
169             ret = new Integer JavaDoc[input.length];
170             for (int i = 0 ; i < ret.length ; i++)
171             {
172                ret[i] = new Integer JavaDoc(input[i]);
173             }
174             
175          }
176          else if (typeName.equals("long"))
177          {
178             long[] input = (long[])value;
179             ret = new Long JavaDoc[input.length];
180             for (int i = 0 ; i < ret.length ; i++)
181             {
182                ret[i] = new Long JavaDoc(input[i]);
183             }
184
185          }
186          else if (typeName.equals("short"))
187          {
188             short[] input = (short[])value;
189             ret = new Short JavaDoc[input.length];
190             for (int i = 0 ; i < ret.length ; i++)
191             {
192                ret[i] = new Short JavaDoc(input[i]);
193             }
194             
195          }
196          
197          if (ret == null)
198          {
199             throw new RuntimeException JavaDoc("Array component type " + componentType + " is not handled");
200          }
201           
202          return ret;
203       }
204    }
205    
206    private static Method JavaDoc[] getDeclaredMethods(final Class JavaDoc clazz)
207    {
208       if (System.getSecurityManager() == null)
209          return clazz.getDeclaredMethods();
210       else
211       {
212          PrivilegedAction JavaDoc<Method JavaDoc[]> action = new PrivilegedAction JavaDoc<Method JavaDoc[]>()
213          {
214             public Method JavaDoc[] run()
215             {
216                return clazz.getDeclaredMethods();
217             }
218          };
219          return AccessController.doPrivileged(action);
220       }
221    }
222 }
223
Popular Tags