Javassist不调用超类中定义的方法

时间:2019-02-04 09:17:00

标签: java jersey-2.0 javassist

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

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在匿名内部类方面存在一些局限性,因此代码已移至InjectorDemo类。

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)

1 个答案:

答案 0 :(得分:0)

我可以使用它。对于遇到相同问题的任何人,解决方案是在我的情况下,使用适当的参数正确调用该方法。 packages 方法在传递字符串时除了varargs参数外,我发现了如何通过javassist调用带有varargs参数的方法,我使代码可以工作。这是为我成功的窍门 https://issues.jboss.org/browse/JASSIST-192?_sscc=t