Parcourir la source

reflective: type params

Ranides Atterwim il y a 10 ans
Parent
commit
72a9acbaa6

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

@@ -62,7 +62,7 @@ import net.ranides.assira.reflection.impl.RCompareUtils;
  * 
  * @author Ranides Atterwim <ranides@gmail.com>
  */
-public abstract class TypeToken<T> extends AClass implements IClass {
+public abstract class TypeToken<T> extends AClass {
     
     private final IClass type;
 

+ 10 - 22
assira/src/main/java/net/ranides/assira/reflection/impl/RClass.java

@@ -64,12 +64,15 @@ public class RClass extends AClass {
     
     @Override
     public List<IClass> params() {
-        Type gtype = type.getGenericSuperclass();
-        if(gtype instanceof ParameterizedType) {
-            return new RPClass(type, (ParameterizedType)gtype).params();
-        } else {
-            return ListUtils.map(Arrays.asList(type.getTypeParameters()), IClass::typeinfo);
-        }
+//        try {
+            Type gtype = type.getGenericSuperclass();
+            if(gtype instanceof ParameterizedType) {
+                return new RPClass(type, (ParameterizedType)gtype).params();
+            } 
+//        } catch(MalformedParameterizedTypeException | GenericSignatureFormatError | TypeNotPresentException e) {
+//            // do nothing... ?
+//        }
+        return ListUtils.map(Arrays.asList(type.getTypeParameters()), IClass::typeinfo);
     }
 
     @Override
@@ -108,7 +111,7 @@ public class RClass extends AClass {
     }
 
     @Override
-    public final IClass outer() {
+    public IClass outer() {
         return IClass.typeinfo(type.getDeclaringClass());
     }
 
@@ -266,21 +269,6 @@ public class RClass extends AClass {
         }
     }
     
-    private static class RPClass extends RClass {
-
-        private final ParameterizedType ptype;
-
-        public RPClass(Class<?> type, ParameterizedType ptype) {
-            super(type);
-            this.ptype = ptype;
-        }
-
-        @Override
-        public List<IClass> params() {
-            return ListUtils.map(Arrays.asList(ptype.getActualTypeArguments()), IClass::typeinfo);
-        }
-
-    }
     
     private static class RawParents extends ForwardIterator<Class<?>> {
 

+ 55 - 0
assira/src/main/java/net/ranides/assira/reflection/impl/RPClass.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.reflection.impl;
+
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+import java.lang.reflect.TypeVariable;
+import java.util.Arrays;
+import java.util.List;
+import net.ranides.assira.collection.lists.ListUtils;
+import net.ranides.assira.reflection.IClass;
+
+/**
+ *
+ * @author Ranides Atterwim <ranides@gmail.com>
+ */
+class RPClass extends RClass {
+
+    private final ParameterizedType ptype;
+    private final TypeVariable<?>[] keys;
+    private final Type[] vars;
+
+    public RPClass(Class<?> type, ParameterizedType ptype) {
+        super(type);
+        this.ptype = ptype;
+        this.keys = ((Class<?>)ptype.getRawType()).getTypeParameters();
+        this.vars = ptype.getActualTypeArguments();
+
+        System.out.printf("%n");
+        System.out.printf("RP.keys %s = %s%n", type, Arrays.toString(keys) );
+        System.out.printf("RP.vars %s = %s%n", type, Arrays.toString(vars));
+        
+        if(null != ptype.getOwnerType()) {
+            System.out.printf("RP.outs %s = %s%n", type, ptype.getOwnerType());
+            IClass.typeinfo(ptype.getOwnerType());
+        }
+        
+    }
+
+    @Override
+    public List<IClass> params() {
+        return ListUtils.map(Arrays.asList(ptype.getActualTypeArguments()), IClass::typeinfo);
+    }
+    
+    @Override
+    public IClass outer() {
+        return IClass.typeinfo(ptype.getOwnerType());
+    }
+
+}

+ 10 - 4
assira/src/test/java/net/ranides/assira/reflection/impl/RMethodTest.java

@@ -19,7 +19,9 @@ public class RMethodTest {
 
     @Test
     public void testMethodReturs() {
-        Object o = new ForRMethod<Float>(){};
+//        ForRMethod<? super Float> p = new ForRMethod<Float>(){};
+        ForRMethod<Float> p = new ForRMethod<Float>(){};
+        Object o = p.new Record<Double>(){};
         IClass ic = IClass.typefor(o);
         
         System.out.printf("---------%n");
@@ -32,9 +34,13 @@ public class RMethodTest {
         // tak naprawdę *wiadomo* że lepsza by była opcja (1) bo opcja (2) to nas zbliża z powrotem
         // do rzeźbienia w stylu klasycznego Reflection API.
         
-        System.out.printf("param(T) = %s%n", ic.params().get(0));
-        System.out.printf("returns  = %s%n", ic.methods().require("m1").returns().first());
-        System.out.printf("returns  = %s%n", ic.methods().require("m1").returns().first().params().get(0));
+        System.out.printf("param(T)       = %s%n", ic.params().get(0));
+        
+        System.out.printf("%n");
+        
+        System.out.printf("m1.returns     = %s%n", ic.methods().require("map1").first().returns());
+        
+        System.out.printf("%n");
     }
     
 }

+ 15 - 1
assira/src/test/java/net/ranides/assira/reflection/mockup/ForRMethod.java

@@ -8,6 +8,7 @@
 package net.ranides.assira.reflection.mockup;
 
 import java.util.List;
+import java.util.Map;
 
 /**
  *
@@ -23,6 +24,19 @@ public class ForRMethod<T> {
     
     public List<? extends T> m3() { throw new UnsupportedOperationException(); }
     
-    
+    public class Record<Q> {
+        
+        public <R> Map<T,Map<Q,Map<R, Integer>>> map1(R a,T b,Q c,Integer d) { throw new UnsupportedOperationException(); }
+        
+        public <R> Map<T,Map<Q,Map<R, Integer>>> map2(
+            List<R> a,
+            List<T> b,
+            List<Q> c,
+            List<Integer> d
+        ) { 
+            throw new UnsupportedOperationException(); 
+        }
+        
+    }
     
 }