Ejemplo n.º 1
0
  public void testSetSuper() throws Exception {
    CtClass cc = sloader.makeClass("test2.SetSuper");
    CtClass cc2 = sloader.makeClass("test2.SetSuperParent");
    CtClass intf = sloader.makeInterface("test2.SetSuperIntf");
    CtClass remote = sloader.get("java.rmi.Remote");

    cc.setSuperclass(cc2);
    cc.setInterfaces(new CtClass[] {intf});
    intf.setSuperclass(remote);
    intf.writeFile();
    cc2.writeFile();
    cc.writeFile();

    assertEquals(cc2, cc.getSuperclass());
    assertEquals(intf, cc.getInterfaces()[0]);
    assertEquals(sloader.get("java.lang.Object"), intf.getSuperclass());
    assertEquals(remote, intf.getInterfaces()[0]);

    make(cc.getName());
  }
Ejemplo n.º 2
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();
   }
 }