Esempio n. 1
0
  public void testStaticArrays() throws Exception {
    CtClass cc = sloader.makeClass("StaticArrays");
    CtField f = new CtField(sloader.get("test2.StaticArraysMem[]"), "myStaticField", cc);

    f.setModifiers(Modifier.STATIC);
    cc.addField(f);
    CtConstructor init = cc.makeClassInitializer();
    String body = "{\n";
    body += ("myStaticField = new test2.StaticArraysMem[2];\n");
    body += ("\n}");
    init.setBody(body);
  }
Esempio n. 2
0
  public void testSwitch() throws Exception {
    CtClass cc = sloader.makeClass("test2.Switch");

    cc.addMethod(
        CtNewMethod.make(
            "public int test1() {"
                + "  int i = 1;"
                + "  int j;"
                + "  switch (i) {"
                + "  case 0: j = i; break;"
                + "  case 1: j = -i; break;"
                + "  default: j = 0; break;"
                + "  }"
                + "  return j; }",
            cc));

    cc.addMethod(
        CtNewMethod.make(
            "public int test2() {"
                + "  int i = 2;"
                + "  int j = 7;"
                + "  switch (i) {"
                + "  case 0: j = i; break;"
                + "  case 1: j = -i; break;"
                + "  }"
                + "  return j; }",
            cc));

    cc.addMethod(
        CtNewMethod.make(
            "public int test3() {"
                + "  int i = Byte.MAX_VALUE;"
                + "  int j;"
                + "  switch (i) {"
                + "  case Byte.MAX_VALUE: j = i; break;"
                + "  case Byte.MIN_VALUE: j = -i; break;"
                + "  default: j = 0; break;"
                + "  }"
                + "  return j; }",
            cc));

    try {
      cc.addMethod(
          CtNewMethod.make(
              "public int test4() {"
                  + "  int i = Byte.MAX_VALUE;"
                  + "  int j;"
                  + "  switch (i) {"
                  + "  case Byte.MAX_VALUE: j = i; return j;"
                  + "  case Byte.MIN_VALUE: j = -i; return j;"
                  + "  default: j = 0;"
                  + "  }"
                  + "}",
              cc));
      fail("does not report an error (no return)");
    } catch (CannotCompileException e) {
      System.out.println(e);
    }

    try {
      cc.addMethod(
          CtNewMethod.make(
              "public int test5() {"
                  + "  int i = Byte.MAX_VALUE;"
                  + "  int j;"
                  + "  switch (i) {"
                  + "  case Byte.MAX_VALUE: j = i; return j;"
                  + "  case Byte.MIN_VALUE: j = -i; return j;"
                  + "  }"
                  + "}",
              cc));
      fail("does not report an error (not default)");
    } catch (CannotCompileException e) {
      System.out.println(e);
    }

    try {
      cc.addMethod(
          CtNewMethod.make(
              "public int test6() {"
                  + "  int i = Byte.MAX_VALUE;"
                  + "  int j;"
                  + "  switch (i) {"
                  + "  case Byte.MAX_VALUE: j = i; break;"
                  + "  default: j = -i; return j;"
                  + "  }"
                  + "  }",
              cc));
      fail("does not report an error (break)");
    } catch (CannotCompileException e) {
      System.out.println(e);
    }

    cc.addField(CtField.make("public static int k;", cc));

    cc.addMethod(
        CtNewMethod.make(
            "public void foo() {"
                + "  int i = 0;"
                + "  k = 3;"
                + "  switch (i) {"
                + "  case Byte.MAX_VALUE: k = 1;"
                + "  case Byte.MIN_VALUE: k = 2;"
                + "  }"
                + "}",
            cc));

    cc.addMethod(
        CtNewMethod.make(
            "public int test7() {"
                + "  int i = Byte.MAX_VALUE;"
                + "  int j = 3; foo();"
                + "  System.out.println(k);"
                + "  switch (i) {"
                + "  case Byte.MAX_VALUE: return k;"
                + "  case Byte.MIN_VALUE: return j;"
                + "  default: return 0;"
                + "  }"
                + "}",
            cc));

    cc.writeFile();
    Object obj = make(cc.getName());
    assertEquals(-1, invoke(obj, "test1"));
    assertEquals(7, invoke(obj, "test2"));
    assertEquals(Byte.MAX_VALUE, invoke(obj, "test3"));
    assertEquals(3, invoke(obj, "test7"));
  }
Esempio n. 3
0
  public void testConstField() throws Exception {
    CtClass cc = sloader.get("test2.ConstField");
    CtField f;
    f = cc.getField("b");
    assertEquals(true, ((Boolean) f.getConstantValue()).booleanValue());
    f = cc.getField("i");
    assertEquals(3, ((Integer) f.getConstantValue()).intValue());
    f = cc.getField("j");
    assertEquals(7L, ((Long) f.getConstantValue()).longValue());
    f = cc.getField("f");
    assertEquals(8.0F, ((Float) f.getConstantValue()).floatValue(), 0.0);
    f = cc.getField("d");
    assertEquals(9.0, ((Double) f.getConstantValue()).doubleValue(), 0.0);
    f = cc.getField("s");
    assertEquals("const", f.getConstantValue());
    f = cc.getField("obj");
    assertEquals(null, f.getConstantValue());
    f = cc.getField("integer");
    assertEquals(null, f.getConstantValue());
    f = cc.getField("k");
    assertEquals(null, f.getConstantValue());

    cc.getClassFile().prune();

    f = cc.getField("i");
    assertEquals(3, ((Integer) f.getConstantValue()).intValue());
    f = cc.getField("k");
    assertEquals(null, f.getConstantValue());
  }
Esempio n. 4
0
  public void testStaticFinal() throws Exception {
    CtClass cc = sloader.makeClass("test2.StaticFinal");
    CtField f = new CtField(CtClass.intType, "sff1", cc);
    f.setModifiers(Modifier.STATIC | Modifier.FINAL);
    cc.addField(f, "5");
    assertEquals(new Integer(5), f.getConstantValue());

    f = new CtField(CtClass.longType, "sff2", cc);
    f.setModifiers(Modifier.STATIC | Modifier.FINAL);
    cc.addField(f, "6");
    assertEquals(new Long(6), f.getConstantValue());

    f = new CtField(CtClass.floatType, "sff3", cc);
    f.setModifiers(Modifier.STATIC | Modifier.FINAL);
    cc.addField(f, "7");
    assertEquals(new Float(7.0F), f.getConstantValue());

    f = new CtField(CtClass.floatType, "sff4", cc);
    f.setModifiers(Modifier.STATIC | Modifier.FINAL);
    cc.addField(f, "8.0");
    assertEquals(new Float(8.0F), f.getConstantValue());

    f = new CtField(CtClass.doubleType, "sff5", cc);
    f.setModifiers(Modifier.STATIC | Modifier.FINAL);
    cc.addField(f, "9");
    assertEquals(new Double(9.0), f.getConstantValue());

    f = new CtField(CtClass.doubleType, "sff6", cc);
    f.setModifiers(Modifier.STATIC | Modifier.FINAL);
    cc.addField(f, "10.0");
    assertEquals(new Double(10.0), f.getConstantValue());

    f = new CtField(sloader.get("java.lang.String"), "sff7", cc);
    f.setModifiers(Modifier.STATIC | Modifier.FINAL);
    cc.addField(f, "\"test\"");
    assertEquals("test", f.getConstantValue());

    f = new CtField(sloader.get("java.lang.String"), "sff8", cc);
    f.setModifiers(Modifier.STATIC);
    cc.addField(f, "\"static\"");
    assertEquals(null, f.getConstantValue());

    cc.addMethod(CtNewMethod.make("public int foo(){ return sff1 + sff7.length(); }", cc));
    cc.writeFile();
    Object obj = make(cc.getName());
    assertEquals(9, invoke(obj, "foo"));
  }
Esempio n. 5
0
  private boolean addSerializeMethods(CtClass cc, boolean callSuper)
      throws NotFoundException, CannotCompileException {
    CtField[] fields = cc.getDeclaredFields();
    int size = 0;
    StringBuffer sb = new StringBuffer();
    sb.append(callSuper ? "{$2=super.pack($1, $2);$1.extend($2" : "{$1.extend($2");
    for (int i = 0; i < fields.length; i++) {
      CtField f = fields[i];
      if ((f.getModifiers() & (Modifier.STATIC | Modifier.TRANSIENT)) == 0) {
        CtClass type = f.getType();
        if (type.isPrimitive()) {
          if (type == CtClass.booleanType || type == CtClass.byteType) {
            size += 1;
          } else if (type == CtClass.charType || type == CtClass.shortType) {
            size += 2;
          } else if (type == CtClass.longType || type == CtClass.doubleType) {
            size += 8;
          } else {
            size += 4;
          }
        } else if (type.getName().equals("java.lang.String")) {
          sb.append("+org.nachodb.impl.Bytes#sizeof(");
          sb.append(f.getName());
          sb.append(",$3)");
        } else {
          return false;
        }
      }
    }
    cc.addInterface(serializable);

    CtMethod m = new CtMethod(pack, cc, null);
    sb.append('+');
    sb.append(size);
    sb.append(");");
    for (int i = 0; i < fields.length; i++) {
      CtField f = fields[i];
      if ((f.getModifiers() & (Modifier.STATIC | Modifier.TRANSIENT)) == 0) {
        CtClass type = f.getType();
        String name = f.getName();
        if (type == CtClass.booleanType) {
          sb.append("$1.arr[$2++]=(byte)(");
          sb.append(name);
          sb.append("?1:0);");
        } else if (type == CtClass.charType) {
          sb.append("org.nachodb.impl.Bytes#pack2($1.arr,$2,(short)");
          sb.append(name);
          sb.append(");$2+=2;");
        } else if (type == CtClass.byteType) {
          sb.append("$1.arr[$2++]=");
          sb.append(name);
          sb.append(";");
        } else if (type == CtClass.shortType) {
          sb.append("org.nachodb.impl.Bytes#pack2($1.arr,$2,");
          sb.append(name);
          sb.append(");$2+=2;");
        } else if (type == CtClass.intType) {
          sb.append("org.nachodb.impl.Bytes#pack4($1.arr,$2,");
          sb.append(name);
          sb.append(");$2+=4;");
        } else if (type == CtClass.longType) {
          sb.append("org.nachodb.impl.Bytes#pack8($1.arr,$2,");
          sb.append(name);
          sb.append(");$2+=8;");
        } else if (type == CtClass.doubleType) {
          sb.append("org.nachodb.impl.Bytes#packF8($1.arr,$2,");
          sb.append(name);
          sb.append(");$2+=8;");
        } else if (type == CtClass.floatType) {
          sb.append("org.nachodb.impl.Bytes#packF4($1.arr,$2,");
          sb.append(name);
          sb.append(");$2+=4;");
        } else {
          sb.append("$2=org.nachodb.impl.Bytes#packStr($1.arr,$2,");
          sb.append(name);
          sb.append(",$3);");
        }
      }
    }
    sb.append("return $2;}");
    m.setBody(sb.toString());
    cc.addMethod(m);

    m = new CtMethod(unpack, cc, null);
    sb = new StringBuffer();
    sb.append(callSuper ? "{$2=super.unpack($1, $2);" : "{");
    for (int i = 0; i < fields.length; i++) {
      CtField f = fields[i];
      if ((f.getModifiers() & (Modifier.STATIC | Modifier.TRANSIENT)) == 0) {
        CtClass type = f.getType();
        String name = f.getName();
        sb.append(name);
        sb.append('=');
        if (type == CtClass.booleanType) {
          sb.append("$1[$2++]!=0;");
        } else if (type == CtClass.charType) {
          sb.append("(char)org.nachodb.impl.Bytes#unpack2($1,$2);$2+=2;");
        } else if (type == CtClass.byteType) {
          sb.append("$1[$2++];");
        } else if (type == CtClass.shortType) {
          sb.append("org.nachodb.impl.Bytes#unpack2($1,$2);$2+=2;");
        } else if (type == CtClass.intType) {
          sb.append("org.nachodb.impl.Bytes#unpack4($1,$2);$2+=4;");
        } else if (type == CtClass.longType) {
          sb.append("org.nachodb.impl.Bytes#unpack8($1,$2);$2+=8;");
        } else if (type == CtClass.doubleType) {
          sb.append("org.nachodb.impl.Bytes#unpackF8($1,$2);$2+=8;");
        } else if (type == CtClass.floatType) {
          sb.append("org.nachodb.impl.Bytes#unpackF4($1,$2);$2+=4;");
        } else {
          sb.append(
              "org.nachodb.impl.Bytes#unpackStr($1,$2,$3);$2+=org.nachodb.impl.Bytes#sizeof($1,$2);");
        }
      }
    }
    sb.append("return $2;}");
    m.setBody(sb.toString());
    cc.addMethod(m);
    return true;
  }