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