Beispiel #1
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 #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 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 #4
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 #5
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 #6
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 #7
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"));
 }
Beispiel #8
0
  public void testNewExprInTry() throws Exception {
    ExprEditor ed =
        new ExprEditor() {
          public void edit(NewExpr expr) throws CannotCompileException {
            expr.replace("$_ = new test2.HashMapWrapper($1, 1);");
          }
        };

    CtClass cc = sloader.get("test2.NewExprInTry");
    CtMethod m1 = cc.getDeclaredMethod("foo");
    m1.instrument(ed);
    cc.writeFile();
    Object obj = make(cc.getName());
    assertEquals(1, invoke(obj, "run"));
  }
Beispiel #9
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();
   }
 }
Beispiel #10
0
  public void testMethodCall() throws Exception {
    CtClass cc = sloader.get("test2.MethodCall");

    CtMethod m1 = cc.getDeclaredMethod("bar");
    m1.instrument(
        new ExprEditor() {
          public void edit(MethodCall m) throws CannotCompileException {
            if ("clone".equals(m.getMethodName())) methodCallData = m.getClassName();
          }
        });

    cc.writeFile();
    assertEquals("java.lang.String[]", methodCallData);

    assertEquals("java.lang.String[]", sloader.get("[Ljava/lang/String;").getName());
    assertEquals("int[][]", sloader.get("[[I").getName());
  }
Beispiel #11
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"));
  }
Beispiel #12
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 #13
0
  public void testNewExprTry() throws Exception {
    ExprEditor ed =
        new ExprEditor() {
          public void edit(NewExpr expr) throws CannotCompileException {
            StringBuffer code = new StringBuffer(300);
            code.append("{ try ");
            code.append("{ $_ = $proceed($$); }");
            code.append("catch (OutOfMemoryError e) {}}");
            expr.replace(code.toString());
          }
        };

    CtClass cc = sloader.get("test2.NewExprTry");
    CtMethod m1 = cc.getDeclaredMethod("foo");
    m1.instrument(ed);
    cc.writeFile();
    Object obj = make(cc.getName());
    assertEquals(16, invoke(obj, "run"));
  }
Beispiel #14
0
 private void preprocessMethods(CtClass cc, boolean insertLoad, boolean wrapFieldAccess)
     throws CannotCompileException {
   CtMethod[] methods = cc.getDeclaredMethods();
   for (int i = 0; i < methods.length; i++) {
     CtMethod m = methods[i];
     if (wrapFieldAccess) {
       m.instrument(
           new ExprEditor() {
             public void edit(FieldAccess fa) throws CannotCompileException {
               try {
                 if ((fa.getField().getModifiers() & (Modifier.TRANSIENT | Modifier.STATIC)) == 0
                     && fa.getField().getDeclaringClass().subtypeOf(persistentInterface)) {
                   if (fa.isWriter()) {
                     fa.replace("{ $0.loadAndModify(); $proceed($$); }");
                   }
                   // isSelfReader is my extension of JAssist, if you
                   // use original version of JAssist comment the
                   // branch below or replace "else if" with "else".
                   // In first case Perst will not be able to handle
                   // access to foreign (non-this) fields. You should use
                   // getter/setter methods instead.
                   // In second case access to foreign fields still will be possible,
                   // but with significant degradation of performance and
                   // increased code size, because in this case before ALL access
                   // to fields of persistent capable object call of load() method
                   // will be inserted.
                   else if (!fa.isSelfReader()) {
                     fa.replace("{ $0.load(); $_ = $proceed($$); }");
                   }
                 }
               } catch (NotFoundException x) {
               }
             }
           });
     }
     if (insertLoad
         && !"recursiveLoading".equals(m.getName())
         && (m.getModifiers() & (Modifier.STATIC | Modifier.ABSTRACT)) == 0) {
       m.insertBefore("load();");
     }
   }
 }
Beispiel #15
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();
  }
Beispiel #16
0
  public void testInsertAt() throws Exception {
    CtClass cc = sloader.get("test2.InsertAt");
    CtMethod m1 = cc.getDeclaredMethod("foo");
    int line = 6;
    int ln = m1.insertAt(line, false, null);
    int ln2 = m1.insertAt(line, "counter++;");
    assertEquals(ln, ln2);
    assertEquals(7, ln2);

    line = 8;
    ln = m1.insertAt(line, false, null);
    ln2 = m1.insertAt(line, "counter++;");
    assertEquals(ln, ln2);
    assertEquals(8, ln2);

    CtMethod m2 = cc.getDeclaredMethod("bar2");
    int ln3 = m2.insertAt(20, "{ int m = 13; j += m; }");
    assertEquals(20, ln3);

    cc.writeFile();
    Object obj = make(cc.getName());
    assertEquals(7, invoke(obj, "foo"));
    assertEquals(25, invoke(obj, "bar"));
  }
Beispiel #17
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;
  }
Beispiel #18
0
 public void testArrayAndNull() throws Exception {
   CtClass cc = sloader.get("test2.ArrayAndNull");
   CtMethod m = cc.getDeclaredMethod("test");
   m.insertAfter("if ($_ == null) $_ = new int[0];");
 }