Return-Path: X-Original-To: apmail-openwebbeans-commits-archive@www.apache.org Delivered-To: apmail-openwebbeans-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id B704CE7FB for ; Thu, 3 Jan 2013 21:45:28 +0000 (UTC) Received: (qmail 64444 invoked by uid 500); 3 Jan 2013 21:45:28 -0000 Delivered-To: apmail-openwebbeans-commits-archive@openwebbeans.apache.org Received: (qmail 64426 invoked by uid 500); 3 Jan 2013 21:45:28 -0000 Mailing-List: contact commits-help@openwebbeans.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@openwebbeans.apache.org Delivered-To: mailing list commits@openwebbeans.apache.org Received: (qmail 64416 invoked by uid 99); 3 Jan 2013 21:45:28 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 03 Jan 2013 21:45:28 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 03 Jan 2013 21:45:25 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 828C323888EA; Thu, 3 Jan 2013 21:45:04 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: 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 Date: Thu, 03 Jan 2013 21:45:04 -0000 To: commits@openwebbeans.apache.org From: struberg@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20130103214504.828C323888EA@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 createProxyInstance(Class proxyClass, T instance, InvocationHandler interceptorDecoratorStack) + public T createProxyInstance(Class 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 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 ("".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 (() invocationHandler.invoke(this, method, new Object[] { 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 invokedMethodNames = new ArrayList(); - private final Object instance; + private List interceptedMethods; - public TestInvocationHandler(Object instance) + public TestInvocationHandler(List 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); } } }