示例#1
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());
 }
示例#2
0
 private static boolean checkOnlyDefaultConstructor(CtClass<?> ctClass) {
   Set<? extends CtConstructor<?>> constructors = ctClass.getConstructors();
   if (constructors.size() > 1) return false;
   if (constructors.isEmpty()) return true;
   CtConstructor<?> constructor = constructors.iterator().next();
   return constructor.isImplicit();
 }
示例#3
0
 public void testWhere() throws Exception {
   CtClass cc = sloader.get("test2.Where");
   CtConstructor cons = cc.getClassInitializer();
   cons.instrument(
       new ExprEditor() {
         public void edit(MethodCall m) throws CannotCompileException {
           System.out.println(m.where().getName());
         }
       });
 }
示例#4
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"));
 }
示例#5
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);
  }
示例#6
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();
   }
 }
示例#7
0
  private static <T> void processConstructor(CtConstructor<T> c, CtClass<T> toMerge) {
    CtStatement firstStmt = c.getBody().getStatements().get(0);
    if (firstStmt instanceof CtInvocation) {
      CtInvocation<?> superConstructorCall = (CtInvocation) firstStmt;
      if (!(superConstructorCall.getExecutable().getDeclaration() instanceof CtConstructor)) return;
      CtConstructor superConstructor =
          (CtConstructor) superConstructorCall.getExecutable().getDeclaration();
      if (superConstructor.getDeclaringType() == toMerge) {
        CtBlock superConstructorBody = c.getFactory().Core().clone(superConstructor.getBody());
        superConstructorBody.accept(
            new CtScanner() {
              @Override
              public <T> void visitCtParameterReference(CtParameterReference<T> ref) {
                int parameterOrder = superConstructor.getParameters().indexOf(ref.getDeclaration());
                ref.setDeclaringExecutable(c.getReference());
                CtExpression<?> arg = superConstructorCall.getArguments().get(parameterOrder);
                if (!(arg instanceof CtVariableAccess))
                  throw sgce("super() should be directly called in " + c);
                CtVariable param = ((CtVariableAccess) arg).getVariable().getDeclaration();
                if (!(param instanceof CtParameter))
                  throw sgce("super() should be directly called in " + c);
                ref.setSimpleName(param.getSimpleName());

                super.visitCtParameterReference(ref);
              }
            });
        c.getBody().removeStatement(firstStmt);
        List<CtStatement> superConstructorBodyStatements = superConstructorBody.getStatements();
        for (int i = superConstructorBodyStatements.size() - 1; i >= 0; i--) {
          c.getBody().insertBegin(superConstructorBodyStatements.get(i));
        }
      }
    }
  }