Javassist 不调用 super class 中定义的方法

Javassist not invoking method defined in super class

大家好,我一直在尝试使用 javassist 库,但似乎遇到了一个我无法跨越的障碍。问题是我想动态创建一个配置 class 来扩展 jersey 的 ResourceConfig class 并且在构造函数中我想注册要注入的包和工厂对象。但每次我得到错误(在下面发布)

public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        ClassPool pool = ClassPool.getDefault();

        CtClass superCc = null;
        try {
            superCc = pool.get(ResourceConfig.class.getCanonicalName());

        } catch (NotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            CtClass cc = pool.makeClass("org.demonking.AppConfig");
            cc.setSuperclass(superCc);
            System.out.println(cc.getSuperclass().getName());
            CtConstructor constructor = new CtConstructor(new CtClass[0], cc);

            constructor.setBody("{\n"+"packages(\"org.demonking\");"+"\n"+"register(InjectorDemo.class);"+"\n"+"}");


            cc.addConstructor(constructor);

        } catch (CannotCompileException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
//rest of my jersey code
}

我想动态创建的原始 AppConfig(工作正常)

public class AppConfig extends ResourceConfig{

    public AppConfig()
    {
        packages("org.demonking");
        register(new AbstractBinder(){
            @Override
            protected void configure() {
                bindFactory(ContextHelperFactory.class)
                        .to(ContextHelper.class)
                        .in(RequestScoped.class);
            } 
        });
    }

请注意,在研究时我发现 javassist 对匿名内部 classes 有一些限制,因此代码已转移到 InjectorDemo class.

public class InjectorDemo extends AbstractBinder {

    @Override
    protected void configure() {
        // TODO Auto-generated method stub
             bindFactory(ContextHelperFactory.class)
                     .to(ContextHelper.class)
                     .in(RequestScoped.class);
    }
}

错误

javassist.CannotCompileException: [source error] packages(java.lang.String) not found in org.demonking.AppConfig
    at javassist.CtBehavior.setBody(CtBehavior.java:446)
    at javassist.CtBehavior.setBody(CtBehavior.java:412)
    at javassist.CtConstructor.setBody(CtConstructor.java:217)
    at org.logicunit.JavassistDemoMain.main(JavassistDemoMain.java:42)
Caused by: compile error: packages(java.lang.String) not found in org.demonking.AppConfig
    at javassist.compiler.TypeChecker.atMethodCallCore(TypeChecker.java:749)
    at javassist.compiler.TypeChecker.atCallExpr(TypeChecker.java:695)
    at javassist.compiler.JvstTypeChecker.atCallExpr(JvstTypeChecker.java:157)
    at javassist.compiler.ast.CallExpr.accept(CallExpr.java:46)
    at javassist.compiler.CodeGen.doTypeCheck(CodeGen.java:242)
    at javassist.compiler.CodeGen.atStmnt(CodeGen.java:330)
    at javassist.compiler.ast.Stmnt.accept(Stmnt.java:50)
    at javassist.compiler.CodeGen.atStmnt(CodeGen.java:351)
    at javassist.compiler.ast.Stmnt.accept(Stmnt.java:50)
    at javassist.compiler.CodeGen.atMethodBody(CodeGen.java:292)
    at javassist.compiler.Javac.compileBody(Javac.java:223)
    at javassist.CtBehavior.setBody(CtBehavior.java:438)

我把它发给了 work.for 遇到同样问题的人解决方案是使用正确的参数正确调用方法在我的例子中 packages 方法除了可变参数当我传递字符串时,一旦我发现如何通过 javassist 调用带有 varargs 参数的方法,我就可以使用代码。这是对我有用的线程 https://issues.jboss.org/browse/JASSIST-192?_sscc=t