Example #1
0
  public void testCodeGen2() throws Exception {
    CtClass cc = sloader.makeClass("test2.CodeGen2");

    CtMethod m1 =
        CtNewMethod.make(
            "public int test() {"
                + "  int len;"
                + "  String s = \"foo\" + \"bar\" + 3;"
                + "  System.out.println(s); len = s.length();"
                + "  len = -3 + len; len = len - (7 - 2 + -1);"
                + "  int k = 3; len += ~k - ~3;"
                + "  return len; }",
            cc);
    cc.addMethod(m1);

    CtMethod m2 =
        CtNewMethod.make(
            "public int test2() {"
                + "  double d = 0.2 - -0.1;"
                + "  d += (0.2 + 0.3) * 1.0;"
                + "  return (int)(d * 10); }",
            cc);
    cc.addMethod(m2);

    cc.writeFile();
    Object obj = make(cc.getName());
    assertEquals(0, invoke(obj, "test"));
    assertEquals(8, invoke(obj, "test2"));
  }
Example #2
0
 public void testNullArg() throws Exception {
   CtClass cc = sloader.makeClass("test2.NullArgTest");
   CtMethod m1 =
       CtNewMethod.make(
           "public Object foo(Object[] obj, int idx) throws Throwable {" + "    return null; }",
           cc);
   cc.addMethod(m1);
   CtMethod m2 = CtNewMethod.make("public void bar() { this.foo(null, 0); }", cc);
   cc.addMethod(m2);
   CtMethod m3 = CtNewMethod.make("public void bar2() { this.foo((Object[])null, 0); }", cc);
   cc.addMethod(m3);
   cc.writeFile();
 }
Example #3
0
  public void testCodeGen() throws Exception {
    CtClass cc = sloader.get("test2.CodeGen");
    CtMethod m1 = cc.getDeclaredMethod("run");
    m1.insertBefore(
        "{ double d = true ? 1 : 0.1; "
            + "  d = d > 0.5 ? 0.0 : - 1.0; "
            + "  System.out.println(d); "
            + "  String s = \"foo\"; "
            + "  s = 1 + 2 + s + \"bar\"; "
            + "  s += \"poi\" + 3 + seven() + seven(\":\" + ' '); "
            + "  s += .14; "
            + "  msg = s; "
            + "  System.out.println(s); }");

    // recursive type check is done if $proceed is used.
    CtMethod m2 =
        CtNewMethod.make(
            "public int test() {"
                + "  String s = $proceed(\"int\" + (3 + 0.14)) + '.'; "
                + "  System.out.println(s); return s.length(); }",
            cc,
            "this",
            "seven");
    cc.addMethod(m2);
    cc.writeFile();
    Object obj = make(cc.getName());
    assertEquals(19, invoke(obj, "run"));
    assertEquals(9, invoke(obj, "test"));
  }
Example #4
0
 public void testAddMethod() throws Exception {
   CtClass cc = sloader.get("test2.AddMethod");
   CtMethod m = CtNewMethod.make("public void f() { return 1; }", cc);
   try {
     cc.addMethod(m);
     fail();
   } catch (CannotCompileException e) {
   }
   CtMethod m2 = CtNewMethod.make("public void f(int i, int j) { return 1; }", cc);
   cc.addMethod(m2);
   try {
     cc.addField(new CtField(CtClass.longType, "f", cc));
     fail();
   } catch (CannotCompileException e) {
   }
 }
Example #5
0
  public void testMethodInInner2() throws Exception {
    CtClass inner = sloader.get("test2.Nested3$Inner");
    CtClass outer = sloader.get("test2.Nested3");
    String src =
        "public int f() {"
            + "  int k = 0;"
            + "  test2.Nested3 n = new test2.Nested3(3);"
            + "  k += n.geti();"
            + "  n = new test2.Nested3();"
            + "  k += n.geti();"
            + "  n = new test2.Nested3(\"foo\");"
            + "  k += n.geti();"
            + "  return k; }";

    outer.stopPruning(true);
    outer.writeFile();
    try {
      CtMethod m = CtNewMethod.make(src, inner);
      fail();
    } catch (RuntimeException e) {
    }
    outer.defrost();

    CtMethod m = CtNewMethod.make(src, inner);
    inner.addMethod(m);

    inner.writeFile();
    outer.writeFile();

    Object iobj = make(inner.getName());
    assertEquals(6, invoke(iobj, "f"));
  }
Example #6
0
 public void testArrayLen() throws Exception {
   CtClass cc = sloader.get("test2.ArrayLenTest");
   cc.addMethod(CtNewMethod.make("public int foo(){ return this.length; }", cc));
   cc.writeFile();
   Object obj = make(cc.getName());
   assertEquals(1, invoke(obj, "foo"));
 }
Example #7
0
 public void testObjectSuper() throws Exception {
   CtClass cc = sloader.get("java.lang.Object");
   try {
     cc.addMethod(CtNewMethod.make("public int foo(){ return super.hashCode(); }", cc));
     fail("could access the super of java.lang.Object");
   } catch (CannotCompileException e) {
   }
 }
Example #8
0
 public void testMakeStaticMethod() throws Exception {
   CtClass cc = sloader.makeClass("test2.MakeStaticMethod");
   CtMethod m =
       CtNewMethod.make(
           Modifier.PUBLIC | Modifier.STATIC,
           CtClass.intType,
           "create",
           new CtClass[] {CtClass.intType},
           null,
           "{ return $1; }",
           cc);
   cc.addMethod(m);
   cc.addMethod(CtNewMethod.make("public int test() { return create(13); }", cc));
   cc.writeFile();
   Object obj = make(cc.getName());
   assertEquals(13, invoke(obj, "test"));
 }
Example #9
0
 public void testSuperInterface() throws Exception {
   CtClass cc = sloader.makeClass("test2.SuperInterface3");
   CtClass cc2 = sloader.get("test2.SuperInterface2");
   cc.addInterface(cc2);
   cc.addField(new CtField(cc2, "inner", cc));
   CtMethod m = CtNewMethod.make("public int getAge() { return inner.getAge(); }", cc);
   cc.addMethod(m);
   cc.writeFile();
 }
Example #10
0
 public void testArrayLength() throws Exception {
   CtClass cc = sloader.makeClass("test2.ArrayLength");
   CtMethod m2 =
       CtNewMethod.make(
           "public int f() { String[] s = new String[3]; " + "return s.length; }", cc);
   cc.addMethod(m2);
   cc.writeFile();
   Object obj = make(cc.getName());
   assertEquals(3, invoke(obj, "f"));
 }
Example #11
0
  public void testMethodInInner() throws Exception {
    CtClass inner = sloader.get("test2.Nested2$Inner");
    CtClass outer = sloader.get("test2.Nested2");
    String src = "public int f(test2.Nested2 n) {" + "  n.i = 1; n.i++; n.i += 2; return n.i; }";

    outer.writeFile();
    try {
      CtMethod m = CtNewMethod.make(src, inner);
      fail();
    } catch (RuntimeException e) {
    }
    outer.defrost();

    CtMethod m = CtNewMethod.make(src, inner);
    inner.addMethod(m);

    src =
        "public int g(test2.Nested2 n) {"
            + "  n.d = 1.0; n.d++; n.d += 2.0;"
            + "  return n.d == 4.0 ? 7 : 8; }";
    m = CtNewMethod.make(src, inner);
    inner.addMethod(m);

    src =
        "public int h(test2.Nested2 n) {"
            + "  n.s = \"poi\";"
            + "return n.s.length() + f(n) + g(n); }";
    m = CtNewMethod.make(src, inner);
    inner.addMethod(m);

    inner.writeFile();
    outer.writeFile();

    Object nobj = make(outer.getName());
    Object iobj = make(inner.getName());
    Method mth = iobj.getClass().getMethod("h", new Class[] {nobj.getClass()});
    Object resobj = mth.invoke(iobj, new Object[] {nobj});
    int res = ((Integer) resobj).intValue();
    assertEquals(14, res);
  }
Example #12
0
  public void testStaticMember2() throws Exception {
    CtClass cc = sloader.get("test2.StaticMember2");

    cc.addMethod(
        CtNewMethod.make(
            "public int run() {"
                + "  return test2.StaticMember2.k + test2.StaticMember2.seven()"
                + "         + (test2.StaticMember2.f + f)"
                + "         + test2.StaticMember2.f + f; }",
            cc));

    cc.addMethod(
        CtNewMethod.make(
            "public int run1() {"
                + "  long j = 1L;"
                + "  return (int)(j + (test2.StaticMember2.fj + fj)"
                + "         + test2.StaticMember2.fj + fj); }",
            cc));

    cc.addMethod(
        CtNewMethod.make(
            "public int run2() {"
                + "  double x = 1.0;"
                + "  double d = x + test2.StaticMember2.fd + fd"
                + "             + (test2.StaticMember2.fd + fd);"
                + "  return (int)(d * 10); }",
            cc));

    cc.addMethod(
        CtNewMethod.make(
            "public int run3() {" + "  return (test2.StaticMember2.fb & fb) ? 1 : 0; }", cc));

    cc.writeFile();
    Object obj = make(cc.getName());
    assertEquals(54, invoke(obj, "run"));
    assertEquals(53, invoke(obj, "run1"));
    assertEquals(958, invoke(obj, "run2"));
    assertEquals(0, invoke(obj, "run3"));
  }
Example #13
0
 public void testArrayInit() throws Exception {
   CtClass cc = sloader.makeClass("test2.ArrayInit");
   cc.addMethod(
       CtNewMethod.make(
           "public int foo(){ "
               + "  int[] i = new int[] { 1, 2 };"
               + "  double[] d = new double[] { 3.0, 4.0 };"
               + "  String[] s = new String[] { \"foo\", \"12345\" };"
               + "  return i[0] + (int)d[0] + s[1].length(); }",
           cc));
   cc.addMethod(
       CtNewMethod.make(
           "public int bar(){ "
               + "  int[] i = { 1, 2.0 };"
               + "  double[] d = { 3.0, 4 };"
               + "  String[] s = { \"foo\", \"12345\" };"
               + "  return i[0] + (int)d[0] + s[1].length(); }",
           cc));
   cc.writeFile();
   Object obj = make(cc.getName());
   assertEquals(9, invoke(obj, "foo"));
   assertEquals(9, invoke(obj, "bar"));
 }
Example #14
0
 public void testBrennan() throws Exception {
   CtClass cc = sloader.get("test2.Brennan");
   cc.addMethod(
       CtNewMethod.make(
           "public int foo(){"
               + "  java.text.SimpleDateFormat df;"
               + "  if((df = (java.text.SimpleDateFormat)format) == null)"
               + "    df = new java.text.SimpleDateFormat(\"yyyyMMdd\");"
               + "  return 1;}",
           cc));
   cc.writeFile();
   Object obj = make(cc.getName());
   assertEquals(1, invoke(obj, "foo"));
 }
Example #15
0
  public void testIncOp() throws Exception {
    CtClass target = sloader.makeClass("test2.IncOp");
    String src =
        "public int sample() {"
            + "    int ia[] = new int[50];"
            + "    ia[0] = 1;"
            + "    int v = ++(ia[0]);"
            + "    return v; }";

    CtMethod newmethod = CtNewMethod.make(src, target);
    target.addMethod(newmethod);
    target.writeFile();
    Object obj = make(target.getName());
    assertEquals(2, invoke(obj, "sample"));
  }
Example #16
0
  public void testInner() throws Exception {
    ClassPool pool = ClassPool.getDefault();
    String classname = "test2.Inner";
    CtClass target = pool.get(classname);
    String src =
        "public void sampleMethod() throws Exception {"
            + "java.util.Properties props = new java.util.Properties();"
            + "java.rmi.activation.ActivationGroupDesc.CommandEnvironment ace "
            + " = null;"
            + "java.rmi.activation.ActivationGroupDesc agd "
            + " = new java.rmi.activation.ActivationGroupDesc(props,ace);}";
    CtMethod newmethod = CtNewMethod.make(src, target);
    target.addMethod(newmethod);

    String src2 =
        "public java.lang.Character.Subset sampleMethod2() {"
            + "  java.lang.Character.Subset s "
            + "    = Character.UnicodeBlock.HIRAGANA; "
            + "  return s; }";
    CtMethod newmethod2 = CtNewMethod.make(src2, target);
    target.addMethod(newmethod2);

    target.writeFile();
  }
Example #17
0
  public void testInheritance() throws Exception {
    ClassPool pool = ClassPool.getDefault();
    String classname = "test2.Inherit";
    CtClass target = pool.get(classname);
    String src =
        "public void sampleMethod() {"
            + "  test2.Inherit i = new test2.Inherit();"
            + "  test2.Inherit2 i2 = i;"
            + "  test2.Inherit3 i3 = i;"
            + "  i3.foo2(); i3.foo2(); i2.foo1(); }";

    CtMethod newmethod = CtNewMethod.make(src, target);
    target.addMethod(newmethod);
    target.writeFile();
  }
Example #18
0
  private void testDotClass(String cname, boolean java5) throws Exception {
    CtClass cc = sloader.makeClass(cname);
    if (java5) cc.getClassFile2().setVersionToJava5();

    CtMethod m =
        CtNewMethod.make(
            "public String getclass() {"
                + "  return int.class.getName() + int[].class.getName()"
                + "          + String.class.getName() + String[].class.getName()"
                + "          + java.lang.Object.class.getName()"
                + "          + java.util.Vector.class.getName(); }",
            cc);
    cc.addMethod(m);
    CtMethod m2 =
        CtNewMethod.make(
            "public int test() {"
                + "  String s = getclass(); System.out.println(s);"
                + "  return s.length(); }",
            cc);
    cc.addMethod(m2);
    cc.writeFile();
    Object obj = make(cc.getName());
    assertEquals(72, invoke(obj, "test"));
  }
Example #19
0
  private void testDotClass4(String cname, boolean java5) throws Exception {
    CtClass cc = sloader.makeClass(cname);
    if (java5) cc.getClassFile2().setVersionToJava5();

    CtMethod m =
        CtNewMethod.make(
            "public int test() {"
                + "  String s = Object.class.getName()"
                + "      + Object[].class.getName();"
                + "  return s.length(); }",
            cc);
    cc.addMethod(m);
    cc.writeFile();
    Object obj = make(cc.getName());
    assertEquals(35, invoke(obj, "test"));
  }
Example #20
0
 public void testImportPackage() throws Exception {
   CtClass cc2 = sloader.makeClass("test2.Imported");
   cc2.writeFile();
   CtClass cc = sloader.makeClass("test2.Importer");
   sloader.importPackage("test2");
   cc.addMethod(
       CtNewMethod.make(
           "public int foo(){ "
               + "  Imported obj = new Imported();"
               + "  return obj.getClass().getName().length(); }",
           cc));
   sloader.clearImportedPackages();
   cc.writeFile();
   Object obj = make(cc.getName());
   assertEquals(14, invoke(obj, "foo"));
 }
Example #21
0
 public void testKmatcha() throws Exception {
   CtClass cc = sloader.makeClass("test2.Kmatcha");
   cc.addMethod(
       CtNewMethod.make(
           "public void display(String [] params){"
               + "  if(params == null){"
               + "    System.out.println(\"Nothing to display\");"
               + "  }else{"
               + "    int k = params.length - 1;"
               + "    if(k >= 0)"
               + "      do "
               + "        System.out.println(params[k]);"
               + "      while(--k >= 0);"
               + "  }}",
           cc));
 }
Example #22
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"));
  }
Example #23
0
  private void testDotClass2(String cname, boolean java5) throws Exception {
    CtClass cc = sloader.makeClass(cname);
    CtClass cc3 = sloader.makeClass("test2.DotClass3");
    if (java5) cc.getClassFile2().setVersionToJava5();

    CtMethod m =
        CtNewMethod.make(
            "public int test() {" + "  return test2.DotClass3.class.getName().length(); }", cc);
    cc.addMethod(m);
    cc.writeFile();
    // don't execute cc3.writeFile();
    Object obj = make(cc.getName());
    try {
      assertEquals(15, invoke(obj, "test"));
    } catch (java.lang.reflect.InvocationTargetException e) {
      Throwable t = e.getCause();
      assertTrue(t instanceof java.lang.NoClassDefFoundError);
    }
  }
Example #24
0
  public void testMakeNestedClass() throws Exception {
    CtClass outer = sloader.get("test2.Nested4");
    try {
      CtClass inner = outer.makeNestedClass("Inner", false);
      fail();
    } catch (RuntimeException e) {
      print(e.getMessage());
    }

    CtClass nested = outer.makeNestedClass("Inner", true);
    outer.stopPruning(true);
    outer.writeFile();
    outer.defrost();
    String src = "public int f() { return test2.Nested4.value; }";

    CtMethod m = CtNewMethod.make(src, nested);
    nested.addMethod(m);
    nested.writeFile();
    outer.writeFile();

    Object iobj = make(nested.getName());
    assertEquals(6, invoke(iobj, "f"));
  }
Example #25
0
  public void testNewOp() throws Exception {
    CtClass cc = sloader.get("test2.NewOp");

    CtMethod add =
        CtNewMethod.make(
            "public test2.NewOp2 "
                + "    addMonitoringRemoteEventListener("
                + "        test2.NewOp2 listener)"
                + "        throws java.rmi.RemoteException {"
                + "    $0.listenerList.addElement(listener);"
                + "    return new test2.NewOp2(0L, this, null, 0L);"
                + "}",
            cc);

    add.setModifiers(Modifier.PUBLIC);
    cc.addMethod(add);
    /*
        CtMethod type= CtNewMethod.make(
           "public test2.Where getNewType() { return new test2.Where(); }",
            cc);
        cc.addMethod(type);
    */
    cc.writeFile();
  }
Example #26
0
  public void testDeclaringClass() throws Exception {
    try {
      CtClass cc = sloader.get("test2.NotExistingClass");
    } catch (NotFoundException e) {
      System.out.println(e);
    }
    CtClass inner = sloader.get("test2.Nested$Inner");
    CtClass outer = sloader.get("test2.Nested");
    assertEquals(outer, inner.getDeclaringClass());
    assertEquals(null, outer.getDeclaringClass());
    assertEquals(null, CtClass.intType.getDeclaringClass());

    CtClass inner3 = sloader.get("test2.Nested$Inner3");
    outer.writeFile();
    try {
      CtMethod m = CtNewMethod.make("public void f(test2.Nested n) { return n.geti(); }", inner3);
      fail();
    } catch (RuntimeException e) {
    }
    outer.defrost();
    CtMethod m =
        CtNewMethod.make(
            "public int f(test2.Nested n) { "
                + "return n.geti() + test2.Nested.getj(1) + f() + g(); } ",
            inner3);
    inner3.addMethod(m);
    inner3.writeFile();
    outer.writeFile();

    Object nobj = make(outer.getName());
    Object iobj = make(inner3.getName());
    Method mth = iobj.getClass().getMethod("f", new Class[] {nobj.getClass()});
    Object resobj = mth.invoke(iobj, new Object[] {nobj});
    int res = ((Integer) resobj).intValue();
    assertEquals(6, res);
  }
Example #27
0
  public void testStaticMember() throws Exception {
    CtClass cc = sloader.get("test2.StaticMember");
    CtMethod m =
        CtNewMethod.make(
            "public int run() {" + "return test2.StaticMember#k + test2.StaticMember#foo(); }", cc);
    cc.addMethod(m);

    m = CtNewMethod.make("public int run2() {" + "return k + foo(); }", cc);
    cc.addMethod(m);

    m =
        CtNewMethod.make(
            "public int run3() {" + " test2.StaticMember sm = this;" + " return sm.k + sm.foo(); }",
            cc);
    cc.addMethod(m);

    m = CtNewMethod.make("public int run4() {" + " return this.k + this.foo(); }", cc);
    cc.addMethod(m);

    m = CtNewMethod.make("public static int run5() {" + " return k + foo(); }", cc);
    cc.addMethod(m);

    m =
        CtNewMethod.make(
            "public int run6() {" + " test2.IStaticMember i = this; return i.bar(); }", cc);
    cc.addMethod(m);

    cc.writeFile();
    Object obj = make(cc.getName());
    assertEquals(10, invoke(obj, "run"));
    assertEquals(10, invoke(obj, "run2"));
    assertEquals(10, invoke(obj, "run3"));
    assertEquals(10, invoke(obj, "run4"));
    assertEquals(10, invoke(obj, "run5"));
    assertEquals(3, invoke(obj, "run6"));
  }
Example #28
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"));
  }
Example #29
0
 public void testStrict() throws Exception {
   CtClass cc = sloader.makeClass("test2.StrictTest");
   cc.addMethod(
       CtNewMethod.make(
           "public strictfp int foo(){ " + "  int strict = 1; return strict + 1; }", cc));
 }
Example #30
0
 public void testUnicodeIdentifier() throws Exception {
   CtClass cc = sloader.makeClass("test2.UnicodeIdentifier");
   String src = "public int foo(){ int \u5206 = 0; return \u5206; }";
   cc.addMethod(CtNewMethod.make(src, cc));
 }