1 7 package org.jboss.aop.annotation; 8 9 import javassist.bytecode.annotation.AnnotationMemberValue; 10 import javassist.bytecode.annotation.ArrayMemberValue; 11 import javassist.bytecode.annotation.BooleanMemberValue; 12 import javassist.bytecode.annotation.ByteMemberValue; 13 import javassist.bytecode.annotation.CharMemberValue; 14 import javassist.bytecode.annotation.ClassMemberValue; 15 import javassist.bytecode.annotation.DoubleMemberValue; 16 import javassist.bytecode.annotation.EnumMemberValue; 17 import javassist.bytecode.annotation.FloatMemberValue; 18 import javassist.bytecode.annotation.IntegerMemberValue; 19 import javassist.bytecode.annotation.LongMemberValue; 20 import javassist.bytecode.annotation.MemberValue; 21 import javassist.bytecode.annotation.MemberValueVisitor; 22 import javassist.bytecode.annotation.ShortMemberValue; 23 import javassist.bytecode.annotation.StringMemberValue; 24 25 import java.lang.reflect.Array ; 26 import java.lang.reflect.Field ; 27 import java.lang.reflect.Method ; 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 import java.util.Map ; 31 import java.util.Set ; 32 33 39 public class ProxyMapCreator implements MemberValueVisitor 40 { 41 public Object value; 42 private Class type; 43 44 45 public ProxyMapCreator(Class type) 46 { 47 this.type = type; 48 } 49 50 public void visitAnnotationMemberValue(AnnotationMemberValue annotationMemberValue) 51 { 52 try 53 { 54 value = AnnotationProxy.createProxy(annotationMemberValue.getValue()); 55 } 56 catch (Exception e) 57 { 58 throw new RuntimeException (e); } 60 } 61 62 public void visitArrayMemberValue(ArrayMemberValue arrayMemberValue) 63 { 64 Class baseType = type.getComponentType(); 65 int size = 0; 66 if (arrayMemberValue.getValue() == null || arrayMemberValue.getValue().length == 0) 67 { 68 size = 0; 69 } 70 else 71 { 72 size = arrayMemberValue.getValue().length; 73 } 74 value = Array.newInstance(baseType, size); 75 MemberValue[] elements = arrayMemberValue.getValue(); 76 for (int i = 0; i < size; i++) 77 { 78 ProxyMapCreator creator = new ProxyMapCreator(baseType); 79 elements[i].accept(creator); 80 Array.set(value, i, creator.value); 81 } 82 } 83 84 public void visitBooleanMemberValue(BooleanMemberValue booleanMemberValue) 85 { 86 value = new Boolean (booleanMemberValue.getValue()); 87 } 88 89 public void visitByteMemberValue(ByteMemberValue byteMemberValue) 90 { 91 value = new Byte (byteMemberValue.getValue()); 92 } 93 94 public void visitCharMemberValue(CharMemberValue charMemberValue) 95 { 96 value = new Character (charMemberValue.getValue()); 97 } 98 99 public void visitDoubleMemberValue(DoubleMemberValue doubleMemberValue) 100 { 101 value = new Double (doubleMemberValue.getValue()); 102 } 103 104 public void visitEnumMemberValue(EnumMemberValue enumMemberValue) 105 { 106 try 107 { 108 Field enumVal = type.getField(enumMemberValue.getValue()); 109 value = enumVal.get(null); 110 } 111 catch (NoSuchFieldException e) 112 { 113 throw new RuntimeException (e); } 115 catch (SecurityException e) 116 { 117 throw new RuntimeException (e); } 119 catch (IllegalArgumentException e) 120 { 121 throw new RuntimeException (e); } 123 catch (IllegalAccessException e) 124 { 125 throw new RuntimeException (e); } 127 } 128 129 public void visitFloatMemberValue(FloatMemberValue floatMemberValue) 130 { 131 value = new Float (floatMemberValue.getValue()); 132 } 133 134 public void visitIntegerMemberValue(IntegerMemberValue integerMemberValue) 135 { 136 value = new Integer (integerMemberValue.getValue()); 137 } 138 139 public void visitLongMemberValue(LongMemberValue longMemberValue) 140 { 141 value = new Long (longMemberValue.getValue()); 142 } 143 144 public void visitShortMemberValue(ShortMemberValue shortMemberValue) 145 { 146 value = new Short (shortMemberValue.getValue()); 147 } 148 149 public void visitStringMemberValue(StringMemberValue stringMemberValue) 150 { 151 value = stringMemberValue.getValue(); 152 } 153 154 public void visitClassMemberValue(ClassMemberValue classMemberValue) 155 { 156 try 157 { 158 String classname = classMemberValue.getValue(); 159 if (classname.equals("void")) 160 { 161 value = void.class; 162 } 163 else if (classname.equals("int")) 164 { 165 value = int.class; 166 } 167 else if (classname.equals("byte")) 168 { 169 value = byte.class; 170 } 171 else if (classname.equals("long")) 172 { 173 value = long.class; 174 } 175 else if (classname.equals("double")) 176 { 177 value = double.class; 178 } 179 else if (classname.equals("float")) 180 { 181 value = float.class; 182 } 183 else if (classname.equals("char")) 184 { 185 value = char.class; 186 } 187 else if (classname.equals("short")) 188 { 189 value = short.class; 190 } 191 else if (classname.equals("boolean")) 192 { 193 value = boolean.class; 194 } 195 else 196 { 197 value = Thread.currentThread().getContextClassLoader().loadClass(classMemberValue.getValue()); 198 } 199 } 200 catch (ClassNotFoundException e) 201 { 202 throw new RuntimeException (e); } 204 205 } 206 207 public static Class getMemberType(Class annotation, String member) 208 { 209 Method [] methods = annotation.getMethods(); 210 for (int i = 0; i < methods.length; i++) 211 { 212 if (methods[i].getName().equals(member)) 213 { 214 return methods[i].getReturnType(); 215 } 216 } 217 throw new RuntimeException ("unable to determine member type for annotation: " + annotation.getName() + "." + member); 218 } 219 220 public static Map createProxyMap(Class annotation, javassist.bytecode.annotation.Annotation info) 221 { 222 Map map = new HashMap (); 224 225 if (info.getMemberNames() == null) return map; 226 Set members = info.getMemberNames(); 227 Iterator it = members.iterator(); 228 while (it.hasNext()) 229 { 230 String name = (String ) it.next(); 231 MemberValue mv = info.getMemberValue(name); 232 ProxyMapCreator creator = new ProxyMapCreator(getMemberType(annotation, name)); 233 mv.accept(creator); 234 map.put(name, creator.value); 235 } 236 return map; 237 } 238 } 239 | Popular Tags |