Fixing getGenericReturnType to recognize the GenericArrayTypeImpl and ParameterizedTy...
[jpf-core.git] / examples / Reflection.java
index 8bead2e9b4fa2a4228ad79066bf7eb2fb08ffd06..b32715832912b6b30b60dc1e25510a89f5c6cb26 100644 (file)
@@ -1,6 +1,7 @@
 import java.lang.reflect.Method;
 import java.lang.reflect.Type;
 import java.lang.reflect.TypeVariable;
+import java.io.Serializable;
 
 import java.util.List;
 import java.util.Map;
@@ -9,23 +10,30 @@ import java.util.Arrays;
 
 public class Reflection {
 
-       class GenericShort<TUVW,ABCD> {
-       }
+    interface GenericSuperShort<XYZ> {
 
-       class Generic<TUVW,ABCD,KLM,NOP> {
-       
-       }
+    }
 
-       class SampleClass {
-          private String sampleField;
+    class GenericShort<TUVW,ABCD> {
+    }
 
-          public Generic<Integer,String,Double,Short> setSampleField(List<String> listString, Map<Integer,String> mapString,
-                               Generic<Integer,String,Double,Short> test,
-                               String sampleField, int one, short two, double three, Object obj) {
-                 this.sampleField = sampleField; 
-                 return test;
-          }
-          
+    class Generic<TUVW,ABCD,KLM,NOP,XYZ> extends GenericShort<TUVW,ABCD> implements GenericSuperShort<XYZ>, Serializable {
+
+    }
+
+    class SampleClass<VWXZ> {
+        private String sampleField;
+
+        public Class<?> setSampleField(Class<?> clazz,
+                       Class<? extends List> list, Class<? super Map> map,
+                       List<String> listString, Map<Integer,String> mapString, 
+                       Generic<Integer,String,Double,Short,Float> test, 
+                       String sampleField, int one, short two, double three, Object obj) {
+            
+                       this.sampleField = sampleField;
+            return clazz;
+        }
+                
           
           /*public String getSampleField() {
                  return sampleField;
@@ -42,8 +50,39 @@ public class Reflection {
 
    public static void main(String[] args) {
 
-      Method[] methods = SampleClass.class.getMethods();
-      Type[] parameters = methods[0].getGenericParameterTypes();
+      /*Method[] methods = SampleClass.class.getMethods();
+      //  Method[] methods = Class.class.getMethods();
+        Method method = null;
+        for(Method meth : methods) {
+            if (meth.getName().equals("setSampleField")) {
+                method = meth;
+            }
+        }
+        Type[] parameters = method.getGenericParameterTypes();
+      //Type[] parameters = methods[0].getGenericParameterTypes();
+      for (int i = 0; i < parameters.length; i++) {
+         System.out.println(parameters[i]);
+      }
+      System.out.println();*/
+      /*Type superCls = Generic.class.getGenericSuperclass();
+      //Type superCls = String.class.getGenericSuperclass();
+      System.out.println(superCls);
+        System.out.println();
+        Type[] interfaces = Generic.class.getGenericInterfaces();
+        for (int i = 0; i < interfaces.length; i++) {
+            System.out.println(interfaces[i]);
+        }*/
+      
+         Method[] methods = Class.class.getMethods();
+         Method method = null;
+      for(Method mth : methods) {
+        //if (mth.getName().equals("getConstructor")) {
+        //if (mth.getName().equals("isAssignableFrom")) {
+        if (mth.getName().equals("getTypeParameters")) {
+           method = mth;
+        }
+      }
+      Type[] parameters = method.getGenericParameterTypes();
       //Type[] parameters = methods[0].getGenericParameterTypes();
       for (int i = 0; i < parameters.length; i++) {
          System.out.println(parameters[i]);
@@ -55,15 +94,20 @@ public class Reflection {
  
       }
       System.out.println();*/
-      //TypeVariable[] typeParameters = Generic.class.getTypeParameters();
-      TypeVariable[] typeParameters = SampleClass.class.getTypeParameters();
+      /*TypeVariable[] typeParameters = Generic.class.getTypeParameters();
+      //TypeVariable[] typeParameters = SampleClass.class.getTypeParameters();
       for(TypeVariable typeVar: typeParameters){
          System.out.println(typeVar);   
  
       }
       System.out.println();
-      
-      Type returnType = methods[0].getGenericReturnType();
+
+      Type[] bounds = typeParameters[0].getBounds();
+      for (Type bound : bounds) {
+          System.out.println(bound);
+      }
+      System.out.println();*/
+      Type returnType = method.getGenericReturnType();
       System.out.println(returnType);
          
    }