public static boolean equals__Ljava_lang_Object_2__Z(MJIEnv env, int objRef, int fobjRef) {
   int fidx = env.getIntField(fobjRef, "regIdx");
   if (fidx >= 0 && fidx < nRegistered) {
     FieldInfo fi1 = getFieldInfo(env, objRef);
     FieldInfo fi2 = getFieldInfo(env, fobjRef);
     return ((fi1.getClassInfo() == fi2.getClassInfo())
         && fi1.getName().equals(fi2.getName())
         && fi1.getType().equals(fi2.getType()));
   }
   return false;
 }
Example #2
0
  /**
   * Creates a public setter method. The setter method assigns the value of the first parameter to
   * the specified field in the class to which this method is added. The created method is not
   * static even if the field is static. You may not change it to be static by <code>setModifiers()
   * </code> in <code>CtBehavior</code>.
   *
   * @param methodName the name of the setter
   * @param field the field accessed.
   */
  public static CtMethod setter(String methodName, CtField field) throws CannotCompileException {
    FieldInfo finfo = field.getFieldInfo2();
    String fieldType = finfo.getDescriptor();
    String desc = "(" + fieldType + ")V";
    ConstPool cp = finfo.getConstPool();
    MethodInfo minfo = new MethodInfo(cp, methodName, desc);
    minfo.setAccessFlags(AccessFlag.PUBLIC);

    Bytecode code = new Bytecode(cp, 3, 3);
    try {
      String fieldName = finfo.getName();
      if ((finfo.getAccessFlags() & AccessFlag.STATIC) == 0) {
        code.addAload(0);
        code.addLoad(1, field.getType());
        code.addPutfield(Bytecode.THIS, fieldName, fieldType);
      } else {
        code.addLoad(1, field.getType());
        code.addPutstatic(Bytecode.THIS, fieldName, fieldType);
      }

      code.addReturn(null);
    } catch (NotFoundException e) {
      throw new CannotCompileException(e);
    }

    minfo.setCodeAttribute(code.toCodeAttribute());
    return new CtMethod(minfo, field.getDeclaringClass());
  }
Example #3
0
 protected void checkFieldInfo(FieldInfo fi) {
   if (getClassInfo() != fi.getClassInfo()) {
     throw new JPFException(
         "wrong static FieldInfo : "
             + fi.getName()
             + " , no such field in class "
             + getClassInfo().getName());
   }
 }
  public static int getName____Ljava_lang_String_2(MJIEnv env, int objRef) {
    FieldInfo fi = getFieldInfo(env, objRef);

    int nameRef = env.getReferenceField(objRef, "name");
    if (nameRef == -1) {
      nameRef = env.newString(fi.getName());
      env.setReferenceField(objRef, "name", nameRef);
    }

    return nameRef;
  }
  @MJI
  public int getName____Ljava_lang_String_2(MJIEnv env, int objRef, FeatureExpr ctx) {
    FieldInfo fi = getFieldInfo(ctx, env, objRef);

    int nameRef = env.getReferenceField(ctx, objRef, "name").getValue();
    if (nameRef == MJIEnv.NULL) {
      nameRef = env.newString(ctx, fi.getName());
      env.setReferenceField(ctx, objRef, "name", nameRef);
    }

    return nameRef;
  }
  /**
   * Peer method for the <code>java.lang.reflect.Field.set</code> method.
   *
   * @author Mirko Stojmenovic ([email protected])
   * @author Igor Andjelkovic ([email protected])
   * @author Milos Gligoric ([email protected])
   */
  public static void set__Ljava_lang_Object_2Ljava_lang_Object_2__V(
      MJIEnv env, int objRef, int fobjRef, int val) {
    FieldInfo fi = getFieldInfo(env, objRef);
    int modifiers = fi.getModifiers();

    if (!isAvailable(env, fi, fobjRef)) {
      return;
    }

    if (Modifier.isFinal(modifiers)) {
      env.throwException("java.lang.IllegalAccessException", "field " + fi.getName() + " is final");
      return;
    }
    ClassInfo ci = fi.getClassInfo();
    ClassInfo cio = env.getClassInfo(fobjRef);

    if (!fi.isStatic() && !cio.isInstanceOf(ci)) {
      env.throwException(
          "java.lang.IllegalArgumentException",
          fi.getType() + "field " + fi.getName() + " does not belong to this object");
      return;
    }

    Object[] attrs = env.getArgAttributes();
    Object attr = (attrs == null) ? null : attrs[2];

    if (!setValue(env, fi, fobjRef, val, attr)) {
      env.throwException(
          "java.lang.IllegalArgumentException",
          "Can not set "
              + fi.getType()
              + " field "
              + fi.getFullName()
              + " to "
              + ((MJIEnv.NULL != val) ? env.getClassInfo(val).getName() + " object " : "null"));
    }
  }
 public static int hashCode____I(MJIEnv env, int objRef) {
   FieldInfo fi = getFieldInfo(env, objRef);
   return fi.getClassInfo().getName().hashCode() ^ fi.getName().hashCode();
 }
  private static boolean setValue(MJIEnv env, FieldInfo fi, int obj, int value, Object attr) {
    ClassInfo fieldClassInfo = fi.getClassInfo();

    String className = fieldClassInfo.getName();
    String fieldType = fi.getType();

    try {
      ClassInfo tci = fi.getTypeClassInfo();

      if (tci.isPrimitive()) {
        if (value == MJIEnv.NULL) {
          return false;
        }

        // checks whether unboxing can be done by accessing the field "value"
        final String fieldName = "value";
        FieldInfo finfo = env.getElementInfo(value).getFieldInfo(fieldName);
        if (finfo == null) {
          return false;
        }

        ElementInfo ei =
            fi.isStatic() ? fi.getClassInfo().getStaticElementInfo() : env.getElementInfo(obj);
        ei.setFieldAttr(fi, attr);

        if ("boolean".equals(fieldType)) {
          boolean val = env.getBooleanField(value, fieldName);
          ei.setBooleanField(fi, val);
          return true;
        } else if ("byte".equals(fieldType)) {
          byte val = env.getByteField(value, fieldName);
          ei.setByteField(fi, val);
          return true;
        } else if ("char".equals(fieldType)) {
          char val = env.getCharField(value, fieldName);
          ei.setCharField(fi, val);
          return true;
        } else if ("short".equals(fieldType)) {
          short val = env.getShortField(value, fieldName);
          ei.setShortField(fi, val);
          return true;
        } else if ("int".equals(fieldType)) {
          int val = env.getIntField(value, fieldName);
          ei.setIntField(fi, val);
          return true;
        } else if ("long".equals(fieldType)) {
          long val = env.getLongField(value, fieldName);
          ei.setLongField(fi, val);
          return true;
        } else if ("float".equals(fieldType)) {
          float val = env.getFloatField(value, fieldName);
          ei.setFloatField(fi, val);
          return true;
        } else if ("double".equals(fieldType)) {
          double val = env.getDoubleField(value, fieldName);
          ei.setDoubleField(fi, val);
          return true;
        } else {
          return false;
        }

      } else { // it's a reference
        if (value != MJIEnv.NULL) {
          String type = env.getTypeName(value);
          // this is an instance so the ClassInfo has to be registered
          ClassInfo valueCI = ClassInfo.getResolvedClassInfo(type);
          if (!valueCI.isInstanceOf(tci)) {
            return false;
          }
        }

        ElementInfo ei =
            fi.isStatic() ? fi.getClassInfo().getStaticElementInfo() : env.getElementInfo(obj);
        ei.setFieldAttr(fi, attr);

        if (fi.isStatic()) {
          env.setStaticReferenceField(className, fi.getName(), value);
        } else {
          env.setReferenceField(obj, fi.getName(), value);
        }
        return true;
      }

    } catch (NoClassInfoException cx) {
      env.throwException("java.lang.NoClassDefFoundError", cx.getMessage());
      return false;
    }
  }
 public static boolean isSynthetic____Z(MJIEnv env, int objref) {
   FieldInfo fi = getFieldInfo(env, objref);
   String fn = fi.getName();
   return (fn.startsWith("this$") || fn.startsWith("val$"));
 }
 @MJI
 public int hashCode____I(MJIEnv env, int objRef, FeatureExpr ctx) {
   FieldInfo fi = getFieldInfo(ctx, env, objRef);
   return fi.getClassInfo().getName().hashCode() ^ fi.getName().hashCode();
 }
  private static boolean setValue(
      FeatureExpr ctx, MJIEnv env, FieldInfo fi, int obj, int value, Object attr) {
    ClassInfo fieldClassInfo = fi.getClassInfo();
    String className = fieldClassInfo.getName();
    String fieldType = fi.getType();
    ClassInfo tci = fi.getTypeClassInfo();

    ElementInfo ei = null;
    if (fi.isStatic()) {
      ei = fi.getClassInfo().getModifiableStaticElementInfo();
    } else {
      ei = env.getModifiableElementInfo(obj);
    }

    if (tci.isPrimitive()) {
      if (value == MJIEnv.NULL) {
        return false;
      }

      // checks whether unboxing can be done by accessing the field "value"
      final String fieldName = "value";
      FieldInfo finfo = env.getElementInfo(value).getFieldInfo(fieldName);
      if (finfo == null) {
        return false;
      }

      ei.setFieldAttr(fi, attr);

      if ("boolean".equals(fieldType)) {
        Conditional<Boolean> val = env.getBooleanField(value, fieldName);
        ei.setBooleanField(ctx, fi, val);
        return true;
      } else if ("byte".equals(fieldType)) {
        Conditional<Byte> val = env.getByteField(value, fieldName);
        ei.setByteField(ctx, fi, val);
        return true;
      } else if ("char".equals(fieldType)) {
        Conditional<Character> val = env.getCharField(value, fieldName);
        ei.setCharField(ctx, fi, val);
        return true;
      } else if ("short".equals(fieldType)) {
        Conditional<Short> val = env.getShortField(value, fieldName);
        ei.setShortField(ctx, fi, val);
        return true;
      } else if ("int".equals(fieldType)) {
        Conditional<Integer> val = env.getIntField(value, fieldName);
        ei.setIntField(ctx, fi, val);
        return true;
      } else if ("long".equals(fieldType)) {
        Conditional<Long> val = env.getLongField(value, fieldName);
        ei.setLongField(ctx, fi, val);
        return true;
      } else if ("float".equals(fieldType)) {
        Conditional<Float> val = env.getFloatField(value, fieldName);
        ei.setFloatField(ctx, fi, val);
        return true;
      } else if ("double".equals(fieldType)) {
        Conditional<Double> val = env.getDoubleField(value, fieldName);
        ei.setDoubleField(ctx, fi, val);
        return true;
      } else {
        return false;
      }

    } else { // it's a reference
      if (value != MJIEnv.NULL) {
        ClassInfo ciValue = env.getClassInfo(value);
        if (!ciValue.isInstanceOf(tci)) {
          return false;
        }
      }

      ei.setFieldAttr(fi, attr);

      if (fi.isStatic()) {
        env.setStaticReferenceField(ctx, className, fi.getName(), value);
      } else {
        env.setReferenceField(ctx, obj, fi.getName(), value);
      }
      return true;
    }
  }
  /**
   * Peer method for the <code>java.lang.reflect.Field.set</code> method.
   *
   * @author Mirko Stojmenovic ([email protected])
   * @author Igor Andjelkovic ([email protected])
   * @author Milos Gligoric ([email protected])
   */
  @MJI
  public void set__Ljava_lang_Object_2Ljava_lang_Object_2__V(
      MJIEnv env, int objRef, int fobjRef, int val, FeatureExpr ctx) {
    FieldInfo fi = getFieldInfo(ctx, env, objRef);
    if (!isAvailable(env, fi, fobjRef, ctx)) {
      return;
    }

    if (fi.isFinal() && fi.isStatic()) {
      env.throwException(
          ctx,
          IllegalAccessException.class.getName(),
          "Can not set static final " + fi.getType() + " field " + fi.getFullName());
      return;
    }
    if (!env.getBooleanField(objRef, "isAccessible").getValue() && fi.isFinal()) {
      env.throwException(
          ctx,
          IllegalAccessException.class.getName(),
          "Can not set final " + fi.getType() + " field " + fi.getFullName());
      return;
    }

    ClassInfo ci = fi.getClassInfo();
    ClassInfo cio = env.getClassInfo(fobjRef);
    if (!fi.isStatic() && !cio.isInstanceOf(ci)) {
      env.throwException(
          ctx,
          IllegalAccessException.class.getName(),
          fi.getType() + "field " + fi.getName() + " does not belong to this object");
      return;
    }

    if (!env.getBooleanField(objRef, "isAccessible").getValue()) {
      if (!fi.isStatic() && cio.isInstanceOf(ci)) {
        if (!fi.isPublic()) {
          env.throwException(
              ctx, IllegalAccessException.class.getName(), fi.getType() + " field " + fi.getName());
          return;
        }
      } else {
        if (!fi.isPublic()) {
          env.throwException(
              ctx, IllegalAccessException.class.getName(), fi.getType() + " field " + fi.getName());
          return;
        }
      }
    }

    Object[] attrs = env.getArgAttributes();
    Object attr = (attrs == null) ? null : attrs[2];

    if (!setValue(ctx, env, fi, fobjRef, val, attr)) {
      env.throwException(
          ctx,
          IllegalAccessException.class.getName(),
          "Can not set "
              + fi.getType()
              + " field "
              + fi.getFullName()
              + " to "
              + ((MJIEnv.NULL != val) ? env.getClassInfo(val).getName() + " object " : "null"));
    }
  }
  /** Prints the contents of a class file. */
  public static void print(ClassFile cf, PrintWriter out) {
    List<?> list;
    int n;

    /*
     * 0x0020 (SYNCHRONIZED) means ACC_SUPER if the modifiers are of a
     * class.
     */
    int mod = AccessFlag.toModifier(cf.getAccessFlags() & ~AccessFlag.SYNCHRONIZED);
    out.println(
        "major: "
            + cf.major
            + ", minor: "
            + cf.minor
            + " modifiers: "
            + Integer.toHexString(cf.getAccessFlags()));
    out.println(
        Modifier.toString(mod) + " class " + cf.getName() + " extends " + cf.getSuperclass());

    String[] infs = cf.getInterfaces();
    if (infs != null && infs.length > 0) {
      out.print("    implements ");
      out.print(infs[0]);
      for (int i = 1; i < infs.length; ++i) {
        out.print(", " + infs[i]);
      }

      out.println();
    }

    out.println();
    list = cf.getFields();
    n = list.size();
    for (int i = 0; i < n; ++i) {
      FieldInfo finfo = (FieldInfo) list.get(i);
      int acc = finfo.getAccessFlags();
      out.println(
          Modifier.toString(AccessFlag.toModifier(acc))
              + " "
              + finfo.getName()
              + "\t"
              + finfo.getDescriptor());
      printAttributes(finfo.getAttributes(), out, 'f');
    }

    out.println();
    list = cf.getMethods();
    n = list.size();
    for (int i = 0; i < n; ++i) {
      MethodInfo minfo = (MethodInfo) list.get(i);
      int acc = minfo.getAccessFlags();
      out.println(
          Modifier.toString(AccessFlag.toModifier(acc))
              + " "
              + minfo.getName()
              + "\t"
              + minfo.getDescriptor());
      printAttributes(minfo.getAttributes(), out, 'm');
      out.println();
    }

    out.println();
    printAttributes(cf.getAttributes(), out, 'c');
  }