You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltaspike.apache.org by ta...@apache.org on 2015/03/11 23:46:05 UTC

deltaspike git commit: DELTASPIKE-420 removed exception handling from new partial bean implementation

Repository: deltaspike
Updated Branches:
  refs/heads/master 7321797fe -> ef13b74d6


DELTASPIKE-420 removed exception handling from new partial bean implementation

Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/ef13b74d
Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/ef13b74d
Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/ef13b74d

Branch: refs/heads/master
Commit: ef13b74d646b1d11a0a732c4c3359df720da6d7a
Parents: 7321797
Author: Thomas Andraschko <ta...@apache.org>
Authored: Wed Mar 11 23:44:35 2015 +0100
Committer: Thomas Andraschko <ta...@apache.org>
Committed: Wed Mar 11 23:45:56 2015 +0100

----------------------------------------------------------------------
 deltaspike/modules/partial-bean/impl/pom.xml    |  6 --
 .../interception/ManualInvocationContext.java   | 68 +++++++++-----------
 .../interception/ManualInvocationHandler.java   | 11 +++-
 .../ProceedOriginalMethodException.java         | 30 ---------
 .../impl/proxy/AsmProxyClassGenerator.java      | 27 +++++---
 5 files changed, 57 insertions(+), 85 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/ef13b74d/deltaspike/modules/partial-bean/impl/pom.xml
----------------------------------------------------------------------
diff --git a/deltaspike/modules/partial-bean/impl/pom.xml b/deltaspike/modules/partial-bean/impl/pom.xml
index c2c3f05..6fdf84e 100644
--- a/deltaspike/modules/partial-bean/impl/pom.xml
+++ b/deltaspike/modules/partial-bean/impl/pom.xml
@@ -92,12 +92,6 @@
         </dependency>
 
         <dependency>
-            <groupId>org.apache.deltaspike.core</groupId>
-            <artifactId>deltaspike-core-impl</artifactId>
-            <scope>test</scope>
-        </dependency>
-
-        <dependency>
             <groupId>org.apache.deltaspike.modules</groupId>
             <artifactId>deltaspike-partial-bean-module-api</artifactId>
         </dependency>

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/ef13b74d/deltaspike/modules/partial-bean/impl/src/main/java/org/apache/deltaspike/partialbean/impl/interception/ManualInvocationContext.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/partial-bean/impl/src/main/java/org/apache/deltaspike/partialbean/impl/interception/ManualInvocationContext.java b/deltaspike/modules/partial-bean/impl/src/main/java/org/apache/deltaspike/partialbean/impl/interception/ManualInvocationContext.java
index 126d659..ede57cc 100644
--- a/deltaspike/modules/partial-bean/impl/src/main/java/org/apache/deltaspike/partialbean/impl/interception/ManualInvocationContext.java
+++ b/deltaspike/modules/partial-bean/impl/src/main/java/org/apache/deltaspike/partialbean/impl/interception/ManualInvocationContext.java
@@ -25,6 +25,7 @@ import java.util.List;
 import java.util.Map;
 import javax.enterprise.context.spi.CreationalContext;
 import javax.enterprise.inject.Typed;
+import javax.enterprise.inject.spi.BeanManager;
 import javax.enterprise.inject.spi.InterceptionType;
 import javax.enterprise.inject.spi.Interceptor;
 import javax.interceptor.InvocationContext;
@@ -41,6 +42,10 @@ public class ManualInvocationContext<T, H> implements InvocationContext
     protected Map<String, Object> contextData;
     protected Object timer;
     protected ManualInvocationHandler manualInvocationHandler;
+    
+    protected BeanManager beanManager;
+    
+    protected boolean proceedOriginal;
 
     public ManualInvocationContext(ManualInvocationHandler manualInvocationHandler,
             List<Interceptor<H>> interceptors, T target, Method method, Object[] parameters, Object timer)
@@ -92,6 +97,11 @@ public class ManualInvocationContext<T, H> implements InvocationContext
     @Override
     public Object proceed() throws Exception
     {
+        if (proceedOriginal)
+        {
+            return null;
+        }
+        
         if (interceptors.size() > interceptorIndex)
         {
             Interceptor<H> interceptor = null;
@@ -100,26 +110,17 @@ public class ManualInvocationContext<T, H> implements InvocationContext
 
             try
             {
-                interceptor =
-                        interceptors.get(interceptorIndex++);
-                creationalContext =
-                        BeanManagerProvider.getInstance().getBeanManager().createCreationalContext(interceptor);
-                interceptorInstance =
-                        interceptor.create(creationalContext);
-
-                Object value = interceptor.intercept(InterceptionType.AROUND_INVOKE, interceptorInstance, this);
-                return value;
-            }
-            catch (Exception e)
-            {
-                ProceedOriginalMethodException proceedOriginalMethodException =
-                        extractProceedOriginalMethodException(e);
-                if (proceedOriginalMethodException != null)
+                // lazy init beanManager
+                if (beanManager == null)
                 {
-                    return manualInvocationHandler.proceedOriginal(target, method, parameters);
+                    beanManager = BeanManagerProvider.getInstance().getBeanManager();
                 }
 
-                throw e;
+                interceptor = interceptors.get(interceptorIndex++);
+                creationalContext = beanManager.createCreationalContext(interceptor);
+                interceptorInstance = interceptor.create(creationalContext);
+
+                return interceptor.intercept(InterceptionType.AROUND_INVOKE, interceptorInstance, this);
             }
             finally
             {
@@ -135,28 +136,14 @@ public class ManualInvocationContext<T, H> implements InvocationContext
             }
         }
 
+        
+        // workaround for OWB 1.1
+        // interceptor#intercept always return null. Therefore we must remember here,
+        // that our interceptor chain is finished and #proceedOriginal should be called outside
+        proceedOriginal = true;
+        
         // all interceptors handled without return a value
-        Object value = manualInvocationHandler.proceedOriginal(target, method, parameters);
-        return value;
-    }
-
-    protected ProceedOriginalMethodException extractProceedOriginalMethodException(Throwable throwable)
-    {
-        if (throwable instanceof ProceedOriginalMethodException)
-        {
-            return (ProceedOriginalMethodException) throwable;
-        }
-
-        while (throwable.getCause() != null)
-        {
-            throwable = throwable.getCause();
-            if (throwable instanceof ProceedOriginalMethodException)
-            {
-                return (ProceedOriginalMethodException) throwable;
-            }
-        }
-
-        return null;
+        return manualInvocationHandler.proceedOriginal(target, method, parameters);
     }
 
     @Override
@@ -171,4 +158,9 @@ public class ManualInvocationContext<T, H> implements InvocationContext
     {
         return null;
     }
+
+    public boolean isProceedOriginal()
+    {
+        return proceedOriginal;
+    }
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/ef13b74d/deltaspike/modules/partial-bean/impl/src/main/java/org/apache/deltaspike/partialbean/impl/interception/ManualInvocationHandler.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/partial-bean/impl/src/main/java/org/apache/deltaspike/partialbean/impl/interception/ManualInvocationHandler.java b/deltaspike/modules/partial-bean/impl/src/main/java/org/apache/deltaspike/partialbean/impl/interception/ManualInvocationHandler.java
index 5f9a7d6..b882bba 100644
--- a/deltaspike/modules/partial-bean/impl/src/main/java/org/apache/deltaspike/partialbean/impl/interception/ManualInvocationHandler.java
+++ b/deltaspike/modules/partial-bean/impl/src/main/java/org/apache/deltaspike/partialbean/impl/interception/ManualInvocationHandler.java
@@ -33,6 +33,8 @@ import org.apache.deltaspike.core.api.provider.BeanManagerProvider;
 @Typed
 public class ManualInvocationHandler implements InvocationHandler
 {
+    public static final Object PROCEED_ORIGINAL = new Object();
+    
     private static final ManualInvocationHandler INSTANCE = new ManualInvocationHandler();
 
     public static <T> Object staticInvoke(T proxy, Method method, Object[] parameters) throws Throwable
@@ -48,7 +50,12 @@ public class ManualInvocationHandler implements InvocationHandler
         {
             ManualInvocationContext invocationContext =
                     new ManualInvocationContext(this, interceptors, proxy, method, parameters, null);
-            return invocationContext.proceed();
+            Object returnValue = invocationContext.proceed();
+            if (invocationContext.isProceedOriginal())
+            {
+                return proceedOriginal(proxy, method, parameters);
+            }
+            return returnValue;
         }
 
         return proceedOriginal(proxy, method, parameters);
@@ -56,7 +63,7 @@ public class ManualInvocationHandler implements InvocationHandler
 
     protected Object proceedOriginal(Object proxy, Method method, Object[] parameters) throws Exception
     {
-        throw new ProceedOriginalMethodException();
+        return PROCEED_ORIGINAL;
     }
 
     protected List<Interceptor<?>> resolveInterceptors(Object instance, Method method)

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/ef13b74d/deltaspike/modules/partial-bean/impl/src/main/java/org/apache/deltaspike/partialbean/impl/interception/ProceedOriginalMethodException.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/partial-bean/impl/src/main/java/org/apache/deltaspike/partialbean/impl/interception/ProceedOriginalMethodException.java b/deltaspike/modules/partial-bean/impl/src/main/java/org/apache/deltaspike/partialbean/impl/interception/ProceedOriginalMethodException.java
deleted file mode 100644
index 40021e2..0000000
--- a/deltaspike/modules/partial-bean/impl/src/main/java/org/apache/deltaspike/partialbean/impl/interception/ProceedOriginalMethodException.java
+++ /dev/null
@@ -1,30 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.deltaspike.partialbean.impl.interception;
-
-import java.io.Serializable;
-
-/**
- * Exception which will be throewd by the {@link ManualInvocationContext} if all interecptors
- * are successfully handled and the original method should be called.
- */
-public class ProceedOriginalMethodException extends RuntimeException implements Serializable
-{
-
-}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/ef13b74d/deltaspike/modules/partial-bean/impl/src/main/java/org/apache/deltaspike/partialbean/impl/proxy/AsmProxyClassGenerator.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/partial-bean/impl/src/main/java/org/apache/deltaspike/partialbean/impl/proxy/AsmProxyClassGenerator.java b/deltaspike/modules/partial-bean/impl/src/main/java/org/apache/deltaspike/partialbean/impl/proxy/AsmProxyClassGenerator.java
index b964bab..eb6de01 100644
--- a/deltaspike/modules/partial-bean/impl/src/main/java/org/apache/deltaspike/partialbean/impl/proxy/AsmProxyClassGenerator.java
+++ b/deltaspike/modules/partial-bean/impl/src/main/java/org/apache/deltaspike/partialbean/impl/proxy/AsmProxyClassGenerator.java
@@ -24,7 +24,6 @@ import java.lang.reflect.UndeclaredThrowableException;
 import java.util.Arrays;
 import javax.enterprise.inject.Typed;
 import org.apache.deltaspike.partialbean.impl.interception.ManualInvocationHandler;
-import org.apache.deltaspike.partialbean.impl.interception.ProceedOriginalMethodException;
 import org.objectweb.asm.ClassWriter;
 import org.objectweb.asm.Label;
 import org.objectweb.asm.Opcodes;
@@ -57,6 +56,7 @@ public abstract class AsmProxyClassGenerator
 
         byte[] proxyBytes = generateProxyClassBytes(targetClass, invocationHandlerClass,
                 classFileName, redirectMethods, interceptionMethods);
+
         Class<T> proxyClass = (Class<T>) loadClass(classLoader, proxyName, proxyBytes);
 
         return proxyClass;
@@ -205,11 +205,21 @@ public abstract class AsmProxyClassGenerator
         loadCurrentMethod(mg, method, methodType);
         loadArguments(mg, method, methodType);
 
-        // invoke our ProxyInvocationHandler
+        // invoke our ProxyInvocationHandler and store it in a local variable
+        int manualInvocationHandlerReturnValue = mg.newLocal(TYPE_OBJECT);
         mg.invokeStatic(Type.getType(ManualInvocationHandler.class),
                 Method.getMethod("Object staticInvoke(Object, java.lang.reflect.Method, Object[])"));
-
+        mg.storeLocal(manualInvocationHandlerReturnValue);
+        
+        // check if ManualInvocationHandler returned the PROCEED_ORIGINAL object
+        // if true, we switch to our special logic, otherwise return the returned value
+        Label proceedOriginalStart = new Label();
+        mg.getStatic(Type.getType(ManualInvocationHandler.class), "PROCEED_ORIGINAL", TYPE_OBJECT);
+        mg.loadLocal(manualInvocationHandlerReturnValue);
+        mg.ifCmp(TYPE_OBJECT, GeneratorAdapter.EQ, proceedOriginalStart);
+        
         // cast the result
+        mg.loadLocal(manualInvocationHandlerReturnValue);
         mg.unbox(methodType.getReturnType());
 
         Label tryBlockEnd = mg.mark();
@@ -217,10 +227,7 @@ public abstract class AsmProxyClassGenerator
         // push return
         mg.returnValue();
 
-        boolean throwableCatched = false;
-
-        // catch ProceedOriginalRuntimeException
-        Label proceedOriginal = mg.mark();
+        mg.mark(proceedOriginalStart);
         if (callInvocationHandler)
         {
             // call stored InvocationHandler
@@ -246,8 +253,10 @@ public abstract class AsmProxyClassGenerator
                     false);
             mg.returnValue();
         }
-        mg.visitTryCatchBlock(tryBlockStart, tryBlockEnd, proceedOriginal,
-                Type.getInternalName(ProceedOriginalMethodException.class));
+        
+        
+        
+        boolean throwableCatched = false;
 
         // catch declared exceptions
         if (exceptionTypes.length > 0)