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.Modifier ; 42 43 49 public class PerJoinpointInterceptor implements Interceptor 50 { 51 public static Interceptor createInterceptor(Advisor advisor, Joinpoint joinpoint, AspectDefinition def) throws Exception 52 { 53 if (joinpoint instanceof MethodJoinpoint) 54 { 55 MethodJoinpoint method = (MethodJoinpoint) joinpoint; 56 if (Modifier.isStatic(method.getMethod().getModifiers())) 57 { 58 return (Interceptor) def.getFactory().createPerJoinpoint(advisor, joinpoint); 59 } 60 } 61 else if (joinpoint instanceof ConstructorJoinpoint 62 || joinpoint instanceof ConstructorCalledByConstructorJoinpoint 63 || joinpoint instanceof MethodCalledByConstructorJoinpoint) 64 { 65 return (Interceptor) def.getFactory().createPerJoinpoint(advisor, joinpoint); 66 } 67 else if (joinpoint instanceof MethodCalledByMethodJoinpoint) 68 { 69 MethodCalledByMethodJoinpoint method = (MethodCalledByMethodJoinpoint) joinpoint; 70 if (Modifier.isStatic(method.getCalling().getModifiers())) 71 { 72 return (Interceptor) def.getFactory().createPerJoinpoint(advisor, joinpoint); 73 } 74 } 75 else if (joinpoint instanceof ConstructorCalledByMethodJoinpoint) 76 { 77 ConstructorCalledByMethodJoinpoint method = (ConstructorCalledByMethodJoinpoint) joinpoint; 78 if (Modifier.isStatic(method.getCalling().getModifiers())) 79 { 80 return (Interceptor) def.getFactory().createPerJoinpoint(advisor, joinpoint); 81 } 82 } 83 else if (joinpoint instanceof FieldJoinpoint) 84 { 85 FieldJoinpoint field = (FieldJoinpoint) joinpoint; 86 if (Modifier.isStatic(field.getField().getModifiers())) 87 { 88 ClassAdvisor classAdvisor = (ClassAdvisor) advisor; 89 return (Interceptor)classAdvisor.getFieldAspect(field, def); 90 } 91 92 } 93 return new PerJoinpointInterceptor(def, advisor, joinpoint); 94 } 95 96 AspectDefinition aspectDefinition; 97 Joinpoint joinpoint; 98 99 public PerJoinpointInterceptor(AspectDefinition a, Advisor advisor, Joinpoint joinpoint) 100 { 101 aspectDefinition = a; 102 advisor.addPerInstanceJoinpointAspect(joinpoint, a); 103 this.joinpoint = joinpoint; 104 } 105 106 public String getName() 107 { 108 return aspectDefinition.getName(); 109 } 110 111 public Object invoke(Invocation invocation) throws Throwable 112 { 113 if (invocation instanceof CallerInvocation) 114 { 115 Object callingObject = null; 118 119 if (invocation instanceof ConstructorCalledByMethodInvocation) 120 { 121 callingObject = ((ConstructorCalledByMethodInvocation)invocation).getCallingObject(); 122 } 123 else if (invocation instanceof MethodCalledByMethodInvocation) 124 { 125 callingObject = ((MethodCalledByMethodInvocation)invocation).getCallingObject(); 126 } 127 128 if (callingObject == null) return invocation.invokeNext(); 130 Advised advised = (Advised) callingObject; 131 InstanceAdvisor advisor = advised._getInstanceAdvisor(); 132 Interceptor interceptor = (Interceptor) advisor.getPerInstanceJoinpointAspect(joinpoint, aspectDefinition); 133 return interceptor.invoke(invocation); 134 135 } 136 else 137 { 138 Object targetObject = invocation.getTargetObject(); 139 if (targetObject == null) return invocation.invokeNext(); 141 Advised advised = (Advised) targetObject; 142 InstanceAdvisor advisor = advised._getInstanceAdvisor(); 143 Interceptor interceptor = (Interceptor) advisor.getPerInstanceJoinpointAspect(joinpoint, aspectDefinition); 144 return interceptor.invoke(invocation); 145 } 146 } 147 148 149 } 150 | Popular Tags |