Forráskód Böngészése

new: BeanPropertyBinding
new: BeanMethodBinding

Ranides Atterwim 2 éve
szülő
commit
58416bb9be

+ 1 - 1
assira.core/src/main/java/net/ranides/assira/reflection/BeanMethod.java

@@ -30,7 +30,7 @@ public interface BeanMethod {
      * @param that that
      * @return new function
      */
-    AnyFunction<Object> bind(Object that);
+    BeanMethodBinding bind(Object that);
 
     /**
      * Invokes method with specified arguments. First argument is used as "this".

+ 51 - 0
assira.core/src/main/java/net/ranides/assira/reflection/BeanMethodBinding.java

@@ -0,0 +1,51 @@
+package net.ranides.assira.reflection;
+
+import net.ranides.assira.functional.special.AnyFunction;
+
+public interface BeanMethodBinding extends AnyFunction<Object> {
+
+    String name();
+
+    /**
+     * Returns true if this method can be invoked with arguments of specified types.
+     * Behaviour of invoke is identical as {@link IMethod#matches(IClasses arguments)}
+     *
+     * @param arguments arguments
+     * @return bool
+     */
+    boolean matches(IClasses arguments);
+
+    /**
+     * Returns true if this method can be invoked with arguments of specified types.
+     * Behaviour of invoke is identical as {@link IMethod#matches(IClasses arguments)}
+     *
+     * @param arguments arguments
+     * @return bool
+     */
+    default boolean matches(IClass<?>... arguments) {
+        return matches(IClasses.typeinfo(arguments));
+    }
+
+    /**
+     * Returns true if this method can be invoked with arguments of specified types.
+     * Behaviour of invoke is identical as {@link IMethod#matches(Class... arguments)}
+     *
+     * @param arguments arguments
+     * @return bool
+     */
+    default boolean matches(Class<?>... arguments) {
+        return matches(IClasses.typeinfo(arguments));
+    }
+
+    /**
+     * Returns true if this method can be invoked without arguments.
+     * Effectively it means that checks if method is static.
+     * Behaviour of invoke is identical as {@link IMethod#matches()}
+     *
+     * @return bool
+     */
+    default boolean matches() {
+        return matches(IClasses.empty());
+    }
+
+}

+ 2 - 3
assira.core/src/main/java/net/ranides/assira/reflection/BeanModelUtils.java

@@ -1,7 +1,6 @@
 package net.ranides.assira.reflection;
 
 import net.ranides.assira.functional.special.AnyFunction;
-import net.ranides.assira.generic.Wrapper;
 import net.ranides.assira.reflection.BeanModel.FluentMap;
 
 import java.util.Map;
@@ -9,7 +8,7 @@ import java.util.Optional;
 
 public class BeanModelUtils {
 
-    public static Optional<AnyFunction<Object>> method(Object that, String name) {
+    public static Optional<BeanMethodBinding> method(Object that, String name) {
         return BeanModel.typefor(that).method(name).map(v -> v.bind(that));
     }
 
@@ -17,7 +16,7 @@ public class BeanModelUtils {
         return BeanModel.typefor(that).properties(that);
     }
 
-    public static Optional<BeanPropertyWrapper<Object>> property(Object that, String property) {
+    public static Optional<BeanPropertyBinding<Object>> property(Object that, String property) {
         return BeanModel.typefor(that).property(property).map(v -> v.bind(that));
     }
 

+ 2 - 4
assira.core/src/main/java/net/ranides/assira/reflection/BeanProperty.java

@@ -7,8 +7,6 @@
 
 package net.ranides.assira.reflection;
 
-import net.ranides.assira.generic.Wrapper;
-
 /**
  * Model for JavaBean property.
  *
@@ -34,7 +32,7 @@ public interface BeanProperty {
      * @param that object
      * @return wrapper
      */
-    BeanPropertyWrapper<Object> bind(Object that);
+    BeanPropertyBinding<Object> bind(Object that);
 
     /**
      * Creates new wrapper object, which allows you to modify described property in specified object.
@@ -46,7 +44,7 @@ public interface BeanProperty {
      * @param <T> inferred type of property
      * @return wrapper
      */
-    <T> BeanPropertyWrapper<T> $bind(Object that);
+    <T> BeanPropertyBinding<T> $bind(Object that);
 
     /**
      * Changes property inside specified object. Is uses setter method.

+ 1 - 1
assira.core/src/main/java/net/ranides/assira/reflection/BeanPropertyWrapper.java

@@ -2,7 +2,7 @@ package net.ranides.assira.reflection;
 
 import net.ranides.assira.generic.Wrapper;
 
-public interface BeanPropertyWrapper<T> extends Wrapper<T> {
+public interface BeanPropertyBinding<T> extends Wrapper<T> {
 
     /**
      * Property name, derived from getter & setter name, using various JavaBean conventions.

+ 7 - 7
assira.core/src/main/java/net/ranides/assira/reflection/impl/bean/ABeanProperty.java

@@ -8,7 +8,7 @@
 package net.ranides.assira.reflection.impl.bean;
 
 import net.ranides.assira.reflection.BeanProperty;
-import net.ranides.assira.reflection.BeanPropertyWrapper;
+import net.ranides.assira.reflection.BeanPropertyBinding;
 import net.ranides.assira.reflection.IClass;
 
 /**
@@ -39,14 +39,14 @@ public abstract class ABeanProperty implements BeanProperty {
     }
 
     @Override
-    public BeanPropertyWrapper<Object> bind(Object that) {
-        return new RWrapper(that);
+    public BeanPropertyBinding<Object> bind(Object that) {
+        return new RBinding(that);
     }
     
     @SuppressWarnings({"unchecked", "rawtypes"})
     @Override
-    public <T> BeanPropertyWrapper<T> $bind(Object that) {
-        return (BeanPropertyWrapper)new RWrapper(that);
+    public <T> BeanPropertyBinding<T> $bind(Object that) {
+        return (BeanPropertyBinding)new RBinding(that);
     }
 
     @SuppressWarnings("unchecked")
@@ -62,11 +62,11 @@ public abstract class ABeanProperty implements BeanProperty {
         return prv;
     }
 
-    private class RWrapper implements BeanPropertyWrapper<Object> {
+    private class RBinding implements BeanPropertyBinding<Object> {
         
         private final Object that;
 
-        public RWrapper(Object that) {
+        public RBinding(Object that) {
             this.that = that;
         }
 

+ 24 - 3
assira.core/src/main/java/net/ranides/assira/reflection/impl/bean/RBeanMethod.java

@@ -7,6 +7,7 @@
 
 package net.ranides.assira.reflection.impl.bean;
 
+import lombok.RequiredArgsConstructor;
 import net.ranides.assira.functional.special.AnyFunction;
 import net.ranides.assira.reflection.*;
 
@@ -36,11 +37,11 @@ public class RBeanMethod implements BeanMethod, Serializable {
     }
 
     @Override
-    public AnyFunction<Object> bind(Object that) {
+    public BeanMethodBinding bind(Object that) {
         if(null == that) {
             throw new IReflectiveException("non-static method requires non-null 'this'");
         }
-        return method.bind(that);
+        return new RBinding(that);
     }
     
     @Override
@@ -77,5 +78,25 @@ public class RBeanMethod implements BeanMethod, Serializable {
     public boolean accepts(Object... arguments) {
         return method.accepts(arguments);
     }
-    
+
+    @RequiredArgsConstructor
+    private class RBinding implements BeanMethodBinding {
+
+        private final Object that;
+
+        @Override
+        public String name() {
+            return method.name();
+        }
+
+        @Override
+        public boolean matches(IClasses arguments) {
+            return method.matches(arguments);
+        }
+
+        @Override
+        public Object apply(Object[] arguments) {
+            return method.apply(that, arguments);
+        }
+    }
 }

+ 24 - 4
assira.core/src/main/java/net/ranides/assira/reflection/impl/bean/RBeanMethods.java

@@ -7,6 +7,7 @@
 
 package net.ranides.assira.reflection.impl.bean;
 
+import lombok.RequiredArgsConstructor;
 import net.ranides.assira.functional.special.AnyFunction;
 import net.ranides.assira.reflection.*;
 
@@ -35,11 +36,11 @@ public class RBeanMethods implements BeanMethod, Serializable {
     }
 
     @Override
-    public AnyFunction<Object> bind(Object that) {
+    public BeanMethodBinding bind(Object that) {
         if(null == that) {
             throw new IReflectiveException("non-static method requires non-null 'this'");
         }
-        return (arguments) -> invoke(that, arguments);
+        return new RBinding(that);
     }
 
     @Override
@@ -103,6 +104,25 @@ public class RBeanMethods implements BeanMethod, Serializable {
         }
         return false;
     }
-    
-    
+
+    @RequiredArgsConstructor
+    private class RBinding implements BeanMethodBinding {
+
+        private final Object that;
+
+        @Override
+        public String name() {
+            return RBeanMethods.this.name();
+        }
+
+        @Override
+        public boolean matches(IClasses arguments) {
+            return RBeanMethods.this.matches(arguments);
+        }
+
+        @Override
+        public Object apply(Object[] arguments) {
+            return RBeanMethods.this.invoke(that, arguments);
+        }
+    }
 }

+ 1 - 1
assira.core/src/test/java/net/ranides/assira/reflection/impl/bean/RBeanModelTest.java

@@ -153,7 +153,7 @@ public class RBeanModelTest {
         assertThrows(IReflectiveException.class, ()->{
             model.method("setConfig").get().bind(null);
         });
-        assertThrows(ClassCastException.class, ()->{
+        assertThrows(IReflectiveException.CallException.class, ()->{
             model.method("setConfig").get().bind(bean).call("home",17);
         });
         assertThrows(IReflectiveException.class, ()->{