private static <T> void mergeStagedChainInner(List<CtClass<T>> chain) {
   if (chain.size() == 1) return;
   reverse(chain);
   CtClass<T> toMerge = chain.get(0);
   for (int i = 1; i < chain.size(); i++) {
     CtClass<T> mergeInto = chain.get(i);
     replaceInstantiatedTypeParams(toMerge, mergeInto);
     toMerge
         .getAnnotations()
         .stream()
         .forEach(
             (CtAnnotation<? extends Annotation> a) -> {
               if (mergeInto.getAnnotation(a.getActualAnnotation().getClass()) == null)
                 add(mergeInto, a, mergeInto::addAnnotation);
             });
     toMerge.getSuperInterfaces().forEach(mergeInto::addSuperInterface);
     toMerge
         .getAnonymousExecutables()
         .forEach(b -> add(mergeInto, b, mergeInto::addAnonymousExecutable));
     toMerge.getNestedTypes().forEach(nt -> add(mergeInto, nt, mergeInto::addNestedType));
     toMerge.getFields().forEach(f -> add(mergeInto, f, mergeInto::addField));
     for (CtMethod<?> methodToMerge : toMerge.getMethods()) {
       processMethod(mergeInto, toMerge, methodToMerge);
     }
     final CtClass<T> finalToMerge = toMerge;
     mergeInto.getConstructors().forEach(c -> processConstructor(c, finalToMerge));
     mergeInto.setSuperclass(toMerge.getSuperclass());
     toMerge = mergeInto;
   }
 }
  public T createJavassistProxy(
      LoadBalancer loadBalance, ConcurrentMap<String, T> map, Class ifaces) throws Exception {

    Class<?>[] interfaces = ifaces.getInterfaces();

    if (interfaces.length == 1) {
      ClassPool mPool = new ClassPool(true);
      CtClass ctClass = mPool.get(interfaces[0].getName());

      // 新建代理类
      CtClass mCtc = mPool.makeClass(ifaces.getName() + "$JavassistProxy");
      mCtc.setSuperclass(ctClass);

      for (CtMethod method : ctClass.getDeclaredMethods()) {
        System.out.println(method.getName());

        //                CtMethod m = new CtMethod(method.getReturnType(),
        // ,method.getParameterTypes(), mCtc);
        //                cc.addMethod(m);
        //                m.setBody("{ x += $1; }");

        mCtc.addMethod(method);
        //                method.setBody("");

      }
      //            mCtc.debugWriteFile("/home/liguojun");

      return null;
    } else {
      return null;
    }
  }
Example #3
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());
  }
Example #4
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();
   }
 }