1 22 package org.jboss.aop.annotation.factory.duplicate.javassist; 23 24 import java.lang.reflect.Method ; 25 import java.security.AccessController ; 26 import java.security.PrivilegedAction ; 27 import java.util.ArrayList ; 28 import java.util.Map ; 29 30 import org.jboss.aop.annotation.factory.duplicate.AnnotationValidationException; 31 import org.jboss.aop.annotation.factory.duplicate.AnnotationValidator; 32 33 import javassist.ClassPool; 34 import javassist.CtClass; 35 import javassist.CtMethod; 36 import javassist.NotFoundException; 37 import javassist.bytecode.AnnotationDefaultAttribute; 38 import javassist.bytecode.MethodInfo; 39 import javassist.bytecode.annotation.MemberValue; 40 import javassist.scopedpool.ScopedClassPoolRepository; 41 import javassist.scopedpool.ScopedClassPoolRepositoryImpl; 42 43 50 public class DefaultValueAnnotationValidator implements AnnotationValidator 51 { 52 ScopedClassPoolRepository repository = ScopedClassPoolRepositoryImpl.getInstance(); 53 54 public void validate(Map map, Class annotation) 55 { 56 ArrayList notAssignedAttributes = null; 57 CtClass ctClass = null; 58 59 Method [] methods = getDeclaredMethods(annotation); 60 for (int i = 0 ; i < methods.length ; i++) 61 { 62 if (map.get(methods[i].getName()) == null) 63 { 64 if (ctClass == null) 65 { 66 ctClass = getCtClass(annotation); 67 } 68 69 CtMethod method; 70 try 71 { 72 method = ctClass.getDeclaredMethod(methods[i].getName()); 73 } 74 catch (NotFoundException e) 75 { 76 throw new RuntimeException ("Unable to find method " + methods[i].getName() + " for " + annotation.getName()); 77 } 78 Object defaultValue = null; 79 MethodInfo minfo = method.getMethodInfo2(); 80 AnnotationDefaultAttribute defAttr = (AnnotationDefaultAttribute)minfo.getAttribute(AnnotationDefaultAttribute.tag); 81 82 if (defAttr != null) 83 { 84 MemberValue value = defAttr.getDefaultValue(); MemberValueGetter getter = new MemberValueGetter(methods[i]); 86 value.accept(getter); 87 defaultValue = getter.getValue(); 88 } 89 90 if (defaultValue != null) 91 { 92 map.put(methods[i].getName(), defaultValue); 93 } 94 else 95 { 96 if (notAssignedAttributes == null) 97 { 98 notAssignedAttributes = new ArrayList (); 99 } 100 notAssignedAttributes.add(methods[i].getName()); 101 } 102 103 } 104 } 105 106 if (notAssignedAttributes != null) 107 { 108 throw new AnnotationValidationException("Unable to fill in default attributes for " + annotation + " " + notAssignedAttributes); 109 } 110 } 111 112 113 CtClass getCtClass(Class clazz) 114 { 115 try 116 { 117 ClassPool pool = repository.findClassPool(clazz.getClassLoader()); 118 return pool.get(clazz.getName()); 119 } 120 catch (NotFoundException e) 121 { 122 throw new RuntimeException ("Unable to load CtClass for " + clazz, e); 123 } 124 } 125 126 private Method [] getDeclaredMethods(final Class clazz) 127 { 128 return (Method [])AccessController.doPrivileged(new PrivilegedAction () { 129 public Object run() 130 { 131 return clazz.getDeclaredMethods(); 132 }; 133 }); 134 } 135 } 136 137 | Popular Tags |