1 22 package org.jboss.reflect.plugins; 23 24 import java.lang.annotation.Annotation ; 25 import java.lang.reflect.Method ; 26 import java.security.AccessController ; 27 import java.security.PrivilegedAction ; 28 import java.util.HashMap ; 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 46 public class AnnotationValueFactory 47 { 48 49 public static Value createValue(AnnotationHelper annotationHelper, TypeInfo type, Object value) 50 { 51 Value rtnValue = null; 52 if (type instanceof ArrayInfo) 53 { 54 Object [] 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 )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 )value).getName(), type); } 82 83 return rtnValue; 84 } 85 86 public static AnnotationValue createAnnotationValue(TypeInfoFactory typeInfoFactory, AnnotationHelper annotationHelper, AnnotationInfo info, Object ann) 87 { 88 Annotation annotation = (Annotation )ann; 89 Class clazz = annotation.annotationType(); 90 91 Method [] methods = getDeclaredMethods(clazz); 92 93 HashMap <String , Value> attributes = new HashMap <String , Value>(); 94 95 for (int j = 0 ; j < methods.length ; j++) 96 { 97 try 98 { 99 Class typeClass = methods[j].getReturnType(); 100 Object val = methods[j].invoke(annotation, new Object [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 e) 109 { 110 throw new RuntimeException (e); 111 } 112 } 113 return new AnnotationValueImpl(info, attributes); 114 } 115 116 117 private static Object [] getArray(ArrayInfo arrayInfo, Object value) 118 { 119 TypeInfo componentType = arrayInfo.getComponentType(); 120 if (!(componentType instanceof PrimitiveInfo)) 121 { 122 return (Object [])value; 123 } 124 else 125 { 126 Object [] ret = null; 127 String typeName = componentType.getName(); 128 129 if (typeName.equals("boolean")) 130 { 131 boolean[] input = (boolean[])value; 132 ret = new Boolean [input.length]; 133 for (int i = 0 ; i < ret.length ; i++) 134 { 135 ret[i] = new Boolean (input[i]); 136 } 137 } 138 else if (typeName.equals("char")) 139 { 140 char[] input = (char[])value; 141 ret = new Character [input.length]; 142 for (int i = 0 ; i < ret.length ; i++) 143 { 144 ret[i] = new Character (input[i]); 145 } 146 } 147 else if (typeName.equals("double")) 148 { 149 double[] input = (double[])value; 150 ret = new Double [input.length]; 151 for (int i = 0 ; i < ret.length ; i++) 152 { 153 ret[i] = new Double (input[i]); 154 } 155 } 156 else if (typeName.equals("float")) 157 { 158 float[] input = (float[])value; 159 ret = new Float [input.length]; 160 for (int i = 0 ; i < ret.length ; i++) 161 { 162 ret[i] = new Float (input[i]); 163 } 164 165 } 166 else if (typeName.equals("int")) 167 { 168 int[] input = (int[])value; 169 ret = new Integer [input.length]; 170 for (int i = 0 ; i < ret.length ; i++) 171 { 172 ret[i] = new Integer (input[i]); 173 } 174 175 } 176 else if (typeName.equals("long")) 177 { 178 long[] input = (long[])value; 179 ret = new Long [input.length]; 180 for (int i = 0 ; i < ret.length ; i++) 181 { 182 ret[i] = new Long (input[i]); 183 } 184 185 } 186 else if (typeName.equals("short")) 187 { 188 short[] input = (short[])value; 189 ret = new Short [input.length]; 190 for (int i = 0 ; i < ret.length ; i++) 191 { 192 ret[i] = new Short (input[i]); 193 } 194 195 } 196 197 if (ret == null) 198 { 199 throw new RuntimeException ("Array component type " + componentType + " is not handled"); 200 } 201 202 return ret; 203 } 204 } 205 206 private static Method [] getDeclaredMethods(final Class clazz) 207 { 208 if (System.getSecurityManager() == null) 209 return clazz.getDeclaredMethods(); 210 else 211 { 212 PrivilegedAction <Method []> action = new PrivilegedAction <Method []>() 213 { 214 public Method [] run() 215 { 216 return clazz.getDeclaredMethods(); 217 } 218 }; 219 return AccessController.doPrivileged(action); 220 } 221 } 222 } 223 | Popular Tags |