openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From strub...@apache.org
Subject svn commit: r1444455 - in /openwebbeans/trunk/webbeans-impl/src: main/java/org/apache/webbeans/event/ main/java/org/apache/webbeans/portable/ main/java/org/apache/webbeans/proxy/ test/java/org/apache/webbeans/newtests/interceptors/factory/beans/
Date Sat, 09 Feb 2013 22:16:38 GMT
Author: struberg
Date: Sat Feb  9 22:16:38 2013
New Revision: 1444455

URL: http://svn.apache.org/r1444455
Log:
OWB-344 continue with proxying support for protected methods

Modified:
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/ObserverMethodImpl.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/InjectionTargetImpl.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/AbstractProxyFactory.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/InterceptorDecoratorProxyFactory.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/NormalScopeProxyFactory.java
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/interceptors/factory/beans/ClassInterceptedClass.java

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/ObserverMethodImpl.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/ObserverMethodImpl.java?rev=1444455&r1=1444454&r2=1444455&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/ObserverMethodImpl.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/ObserverMethodImpl.java
Sat Feb  9 22:16:38 2013
@@ -291,7 +291,7 @@ public class ObserverMethodImpl<T> imple
                         // since private methods cannot be intercepted, we have to unwrap
anny possible proxy
                         if (object instanceof OwbNormalScopeProxy)
                         {
-                            object = getWebBeansContext().getInterceptorDecoratorProxyFactory().getInternalInstance(object);
+                            object = getWebBeansContext().getInterceptorDecoratorProxyFactory().unwrapInstance(object);
                         }
                     }
 

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/InjectionTargetImpl.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/InjectionTargetImpl.java?rev=1444455&r1=1444454&r2=1444455&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/InjectionTargetImpl.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/InjectionTargetImpl.java
Sat Feb  9 22:16:38 2013
@@ -167,7 +167,7 @@ public class InjectionTargetImpl<T> exte
     {
         if (probableProxyInstance instanceof OwbInterceptorProxy)
         {
-            return webBeansContext.getInterceptorDecoratorProxyFactory().getInternalInstance(probableProxyInstance);
+            return webBeansContext.getInterceptorDecoratorProxyFactory().unwrapInstance(probableProxyInstance);
         }
 
         return probableProxyInstance;

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/AbstractProxyFactory.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/AbstractProxyFactory.java?rev=1444455&r1=1444454&r2=1444455&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/AbstractProxyFactory.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/AbstractProxyFactory.java
Sat Feb  9 22:16:38 2013
@@ -274,7 +274,7 @@ public abstract class AbstractProxyFacto
         int modifiers = delegatedMethod.getModifiers();
 
         //X TODO how to deal with native functions?
-        return (modifiers & (Modifier.PRIVATE | Modifier.PROTECTED | Modifier.STATIC
| Modifier.FINAL | Modifier.NATIVE)) > 0 ||
+        return (modifiers & (Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL | Modifier.NATIVE))
> 0 ||
                "finalize".equals(delegatedMethod.getName());
     }
 

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=1444455&r1=1444454&r2=1444455&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
Sat Feb  9 22:16:38 2013
@@ -102,13 +102,20 @@ public class InterceptorDecoratorProxyFa
     /**
      * @return the internal instance which gets proxied.
      */
-    public <T> T getInternalInstance(T proxyInstance)
+    public static <T> T unwrapInstance(T proxyInstance)
     {
         try
         {
-            Field internalInstanceField = proxyInstance.getClass().getDeclaredField(FIELD_PROXIED_INSTANCE);
-            internalInstanceField.setAccessible(true);
-            return (T) internalInstanceField.get(proxyInstance);
+            if (proxyInstance instanceof OwbInterceptorProxy)
+            {
+                Field internalInstanceField = proxyInstance.getClass().getDeclaredField(FIELD_PROXIED_INSTANCE);
+                internalInstanceField.setAccessible(true);
+                return (T) internalInstanceField.get(proxyInstance);
+            }
+            else
+            {
+                return proxyInstance;
+            }
         }
         catch (Exception e)
         {
@@ -143,7 +150,7 @@ public class InterceptorDecoratorProxyFa
      * <ol>
      *     <li>
      *         private methods - they do not get proxied at all! If you like to invoke a
private method,
-     *         then you can use {@link #getInternalInstance(Object)} and use reflection on
it.
+     *         then you can use {@link #unwrapInstance(Object)} and use reflection on it.
      *     </li>
      *     <li>
      *         non-proxied methods - all methods which do not have a business interceptor
nor decorator

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/NormalScopeProxyFactory.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/NormalScopeProxyFactory.java?rev=1444455&r1=1444454&r2=1444455&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/NormalScopeProxyFactory.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/NormalScopeProxyFactory.java
Sat Feb  9 22:16:38 2013
@@ -27,6 +27,7 @@ import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.concurrent.ConcurrentHashMap;
 
@@ -52,6 +53,9 @@ public class NormalScopeProxyFactory ext
     /** the name of the field which stores the {@link Provider} for the Contextual Instance
*/
     public static final String FIELD_INSTANCE_PROVIDER = "owbContextualInstanceProvider";
 
+    /** the Method[] for all protected methods. We need to invoke them via reflection. */
+    public static final String FIELD_PROTECTED_METHODS = "owbProtectedMethods";
+
     /**
      * Caches the proxy classes for each bean.
      * We need this to prevent filling up the ClassLoaders by
@@ -70,6 +74,27 @@ public class NormalScopeProxyFactory ext
         return OwbNormalScopeProxy.class;
     }
 
+
+    public static <T> T unwrapInstance(T proxyInstance)
+    {
+        if (proxyInstance instanceof OwbNormalScopeProxy)
+        {
+            try
+            {
+                Field internalInstanceField = proxyInstance.getClass().getDeclaredField(FIELD_INSTANCE_PROVIDER);
+                internalInstanceField.setAccessible(true);
+                Provider<T> provider = (Provider<T>) internalInstanceField.get(proxyInstance);
+                return provider.get();
+            }
+            catch (Exception e)
+            {
+                ExceptionUtil.throwAsRuntimeException(e);
+            }
+        }
+
+        return proxyInstance;
+    }
+
     /**
      * @return the internal instance which gets proxied.
      */
@@ -196,18 +221,52 @@ public class NormalScopeProxyFactory ext
         String proxyClassName = getUnusedProxyClassName(classLoader, classToProxy.getName()
+ "$OwbNormalScopeProxy");
 
         Method[] nonInterceptedMethods;
+        Method[] interceptedMethods = null;
         if (classToProxy.isInterface())
         {
             nonInterceptedMethods = classToProxy.getMethods();
         }
         else
         {
-            List<Method> methods = ClassUtil.getNonPrivateMethods(classToProxy, true);
+            List<Method> methods = new ArrayList<Method>();
+            List<Method> protectedMethods = new ArrayList<Method>();
+
+
+            for (Method method : ClassUtil.getNonPrivateMethods(classToProxy, true))
+            {
+                if (unproxyableMethod(method))
+                {
+                    continue;
+                }
+                if (Modifier.isProtected(method.getModifiers()))
+                {
+                    protectedMethods.add(method);
+                }
+                else
+                {
+                    methods.add(method);
+                }
+            }
+
             nonInterceptedMethods = methods.toArray(new Method[methods.size()]);
+            interceptedMethods = protectedMethods.toArray(new Method[protectedMethods.size()]);
         }
 
-        Class<T> clazz = createProxyClass(classLoader, proxyClassName, classToProxy,
null, nonInterceptedMethods);
+        Class<T> clazz = createProxyClass(classLoader, proxyClassName, classToProxy,
interceptedMethods, nonInterceptedMethods);
 
+        if (interceptedMethods != null && interceptedMethods.length > 0)
+        {
+            try
+            {
+                Field protectedMethodsField = clazz.getDeclaredField(FIELD_PROTECTED_METHODS);
+                protectedMethodsField.setAccessible(true);
+                protectedMethodsField.set(null, interceptedMethods);
+            }
+            catch (Exception e)
+            {
+                throw new ProxyGenerationException(e);
+            }
+        }
         return clazz;
     }
 
@@ -255,6 +314,7 @@ public class NormalScopeProxyFactory ext
             mv.visitVarInsn(Opcodes.ALOAD, 0);
             mv.visitMethodInsn(Opcodes.INVOKESPECIAL, parentClassFileName, "<init>",
descriptor);
 
+            // the instance provider field
             mv.visitVarInsn(Opcodes.ALOAD, 0);
             mv.visitInsn(Opcodes.ACONST_NULL);
             mv.visitFieldInsn(Opcodes.PUTFIELD, proxyClassFileName, FIELD_INSTANCE_PROVIDER,
Type.getDescriptor(Provider.class));
@@ -275,14 +335,22 @@ public class NormalScopeProxyFactory ext
         // variable #1, the Provider<?> for the Contextual Instance
         cw.visitField(Opcodes.ACC_PRIVATE,
                 FIELD_INSTANCE_PROVIDER, Type.getDescriptor(Provider.class), null, null).visitEnd();
+
+        // variable #2, the Method[] for all protected methods
+        cw.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC,
+                FIELD_PROTECTED_METHODS, Type.getDescriptor(Method[].class), null, null).visitEnd();
     }
 
+    /**
+     * In the NormalScope proxying case this is used for all the protected methods
+     * as they need to get invoked via reflection.
+     */
     @Override
     protected void delegateInterceptedMethods(ClassLoader classLoader, ClassWriter cw, String
proxyClassFileName,
                                               Class<?> classToProxy, Method[] interceptedMethods)
             throws ProxyGenerationException
     {
-        // nothing to do ;)
+        //X TODO invoke protected methods via reflection
     }
 
     @Override
@@ -290,13 +358,9 @@ public class NormalScopeProxyFactory ext
                                                  Class<?> classToProxy, Method[] noninterceptedMethods)
             throws ProxyGenerationException
     {
+
         for (Method delegatedMethod : noninterceptedMethods)
         {
-            if (unproxyableMethod(delegatedMethod))
-            {
-                continue;
-            }
-
             String methodDescriptor = Type.getMethodDescriptor(delegatedMethod);
 
             //X TODO handle generic exception types?
@@ -346,6 +410,8 @@ public class NormalScopeProxyFactory ext
 
             mv.visitEnd();
         }
+
     }
 
+
 }

Modified: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/interceptors/factory/beans/ClassInterceptedClass.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/interceptors/factory/beans/ClassInterceptedClass.java?rev=1444455&r1=1444454&r2=1444455&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/interceptors/factory/beans/ClassInterceptedClass.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/interceptors/factory/beans/ClassInterceptedClass.java
Sat Feb  9 22:16:38 2013
@@ -91,9 +91,9 @@ public class ClassInterceptedClass
         throw new NumberFormatException("should fit");
     }
 
-    protected void initConf()
+    protected int protectedMethod()
     {
-        //
+        return 21;
     }
 
 }



Mime
View raw message