static ElementInfo getCheckedElementInfo(
      MJIEnv env, FieldInfo fi, int fobjRef, Class<?> fiType, String type) {
    ElementInfo ei;

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

    if (fi.isStatic()) {
      ei = fi.getClassInfo().getStaticElementInfo();
    } else { // instance field
      ei = env.getElementInfo(fobjRef);
    }

    // our guards (still need IllegalAccessException)
    if (ei == null) {
      env.throwException("java.lang.NullPointerException");
      return null;
    }
    if (fiType != null && !fiType.isInstance(fi)) {
      env.throwException(
          "java.lang.IllegalArgumentException", "field type incompatible with " + type);
      return null;
    }

    return ei;
  }
  public static void bytesToDoubles___3BI_3DII__(
      MJIEnv env, int clsRef, int baRef, int bOff, int daRef, int dOff, int nDoubles) {
    int imax = dOff + nDoubles;
    int j = bOff;

    for (int i = dOff; i < imax; i++) {
      byte b0 = env.getByteArrayElement(baRef, j++);
      byte b1 = env.getByteArrayElement(baRef, j++);
      byte b2 = env.getByteArrayElement(baRef, j++);
      byte b3 = env.getByteArrayElement(baRef, j++);
      byte b4 = env.getByteArrayElement(baRef, j++);
      byte b5 = env.getByteArrayElement(baRef, j++);
      byte b6 = env.getByteArrayElement(baRef, j++);
      byte b7 = env.getByteArrayElement(baRef, j++);

      long l = 0x00000000000000ff & b7;
      l <<= 8;
      l |= 0x00000000000000ff & b6;
      l <<= 8;
      l |= 0x00000000000000ff & b5;
      l <<= 8;
      l |= 0x00000000000000ff & b4;
      l <<= 8;
      l |= 0x00000000000000ff & b3;
      l <<= 8;
      l |= 0x00000000000000ff & b2;
      l <<= 8;
      l |= 0x00000000000000ff & b1;
      l <<= 8;
      l |= 0x00000000000000ff & b0;

      double d = Double.longBitsToDouble(l);
      env.setDoubleArrayElement(daRef, i, d);
    }
  }
  @MJI
  public int delete__II__Ljava_lang_StringBuilder_2(
      final MJIEnv env,
      final int objref,
      final Integer beginIndex,
      final Integer endIndex,
      FeatureExpr ctx) {
    final Integer aref = env.getReferenceField(ctx, objref, "value").getValue();
    Conditional<Integer> count = env.getIntField(objref, "count");
    final int diff = endIndex - beginIndex;
    count.mapf(
        ctx,
        new VoidBiFunction<FeatureExpr, Integer>() {

          @Override
          public void apply(FeatureExpr ctx, Integer count) {
            for (int i = beginIndex, j = endIndex; i < count; i++, j++) {
              if (j < count) {
                env.setCharArrayElement(ctx, aref, i, env.getCharArrayElement(aref, j));
              } else {
                env.setCharArrayElement(ctx, aref, i, new One<>(' '));
              }
            }

            env.setIntField(ctx, objref, "count", new One<>(count - diff));
          }
        });
    return objref;
  }
 private static byte getDataValue(
     MJIEnv env, int chunk_obj, long position, int chunk_size, FeatureExpr ctx) {
   int offset = (int) (position % chunk_size);
   int index = offset / INT_SIZE;
   int bit_shift = 8 * (offset % INT_SIZE);
   int int_array = env.getReferenceField(ctx, chunk_obj, data).getValue();
   return (byte) (env.getIntArrayElement(int_array, index) >> bit_shift);
 }
 @MJI
 public int substring__I__Ljava_lang_String_2(
     MJIEnv env, int objRef, int beginIndex, FeatureExpr ctx) {
   Integer aref = env.getReferenceField(ctx, objRef, "value").getValue();
   char[] buf = env.getCharArrayObject(aref).getValue();
   String obj = new String(buf);
   String result = obj.substring(beginIndex);
   return env.newString(ctx, result);
 }
  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;
  }
Exemplo n.º 7
0
  @MJI
  public short parseShort__Ljava_lang_String_2I__S(
      MJIEnv env, int clsObjRef, int strRef, int radix, FeatureExpr ctx) {
    try {
      return Short.parseShort(env.getStringObject(ctx, strRef), radix);
    } catch (NumberFormatException e) {
      env.throwException(ctx, "java.lang.NumberFormatException");

      return 0;
    }
  }
  public static int getAnnotations_____3Ljava_lang_annotation_Annotation_2(MJIEnv env, int objRef) {
    FieldInfo fi = getFieldInfo(env, objRef);
    AnnotationInfo[] ai = fi.getAnnotations();

    try {
      return env.newAnnotationProxies(ai);
    } catch (ClinitRequired x) {
      env.handleClinitRequest(x.getRequiredClassInfo());
      return MJIEnv.NULL;
    }
  }
Exemplo n.º 9
0
  @MJI
  public int parseInt__Ljava_lang_String_2__I(
      MJIEnv env, int clsObjRef, int strRef, FeatureExpr ctx) {
    try {
      return Integer.parseInt(env.getStringObject(ctx, strRef));
    } catch (NumberFormatException e) {
      env.throwException(ctx, "java.lang.NumberFormatException");

      return 0;
    }
  }
  static ClassInfo getDataRepresentationClassInfo(MJIEnv env, FeatureExpr ctx) {
    ThreadInfo ti = env.getThreadInfo();
    //    Instruction insn = ti.getPC().getValue();

    ClassInfo ci = ClassLoaderInfo.getSystemResolvedClassInfo(DataRepresentation);
    if (ci.pushRequiredClinits(ctx, ti)) {
      env.repeatInvocation();
      return null;
    }

    return ci;
  }
  @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;
  }
 private static void setDataValue(
     MJIEnv env, int chunk_obj, long position, byte data_value, int chunk_size, FeatureExpr ctx) {
   int offset = (int) (position % chunk_size);
   int index = offset / INT_SIZE;
   int bit_shift = 8 * (offset % INT_SIZE);
   int int_array = env.getReferenceField(ctx, chunk_obj, data).getValue();
   int old_value = env.getIntArrayElement(int_array, index);
   env.setIntArrayElement(
       ctx,
       int_array,
       index,
       new One<>((old_value & ~(0xff << bit_shift)) | data_value << bit_shift));
 }
 @MJI
 public byte readByte____B(MJIEnv env, int this_ptr, FeatureExpr ctx) {
   long current_posn = env.getLongField(this_ptr, current_position).getValue();
   long current_len = env.getLongField(this_ptr, current_length).getValue();
   int chunk_size = env.getStaticIntField(RandomAccessFile, CHUNK_SIZE);
   if (current_posn >= current_len) {
     env.throwException(ctx, EOFException);
   }
   int chunk = findDataChunk(env, this_ptr, current_posn, chunk_size, ctx);
   byte result = getDataValue(env, chunk, current_posn, chunk_size, ctx);
   env.setLongField(ctx, this_ptr, current_position, new One<>(current_posn + 1));
   return result;
 }
  @MJI
  public void setLength__J__V(MJIEnv env, int this_ptr, long len, FeatureExpr ctx) {
    long current_posn = env.getLongField(this_ptr, current_position).getValue();
    long current_len = env.getLongField(this_ptr, current_length).getValue();
    if (current_posn >= len && len < current_len) {

      env.setLongField(ctx, this_ptr, current_position, new One<>(len));
    }
    env.setLongField(ctx, this_ptr, current_length, new One<>(len));
    // update length in the mapped object if it exists
    env.setLongField(
        ctx, getMapping(env, this_ptr, ctx), current_length, new One<>(current_posn + 1));
  }
  int appendString(
      FeatureExpr ctx, final MJIEnv env, final int objref, final Conditional<String> conditionalS) {
    Conditional<Integer> condAref = env.getReferenceField(ctx, objref, "value");

    condAref.mapf(
        ctx,
        new VoidBiFunction<FeatureExpr, Integer>() {

          @Override
          public void apply(FeatureExpr ctx, final Integer aref) {
            conditionalS.mapf(
                ctx,
                new VoidBiFunction<FeatureExpr, String>() {

                  @Override
                  public void apply(FeatureExpr ctx, final String s) {
                    final int slen = s.length();
                    final int alen = env.getArrayLength(ctx, aref);
                    Conditional<Integer> count = env.getIntField(objref, "count");
                    count.mapf(
                        ctx,
                        new VoidBiFunction<FeatureExpr, Integer>() {

                          @Override
                          public void apply(FeatureExpr ctx, Integer count) {
                            if (Conditional.isContradiction(ctx)) {
                              return;
                            }
                            int i, j;
                            int n = count + slen;

                            if (n < alen) {
                              for (i = count, j = 0; i < n; i++, j++) {
                                env.setCharArrayElement(ctx, aref, i, new One<>(s.charAt(j)));
                              }
                            } else {
                              int m = 3 * alen / 2;
                              if (m < n) {
                                m = n;
                              }
                              int arefNew = env.newCharArray(ctx, m);
                              for (i = 0; i < count; i++) {
                                env.setCharArrayElement(
                                    ctx, arefNew, i, env.getCharArrayElement(aref, i));
                              }
                              for (j = 0; i < n; i++, j++) {
                                env.setCharArrayElement(ctx, arefNew, i, new One<>(s.charAt(j)));
                              }
                              env.setReferenceField(ctx, objref, "value", arefNew);
                            }

                            env.setIntField(ctx, objref, "count", new One<>(n));
                          }
                        });
                  }
                });
          }
        });
    return objref;
  }
  static FieldInfo getFieldInfo(MJIEnv env, int objRef) {
    int fidx = env.getIntField(objRef, "regIdx");
    assert ((fidx >= 0) || (fidx < nRegistered))
        : "illegal FieldInfo request: " + fidx + ", " + nRegistered;

    return registered[fidx];
  }
  static FieldInfo getFieldInfo(FeatureExpr ctx, MJIEnv env, int objRef) {
    int fidx = env.getIntField(objRef, "regIdx").simplify(ctx).getValue().intValue();
    assert ((fidx >= 0) || (fidx < nRegistered))
        : "illegal FieldInfo request: " + fidx + ", " + nRegistered;

    return registered[fidx];
  }
  @SuppressWarnings("deprecation")
  @MJI
  public Conditional<Integer> indexOf__Ljava_lang_String_2I__I(
      final MJIEnv env, int objref, final int str, final int fromIndex, FeatureExpr ctx) {
    Integer aref = env.getReferenceField(ctx, objref, "value").getValue();
    Conditional<char[]> buf = env.getCharArrayObject(aref);
    return buf.mapf(
        ctx,
        new BiFunction<FeatureExpr, char[], Conditional<Integer>>() {

          @Override
          public Conditional<Integer> apply(FeatureExpr ctx, char[] buf) {
            String indexStr = env.getStringObject(ctx, str);
            return new One<>(new String(buf).indexOf(indexStr, fromIndex));
          }
        });
  }
  /** >2do> that doesn't take care of class init yet */
  public static int getType____Ljava_lang_Class_2(MJIEnv env, int objRef) {
    ThreadInfo ti = env.getThreadInfo();
    FieldInfo fi = getFieldInfo(env, objRef);

    try {
      ClassInfo ci = fi.getTypeClassInfo();
      if (!ci.isRegistered()) {
        ci.registerClass(ti);
      }

      return ci.getClassObjectRef();

    } catch (NoClassInfoException cx) {
      env.throwException("java.lang.NoClassDefFoundError", cx.getMessage());
      return MJIEnv.NULL;
    }
  }
 @MJI
 public int read___3BII__I(
     MJIEnv env, int this_ptr, int data_array, int start, int len, FeatureExpr ctx) {
   int i = 0;
   long current_posn = env.getLongField(this_ptr, current_position).getValue();
   long current_len = env.getLongField(this_ptr, current_length).getValue();
   while (i < len && current_posn < current_len) {
     env.setByteArrayElement(
         ctx, data_array, start + i, new One<>(readByte____B(env, this_ptr, ctx)));
     i += 1;
     current_posn += 1;
   }
   if (i == 0) {
     return -1;
   }
   return i;
 }
  public static int getAnnotation__Ljava_lang_Class_2__Ljava_lang_annotation_Annotation_2(
      MJIEnv env, int objRef, int annotationClsRef) {
    FieldInfo fi = getFieldInfo(env, objRef);
    ClassInfo aci = env.getReferredClassInfo(annotationClsRef);

    AnnotationInfo ai = fi.getAnnotation(aci.getName());
    if (ai != null) {
      ClassInfo aciProxy = ClassInfo.getAnnotationProxy(aci);
      try {
        return env.newAnnotationProxy(aciProxy, ai);
      } catch (ClinitRequired x) {
        env.handleClinitRequest(x.getRequiredClassInfo());
        return MJIEnv.NULL;
      }
    }

    return MJIEnv.NULL;
  }
  static boolean isAvailable(MJIEnv env, FieldInfo fi, int fobjRef) {
    if (fi.isStatic()) {
      ClassInfo fci = fi.getClassInfo();
      if (fci.requiresClinitExecution(env.getThreadInfo())) {
        env.repeatInvocation();
        return false;
      }

    } else {
      if (fobjRef == MJIEnv.NULL) {
        env.throwException("java.lang.NullPointerException");
        return false;
      }
      // class had obviously been initialized, otherwise we won't have an instance of it
    }

    return true;
  }
 /** This is a bit lame doing it this way, but it is easy. */
 @SuppressWarnings("deprecation")
 @MJI
 public void write___3BII__V(
     MJIEnv env, int this_ptr, int data_array, int start, int len, FeatureExpr ctx) {
   byte[] data_values = env.getByteArrayObjectDeprecated(ctx, data_array);
   for (int i = start; i < len; ++i) {
     writeByte__I__V(env, this_ptr, data_values[i], ctx);
   }
 }
  @MJI
  public int get__Ljava_lang_Object_2__Ljava_lang_Object_2(
      MJIEnv env, int objRef, int fobjRef, FeatureExpr ctx) {
    FieldInfo fi = getFieldInfo(ctx, env, objRef);
    ElementInfo ei =
        getCheckedElementInfo(
            env, objRef, fi, fobjRef, null, null, false, ctx); // no type check here
    if (ei == null) {
      return 0;
    }

    if (!(fi instanceof ReferenceFieldInfo)) { // primitive type, we need to box it
      if (fi instanceof DoubleFieldInfo) {
        Conditional<Double> d = ei.getDoubleField(fi);
        return env.newDouble(ctx, d);
      } else if (fi instanceof FloatFieldInfo) {
        Conditional<Float> f = ei.getFloatField(fi);
        return env.newFloat(ctx, f);
      } else if (fi instanceof LongFieldInfo) {
        Conditional<Long> l = ei.getLongField(fi);
        return env.newLong(ctx, l);
      } else if (fi instanceof IntegerFieldInfo) {
        // this might actually represent a plethora of types
        Conditional<Integer> i = ei.getIntField(fi);
        return env.newInteger(ctx, i);
      } else if (fi instanceof BooleanFieldInfo) {
        Conditional<Boolean> b = ei.getBooleanField(fi);
        return env.newBoolean(b.getValue());
      } else if (fi instanceof ByteFieldInfo) {
        Conditional<Byte> z = ei.getByteField(fi);
        return env.newByte(ctx, z);
      } else if (fi instanceof CharFieldInfo) {
        Conditional<Character> c = ei.getCharField(fi);
        return env.newCharacter(ctx, c);
      } else if (fi instanceof ShortFieldInfo) {
        Conditional<Short> s = ei.getShortField(fi);
        return env.newShort(ctx, s);
      }

    } else { // it's a reference
      int ref = ei.getReferenceField(fi).simplify(ctx).getValue(); // we internally store it as int
      return ref;
    }

    env.throwException(ctx, "java.lang.IllegalArgumentException", "unknown field type");
    return MJIEnv.NULL;
  }
 public static int toString____Ljava_lang_String_2(MJIEnv env, int objRef) {
   StringBuilder sb = new StringBuilder();
   FieldInfo fi = getFieldInfo(env, objRef);
   sb.append(Modifier.toString(fi.getModifiers()));
   sb.append(' ');
   sb.append(fi.getType());
   sb.append(' ');
   sb.append(fi.getFullName());
   int sref = env.newString(sb.toString());
   return sref;
 }
 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;
 }
  /** >2do> that doesn't take care of class init yet */
  @MJI
  public int getType____Ljava_lang_Class_2(MJIEnv env, int objRef, FeatureExpr ctx) {
    ThreadInfo ti = env.getThreadInfo();
    FieldInfo fi = getFieldInfo(ctx, env, objRef);

    ClassInfo ci = fi.getTypeClassInfo();
    if (!ci.isRegistered()) {
      ci.registerClass(ctx, ti);
    }

    return ci.getClassObjectRef();
  }
  @MJI
  public int substring__II__Ljava_lang_String_2(
      MJIEnv env,
      int objRef,
      final Conditional<Integer> beginIndex,
      final Conditional<Integer> endIndex,
      FeatureExpr ctx) {
    Integer aref = env.getReferenceField(ctx, objRef, "value").getValue();
    Conditional<char[]> buf = env.getCharArrayObject(aref);
    Conditional<String> result =
        buf.mapf(
            ctx,
            new BiFunction<FeatureExpr, char[], Conditional<String>>() {

              @Override
              public Conditional<String> apply(FeatureExpr ctx, char[] buf) {
                String obj = new String(buf);
                return new One<>(obj.substring(beginIndex.getValue(), endIndex.getValue()));
              }
            });
    return env.newString(ctx, result);
  }
  public static int get__Ljava_lang_Object_2__Ljava_lang_Object_2(
      MJIEnv env, int objRef, int fobjRef) {
    FieldInfo fi = getFieldInfo(env, objRef);
    ElementInfo ei = getCheckedElementInfo(env, fi, fobjRef, null, null); // no type check here
    if (ei == null) {
      return 0;
    }

    if (!(fi instanceof ReferenceFieldInfo)) { // primitive type, we need to box it
      if (fi instanceof DoubleFieldInfo) {
        double d = ei.getDoubleField(fi);
        return env.newDouble(d);
      } else if (fi instanceof FloatFieldInfo) {
        float f = ei.getFloatField(fi);
        return env.newFloat(f);
      } else if (fi instanceof LongFieldInfo) {
        long l = ei.getLongField(fi);
        return env.newLong(l);
      } else if (fi instanceof IntegerFieldInfo) {
        // this might actually represent a plethora of types
        int i = ei.getIntField(fi);
        return env.newInteger(i);
      } else if (fi instanceof BooleanFieldInfo) {
        boolean b = ei.getBooleanField(fi);
        return env.newBoolean(b);
      } else if (fi instanceof ByteFieldInfo) {
        byte z = ei.getByteField(fi);
        return env.newByte(z);
      } else if (fi instanceof CharFieldInfo) {
        char c = ei.getCharField(fi);
        return env.newCharacter(c);
      } else if (fi instanceof ShortFieldInfo) {
        short s = ei.getShortField(fi);
        return env.newShort(s);
      }

    } else { // it's a reference
      int ref = ei.getReferenceField(fi); // we internally store it as int
      return ref;
    }

    env.throwException("java.lang.IllegalArgumentException", "unknown field type");
    return MJIEnv.NULL;
  }
  @MJI
  public int encode___3CII___3B(
      MJIEnv env, int clsObjRef, int cref, int off, int len, FeatureExpr ctx) {

    int bref = env.newByteArray(len);
    for (int i = 0, j = off; i < len; i++, j++) {
      env.setByteArrayElement(
          ctx,
          bref,
          i,
          env.getCharArrayElement(cref, j)
              .map(
                  new Function<Character, Byte>() {

                    @Override
                    public Byte apply(Character c) {
                      return (byte) c.charValue();
                    }
                  }));
    }

    return bref;
  }