You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwebbeans.apache.org by st...@apache.org on 2013/01/03 22:45:04 UTC
svn commit: r1428615 - in /openwebbeans/trunk/webbeans-impl/src:
main/java/org/apache/webbeans/proxy/InterceptorDecoratorProxyFactory.java
test/java/org/apache/webbeans/newtests/interceptors/factory/InterceptorDecoratorProxyFactoryTest.java
Author: struberg
Date: Thu Jan 3 21:45:04 2013
New Revision: 1428615
URL: http://svn.apache.org/viewvc?rev=1428615&view=rev
Log:
OWB-344 remove the need to dynamically call getDeclaredMethdod for each method invocation
Modified:
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/InterceptorDecoratorProxyFactory.java
openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/interceptors/factory/InterceptorDecoratorProxyFactoryTest.java
Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/InterceptorDecoratorProxyFactory.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/InterceptorDecoratorProxyFactory.java?rev=1428615&r1=1428614&r2=1428615&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/InterceptorDecoratorProxyFactory.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/InterceptorDecoratorProxyFactory.java Thu Jan 3 21:45:04 2013
@@ -21,7 +21,6 @@ package org.apache.webbeans.proxy;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
-import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
@@ -62,7 +61,7 @@ public class InterceptorDecoratorProxyFa
{
}
- public <T> T createProxyInstance(Class<T> proxyClass, T instance, InvocationHandler interceptorDecoratorStack)
+ public <T> T createProxyInstance(Class<T> proxyClass, T instance, InterceptorHandler interceptorDecoratorStack)
throws ProxyGenerationException
{
try
@@ -154,7 +153,7 @@ public class InterceptorDecoratorProxyFa
cw.visitField(Opcodes.ACC_FINAL | Opcodes.ACC_PRIVATE, FIELD_PROXIED_INSTANCE, Type.getDescriptor(classToProxy), null, null).visitEnd();
// variable #2, the invocation handler
- cw.visitField(Opcodes.ACC_FINAL | Opcodes.ACC_PRIVATE, FIELD_INVOCATION_HANDLER, Type.getDescriptor(InvocationHandler.class), null, null).visitEnd();
+ cw.visitField(Opcodes.ACC_FINAL | Opcodes.ACC_PRIVATE, FIELD_INVOCATION_HANDLER, Type.getDescriptor(InterceptorHandler.class), null, null).visitEnd();
}
/**
@@ -187,7 +186,7 @@ public class InterceptorDecoratorProxyFa
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitInsn(Opcodes.ACONST_NULL);
- mv.visitFieldInsn(Opcodes.PUTFIELD, proxyClassFileName, FIELD_INVOCATION_HANDLER, Type.getDescriptor(InvocationHandler.class));
+ mv.visitFieldInsn(Opcodes.PUTFIELD, proxyClassFileName, FIELD_INVOCATION_HANDLER, Type.getDescriptor(InterceptorHandler.class));
mv.visitInsn(Opcodes.RETURN);
mv.visitMaxs(-1, -1);
@@ -265,13 +264,14 @@ public class InterceptorDecoratorProxyFa
private void delegateInterceptedMethods(ClassWriter cw, String proxyClassFileName, Class<?> classToProxy, List<Method> interceptedMethods)
throws ProxyGenerationException
{
- for (Method proxiedMethod : interceptedMethods)
+ for (int i = 0; i < interceptedMethods.size(); i++)
{
- generateInvocationHandlerMethod(cw, proxiedMethod, proxyClassFileName);
+ Method proxiedMethod = interceptedMethods.get(i);
+ generateInvocationHandlerMethod(cw, proxiedMethod, i, classToProxy, proxyClassFileName);
}
}
- private void generateInvocationHandlerMethod(ClassWriter cw, Method method, String proxyName)
+ private void generateInvocationHandlerMethod(ClassWriter cw, Method method, int methodIndex, Class<?> classToProxy, String proxyClassFileName)
throws ProxyGenerationException
{
if ("<init>".equals(method.getName()))
@@ -349,12 +349,14 @@ public class InterceptorDecoratorProxyFa
}
}
+/*X TODO remove. we dont resolve the methods dynamically
// invoke getMethod() with the method name and the array of types
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Class", "getDeclaredMethod",
"(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;");
// store the returned method for later
mv.visitVarInsn(Opcodes.ASTORE, length);
+*/
// the following code generates bytecode equivalent to:
// return ((<returntype>) invocationHandler.invoke(this, method, new Object[] { <function arguments }))[.<primitive>Value()];
@@ -364,13 +366,21 @@ public class InterceptorDecoratorProxyFa
mv.visitVarInsn(Opcodes.ALOAD, 0);
// get the invocationHandler field from this class
- mv.visitFieldInsn(Opcodes.GETFIELD, proxyName, FIELD_INVOCATION_HANDLER, "Ljava/lang/reflect/InvocationHandler;");
+//X mv.visitFieldInsn(Opcodes.GETFIELD, proxyName, FIELD_INVOCATION_HANDLER, "Ljava/lang/reflect/InvocationHandler;");
+ mv.visitFieldInsn(Opcodes.GETFIELD, proxyClassFileName, FIELD_INVOCATION_HANDLER, Type.getDescriptor(InterceptorHandler.class));
// we want to pass "this" in as the first parameter
+ //X mv.visitVarInsn(Opcodes.ALOAD, 0);
+
+ // load the delegate variable as first parameter
mv.visitVarInsn(Opcodes.ALOAD, 0);
+ mv.visitFieldInsn(Opcodes.GETFIELD, proxyClassFileName, FIELD_PROXIED_INSTANCE, Type.getDescriptor(classToProxy));
+
+ // add the methodIndex as context as second parameter
+ mv.visitIntInsn(Opcodes.BIPUSH, methodIndex);
// and the method we fetched earlier
- mv.visitVarInsn(Opcodes.ALOAD, length);
+ //X mv.visitVarInsn(Opcodes.ALOAD, length);
// need to construct the array of objects passed in
@@ -416,8 +426,8 @@ public class InterceptorDecoratorProxyFa
}
// invoke the invocationHandler
- mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/lang/reflect/InvocationHandler", "invoke",
- "(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;");
+ mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(InterceptorHandler.class), "invoke",
+ "(Ljava/lang/Object;I[Ljava/lang/Object;)Ljava/lang/Object;");
// cast the result
mv.visitTypeInsn(Opcodes.CHECKCAST, getCastType(returnType));
Modified: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/interceptors/factory/InterceptorDecoratorProxyFactoryTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/interceptors/factory/InterceptorDecoratorProxyFactoryTest.java?rev=1428615&r1=1428614&r2=1428615&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/interceptors/factory/InterceptorDecoratorProxyFactoryTest.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/interceptors/factory/InterceptorDecoratorProxyFactoryTest.java Thu Jan 3 21:45:04 2013
@@ -19,7 +19,6 @@
package org.apache.webbeans.newtests.interceptors.factory;
import java.lang.reflect.Field;
-import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
@@ -30,6 +29,7 @@ import org.apache.webbeans.newtests.Abst
import org.apache.webbeans.newtests.interceptors.factory.beans.ClassInterceptedClass;
import org.apache.webbeans.proxy.InterceptorDecoratorProxyFactory;
+import org.apache.webbeans.proxy.InterceptorHandler;
import org.apache.webbeans.proxy.OwbInterceptorProxy;
import org.apache.webbeans.util.ClassUtil;
import org.junit.Assert;
@@ -62,7 +62,7 @@ public class InterceptorDecoratorProxyFa
ClassInterceptedClass internalInstance = new ClassInterceptedClass();
internalInstance.init();
- TestInvocationHandler testInvocationHandler = new TestInvocationHandler(internalInstance);
+ TestInvocationHandler testInvocationHandler = new TestInvocationHandler(interceptedMethods);
ClassInterceptedClass proxy = pf.createProxyInstance(proxyClass, internalInstance, testInvocationHandler);
Assert.assertNotNull(proxy);
@@ -87,25 +87,25 @@ public class InterceptorDecoratorProxyFa
Assert.assertEquals(5, testInvocationHandler.invokedMethodNames.size());
}
- public static class TestInvocationHandler implements InvocationHandler
+ public static class TestInvocationHandler implements InterceptorHandler
{
public List<String> invokedMethodNames = new ArrayList<String>();
- private final Object instance;
+ private List<Method> interceptedMethods;
- public TestInvocationHandler(Object instance)
+ public TestInvocationHandler(List<Method> interceptedMethods)
{
- this.instance = instance;
+ this.interceptedMethods = interceptedMethods;
}
@Override
- public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
+ public Object invoke(Object instance, int methodIndex, Object[] args) throws Throwable
{
- invokedMethodNames.add(method.getName());
+ invokedMethodNames.add(interceptedMethods.get(methodIndex).getName());
- System.out.println("TestInvocationHandler got properly invoked for method " + method.getName());
+ System.out.println("TestInvocationHandler got properly invoked for method " + interceptedMethods.get(methodIndex).getName());
- return method.invoke(instance, args);
+ return interceptedMethods.get(methodIndex).invoke(instance, args);
}
}
}