Adding WildcardTypeImpl class.
authorrtrimana <rtrimana@uci.edu>
Fri, 21 Jun 2019 19:29:47 +0000 (12:29 -0700)
committerrtrimana <rtrimana@uci.edu>
Fri, 21 Jun 2019 19:29:47 +0000 (12:29 -0700)
examples/Reflection.java
src/classes/sun/reflect/generics/reflectiveObjects/WildcardTypeImpl.java [new file with mode: 0644]
src/peers/gov/nasa/jpf/vm/JPF_java_lang_reflect_Method.java
src/peers/gov/nasa/jpf/vm/JPF_sun_reflect_generics_reflectiveObjects_WildcardTypeImpl.java [new file with mode: 0644]

index 21fdd57..bebd776 100644 (file)
@@ -61,14 +61,28 @@ public class Reflection {
          System.out.println(parameters[i]);
       }
       System.out.println();*/
-      Type superCls = Generic.class.getGenericSuperclass();
+      /*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("isAnnotationPresent")) {
+                method = mth;
+            }
         }
+      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();
       /*Class[] parameterTypes = methods[0].getParameterTypes();
       for(Class parameterType: parameterTypes){
          System.out.println(parameterType.getName());   
diff --git a/src/classes/sun/reflect/generics/reflectiveObjects/WildcardTypeImpl.java b/src/classes/sun/reflect/generics/reflectiveObjects/WildcardTypeImpl.java
new file mode 100644 (file)
index 0000000..1feb20d
--- /dev/null
@@ -0,0 +1,111 @@
+package sun.reflect.generics.reflectiveObjects;
+
+
+import java.lang.reflect.Type;
+import java.lang.reflect.WildcardType;
+import sun.reflect.generics.factory.GenericsFactory;
+import sun.reflect.generics.tree.FieldTypeSignature;
+import sun.reflect.generics.visitor.Reifier;
+import java.util.Arrays;
+
+
+/**
+ * MJI model class for sun.reflect.generics.reflectiveObjects.WildcardTypeImpl
+ *
+ * This is a JPF specific version of a system class because we can't use the real,
+ * platform VM specific version (it's native all over the place, its field
+ * structure isn't documented, most of its methods are private, hence we can't
+ * even instantiate it properly).
+ *
+ * Note that this class never gets seen by the real VM - it's for JPF's eyes only.
+ *
+ */
+public class WildcardTypeImpl extends LazyReflectiveObjectGenerator
+        implements WildcardType {
+
+    private Type[] upperBounds;
+    private Type[] lowerBounds;
+    private FieldTypeSignature[] upperBoundASTs;
+    private FieldTypeSignature[] lowerBoundASTs;
+
+    // constructor is private to enforce access through static factory
+    private WildcardTypeImpl(FieldTypeSignature[] ubs,
+                             FieldTypeSignature[] lbs,
+                             GenericsFactory f) {
+        super(f);
+        upperBoundASTs = ubs;
+        lowerBoundASTs = lbs;
+    }
+
+    /**
+     * Static factory.
+     */
+    public static WildcardTypeImpl make(FieldTypeSignature[] ubs,
+                                        FieldTypeSignature[] lbs,
+                                        GenericsFactory f) {
+        return new WildcardTypeImpl(ubs, lbs, f);
+    }
+
+    // Accessors
+    private FieldTypeSignature[] getUpperBoundASTs() {
+        throw new UnsupportedOperationException();
+    }
+
+    private FieldTypeSignature[] getLowerBoundASTs() {
+        throw new UnsupportedOperationException();
+    }
+
+    public native Type[] getUpperBounds();
+
+    public native Type[] getLowerBounds();
+
+    public String toString() {
+        Type[] lowerBounds = getLowerBounds();
+        Type[] bounds = lowerBounds;
+        StringBuilder sb = new StringBuilder();
+
+        if (lowerBounds.length > 0)
+            sb.append("? super ");
+        else {
+            Type[] upperBounds = getUpperBounds();
+            if (upperBounds.length > 0 && !upperBounds[0].equals(Object.class) ) {
+                bounds = upperBounds;
+                sb.append("? extends ");
+            } else
+                return "?";
+        }
+
+        assert bounds.length > 0;
+
+        boolean first = true;
+        for(Type bound: bounds) {
+            if (!first)
+                sb.append(" & ");
+
+            first = false;
+            sb.append(bound.getTypeName());
+        }
+        return sb.toString();
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (o instanceof WildcardType) {
+            WildcardType that = (WildcardType) o;
+            return
+                    Arrays.equals(this.getLowerBounds(),
+                            that.getLowerBounds()) &&
+                            Arrays.equals(this.getUpperBounds(),
+                                    that.getUpperBounds());
+        } else
+            return false;
+    }
+
+    @Override
+    public int hashCode() {
+        Type [] lowerBounds = getLowerBounds();
+        Type [] upperBounds = getUpperBounds();
+
+        return Arrays.hashCode(lowerBounds) ^ Arrays.hashCode(upperBounds);
+    }
+}
\ No newline at end of file
index fa6ceee..bf25945 100644 (file)
@@ -188,6 +188,7 @@ public class JPF_java_lang_reflect_Method extends NativePeer {
 
   static int getGenericParameterTypes( MJIEnv env, int objRef, MethodInfo mi) {
     ThreadInfo ti = env.getThreadInfo();
+    String name = mi.getName();
     String[] argTypeNames = mi.getArgumentGenericTypeNames();
     int[] ar = new int[argTypeNames.length];
 
diff --git a/src/peers/gov/nasa/jpf/vm/JPF_sun_reflect_generics_reflectiveObjects_WildcardTypeImpl.java b/src/peers/gov/nasa/jpf/vm/JPF_sun_reflect_generics_reflectiveObjects_WildcardTypeImpl.java
new file mode 100644 (file)
index 0000000..89bab39
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2014, United States Government, as represented by the
+ * Administrator of the National Aeronautics and Space Administration.
+ * All rights reserved.
+ *
+ * The Java Pathfinder core (jpf-core) platform is licensed under the
+ * Apache License, Version 2.0 (the "License"); you may not use this file except
+ * in compliance with the License. You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0.
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package gov.nasa.jpf.vm;
+
+import gov.nasa.jpf.annotation.MJI;
+
+public class JPF_sun_reflect_generics_reflectiveObjects_WildcardTypeImpl {
+
+    // TODO: Need to fix the following 2 methods!!!
+
+    @MJI
+    public int getUpperBounds_____3Ljava_lang_reflect_Type_2 (MJIEnv env, int objRef){
+        ThreadInfo ti = env.getThreadInfo();
+        ClassInfo ci = ClassLoaderInfo.getCurrentResolvedClassInfo("java.lang.Object");
+        if (!ci.isRegistered()) {
+            ci.registerClass(ti);
+        }
+        int refObj = ci.getClassObjectRef();
+
+        int aRef = env.newObjectArray("Ljava/lang/reflect/Type;", 1);
+        // Set references for every array element
+        env.setReferenceArrayElement(aRef, 0, refObj);
+
+        return aRef;
+    }
+
+    @MJI
+    public int getLowerBounds_____3Ljava_lang_reflect_Type_2 (MJIEnv env, int objRef){
+
+        int aRef = env.newObjectArray("Ljava/lang/reflect/Type;", 1);
+        // Set references for every array element
+        env.setReferenceArrayElement(aRef, 0, MJIEnv.NULL);
+
+        return aRef;
+    }
+}