Преглед изворни кода

tests

fix: Wrapper#type
fix: reflective: MethodHandle.invoke
Ranides Atterwim пре 10 година
родитељ
комит
a0b457c77f

+ 0 - 3
assira/pom.xml

@@ -61,9 +61,6 @@
                                 <include>net/ranides/assira/reflection/IHints.class</include>
                                 <include>net/ranides/assira/reflection/IMethod.class</include>
                                 <include>net/ranides/assira/reflection/IMethods.class</include>
-                                <include>net/ranides/assira/generic/SerializationUtils*</include>
-                                <include>net/ranides/assira/generic/Wrapper*</include>
-                                <include>net/ranides/assira/generic/EnumUtils*</include>
                                 
                                 <include>net/ranides/assira/io/IOStreams*</include>
                                 <include>net/ranides/assira/io/IOEvents*</include>

+ 1 - 1
assira/src/main/java/net/ranides/assira/generic/Wrapper.java

@@ -34,7 +34,7 @@ public interface Wrapper<T> {
 
             @Override
             public IClass type() {
-                return new TypeToken<Q>(){};
+                return IClass.typefor(value);
             }
 
         }

+ 5 - 0
assira/src/main/java/net/ranides/assira/reflection/IClass.java

@@ -8,6 +8,7 @@ package net.ranides.assira.reflection;
 
 import java.lang.reflect.Type;
 import java.util.List;
+import net.ranides.assira.reflection.impl.AClass;
 
 /**
  * 
@@ -15,6 +16,10 @@ import java.util.List;
  */
 public interface IClass extends IElement, Comparable<IClass> {
     
+    IClass NULL = new AClass(IContext.DEFAULT){};
+    
+    IClass OBJECT = typeinfo(Object.class);
+    
     /**
      * Collect information using java.reflection API
      * @param type

+ 0 - 2
assira/src/main/java/net/ranides/assira/reflection/impl/AClass.java

@@ -21,8 +21,6 @@ import net.ranides.assira.collection.query.CQueryBuilder;
  */
 public class AClass implements IClass {
     
-    static final IClass NULL = new AClass(IContext.DEFAULT);
-    
     private static final Set<IAttribute> ATTRS = IAttribute.constant(IAttribute.FINAL, IAttribute.NATIVE, IAttribute.PUBLIC);
     
     private static final Object[] EMPTY_ARGS = new Object[0];

+ 1 - 1
assira/src/main/java/net/ranides/assira/reflection/impl/AMethod.java

@@ -51,7 +51,7 @@ public abstract class AMethod implements IMethod {
         MethodHandle mh = handle();
         return (args) -> {
             try {
-                return mh.invoke(args);
+                return mh.invokeWithArguments(args);
             } catch (Throwable cause) { //NOPMD rethrow idiom
                 throw ExceptionUtils.rethrow(cause);
             }

+ 0 - 4
assira/src/main/java/net/ranides/assira/reflection/impl/RClass.java

@@ -14,11 +14,9 @@ import java.util.List;
 import java.util.Set;
 import java.util.Spliterator;
 import java.util.function.Consumer;
-import java.util.function.Function;
 import java.util.function.Supplier;
 import net.ranides.assira.collection.iterators.ForwardIterator;
 import net.ranides.assira.collection.iterators.ForwardSpliterator;
-import net.ranides.assira.collection.iterators.IteratorUtils;
 import net.ranides.assira.collection.lists.ListUtils;
 import net.ranides.assira.collection.query.CQuery;
 import net.ranides.assira.collection.query.CQueryBuilder;
@@ -31,8 +29,6 @@ import net.ranides.assira.reflection.*;
  */
 public class RClass extends AClass {
     
-    static final RClass OBJECT = new RClass(Object.class);
-    
     private final Class<?> type;
     
     private final Supplier<IContext> icontext = LazyReference.concurrent(this::icontext);

+ 1 - 1
assira/src/main/java/net/ranides/assira/reflection/impl/RConstructor.java

@@ -96,7 +96,7 @@ public final class RConstructor extends AMethod {
     @Override
     public Object call(Object... arguments) {
         try {
-            return handle.get().invoke(arguments);
+            return handle.get().invokeWithArguments(arguments);
         } catch (Throwable cause) { // NOPMD rethrow idiom
             throw ExceptionUtils.rethrow(cause);
         }

+ 1 - 1
assira/src/main/java/net/ranides/assira/reflection/impl/RMethod.java

@@ -114,7 +114,7 @@ public final class RMethod extends AMethod implements IMethod {
     @Override
     public Object call(Object... arguments) {
         try {
-            return handle.get().invoke(arguments);
+            return handle.get().invokeWithArguments(arguments);
         } catch (Throwable cause) {
             throw ExceptionUtils.rethrow(cause);
         }

+ 55 - 0
assira/src/test/java/net/ranides/assira/generic/EnumUtilsTest.java

@@ -0,0 +1,55 @@
+/*
+ * @author Ranides Atterwim <ranides@gmail.com>
+ * @copyright Ranides Atterwim
+ * @license WTFPL
+ * @url http://ranides.net/projects/assira
+ */
+package net.ranides.assira.generic;
+
+import java.util.Map;
+import net.ranides.assira.collection.maps.IntMap;
+import static org.junit.Assert.*;
+import org.junit.Test;
+
+/**
+ *
+ * @author Ranides Atterwim <ranides@gmail.com>
+ */
+public class EnumUtilsTest {
+    
+    public EnumUtilsTest() {
+    }
+
+    @Test
+    public void testIntMap() {
+        IntMap<MyEnum> map = EnumUtils.intmap(MyEnum.class, e -> e.v);
+        assertEquals(4, map.size());
+        assertTrue(map.containsKey(4));
+        assertEquals(MyEnum.CONST, map.get(8));
+    }
+    
+    @Test
+    public void testMap() {
+        Map<String, MyEnum> map = EnumUtils.map(MyEnum.class, e -> e.name().toLowerCase());
+        assertEquals(4, map.size());
+        assertTrue(map.containsKey("entry"));
+        assertFalse(map.containsKey("ENTRY"));
+        assertEquals(MyEnum.CONST, map.get("const"));
+    }
+    
+    
+    private enum MyEnum {
+        VALUE(1),
+        ITEM(4),
+        ENTRY(2),
+        CONST(8);
+        
+        private final int v;
+
+        private MyEnum(int v) {
+            this.v = v;
+        }
+
+    }
+    
+}

+ 72 - 7
assira/src/test/java/net/ranides/assira/generic/SerializationUtilsTest.java

@@ -7,6 +7,7 @@
 package net.ranides.assira.generic;
 
 import java.io.IOException;
+import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
@@ -22,7 +23,7 @@ public class SerializationUtilsTest {
 	
 	@Test
 	public void testCopy() throws IOException {
-		List<Integer> a = new ArrayList<Integer>(Arrays.asList(1,2,3,4));
+		List<Integer> a = new ArrayList<>(Arrays.asList(1,2,3,4));
 		List<Integer> b = SerializationUtils.copy(a);
 		
 		assertEquals(a,b);
@@ -31,12 +32,12 @@ public class SerializationUtilsTest {
 	
 	@Test
 	public void testSizeOf() throws IOException {
-		List<Integer> a1 = new ArrayList<Integer>(Arrays.asList(1,2,3,4,5,6,7,8));
-		List<Integer> a2 = new ArrayList<Integer>(Arrays.asList(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16));
-		List<Float> b1 = new ArrayList<Float>(Arrays.asList(1.f, 2.f, 3.f, 4.f));
-		List<Float> b2 = new ArrayList<Float>(Arrays.asList(1.f, 2.f, 3.f, 4.f, 5.f, 6.f, 7.f, 8.f));
-		List<String> c1 = new ArrayList<String>(Arrays.asList("T1", "T2", "T3", "T4", "T5", "T6", "T7", "T8"));
-		List<String> c2 = new ArrayList<String>(Arrays.asList(
+		List<Integer> a1 = new ArrayList<>(Arrays.asList(1,2,3,4,5,6,7,8));
+		List<Integer> a2 = new ArrayList<>(Arrays.asList(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16));
+		List<Float> b1 = new ArrayList<>(Arrays.asList(1.f, 2.f, 3.f, 4.f));
+		List<Float> b2 = new ArrayList<>(Arrays.asList(1.f, 2.f, 3.f, 4.f, 5.f, 6.f, 7.f, 8.f));
+		List<String> c1 = new ArrayList<>(Arrays.asList("T1", "T2", "T3", "T4", "T5", "T6", "T7", "T8"));
+		List<String> c2 = new ArrayList<>(Arrays.asList(
 			"T1....................................1",
 			"T2....................................2",
 			"T3....................................3",
@@ -71,5 +72,69 @@ public class SerializationUtilsTest {
 		assertTrue( ic1.size() < ic2.size());
 		assertTrue( ic1.element()< ic2.element());
 	}
+    
+    @Test
+    public void testProxy() throws IOException {
+        MyValue0 a = SerializationUtils.copy(new MyValue0("x", "22"));
+        MyValue0 b = SerializationUtils.copy(new MyValue1("x", "22"));
+        MyValue0 c = SerializationUtils.copy(new MyValue2("x", "22"));
+        MyValue0 d = SerializationUtils.copy(new MyValue3("x", "22"));
+        
+        assertEquals("null : null", a.toString());
+        assertEquals("x : 22", b.toString());
+        assertEquals("x! : 22??", c.toString());
+        assertEquals("null : null", d.toString());
+        
+        
+    }
+    
+    @SuppressWarnings("PMD")
+    private static class MyValue0 implements Serializable {
+        
+        transient final String a;
+        transient final String b;
+
+        public MyValue0(String a, String b) {
+            this.a = a;
+            this.b = b;
+        }
+
+        @Override
+        public String toString() {
+            return a + " : " + b;
+        }
+        
+    }
+    
+    private static class MyValue1 extends MyValue0 {
+        
+        public MyValue1(String a, String b) {
+            super(a, b);
+        }
+        
+        private Object writeReplace() {
+            return SerializationUtils.proxy(this, a, b);
+        }
+        
+    }
+    
+    private static class MyValue2 extends MyValue1 {
+        
+        public MyValue2(String a, String b) {
+            super(a, b+"?");
+        }
+
+        private Object writeReplace() {
+            return SerializationUtils.proxy(this, a+"!", b);
+        }
+    }
+    
+    private static class MyValue3 extends MyValue1 {
+        
+        public MyValue3(String a, String b) {
+            super(a, b+"?");
+        }
+
+    }
 	
 }

+ 14 - 0
assira/src/test/java/net/ranides/assira/generic/WrapperTest.java

@@ -6,6 +6,8 @@
  */
 package net.ranides.assira.generic;
 
+import net.ranides.assira.reflection.IAttribute;
+import net.ranides.assira.reflection.IClass;
 import org.junit.Test;
 import static org.junit.Assert.*;
 
@@ -23,4 +25,16 @@ public class WrapperTest {
         assertEquals("world", wrapper.get());
     }
     
+    @Test
+    public void testType() {
+        Wrapper<Number> w1 = Wrapper.wrap(14.1);
+        Wrapper<Number> w2 = Wrapper.wrap((Number)14.1);
+        Wrapper<String> w3 = Wrapper.wrap("Hello");
+        Wrapper<String> w4 = Wrapper.wrap(null);
+        
+        assertEquals(IClass.typeinfo(Double.class), w1.type());
+        assertEquals(IClass.typeinfo(Double.class), w2.type());
+        assertEquals(IClass.typeinfo(String.class), w3.type());
+        assertEquals(IClass.NULL, w4.type());
+    }
 }