deltaspike-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tandrasc...@apache.org
Subject deltaspike git commit: DELTASPIKE-1069 evaluate interceptors for producer-methods
Date Mon, 21 Mar 2016 22:54:44 GMT
Repository: deltaspike
Updated Branches:
  refs/heads/master 818dabe09 -> b13e68983


DELTASPIKE-1069 evaluate interceptors for producer-methods

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

Branch: refs/heads/master
Commit: b13e6898349d2021871a5985f85e1d99325461e7
Parents: 818dabe
Author: Thomas Andraschko <tandraschko@apache.org>
Authored: Mon Mar 21 23:54:29 2016 +0100
Committer: Thomas Andraschko <tandraschko@apache.org>
Committed: Mon Mar 21 23:54:29 2016 +0100

----------------------------------------------------------------------
 .../proxy/spi/EnableInterceptorsDelegate.java   |  3 ++
 .../spi/EnableInterceptorsInterceptor.java      | 16 +-----
 .../spi/EnableInterceptorsProxyFactory.java     | 51 +++++++++++++-------
 3 files changed, 39 insertions(+), 31 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/b13e6898/deltaspike/modules/proxy/api/src/main/java/org/apache/deltaspike/proxy/spi/EnableInterceptorsDelegate.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/proxy/api/src/main/java/org/apache/deltaspike/proxy/spi/EnableInterceptorsDelegate.java
b/deltaspike/modules/proxy/api/src/main/java/org/apache/deltaspike/proxy/spi/EnableInterceptorsDelegate.java
index c3323e2..d736340 100644
--- a/deltaspike/modules/proxy/api/src/main/java/org/apache/deltaspike/proxy/spi/EnableInterceptorsDelegate.java
+++ b/deltaspike/modules/proxy/api/src/main/java/org/apache/deltaspike/proxy/spi/EnableInterceptorsDelegate.java
@@ -22,6 +22,9 @@ import java.io.Serializable;
 import java.lang.reflect.InvocationHandler;
 import java.lang.reflect.Method;
 
+/**
+ * {@link InvocationHandler} to delegate every method call to an provided object instance.
+ */
 public class EnableInterceptorsDelegate implements InvocationHandler, Serializable
 {
     private final Object instance;

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/b13e6898/deltaspike/modules/proxy/api/src/main/java/org/apache/deltaspike/proxy/spi/EnableInterceptorsInterceptor.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/proxy/api/src/main/java/org/apache/deltaspike/proxy/spi/EnableInterceptorsInterceptor.java
b/deltaspike/modules/proxy/api/src/main/java/org/apache/deltaspike/proxy/spi/EnableInterceptorsInterceptor.java
index 3c124a4..5ffa4e7 100644
--- a/deltaspike/modules/proxy/api/src/main/java/org/apache/deltaspike/proxy/spi/EnableInterceptorsInterceptor.java
+++ b/deltaspike/modules/proxy/api/src/main/java/org/apache/deltaspike/proxy/spi/EnableInterceptorsInterceptor.java
@@ -19,7 +19,6 @@
 package org.apache.deltaspike.proxy.spi;
 
 import java.io.Serializable;
-import java.lang.reflect.Constructor;
 import javax.enterprise.inject.spi.BeanManager;
 import javax.inject.Inject;
 import javax.interceptor.AroundInvoke;
@@ -37,19 +36,8 @@ public class EnableInterceptorsInterceptor implements Serializable
     @AroundInvoke
     public Object wrapBeanCandidate(InvocationContext invocationContext) throws Exception
     {
-        Object beanCandidate = invocationContext.proceed();
+        Object producerResult = invocationContext.proceed();
         
-        if (beanCandidate == null)
-        {
-            throw new IllegalStateException("Can not apply "
-                    + EnableInterceptors.class.getSimpleName()
-                    + " on a null instance!");
-        }
-
-        Class proxyClass = EnableInterceptorsProxyFactory.getInstance().getProxyClass(beanManager,
-                beanCandidate.getClass(), EnableInterceptorsDelegate.class);
-                
-        Constructor constructor = proxyClass.getConstructor(EnableInterceptorsDelegate.class);
-        return constructor.newInstance(new EnableInterceptorsDelegate(beanCandidate));
+        return EnableInterceptorsProxyFactory.wrap(producerResult, beanManager);
     }
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/b13e6898/deltaspike/modules/proxy/api/src/main/java/org/apache/deltaspike/proxy/spi/EnableInterceptorsProxyFactory.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/proxy/api/src/main/java/org/apache/deltaspike/proxy/spi/EnableInterceptorsProxyFactory.java
b/deltaspike/modules/proxy/api/src/main/java/org/apache/deltaspike/proxy/spi/EnableInterceptorsProxyFactory.java
index 1de27a0..508be8b 100644
--- a/deltaspike/modules/proxy/api/src/main/java/org/apache/deltaspike/proxy/spi/EnableInterceptorsProxyFactory.java
+++ b/deltaspike/modules/proxy/api/src/main/java/org/apache/deltaspike/proxy/spi/EnableInterceptorsProxyFactory.java
@@ -18,10 +18,10 @@
  */
 package org.apache.deltaspike.proxy.spi;
 
+import java.lang.reflect.Constructor;
 import java.lang.reflect.Method;
-import java.lang.reflect.Modifier;
 import java.util.ArrayList;
-import java.util.Iterator;
+import javax.enterprise.inject.spi.BeanManager;
 import org.apache.deltaspike.proxy.api.DeltaSpikeProxyFactory;
 
 public class EnableInterceptorsProxyFactory extends DeltaSpikeProxyFactory
@@ -33,30 +33,47 @@ public class EnableInterceptorsProxyFactory extends DeltaSpikeProxyFactory
         return INSTANCE;
     }
    
-    @Override
-    protected ArrayList<Method> getDelegateMethods(Class<?> targetClass, ArrayList<Method>
allMethods)
+    public static <T> T wrap(T obj, BeanManager beanManager)
     {
-        ArrayList<Method> methods = new ArrayList<Method>();
-        
-        Iterator<Method> it = allMethods.iterator();
-        while (it.hasNext())
+        if (obj == null)
         {
-            Method method = it.next();
-
-            if (Modifier.isPublic(method.getModifiers())
-                    && !Modifier.isFinal(method.getModifiers())
-                    && !Modifier.isAbstract(method.getModifiers()))
-            {
-                methods.add(method);
-            }
+            throw new IllegalArgumentException("obj must not be null!");
         }
         
-        return methods;
+        // generate proxy
+        Class proxyClass = EnableInterceptorsProxyFactory.getInstance().getProxyClass(beanManager,
+                obj.getClass(), EnableInterceptorsDelegate.class);
+
+        // delegate method calls to our original instance from the wrapped producer method
+        EnableInterceptorsDelegate delegate = new EnableInterceptorsDelegate(obj);
+
+        try
+        {
+            // instantiate proxy
+            Constructor constructor = proxyClass.getConstructor(EnableInterceptorsDelegate.class);
+            return (T) constructor.newInstance(delegate);
+        }
+        catch (Exception e)
+        {
+            throw new RuntimeException("Could not create proxy instance by class " + obj.getClass(),
e);
+        }
     }
     
     @Override
+    protected ArrayList<Method> getDelegateMethods(Class<?> targetClass, ArrayList<Method>
allMethods)
+    {
+        // the default #filterInterceptMethods filters all non-public, final and abstract
methods
+        // which means actually every publich proxyable method
+        // as we need to delegate method call to the original object instance -> proxy
all public methods
+        ArrayList<Method> delegateMethods = super.filterInterceptMethods(targetClass,
allMethods);
+        return delegateMethods;
+    }
+
+    @Override
     protected ArrayList<Method> filterInterceptMethods(Class<?> targetClass,
ArrayList<Method> allMethods)
     {
+        // we don't need to overwrite methods to just execute interceptors
+        // all method call are delegated to our EnableInterceptorsDelegate, to delegate to
the original object instance
         return null;
     }
 


Mime
View raw message