You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2018/05/19 14:26:57 UTC

[isis] 02/03: ISIS-1949: reinstate original behavior to not call noarg-constructor

This is an automated email from the ASF dual-hosted git repository.

ahuber pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git

commit c9f71c788107dffeee357d43a20968ebfd3b9b8e
Author: Andi Huber <ah...@apache.org>
AuthorDate: Sat May 19 16:20:42 2018 +0200

    ISIS-1949: reinstate original behavior to not call noarg-constructor
    
    Task-Url: https://issues.apache.org/jira/browse/ISIS-1949
---
 .../codegen/ProxyFactoryPluginUsingJavassist.java  | 66 ++++++++++++++++------
 1 file changed, 49 insertions(+), 17 deletions(-)

diff --git a/core/plugins/codegen-javassist/src/main/java/org/apache/isis/core/runtime/plugins/codegen/ProxyFactoryPluginUsingJavassist.java b/core/plugins/codegen-javassist/src/main/java/org/apache/isis/core/runtime/plugins/codegen/ProxyFactoryPluginUsingJavassist.java
index 1f156c0..f9d43e6 100644
--- a/core/plugins/codegen-javassist/src/main/java/org/apache/isis/core/runtime/plugins/codegen/ProxyFactoryPluginUsingJavassist.java
+++ b/core/plugins/codegen-javassist/src/main/java/org/apache/isis/core/runtime/plugins/codegen/ProxyFactoryPluginUsingJavassist.java
@@ -8,9 +8,13 @@ import java.util.function.Predicate;
 import org.apache.isis.applib.internal.base._Casts;
 import org.apache.isis.applib.internal.base._NullSafe;
 import org.apache.isis.core.commons.exceptions.IsisException;
+import org.objenesis.Objenesis;
+import org.objenesis.ObjenesisStd;
 
-public class ProxyFactoryPluginUsingJavassist implements ProxyFactoryPlugin {
+import javassist.util.proxy.ProxyObject;
 
+public class ProxyFactoryPluginUsingJavassist implements ProxyFactoryPlugin {
+	
 	@Override
 	public <T> ProxyFactory<T> factory(
 			final Class<T> base, 
@@ -19,6 +23,7 @@ public class ProxyFactoryPluginUsingJavassist implements ProxyFactoryPlugin {
 			final Class<?>[] constructorArgTypes) {
 		
         final javassist.util.proxy.ProxyFactory pfDelegate = new javassist.util.proxy.ProxyFactory();
+        final Objenesis objenesis = new ObjenesisStd();
 
         pfDelegate.setSuperclass(base);
         pfDelegate.setInterfaces(interfaces);
@@ -26,35 +31,62 @@ public class ProxyFactoryPluginUsingJavassist implements ProxyFactoryPlugin {
         if(methodFilter!=null) {
         	pfDelegate.setFilter(methodFilter::test);	
         }
-		
-        final int constructorArgCount = _NullSafe.size(constructorArgTypes);
         
 		return new ProxyFactory<T>() {
 
 			@Override
-			public T createInstance(final InvocationHandler handler, final Object[] constructorArgs) {
-				
-				final int constructorArgCountActual = _NullSafe.size(constructorArgTypes);
-				
-				if(constructorArgCount != constructorArgCountActual) {
-					throw new IllegalArgumentException(String.format("Contructor args expected %d, got %d.", 
-							constructorArgCount, constructorArgCountActual));
-				}
+			public T createInstance(InvocationHandler handler, Object[] constructorArgs) {
 				
+				ensureSameSize(constructorArgTypes, constructorArgs);
+
 				try {
-					return _Casts.uncheckedCast( pfDelegate.create(
-							constructorArgTypes, 
-							constructorArgs, 
-							(Object self, Method thisMethod, Method proceed, Object[] args)->{
-								return handler.invoke(self, thisMethod, args);
-							}));
+					
+					if(_NullSafe.isEmpty(constructorArgTypes)) {
+						return _Casts.uncheckedCast( createNotUsingConstructor(handler) );	
+					} else {
+						return _Casts.uncheckedCast( createUsingConstructor(handler, constructorArgs) );
+					}
+					
 				} catch (NoSuchMethodException | IllegalArgumentException | InstantiationException | 
 						IllegalAccessException | InvocationTargetException e) {
 					throw new IsisException(e);
 				}
 			}
+
+			private Object createNotUsingConstructor(InvocationHandler handler) {
+				final Class<?> proxyClass = pfDelegate.createClass();
+				
+				final Object object = objenesis.newInstance(proxyClass);
+				
+				((ProxyObject)object).setHandler((Object self, Method thisMethod, Method proceed, Object[] args)->{
+					return handler.invoke(self, thisMethod, args);
+				});
+				
+				return object;
+			}
+			
+			private Object createUsingConstructor(InvocationHandler handler, Object[] constructorArgs)
+				throws NoSuchMethodException, IllegalArgumentException, InstantiationException, 
+					IllegalAccessException, InvocationTargetException {
+				
+				return pfDelegate.create(
+						constructorArgTypes, 
+						constructorArgs, 
+						(Object self, Method thisMethod, Method proceed, Object[] args)->{
+							return handler.invoke(self, thisMethod, args);
+						});
+			}
 			
 		};
 	}
 
+	// -- HELPER
+	
+	private static void ensureSameSize(Class<?>[] a, Object[] b) {
+		if(_NullSafe.size(a) != _NullSafe.size(b)) {
+			throw new IllegalArgumentException(String.format("Contructor args expected %d, got %d.", 
+					_NullSafe.size(a), _NullSafe.size(b) ));
+		}
+	}
+
 }

-- 
To stop receiving notification emails like this one, please contact
ahuber@apache.org.