1 15 package org.apache.tapestry.annotations; 16 17 import java.lang.annotation.Annotation ; 18 import java.lang.reflect.Method ; 19 import java.util.Map ; 20 21 import org.apache.hivemind.ClassResolver; 22 import org.apache.hivemind.ErrorLog; 23 import org.apache.hivemind.Location; 24 import org.apache.hivemind.Resource; 25 import org.apache.hivemind.util.ClasspathResource; 26 import org.apache.tapestry.enhance.EnhancementOperation; 27 import org.apache.tapestry.enhance.EnhancementWorker; 28 import org.apache.tapestry.spec.IComponentSpecification; 29 30 39 public class AnnotationEnhancementWorker implements EnhancementWorker 40 { 41 private ClassResolver _classResolver; 42 43 private ErrorLog _errorLog; 44 45 private Map _methodWorkers; 46 47 private Map _classWorkers; 48 49 public void setClassWorkers(Map classWorkers) 50 { 51 _classWorkers = classWorkers; 52 } 53 54 public void performEnhancement(EnhancementOperation op, IComponentSpecification spec) 55 { 56 Class clazz = op.getBaseClass(); 57 58 Resource classResource = newClassResource(clazz); 59 60 for (Annotation a : clazz.getAnnotations()) 61 { 62 performClassEnhancement(op, spec, clazz, a, classResource); 63 } 64 65 for (Method m : clazz.getMethods()) 66 { 67 performMethodEnhancement(op, spec, m, classResource); 68 } 69 } 70 71 private ClasspathResource newClassResource(Class clazz) 72 { 73 return new ClasspathResource(_classResolver, clazz.getName().replace('.', '/')); 74 } 75 76 void performClassEnhancement(EnhancementOperation op, IComponentSpecification spec, 77 Class clazz, Annotation annotation, Resource classResource) 78 { 79 ClassAnnotationEnhancementWorker worker = (ClassAnnotationEnhancementWorker) _classWorkers 80 .get(annotation.annotationType()); 81 82 if (worker == null) 83 return; 84 85 try 86 { 87 Location location = new AnnotationLocation(classResource, AnnotationMessages 88 .classAnnotation(annotation, clazz)); 89 90 worker.performEnhancement(op, spec, clazz, location); 91 } 92 catch (Exception ex) 93 { 94 _errorLog.error(AnnotationMessages.failureProcessingClassAnnotation( 95 annotation, 96 clazz, 97 ex), null, ex); 98 } 99 100 } 101 102 void performMethodEnhancement(EnhancementOperation op, IComponentSpecification spec, 103 Method method, Resource classResource) 104 { 105 for (Annotation a : method.getAnnotations()) 106 { 107 performMethodEnhancement(op, spec, method, a, classResource); 108 } 109 } 110 111 void performMethodEnhancement(EnhancementOperation op, IComponentSpecification spec, 112 Method method, Annotation annotation, Resource classResource) 113 { 114 MethodAnnotationEnhancementWorker worker = (MethodAnnotationEnhancementWorker) _methodWorkers 115 .get(annotation.annotationType()); 116 117 if (worker == null) 118 return; 119 120 try 121 { 122 Location location = new AnnotationLocation(classResource, AnnotationMessages 123 .methodAnnotation(annotation, method)); 124 worker.performEnhancement(op, spec, method, location); 125 } 126 catch (Exception ex) 127 { 128 _errorLog.error( 129 AnnotationMessages.failureProcessingAnnotation(annotation, method, ex), 130 null, 131 ex); 132 } 133 134 } 135 136 public void setMethodWorkers(Map methodWorkers) 137 { 138 _methodWorkers = methodWorkers; 139 } 140 141 public void setErrorLog(ErrorLog errorLog) 142 { 143 _errorLog = errorLog; 144 } 145 146 public void setClassResolver(ClassResolver classResolver) 147 { 148 _classResolver = classResolver; 149 } 150 } 151 | Popular Tags |