1 7 package com.tirsen.nanning.definition; 8 9 import java.lang.reflect.Method ; 10 import java.util.*; 11 12 import com.tirsen.nanning.MethodInterceptor; 13 import com.tirsen.nanning.MixinInstance; 14 15 25 public class AspectDefinition { 26 private Class interfaceClass; 27 private List interceptorDefinitions = new ArrayList(); 28 private Class targetClass; 29 private Map methodsToIndex; 30 31 36 public void setInterface(Class interfaceClass) { 37 this.interfaceClass = interfaceClass; 38 methodsToIndex = new HashMap(); 39 Method [] methods = interfaceClass.getMethods(); 40 for (int i = 0; i < methods.length; i++) { 41 Method method = methods[i]; 42 methodsToIndex.put(method, new Integer (i)); 43 } 44 } 45 46 52 public void addInterceptor(Class interceptorClass) { 53 addInterceptor(new InterceptorDefinition(interceptorClass)); 54 } 55 56 61 public void addInterceptor(InterceptorDefinition interceptorDefinition) { 62 interceptorDefinitions.add(interceptorDefinition); 63 } 64 65 70 public void setTarget(Class targetClass) { 71 this.targetClass = targetClass; 72 } 73 74 MixinInstance createMixinInstance() 75 throws IllegalAccessException , InstantiationException { 76 if (targetClass != null) { 77 return newInstance(targetClass.newInstance()); 78 } else { 79 return newInstance(null); 80 } 81 } 82 83 public Class getInterfaceClass() { 84 return interfaceClass; 85 } 86 87 MixinInstance newInstance(Object target) { 88 checkTarget(target); 89 90 MixinInstance mixinInstance = new MixinInstance(); 91 mixinInstance.setInterfaceClass(getInterfaceClass()); 92 93 for (Iterator iterator = interceptorDefinitions.iterator(); iterator.hasNext();) { 94 InterceptorDefinition interceptorDefinition = (InterceptorDefinition) iterator.next(); 95 Method [] methods = mixinInstance.getAllMethods(); 96 for (int j = 0; j < methods.length; j++) { 97 Method method = methods[j]; 98 if (interceptorDefinition.interceptsMethod(method)) { 99 mixinInstance.addInterceptor(method, (MethodInterceptor) interceptorDefinition.newInstance()); 100 } 101 } 102 } 103 104 mixinInstance.setTarget(target); 105 return mixinInstance; 106 } 107 108 private void checkTarget(Object target) { 109 if (target == null) { 110 return; 111 } 112 113 if (!interfaceClass.isInstance(target)) { 114 throw new IllegalArgumentException ("target does not implement interface: " + target); 115 } 116 if (!targetClass.isInstance(target)) { 117 throw new IllegalArgumentException ("target is not an instance of target-class: " + target); 118 } 119 } 120 121 public List getConstructionInterceptors() { 122 List interceptors = new ArrayList(); 123 for (Iterator iterator = interceptorDefinitions.iterator(); iterator.hasNext();) { 124 InterceptorDefinition interceptorDefinition = (InterceptorDefinition) iterator.next(); 125 if (interceptorDefinition.interceptsConstructor(getInterfaceClass())) { 126 interceptors.add(interceptorDefinition.newInstance()); 127 } 128 } 129 return interceptors; 130 } 131 132 public Collection getInterceptorDefinitions() { 133 return interceptorDefinitions; 134 } 135 } 136 | Popular Tags |