소스 검색

fix: reflective: generic fields (context)
fix: reflective: generic array types (everywhere)

Ranides Atterwim 10 년 전
부모
커밋
fc57ac4a97

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

@@ -18,7 +18,7 @@ import net.ranides.assira.reflection.impl.FField;
 public interface IField extends IElement {
     
     static IField typeinfo(Field field) {
-        return FField.newField(field);
+        return FField.newField(IContext.DEFAULT, field);
     }
     
     @Override

+ 5 - 4
assira/src/main/java/net/ranides/assira/reflection/impl/FField.java

@@ -9,6 +9,7 @@ package net.ranides.assira.reflection.impl;
 
 import java.lang.reflect.Field;
 import net.ranides.assira.collection.maps.Cache;
+import net.ranides.assira.reflection.IContext;
 import net.ranides.assira.reflection.IField;
 
 /**
@@ -24,12 +25,12 @@ public final class FField {
         /* utility class */
     }
 
-    public static IField newField(boolean declared, Field field) {
-        return (declared ? D_CACHE : R_CACHE).get(field, () -> new RField(declared, field));
+    public static IField newField(boolean declared, IContext context, Field field) {
+        return (declared ? D_CACHE : R_CACHE).get(field, () -> new RField(declared, context, field));
     }
     
-    public static IField newField(Field field) {
-        return newField(true, field);
+    public static IField newField(IContext context, Field field) {
+        return newField(true, context, field);
     }
     
 }

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

@@ -209,19 +209,20 @@ public class RClass extends AClass {
     }
     
     private CQuery<IField> getRFields(AHints hints) {
+        IContext c = context();
         if(hints.hintDeclared()) {
             return CQueryBuilder
                 .fromArray(type.getDeclaredFields())
-                .map(f -> FField.newField(true, f));
+                .map(f -> FField.newField(true, c, f));
         } else if(hints.hintAny()) {
             return CQueryBuilder
                 .fromIterable(() -> new RawAncestors(type))
                 .split(Class::getDeclaredFields)
-                .map(f -> FField.newField(isdecl(f), f));
+                .map(f -> FField.newField(isdecl(f), c, f));
         } else {
             return CQueryBuilder
                 .fromArray(type.getFields())
-                .map(f -> FField.newField(isdecl(f), f));
+                .map(f -> FField.newField(isdecl(f), c, f));
         }
     }
     

+ 5 - 3
assira/src/main/java/net/ranides/assira/reflection/impl/RField.java

@@ -25,14 +25,16 @@ import net.ranides.assira.collection.query.CQueryBuilder;
 public class RField implements IField {
     
     private final boolean declared;
+    private final IContext context;
     private final IClass parent;
     private final Field field;
     private final Supplier<MethodHandle> getter;
     private final Supplier<MethodHandle> setter;
     
-    RField(boolean declared, Field field) {
+    RField(boolean declared, IContext context, Field field) {
         ReflectUtils.access(field);
-        this.parent = IClass.typeinfo(field.getDeclaringClass());
+        this.context = context;
+        this.parent = context.typeinfo(field.getDeclaringClass());
         this.field = field;
         this.getter = LazyReference.concurrent(() -> {
             try {
@@ -53,7 +55,7 @@ public class RField implements IField {
     
     @Override
     public IContext context() {
-        return parent.context();
+        return context;
     }
     
     @Override

+ 10 - 9
assira/src/main/java/net/ranides/assira/reflection/impl/RGClass.java

@@ -14,7 +14,6 @@ import java.lang.reflect.Type;
 import java.lang.reflect.TypeVariable;
 import java.util.Set;
 import net.ranides.assira.reflection.*;
-import net.ranides.assira.trace.ExceptionUtils;
 
 /**
  *
@@ -28,16 +27,18 @@ public class RGClass extends AClass {
     RGClass(IContext context, GenericArrayType gtype) {
         super(context);
         
-        try {
-            Type gcomponent = gtype.getGenericComponentType();
-            IClass icomponent = context.typeinfo(new TV(cname(gcomponent.getTypeName())));
-            Class<?> rcomponent = Class.forName("[L" + icomponent.name() + ";");
-            this.rtype = context.typeinfo(rcomponent);
-            this.ctype = context.typeinfo(gcomponent);
-            
+        Type gcomponent = gtype.getGenericComponentType();
+        IClass icomponent = context.typeinfo(new TV(cname(gcomponent.getTypeName())));
+
+        Class<?> rcomponent;
+        try  {
+            rcomponent = Class.forName("[L" + icomponent.name() + ";");
         } catch(ClassNotFoundException cne) {
-            throw ExceptionUtils.rethrow(cne);
+            rcomponent = Object[].class;
         }
+        this.rtype = context.typeinfo(rcomponent);
+        this.ctype = context.typeinfo(gcomponent);
+
     }
     
     private static String cname(String typename) {

+ 0 - 50
assira/src/test/java/net/ranides/assira/reflection/impl/AClassTest.java

@@ -115,56 +115,6 @@ public class AClassTest {
         assertNotEquals(IClasses.typeinfo(float.class).list(), IClass.typeinfo(int.class).collect().list());
     }
     
-    @Test
-    public void testArray() throws NoSuchMethodException {
-        TypeToken<Container<String>> ic = new TypeToken<Container<String>>(){};
-        
-         
-        System.out.printf("get1   = %s%n", ic.method("get1").returns());
-        System.out.printf("get1s  = %s%n", ic.method("get1").returns().component());
-        System.out.printf("get2   = %s%n", ic.method("get2").returns());
-        System.out.printf("get2s  = %s%n", ic.method("get2").returns().component());
-        System.out.printf("get3   = %s%n", ic.method("get3").returns());
-        System.out.printf("get3s  = %s%n", ic.method("get3").returns().component());
-        System.out.printf("get4   = %s%n", ic.method("get4").returns());
-        System.out.printf("get4s  = %s%n", ic.method("get4").returns().component() );
-
-        System.out.printf("%n");
-        
-        System.out.printf("set1   = %s%n", ic.method("set1").arguments().types().first());
-        System.out.printf("set1s  = %s%n", ic.method("set1").arguments().types().first().component());
-        System.out.printf("set2   = %s%n", ic.method("set2").arguments().types().first());
-        System.out.printf("set2s  = %s%n", ic.method("set2").arguments().types().first().component());
-        System.out.printf("set3   = %s%n", ic.method("set3").arguments().types().first());
-        System.out.printf("set3s  = %s%n", ic.method("set3").arguments().types().first().component());
-        System.out.printf("set4   = %s%n", ic.method("set4").arguments().types().first());
-        System.out.printf("set4s  = %s%n", ic.method("set4").arguments().types().first().component());
-
-//        
-    }
-    
-    private static class Container<R> {
-        
-        public Float[] field1;
-        public List<Double>[] field2;
-        public List<Map<? extends Number, String>>[] field3;
-        public R[] field4;
-        
-        public Float[] get1() { return null; }
-        
-        public List<Double>[] get2() { return null; }
-        
-        public List<Map<? extends Number, String>>[] get3() { return null; }
-        
-        public R[] get4() { return null; }
-        
-        public void set1(Float[] array) { }
-        public void set2(List<Double>[] array) { }
-        public void set3(List<Map<? extends Number, String>>[] array) { }
-        public void set4(R[] array) { }
-        
-    }
-    
     private static class A { }
     
     private static class B { 

+ 118 - 0
assira/src/test/java/net/ranides/assira/reflection/impl/RGClassTest.java

@@ -0,0 +1,118 @@
+/*
+ * @author Ranides Atterwim <ranides@gmail.com>
+ * @copyright Ranides Atterwim
+ * @license WTFPL
+ * @url http://ranides.net/projects/assira
+ */
+package net.ranides.assira.reflection.impl;
+
+import java.util.List;
+import java.util.Map;
+import net.ranides.assira.generic.TypeToken;
+import net.ranides.assira.reflection.IClass;
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+/**
+ *
+ * @author Ranides Atterwim <ranides@gmail.com>
+ */
+public class RGClassTest {
+    
+    @Test
+    public void testArray() throws NoSuchMethodException {
+        TypeToken<Container<String>> ic = new TypeToken<Container<String>>(){};
+        
+        IClass   t1g = new TypeToken<Float[]>(){};
+        IClass   t1c = new TypeToken<Float>(){};
+        Class<?> t1r = Float[].class;
+         
+        IClass   t2g = new TypeToken<List<Double>[]>(){};
+        IClass   t2c = new TypeToken<List<Double>>(){};
+        Class<?> t2r = List[].class;
+        
+        IClass   t3g = new TypeToken<List<Map<? extends Number, String>>[]>(){};
+        IClass   t3c = new TypeToken<List<Map<? extends Number, String>>>(){};
+        Class<?> t3r = List[].class;
+        
+        IClass   t4g = new TypeToken<String[]>(){};
+        IClass   t4c = new TypeToken<String>(){};
+        Class<?> t4r = String[].class;
+        
+        IClass   t5g = new TypeToken<Map<String,Short>[]>(){};
+        IClass   t5c = new TypeToken<Map<String,Short>>(){};
+        Class<?> t5r = Map[].class;
+        
+        // method return type
+        assertArrayType(t1g, t1c, t1r, ic.method("get1").returns());
+        assertArrayType(t2g, t2c, t2r, ic.method("get2").returns());
+        assertArrayType(t3g, t3c, t3r, ic.method("get3").returns());
+        assertArrayType(t4g, t4c, t4r, ic.method("get4").returns());
+        assertArrayType(t5g, t5c, t5r, ic.method("get5").returns());
+        
+        // method arguments
+        assertArrayType(t1g, t1c, t1r, ic.method("set1").arguments().types().first());
+        assertArrayType(t2g, t2c, t2r, ic.method("set2").arguments().types().first());
+        assertArrayType(t3g, t3c, t3r, ic.method("set3").arguments().types().first());
+        assertArrayType(t4g, t4c, t4r, ic.method("set4").arguments().types().first());
+        assertArrayType(t5g, t5c, t5r, ic.method("set5").arguments().types().first());
+        
+        // fields
+        assertArrayType(t1g, t1c, t1r, ic.field("var1").type());
+        assertArrayType(t2g, t2c, t2r, ic.field("var2").type());
+        assertArrayType(t3g, t3c, t3r, ic.field("var3").type());
+        assertArrayType(t4g, t4c, t4r, ic.field("var4").type());
+        assertArrayType(t5g, t5c, t5r, ic.field("var5").type());
+    }
+    
+    @Test
+    public void testField() {
+        IClass ic = new TypeToken<Debug<String>>(){};
+        assertEquals(ic.method("get1").returns(), ic.field("list1").type());
+    }
+    
+    private static void assertArrayType(IClass array, IClass component, Class<?> raw, IClass type) {
+        assertEquals(array, type);
+        assertEquals(raw, type.raw());
+        assertEquals(component, type.component());
+    }
+    
+    private static class Debug<R> {
+        
+        public List<R> list1;
+        
+        public List<R> get1() { return null; }
+    }
+    
+    private static class IContainer<R> {
+        
+    }
+    
+    private static class Container<R> extends IContainer<R> {
+        
+        public Float[] var1;
+        public List<Double>[] var2;
+        public List<Map<? extends Number, String>>[] var3;
+        public R[] var4;
+        public Map<R,Short>[] var5;
+        
+        public Float[] get1() { return null; }
+        
+        public List<Double>[] get2() { return null; }
+        
+        public List<Map<? extends Number, String>>[] get3() { return null; }
+        
+        public R[] get4() { return null; }
+        
+        public Map<R,Short>[] get5() { return null; }
+        
+        public void set1(Float[] array) { }
+        public void set2(List<Double>[] array) { }
+        public void set3(List<Map<? extends Number, String>>[] array) { }
+        public void set4(R[] array) { }
+        public void set5(Map<R,Short>[] array) { }
+        
+    }
+    
+    
+}