1 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 ; 41 import java.lang.reflect.Field ; 42 import java.lang.reflect.Method ; 43 import java.util.HashMap ; 44 import java.util.Iterator ; 45 import java.util.Map ; 46 import java.util.Set ; 47 48 54 public class ProxyMapCreator implements MemberValueVisitor 55 { 56 public Object value; 57 private Class type; 58 59 60 public ProxyMapCreator(Class 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 e) 72 { 73 throw new RuntimeException (e); } 75 } 76 77 public void visitArrayMemberValue(ArrayMemberValue arrayMemberValue) 78 { 79 Class 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 (booleanMemberValue.getValue()); 98 } 99 100 public void visitByteMemberValue(ByteMemberValue byteMemberValue) 101 { 102 value = new Byte (byteMemberValue.getValue()); 103 } 104 105 public void visitCharMemberValue(CharMemberValue charMemberValue) 106 { 107 value = new Character (charMemberValue.getValue()); 108 } 109 110 public void visitDoubleMemberValue(DoubleMemberValue doubleMemberValue) 111 { 112 value = new Double (doubleMemberValue.getValue()); 113 } 114 115 public void visitEnumMemberValue(EnumMemberValue enumMemberValue) 116 { 117 try 118 { 119 Field enumVal = type.getField(enumMemberValue.getValue()); 120 value = enumVal.get(null); 121 } 122 catch (NoSuchFieldException e) 123 { 124 throw new RuntimeException (e); } 126 catch (SecurityException e) 127 { 128 throw new RuntimeException (e); } 130 catch (IllegalArgumentException e) 131 { 132 throw new RuntimeException (e); } 134 catch (IllegalAccessException e) 135 { 136 throw new RuntimeException (e); } 138 } 139 140 public void visitFloatMemberValue(FloatMemberValue floatMemberValue) 141 { 142 value = new Float (floatMemberValue.getValue()); 143 } 144 145 public void visitIntegerMemberValue(IntegerMemberValue integerMemberValue) 146 { 147 value = new Integer (integerMemberValue.getValue()); 148 } 149 150 public void visitLongMemberValue(LongMemberValue longMemberValue) 151 { 152 value = new Long (longMemberValue.getValue()); 153 } 154 155 public void visitShortMemberValue(ShortMemberValue shortMemberValue) 156 { 157 value = new Short (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 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 e) 212 { 213 throw new RuntimeException (e); } 215 216 } 217 218 public static Class getMemberType(Class annotation, String member) 219 { 220 Method [] 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 ("unable to determine member type for annotation: " + annotation.getName() + "." + member); 229 } 230 231 public static Map <String , Object > createProxyMap(Class annotation, javassist.bytecode.annotation.Annotation info) 232 { 233 Map <String , Object > map = new HashMap <String , Object >(); 235 236 if (info.getMemberNames() == null) return map; 237 Set members = info.getMemberNames(); 238 Iterator it = members.iterator(); 239 while (it.hasNext()) 240 { 241 String name = (String ) 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 |