1 22 package org.jboss.annotation.factory.javassist; 23 24 import java.lang.reflect.Array ; 25 import java.lang.reflect.Method ; 26 27 import org.jboss.reflect.plugins.introspection.IntrospectionTypeInfoFactoryImpl; 28 29 import javassist.bytecode.annotation.Annotation; 30 import javassist.bytecode.annotation.AnnotationMemberValue; 31 import javassist.bytecode.annotation.ArrayMemberValue; 32 import javassist.bytecode.annotation.BooleanMemberValue; 33 import javassist.bytecode.annotation.ByteMemberValue; 34 import javassist.bytecode.annotation.CharMemberValue; 35 import javassist.bytecode.annotation.ClassMemberValue; 36 import javassist.bytecode.annotation.DoubleMemberValue; 37 import javassist.bytecode.annotation.EnumMemberValue; 38 import javassist.bytecode.annotation.FloatMemberValue; 39 import javassist.bytecode.annotation.IntegerMemberValue; 40 import javassist.bytecode.annotation.LongMemberValue; 41 import javassist.bytecode.annotation.MemberValue; 42 import javassist.bytecode.annotation.MemberValueVisitor; 43 import javassist.bytecode.annotation.ShortMemberValue; 44 import javassist.bytecode.annotation.StringMemberValue; 45 46 51 public class MemberValueGetter implements MemberValueVisitor 52 { 53 Object value; 54 Method method; 55 static IntrospectionTypeInfoFactoryImpl typeFactory = new IntrospectionTypeInfoFactoryImpl(); 56 57 public MemberValueGetter(Method method) 58 { 59 this.method = method; 60 } 61 62 public Object getValue() 63 { 64 return value; 65 } 66 67 public void visitAnnotationMemberValue(AnnotationMemberValue node) 68 { 69 try 70 { 71 Annotation ann = node.getValue(); 72 value = AnnotationProxy.createProxy(ann); 73 } 74 catch (Exception e) 75 { 76 throw new RuntimeException (e); 77 } 78 } 79 80 public void visitArrayMemberValue(ArrayMemberValue node) 81 { 82 MemberValue[] values = node.getValue(); 83 value = node.getValue(); 84 Object vals = Array.newInstance(getAttributeType(), values.length); 85 86 for (int i = 0 ; i < values.length ; i++) 87 { 88 values[i].accept(this); 89 Array.set(vals, i, value); 90 } 91 92 value = vals; 93 } 94 95 public void visitBooleanMemberValue(BooleanMemberValue node) 96 { 97 value = new Boolean (node.getValue()); 98 } 99 100 public void visitByteMemberValue(ByteMemberValue node) 101 { 102 value = new Byte (node.getValue()); 103 } 104 105 public void visitCharMemberValue(CharMemberValue node) 106 { 107 value = new Character (node.getValue()); 108 } 109 110 public void visitDoubleMemberValue(DoubleMemberValue node) 111 { 112 value = new Double (node.getValue()); 113 } 114 115 @SuppressWarnings ("unchecked") 116 public void visitEnumMemberValue(EnumMemberValue node) 117 { 118 value = Enum.valueOf(getAttributeType(), node.getValue()); 119 } 120 121 public void visitFloatMemberValue(FloatMemberValue node) 122 { 123 value = new Float (node.getValue()); 124 } 125 126 public void visitIntegerMemberValue(IntegerMemberValue node) 127 { 128 value = new Integer (node.getValue()); 129 } 130 131 public void visitLongMemberValue(LongMemberValue node) 132 { 133 value = new Long (node.getValue()); 134 } 135 136 public void visitShortMemberValue(ShortMemberValue node) 137 { 138 value = new Short (node.getValue()); 139 } 140 141 public void visitStringMemberValue(StringMemberValue node) 142 { 143 value = node.getValue(); 144 } 145 146 public void visitClassMemberValue(ClassMemberValue node) 147 { 148 try 149 { 150 value = Class.forName(node.getValue()); 151 } 152 catch (ClassNotFoundException e) 153 { 154 throw new RuntimeException (e); 155 } 156 } 157 158 private Class getAttributeType() 159 { 160 Class rtn = method.getReturnType(); 161 162 while (rtn.isArray()) 163 { 164 rtn = rtn.getComponentType(); 165 } 166 167 return rtn; 168 } 169 170 } 171 | Popular Tags |