1 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 ; 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 || 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 (booleanMemberValue.getValue()); 102 } 103 104 public void visitByteMemberValue(ByteMemberValue byteMemberValue) 105 { 106 value = new Byte (byteMemberValue.getValue()); 107 } 108 109 public void visitCharMemberValue(CharMemberValue charMemberValue) 110 { 111 value = new Character (charMemberValue.getValue()); 112 } 113 114 public void visitDoubleMemberValue(DoubleMemberValue doubleMemberValue) 115 { 116 value = new Double (doubleMemberValue.getValue()); 117 } 118 119 public void visitEnumMemberValue(EnumMemberValue enumMemberValue) 120 { 121 try 122 { 123 Field enumVal = type.getField(enumMemberValue.getValue()); 124 value = enumVal.get(null); 125 } 126 catch (NoSuchFieldException e) 127 { 128 throw new RuntimeException (e); } 130 catch (SecurityException e) 131 { 132 throw new RuntimeException (e); } 134 catch (IllegalArgumentException e) 135 { 136 throw new RuntimeException (e); } 138 catch (IllegalAccessException e) 139 { 140 throw new RuntimeException (e); } 142 } 143 144 public void visitFloatMemberValue(FloatMemberValue floatMemberValue) 145 { 146 value = new Float (floatMemberValue.getValue()); 147 } 148 149 public void visitIntegerMemberValue(IntegerMemberValue integerMemberValue) 150 { 151 value = new Integer (integerMemberValue.getValue()); 152 } 153 154 public void visitLongMemberValue(LongMemberValue longMemberValue) 155 { 156 value = new Long (longMemberValue.getValue()); 157 } 158 159 public void visitShortMemberValue(ShortMemberValue shortMemberValue) 160 { 161 value = new Short (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 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 e) 216 { 217 throw new RuntimeException (e); } 219 220 } 221 222 public static Class getMemberType(Class annotation, String member) 223 { 224 Method [] 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 ("unable to determine member type for annotation: " + annotation.getName() + "." + member); 233 } 234 235 public static Map createProxyMap(Class annotation, javassist.bytecode.annotation.Annotation info) 236 { 237 Map map = new HashMap (); 239 240 if (info.getMemberNames() == null) return map; 241 Set members = info.getMemberNames(); 242 Iterator it = members.iterator(); 243 while (it.hasNext()) 244 { 245 String name = (String ) 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 |