1 22 package org.jboss.aop.advice; 23 24 import org.jboss.aop.Advised; 25 import org.jboss.aop.Advisor; 26 import org.jboss.aop.ClassAdvisor; 27 import org.jboss.aop.InstanceAdvisor; 28 import org.jboss.aop.joinpoint.CallerInvocation; 29 import org.jboss.aop.joinpoint.ConstructorCalledByConstructorJoinpoint; 30 import org.jboss.aop.joinpoint.ConstructorCalledByMethodInvocation; 31 import org.jboss.aop.joinpoint.ConstructorCalledByMethodJoinpoint; 32 import org.jboss.aop.joinpoint.ConstructorJoinpoint; 33 import org.jboss.aop.joinpoint.FieldJoinpoint; 34 import org.jboss.aop.joinpoint.Invocation; 35 import org.jboss.aop.joinpoint.Joinpoint; 36 import org.jboss.aop.joinpoint.MethodCalledByConstructorJoinpoint; 37 import org.jboss.aop.joinpoint.MethodCalledByMethodInvocation; 38 import org.jboss.aop.joinpoint.MethodCalledByMethodJoinpoint; 39 import org.jboss.aop.joinpoint.MethodJoinpoint; 40 41 import java.lang.reflect.InvocationTargetException ; 42 import java.lang.reflect.Method ; 43 import java.lang.reflect.Modifier ; 44 45 51 public class PerJoinpointAdvice extends AbstractAdvice 52 { 53 public static Interceptor createInterceptor(Advisor advisor, Joinpoint joinpoint, AspectDefinition def, String adviceName) throws Exception 54 { 55 if (joinpoint instanceof MethodJoinpoint) 56 { 57 MethodJoinpoint method = (MethodJoinpoint) joinpoint; 58 if (Modifier.isStatic(method.getMethod().getModifiers())) 59 { 60 return PerVmAdvice.generateInterceptor(joinpoint, def.getFactory().createPerJoinpoint(advisor, joinpoint), adviceName); 61 } 62 } 63 else if (joinpoint instanceof ConstructorJoinpoint 64 || joinpoint instanceof ConstructorCalledByConstructorJoinpoint 65 || joinpoint instanceof MethodCalledByConstructorJoinpoint) 66 { 67 return PerVmAdvice.generateInterceptor(joinpoint, def.getFactory().createPerJoinpoint(advisor, joinpoint), adviceName); 68 } 69 else if (joinpoint instanceof MethodCalledByMethodJoinpoint) 70 { 71 MethodCalledByMethodJoinpoint method = (MethodCalledByMethodJoinpoint) joinpoint; 72 if (Modifier.isStatic(method.getCalling().getModifiers())) 73 { 74 return PerVmAdvice.generateInterceptor(joinpoint, def.getFactory().createPerJoinpoint(advisor, joinpoint), adviceName); 75 } 76 } 77 else if (joinpoint instanceof ConstructorCalledByMethodJoinpoint) 78 { 79 ConstructorCalledByMethodJoinpoint method = (ConstructorCalledByMethodJoinpoint) joinpoint; 80 if (Modifier.isStatic(method.getCalling().getModifiers())) 81 { 82 return PerVmAdvice.generateInterceptor(joinpoint, def.getFactory().createPerJoinpoint(advisor, joinpoint), adviceName); 83 } 84 } 85 else if (joinpoint instanceof FieldJoinpoint) 86 { 87 FieldJoinpoint field = (FieldJoinpoint) joinpoint; 88 if (Modifier.isStatic(field.getField().getModifiers())) 89 { 90 ClassAdvisor classAdvisor = (ClassAdvisor) advisor; 91 Object aspect = classAdvisor.getFieldAspect(field, def); 92 return PerVmAdvice.generateInterceptor(joinpoint, aspect, adviceName); 93 } 94 } 95 return new PerJoinpointAdvice(adviceName, def, advisor, joinpoint); 96 } 97 98 private boolean initialized = false; 99 AspectDefinition aspectDefinition; 100 Joinpoint joinpoint; 101 102 public PerJoinpointAdvice(String adviceName, AspectDefinition a, Advisor advisor, Joinpoint joinpoint) 103 { 104 aspectDefinition = a; 105 this.adviceName = adviceName; 106 advisor.addPerInstanceJoinpointAspect(joinpoint, a); 107 this.joinpoint = joinpoint; 108 } 109 110 public String getName() 111 { 112 return aspectDefinition.getName() + "." + adviceName; 113 } 114 115 public Object invoke(Invocation invocation) throws Throwable 116 { 117 Object aspect = null; 118 if (invocation instanceof CallerInvocation) 119 { 120 Object callingObject = null; 123 124 if (invocation instanceof ConstructorCalledByMethodInvocation) 125 { 126 callingObject = ((ConstructorCalledByMethodInvocation)invocation).getCallingObject(); 127 } 128 else if (invocation instanceof MethodCalledByMethodInvocation) 129 { 130 callingObject = ((MethodCalledByMethodInvocation)invocation).getCallingObject(); 131 } 132 133 if (callingObject == null) return invocation.invokeNext(); 135 Advised advised = (Advised) callingObject; 136 InstanceAdvisor advisor = advised._getInstanceAdvisor(); 137 aspect = advisor.getPerInstanceJoinpointAspect(joinpoint, aspectDefinition); 138 } 139 else 140 { 141 Object targetObject = invocation.getTargetObject(); 142 if (targetObject == null) return invocation.invokeNext(); 144 Advised advised = (Advised) targetObject; 145 InstanceAdvisor advisor = advised._getInstanceAdvisor(); 146 aspect = advisor.getPerInstanceJoinpointAspect(joinpoint, aspectDefinition); 147 } 148 149 if (!initialized) 150 { 151 init(adviceName, aspect.getClass()); 152 initialized = true; 153 } 154 Method advice = resolveAdvice(invocation); 155 Object [] args = {invocation}; 156 157 try 158 { 159 return advice.invoke(aspect, args); 160 } 161 catch (InvocationTargetException e) 162 { 163 throw e.getCause(); } 165 } 166 167 168 } 169 | Popular Tags |