Exemplo n.º 1
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"));
  }
Exemplo n.º 2
0
 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"));
 }
Exemplo n.º 3
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"));
 }
Exemplo n.º 4
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"));
 }
Exemplo n.º 5
0
  private void changeMethod(CtClass ctClass, CtMethod ctMethod) throws CannotCompileException {
    // basically your before-advice...
    ctMethod.insertBefore("System.out.println(\"started method at \" + new java.util.Date());");
    // basically your after-advice...
    ctMethod.insertAfter("System.out.println(\"ended method at \" + new java.util.Date());");

    // basically your around-advice...
    String methodName = ctMethod.getName();
    String proxyName = methodName + "_$proxy";
    CtMethod proxy = CtNewMethod.copy(ctMethod, proxyName, ctClass, null);
    ctMethod.setName(ctMethod.getName() + "_orig");
    proxy.setBody(
        "{ System.out.println(\"hoot!\"); return $proceed($$);}", "this", ctMethod.getName());
    proxy.setName(methodName);
    ctClass.addMethod(proxy);
  }
Exemplo n.º 6
0
  /** Init the plugin from start method. */
  @Transform(classNameRegexp = "org.apache.catalina.loader.WebappLoader")
  public static void patchWebappLoader(CtClass ctClass)
      throws NotFoundException, CannotCompileException, ClassNotFoundException {

    try {
      CtMethod startInternalMethod = ctClass.getDeclaredMethod("startInternal");
      // init the plugin
      String src = PluginManagerInvoker.buildInitializePlugin(TomcatPlugin.class, "classLoader");

      startInternalMethod.insertAfter(src);
    } catch (NotFoundException e) {
      LOGGER.warning(
          "org.apache.catalina.loader.WebappLoader does not contain startInternal method. Tomcat plugin will be disabled.\n"
              + "*** This is Ok, Tomcat plugin handles only special properties ***");
      return;
    }
  }
Exemplo n.º 7
0
 public void testArrayAndNull() throws Exception {
   CtClass cc = sloader.get("test2.ArrayAndNull");
   CtMethod m = cc.getDeclaredMethod("test");
   m.insertAfter("if ($_ == null) $_ = new int[0];");
 }
Exemplo n.º 8
0
 public void testInvalidCastWithDollar() throws Exception {
   String code = "{ new test5.JavassistInvalidCastTest().inspectReturn((Object) ($w) $_); } ";
   CtClass c = sloader.get("test5.InvalidCastDollar");
   for (CtMethod method : c.getDeclaredMethods()) method.insertAfter(code);
 }