| Home >> All >> com >> ghettojedi >> aop >> [ aspect Javadoc ] |
Source code: com/ghettojedi/aop/aspect/Invocation.java
1 package com.ghettojedi.aop.aspect; 2 3 import java.lang.reflect.InvocationTargetException; 4 import java.lang.reflect.Method; 5 import java.util.List; 6 import java.util.ListIterator; 7 8 public class Invocation { 9 private ListIterator interceptors; 10 private Object target; 11 private String methodName; 12 private Object[] arguments; 13 private Method method; 14 15 Invocation(List interceptors, Object target, String methodName, Class[] parameterTypes, Object[] arguments) { 16 this.interceptors = interceptors.listIterator(); 17 this.target = target; 18 this.methodName = methodName; 19 this.arguments = arguments; 20 this.method = getMethod(parameterTypes); 21 } 22 23 public Object getTarget() { 24 return target; 25 } 26 27 public Class getTargetClass() { 28 return target.getClass(); 29 } 30 31 public String getMethodName() { 32 return methodName; 33 } 34 35 public Object[] getArguments() { 36 return arguments; 37 } 38 39 public Object proceed() throws Throwable { 40 if (interceptors.hasNext()) { 41 return invokeNext(); 42 } else { 43 return invokeTarget(); 44 } 45 } 46 47 private Object invokeNext() throws Throwable { 48 return ((Interceptor) interceptors.next()).invoke(this); 49 } 50 51 private Object invokeTarget() throws Throwable { 52 try { 53 return method.invoke(target, arguments); 54 } catch (InvocationTargetException e) { 55 throw e.getCause(); 56 } 57 } 58 59 private Method getMethod(Class[] parameterTypes) { 60 try { 61 return getTargetClass().getMethod(Delegator.getHiddenMethodName(methodName), parameterTypes); 62 } catch (NoSuchMethodException e) { 63 throw new Error("Internal AOP Error: An Invocation was created for a method that doesn't exist: " + methodName); 64 } 65 } 66 }