1 17 18 package org.objectweb.jac.wrappers; 19 20 import org.aopalliance.intercept.ConstructorInvocation; 21 import org.aopalliance.intercept.MethodInvocation; 22 import org.objectweb.jac.core.AspectComponent; 23 import org.objectweb.jac.core.Interaction; 24 import org.objectweb.jac.core.Wrapper; 25 26 import java.lang.reflect.InvocationTargetException ; 27 import java.lang.reflect.Method ; 28 29 37 38 public class ForwardingWrapper extends Wrapper { 39 40 44 45 public ForwardingWrapper(AspectComponent ac, Object forwardee) { 46 super(ac); 47 this.forwardee = forwardee; 48 } 49 50 51 protected Object forwardee; 52 53 60 61 public Object getForwardee() { 62 return forwardee; 63 } 64 65 73 74 public Object forward(Interaction interaction) throws ForwardingException { 75 76 Object ret = null; 77 78 Method [] methods = forwardee.getClass().getMethods(); 80 boolean ok = false; 81 for (int i = 0; i < methods.length; i++) { 82 if (methods[i].getName().equals(interaction.method)) { 84 try { 85 ok = false; 86 ret = methods[i].invoke(forwardee, interaction.args); 88 ok = true; 89 } catch (IllegalArgumentException e) { 90 } catch (InvocationTargetException e) { 91 Throwable t = e.getTargetException(); 92 if (t instanceof RuntimeException ) { 93 throw (RuntimeException ) t; 94 } 95 e.printStackTrace(); 96 System.exit(1); 97 } catch (IllegalAccessException e) { 98 e.printStackTrace(); 99 System.exit(1); 100 } 101 if (ok) 102 break; 103 } 104 } 105 if (!ok) { 106 throw new ForwardingException( 107 "The forwardee '" 108 + forwardee 109 + "' does not support '" 110 + interaction.method 111 + "' to forward from '" 112 + interaction.wrappee 113 + "'."); 114 } 115 116 return ret; 117 } 118 119 public Object invoke(MethodInvocation invocation) throws Throwable { 120 return forward((Interaction) invocation); 121 } 122 123 public Object construct(ConstructorInvocation invocation) 124 throws Throwable { 125 throw new Exception ("Wrapper "+this+" does not support construction interception."); 126 } 127 } 128 | Popular Tags |