public void testDollarClassInStaticMethod() throws Exception {
   CtClass cc = sloader.makeClass("test5.DollarClass");
   CtMethod m =
       CtNewMethod.make("public static int run(){ return $class.getName().length(); }", cc);
   cc.addMethod(m);
   m = CtNewMethod.make("public int run2(){ return $class.getName().length(); }", cc);
   cc.addMethod(m);
   cc.writeFile();
   Object obj = make(cc.getName());
   assertEquals(cc.getName().length(), invoke(obj, "run"));
   assertEquals(cc.getName().length(), invoke(obj, "run2"));
 }
Beispiel #2
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"));
  }
Beispiel #3
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"));
  }
Beispiel #4
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"));
  }
  @Override
  protected synchronized CtClass get0(String className, boolean useCache) throws NotFoundException {
    if (!exclude(className)) {
      if (isSrg) {
        CtClass cachedClass = srgClasses.get(className);
        if (cachedClass != null) {
          return cachedClass;
        }
        cachedClass = super.getCached(className);
        if (cachedClass != null && cachedClass.isPrimitive()) {
          return cachedClass;
        }
        if (!Transformer.remapClassName(className).equals(className)) {
          Log.severe("Attempted to load obfuscated class " + className, new Throwable());
          return null;
        }
        String remappedName = Transformer.unmapClassName(className);

        byte[] bytes = getBytes(remappedName);
        if (bytes != null) {
          bytes = Transformer.transform(bytes);
          try {
            CtClass ctClass = new CtClassType(new ByteArrayInputStream(bytes), this);
            if (!remappedName.equals(className)) {
              ctClass.freeze();
            }
            srgClasses.put(className, ctClass);
            return ctClass;
          } catch (IOException e) {
            Log.severe("Failed to make " + className + " from " + remappedName, e);
          }
          return null;
        }
      } else {
        if (!Transformer.unmapClassName(className).equals(className)) {
          Log.severe(
              "Attempted to load SRG class " + className + " while patching a non-SRG class.",
              new Throwable());
          return null;
        }
      }
    }
    CtClass clazz;
    if (useCache) {
      clazz = getCached(className);
      if (clazz != null) {
        return clazz;
      }
    }

    clazz = createCtClass(className, useCache);
    if (clazz != null) {
      // clazz.getName() != classname if classname is "[L<name>;".
      if (useCache) {
        cacheCtClass(clazz.getName(), clazz, false);
      }
    }

    return clazz;
  }
Beispiel #6
0
  public void testInsertLocal() throws Exception {
    CtClass cc = sloader.get("test2.InsertLocal");
    CtMethod m1 = cc.getDeclaredMethod("foo");
    m1.insertBefore("{ i = s.length(); d = 0.14; }");
    m1.insertAfter("{ field = i; }");

    CtMethod m2 = cc.getDeclaredMethod("run2");
    m2.insertAt(22, "{ s = \"12\"; k = 5; }");

    CtMethod m3 = cc.getDeclaredMethod("run3");
    m3.instrument(
        new ExprEditor() {
          public void edit(NewExpr n) throws CannotCompileException {
            n.replace("{ i++; $_ = $proceed($$); }");
          }

          public void edit(FieldAccess f) throws CannotCompileException {
            f.replace("{ i++; $_ = $proceed($$); }");
          }

          public void edit(MethodCall m) throws CannotCompileException {
            m.replace("{ i++; $_ = $proceed($$); }");
          }
        });

    cc.writeFile();
    Object obj = make(cc.getName());
    assertEquals(317, invoke(obj, "run"));
    assertEquals(7, invoke(obj, "run2"));
    assertEquals(3, invoke(obj, "run3"));
  }
  public void testRemoveAnnotatino() throws Exception {
    CtClass cc = sloader.get("test5.RemoveAnnotation");
    AnnotationsAttribute aa =
        (AnnotationsAttribute) cc.getClassFile().getAttribute(AnnotationsAttribute.invisibleTag);
    assertTrue(aa.removeAnnotation("test5.RemoveAnno1"));
    AttributeInfo ai = cc.getClassFile().removeAttribute(AnnotationsAttribute.invisibleTag);
    assertEquals(ai.getName(), AnnotationsAttribute.invisibleTag);

    CtMethod foo = cc.getDeclaredMethod("foo");
    AnnotationsAttribute aa2 =
        (AnnotationsAttribute) foo.getMethodInfo().getAttribute(AnnotationsAttribute.invisibleTag);
    assertTrue(aa2.removeAnnotation("test5.RemoveAnno1"));

    CtMethod bar = cc.getDeclaredMethod("bar");
    AnnotationsAttribute aa3 =
        (AnnotationsAttribute) bar.getMethodInfo().getAttribute(AnnotationsAttribute.invisibleTag);
    assertFalse(aa3.removeAnnotation("test5.RemoveAnno1"));
    assertTrue(aa3.removeAnnotation("test5.RemoveAnno2"));
    AttributeInfo ai2 = bar.getMethodInfo().removeAttribute(AnnotationsAttribute.invisibleTag);
    assertEquals(ai2.getName(), AnnotationsAttribute.invisibleTag);

    CtMethod run = cc.getDeclaredMethod("run");
    AttributeInfo ai3 = run.getMethodInfo().removeAttribute(AnnotationsAttribute.invisibleTag);
    assertNull(ai3);

    CtField baz = cc.getDeclaredField("baz");
    AttributeInfo ai4 = baz.getFieldInfo().removeAttribute(AnnotationsAttribute.invisibleTag);
    assertEquals(ai4.getName(), AnnotationsAttribute.invisibleTag);

    cc.writeFile();
    Object obj = make(cc.getName());
    assertEquals(3, invoke(obj, "run"));
  }
  public void testJIRA256() throws Exception {
    // CtClass ec = sloader.get("test5.Entity");

    CtClass cc = sloader.makeClass("test5.JIRA256");
    ClassFile ccFile = cc.getClassFile();
    ConstPool constpool = ccFile.getConstPool();

    AnnotationsAttribute attr =
        new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag);
    javassist.bytecode.annotation.Annotation entityAnno =
        new javassist.bytecode.annotation.Annotation("test5.Entity", constpool);
    // = new javassist.bytecode.annotation.Annotation(constpool, ec);

    entityAnno.addMemberValue(
        "value", new javassist.bytecode.annotation.ArrayMemberValue(constpool));
    attr.addAnnotation(entityAnno);
    ccFile.addAttribute(attr);

    cc.writeFile();
    Object o = make(cc.getName());
    assertTrue(o.getClass().getName().equals("test5.JIRA256"));

    java.lang.annotation.Annotation[] annotations = o.getClass().getDeclaredAnnotations();
    assertEquals(1, annotations.length);
  }
Beispiel #9
0
 public void testAddCatchForConstructor() throws Exception {
   CtClass cc = sloader.get("test2.AddCatchForConstructor");
   CtConstructor m1 = cc.getDeclaredConstructors()[0];
   m1.addCatch("return;", sloader.get("java.lang.Exception"));
   cc.writeFile();
   Object obj = make(cc.getName());
 }
Beispiel #10
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"));
 }
Beispiel #11
0
  public void testNewArray() throws Exception {
    ExprEditor ed =
        new ExprEditor() {
          int dim[] = {1, 2, 2, 1, 2, 2, 3};
          int cdim[] = {1, 1, 2, 1, 1, 2, 2};
          int counter = 0;

          public void edit(NewArray expr) throws CannotCompileException {
            try {
              CtClass str = sloader.get("java.lang.String");
              if (counter < 3) assertEquals(str, expr.getComponentType());
              else assertEquals(CtClass.intType, expr.getComponentType());

              assertEquals(dim[counter], expr.getDimension());
              assertEquals(cdim[counter], expr.getCreatedDimensions());
              expr.replace("{ i += $1; $_ = $proceed($$); }");
              ++counter;
            } catch (NotFoundException e) {
              throw new CannotCompileException(e);
            }
          }
        };

    CtClass cc = sloader.get("test2.NewArray");
    CtMethod m1 = cc.getDeclaredMethod("foo");
    m1.instrument(ed);
    cc.writeFile();
    Object obj = make(cc.getName());
    assertEquals(48, invoke(obj, "run"));
  }
Beispiel #12
0
 public void onWrite(ClassPool pool, String className)
     throws NotFoundException, CannotCompileException {
   CtClass cc = pool.get(className);
   try {
     if (isPersistent(className)) {
       CtClass base = cc.getSuperclass();
       CtConstructor cons = new CtConstructor(constructorParams, cc);
       if (base.subclassOf(persistent) || base == object) {
         cons.setBody(null);
         cc.addConstructor(cons);
         if (base == object) {
           cc.setSuperclass(persistent);
         }
       } else {
         if (!isPersistent(base.getName())) {
           throw new NotFoundException(
               "Base class " + base.getName() + " was not declared as persistent");
         }
         cons.setBody("super($0);");
         cc.addConstructor(cons);
       }
       preprocessMethods(cc, true, true);
       if (base == persistent || base == object) {
         CtMethod m = new CtMethod(isRecursive, cc, null);
         m.setBody("return false;");
         cc.addMethod(m);
         addSerializeMethods(cc, false);
       } else if (base.subtypeOf(serializable)) {
         addSerializeMethods(cc, true);
       }
       if ((cc.getModifiers() & Modifier.PRIVATE) == 0) {
         CtClass f = pool.makeClass(className + "LoadFactory");
         f.addInterface(factory);
         CtMethod c = new CtMethod(create, f, null);
         c.setBody("return new " + className + "($1);");
         f.addMethod(c);
         CtNewConstructor.defaultConstructor(f);
       }
     } else {
       preprocessMethods(
           cc, cc.subtypeOf(persistent) && cc != persistent, !className.startsWith("org.nachodb"));
     }
   } catch (Exception x) {
     x.printStackTrace();
   }
 }
 public void testJIRA241() throws Exception {
   CtClass cc = sloader.get("test5.JIRA241");
   CtMethod testMethod = cc.getDeclaredMethod("test");
   testMethod.insertAfter("System.out.println(\"inserted!\");");
   cc.writeFile();
   Object obj = make(cc.getName());
   assertEquals(10, invoke(obj, "run"));
 }
Beispiel #14
0
 public static CtClass getSuperclass(CtClass c) throws CompileError {
   try {
     CtClass sc = c.getSuperclass();
     if (sc != null) return sc;
   } catch (NotFoundException e) {
   }
   throw new CompileError("cannot find the super class of " + c.getName());
 }
 public void testJIRA249() throws Exception {
   CtClass cc = sloader.get("test5.BoolTest");
   CtMethod testMethod = cc.getDeclaredMethod("test");
   testMethod.insertBefore("i = foo(true & true);");
   cc.writeFile();
   Object obj = make(cc.getName());
   assertEquals(1, invoke(obj, "run"));
 }
 public void testTypeAnno() throws Exception {
   CtClass cc = sloader.get("test5.TypeAnno");
   cc.getClassFile().compact();
   cc.writeFile();
   Object obj = make(cc.getName());
   TypeVariable<?> t = obj.getClass().getTypeParameters()[0];
   Annotation[] annos = t.getAnnotations();
   assertEquals("@test5.TypeAnnoA()", annos[0].toString());
 }
Beispiel #17
0
  /*
   * This method is invoked by CtClassType.setName().  It removes a
   * CtClass object from the hash table and inserts it with the new
   * name.  Don't delegate to the parent.
   */
  synchronized void classNameChanged(String oldname, CtClass clazz) {
    CtClass c = (CtClass) getCached(oldname);
    if (c == clazz) // must check this equation.
    removeCached(oldname); // see getAndRename().

    String newName = clazz.getName();
    checkNotFrozen(newName);
    cacheCtClass(newName, clazz, false);
  }
Beispiel #18
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"));
 }
 public void testJIRA248() throws Exception {
   CtClass cc = sloader.get("test5.JIRA248");
   String methodBody =
       "public int run() { return foo() + super.foo() + super.bar() + test5.JIRA248Intf2.super.baz(); }";
   CtMethod ctMethod = CtMethod.make(methodBody, cc);
   cc.addMethod(ctMethod);
   cc.writeFile();
   Object obj = make(cc.getName());
   assertEquals(40271, invoke(obj, "run"));
 }
Beispiel #20
0
 public void testAddLocalVar() throws Exception {
   CtClass cc = sloader.get("test2.AddLocalVar");
   CtMethod m1 = cc.getDeclaredMethod("foo");
   m1.addLocalVariable("i", CtClass.intType);
   m1.insertBefore("i = 3;");
   m1.insertAfter("$_ = i + 1;");
   cc.writeFile();
   Object obj = make(cc.getName());
   assertEquals(4, invoke(obj, "foo"));
 }
Beispiel #21
0
 public void testReplaceClassName() throws Exception {
   String oldName = "test2.ReplaceClassName2";
   String newName = "test2.ReplaceClassName3";
   CtClass cc = sloader.get("test2.ReplaceClassName");
   cc.replaceClassName(oldName, newName);
   cc.writeFile();
   CtClass cc2 = dloader.get(cc.getName());
   CtMethod m = cc2.getDeclaredMethod("foo");
   assertEquals(newName, m.getParameterTypes()[0].getName());
 }
Beispiel #22
0
 public void testLocalVar() throws Exception {
   CtClass cc = sloader.get("test2.LocalVar");
   CtMethod m = cc.getDeclaredMethod("toString");
   m.addLocalVariable("var", CtClass.booleanType);
   m.insertBefore("{var = true; }");
   m.insertAfter("{if (var) hashCode(); }", false);
   cc.writeFile();
   Object obj = make(cc.getName());
   assertEquals(3, invoke(obj, "foo"));
 }
Beispiel #23
0
 public void testConstBody() throws Exception {
   CtClass cc = sloader.get("test2.ConstBody");
   CtConstructor cons =
       new CtConstructor(
           new CtClass[] {sloader.get("java.lang.String"), sloader.get("java.lang.Integer")}, cc);
   cons.setBody("super((String)$1, (Integer)$2);");
   cc.addConstructor(cons);
   cc.writeFile();
   Object obj = make(cc.getName());
   assertEquals(1, invoke(obj, "bar"));
 }
Beispiel #24
0
  public boolean subtypeOf(CtClass clazz) throws NotFoundException {
    if (super.subtypeOf(clazz)) return true;

    String cname = clazz.getName();
    if (cname.equals(javaLangObject)) return true;

    CtClass[] intfs = getInterfaces();
    for (int i = 0; i < intfs.length; i++) if (intfs[i].subtypeOf(clazz)) return true;

    return clazz.isArray() && getComponentType().subtypeOf(clazz.getComponentType());
  }
Beispiel #25
0
  /**
   * Creates a new class (or interface) from the given class file. If there already exists a class
   * with the same name, the new class overwrites that previous class.
   *
   * <p>This method is used for creating a <code>CtClass</code> object directly from a class file.
   * The qualified class name is obtained from the class file; you do not have to explicitly give
   * the name.
   *
   * @param classfile class file.
   * @param ifNotFrozen throws a RuntimeException if this parameter is true and there is a frozen
   *     class with the same name.
   * @see javassist.ByteArrayClassPath
   */
  public CtClass makeClass(InputStream classfile, boolean ifNotFrozen)
      throws IOException, RuntimeException {
    compress();
    classfile = new BufferedInputStream(classfile);
    CtClass clazz = new CtClassType(classfile, this);
    clazz.checkModify();
    String classname = clazz.getName();
    if (ifNotFrozen) checkNotFrozen(classname);

    cacheCtClass(classname, clazz, true);
    return clazz;
  }
Beispiel #26
0
 public void testSetExceptions() throws Exception {
   CtClass cc = sloader.get("test2.SetExceptions");
   CtMethod m = cc.getDeclaredMethod("f");
   CtClass ex = m.getExceptionTypes()[0];
   assertEquals("java.lang.Exception", ex.getName());
   m.setExceptionTypes(null);
   assertEquals(0, m.getExceptionTypes().length);
   m.setExceptionTypes(new CtClass[0]);
   assertEquals(0, m.getExceptionTypes().length);
   m.setExceptionTypes(new CtClass[] {ex});
   assertEquals(ex, m.getExceptionTypes()[0]);
 }
Beispiel #27
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);
  }
Beispiel #28
0
 public void testSuperCall() throws Exception {
   CtClass cc = sloader.get("test2.SuperCall");
   CtMethod m1 = cc.getDeclaredMethod("foo");
   m1.instrument(
       new ExprEditor() {
         public void edit(MethodCall m) throws CannotCompileException {
           m.replace("{ $_ = $proceed($$); }");
         }
       });
   cc.writeFile();
   Object obj = make(cc.getName());
   invoke(obj, "bar");
 }
Beispiel #29
0
  /** @parma classname jvm class name. */
  public CtClass lookupClass(int type, int dim, String classname) throws CompileError {
    String cname = "";
    CtClass clazz;
    if (type == CLASS) {
      clazz = lookupClassByJvmName(classname);
      if (dim > 0) cname = clazz.getName();
      else return clazz;
    } else cname = getTypeName(type);

    while (dim-- > 0) cname += "[]";

    return lookupClass(cname, false);
  }
Beispiel #30
0
 public void testRemoveCall() throws Exception {
   CtClass cc = sloader.get("test2.RemoveCall");
   CtMethod m1 = cc.getDeclaredMethod("bar");
   m1.instrument(
       new ExprEditor() {
         public void edit(MethodCall m) throws CannotCompileException {
           m.replace("{ $_ = ($r)null; }");
         }
       });
   cc.writeFile();
   Object obj = make(cc.getName());
   assertEquals(0, invoke(obj, "bar"));
 }