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
The following commit(s) were added to refs/heads/master by this push: new 21c34ae ISIS-1949: refining ProxyFactory API, also adding java-doc 21c34ae is described below commit 21c34aeda54b951e30b2498991359684a6037952 Author: Andi Huber <ahu...@apache.org> AuthorDate: Sat May 19 17:28:33 2018 +0200 ISIS-1949: refining ProxyFactory API, also adding java-doc Task-Url: https://issues.apache.org/jira/browse/ISIS-1949 --- .../codegen/ProxyFactoryPluginUsingJavassist.java | 50 +++++++++++++++++----- .../core/runtime/plugins/codegen/ProxyFactory.java | 31 +++++++++++--- .../core/runtime/services/ServiceInstantiator.java | 2 +- .../background/BackgroundServiceDefault.java | 12 ++++-- .../isis/core/wrapper/proxy/ProxyCreator.java | 2 +- 5 files changed, 75 insertions(+), 22 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 f9d43e6..2bb6a8f 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 @@ -5,6 +5,9 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.function.Predicate; +import javax.annotation.Nullable; + +import org.apache.isis.applib.internal._Constants; import org.apache.isis.applib.internal.base._Casts; import org.apache.isis.applib.internal.base._NullSafe; import org.apache.isis.core.commons.exceptions.IsisException; @@ -33,26 +36,42 @@ public class ProxyFactoryPluginUsingJavassist implements ProxyFactoryPlugin { } return new ProxyFactory<T>() { - + @Override - public T createInstance(InvocationHandler handler, Object[] constructorArgs) { - - ensureSameSize(constructorArgTypes, constructorArgs); + public T createInstance(InvocationHandler handler, boolean initialize) { try { - if(_NullSafe.isEmpty(constructorArgTypes)) { - return _Casts.uncheckedCast( createNotUsingConstructor(handler) ); + if(initialize) { + ensureSameSize(constructorArgTypes, null); + return _Casts.uncheckedCast( createUsingConstructor(handler, null) ); } else { - return _Casts.uncheckedCast( createUsingConstructor(handler, constructorArgs) ); + return _Casts.uncheckedCast( createNotUsingConstructor(handler) ); } } catch (NoSuchMethodException | IllegalArgumentException | InstantiationException | IllegalAccessException | InvocationTargetException e) { throw new IsisException(e); } + + } + + @Override + public T createInstance(InvocationHandler handler, Object[] constructorArgs) { + + ensureSameSize(constructorArgTypes, constructorArgs); + ensureNonEmtpy(constructorArgs); + + try { + return _Casts.uncheckedCast( createUsingConstructor(handler, constructorArgs) ); + } catch (NoSuchMethodException | IllegalArgumentException | InstantiationException | + IllegalAccessException | InvocationTargetException e) { + throw new IsisException(e); + } } + // -- HELPER (create w/o initialize) + private Object createNotUsingConstructor(InvocationHandler handler) { final Class<?> proxyClass = pfDelegate.createClass(); @@ -65,13 +84,15 @@ public class ProxyFactoryPluginUsingJavassist implements ProxyFactoryPlugin { return object; } - private Object createUsingConstructor(InvocationHandler handler, Object[] constructorArgs) + // -- HELPER (create with initialize) + + private Object createUsingConstructor(InvocationHandler handler, @Nullable Object[] constructorArgs) throws NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { return pfDelegate.create( - constructorArgTypes, - constructorArgs, + constructorArgTypes==null ? _Constants.emptyClasses : constructorArgTypes, + constructorArgs==null ? _Constants.emptyObjects : constructorArgs, (Object self, Method thisMethod, Method proceed, Object[] args)->{ return handler.invoke(self, thisMethod, args); }); @@ -84,9 +105,16 @@ public class ProxyFactoryPluginUsingJavassist implements ProxyFactoryPlugin { 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.", + throw new IllegalArgumentException(String.format("Constructor arg count expected %d, got %d.", _NullSafe.size(a), _NullSafe.size(b) )); } } + + private static void ensureNonEmtpy(Object[] a) { + if(_NullSafe.isEmpty(a)) { + throw new IllegalArgumentException(String.format("Contructor args count expected > 0, got %d.", + _NullSafe.size(a) )); + } + } } diff --git a/core/runtime/src/main/java/org/apache/isis/core/runtime/plugins/codegen/ProxyFactory.java b/core/runtime/src/main/java/org/apache/isis/core/runtime/plugins/codegen/ProxyFactory.java index ec0aad1..b7c0ed2 100644 --- a/core/runtime/src/main/java/org/apache/isis/core/runtime/plugins/codegen/ProxyFactory.java +++ b/core/runtime/src/main/java/org/apache/isis/core/runtime/plugins/codegen/ProxyFactory.java @@ -22,17 +22,36 @@ import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.util.function.Predicate; -import javax.annotation.Nullable; - +/** + * Generates dynamic classes and corresponding instances by rebasing a given 'base' class. + * + * @since 2.0.0 + * @param <T> type of proxy objects this factory creates + */ public interface ProxyFactory<T> { // -- INTERFACE - public default T createInstance(InvocationHandler handler) { - return createInstance(handler, null); - } + /** + * + * @param handler + * @param initialize whether to call a constructor on object instantiation + * @return + * @throws IllegalArgumentException when using initialize=true and the number of + * constructorArgTypes specified while building this factory is greater than 0. + */ + public T createInstance(InvocationHandler handler, boolean initialize); - public T createInstance(InvocationHandler handler, @Nullable Object[] constructorArgs); + /** + * + * @param handler + * @param constructorArgs passed to the constructor with matching signature on object instantiation + * @param initialize whether to call a constructor on object instantiation + * @return + * @throws IllegalArgumentException if constructorArgs is null or empty or does not + * conform to the number of constructorArgTypes specified while building this factory. + */ + public T createInstance(InvocationHandler handler, Object[] constructorArgs); // -- BUILDER (uses plugin) diff --git a/core/runtime/src/main/java/org/apache/isis/core/runtime/services/ServiceInstantiator.java b/core/runtime/src/main/java/org/apache/isis/core/runtime/services/ServiceInstantiator.java index 55f608f..079aaf0 100644 --- a/core/runtime/src/main/java/org/apache/isis/core/runtime/services/ServiceInstantiator.java +++ b/core/runtime/src/main/java/org/apache/isis/core/runtime/services/ServiceInstantiator.java @@ -218,7 +218,7 @@ public final class ServiceInstantiator { }; - return proxyFactory.createInstance(handler); + return proxyFactory.createInstance(handler, false); } private Set<Class<?>> cached = Sets.newHashSet(); diff --git a/core/runtime/src/main/java/org/apache/isis/core/runtime/services/background/BackgroundServiceDefault.java b/core/runtime/src/main/java/org/apache/isis/core/runtime/services/background/BackgroundServiceDefault.java index d3242fe..af07168 100644 --- a/core/runtime/src/main/java/org/apache/isis/core/runtime/services/background/BackgroundServiceDefault.java +++ b/core/runtime/src/main/java/org/apache/isis/core/runtime/services/background/BackgroundServiceDefault.java @@ -123,15 +123,21 @@ public class BackgroundServiceDefault implements BackgroundService2 { cls.getInterfaces(), new Class<?>[] { ProxyEnhanced.class }); - final Class<?>[] constructorArgTypes = mixedInIfAny!=null ? new Class<?>[] {mixedInIfAny.getClass()} : _Constants.emptyClasses; - final Object[] constructorArgs = mixedInIfAny!=null ? new Object[] {mixedInIfAny} : _Constants.emptyObjects; + final boolean initialize = mixedInIfAny!=null; + + + final Class<?>[] constructorArgTypes = initialize ? new Class<?>[] {mixedInIfAny.getClass()} : _Constants.emptyClasses; + final Object[] constructorArgs = initialize ? new Object[] {mixedInIfAny} : _Constants.emptyObjects; final ProxyFactory<T> proxyFactory = ProxyFactory.builder(cls) .interfaces(interfaces) .constructorArgTypes(constructorArgTypes) .build(); - return proxyFactory.createInstance(methodHandler, constructorArgs); + return initialize + ? proxyFactory.createInstance(methodHandler, constructorArgs) + : proxyFactory.createInstance(methodHandler, false) + ; } /** diff --git a/core/wrapper/src/main/java/org/apache/isis/core/wrapper/proxy/ProxyCreator.java b/core/wrapper/src/main/java/org/apache/isis/core/wrapper/proxy/ProxyCreator.java index 9222180..a66acdf 100644 --- a/core/wrapper/src/main/java/org/apache/isis/core/wrapper/proxy/ProxyCreator.java +++ b/core/wrapper/src/main/java/org/apache/isis/core/wrapper/proxy/ProxyCreator.java @@ -59,7 +59,7 @@ public class ProxyCreator { return createInstanceForInterface(base, handler, WrappingObject.class); } else { final ProxyFactory<T> proxyFactory = proxyFactoryFor(base); - return proxyFactory.createInstance(handler); + return proxyFactory.createInstance(handler, false); } } -- To stop receiving notification emails like this one, please contact ahu...@apache.org.