javassist不调用在超类中定义的方法

ep6jt1vc  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(472)

大家好,我一直在javassist库中尝试,似乎遇到了一个我无法跨越的障碍。问题是,我想动态创建一个config类来扩展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)
f45qwnt8

f45qwnt81#

对于任何有相同问题的人来说,解决方法是使用正确的参数正确地调用方法在我的例子中,packages方法在我传递字符串时排除了varargs参数,一旦我发现如何通过javassist调用带有varargs参数的方法,我就让代码正常工作了。这是为我做了诀窍的线https://issues.jboss.org/browse/jassist-192?_sscc=t

相关问题