JBoss Community

insertAfter yields "No such field" error

created by markasllen in Javassist - View the full discussion

I have to edit Labels that are being defined inside the constructor of a class.

Because those labels use getters and setters for the text, I decided to redefine them at the end of the constructor.

 

public class StaticPraxMMod {
 
    private static final String PRAXFRAME = "com.praxemu.client.graphics.widgets.PraxFrame";
 
    public static void main(String[] args) throws Exception {    
        ClassPool classPool = ClassPool.getDefault();
        addEVLink(classPool);
 
    }
 
    private static void addEVLink(ClassPool classPool) throws NotFoundException, CannotCompileException, IOException {
        CtClass praxFrameClass = classPool.get(PRAXFRAME);
        CtConstructor praxFrameConstructor = praxFrameClass.getConstructors()[0]/*(new CtClass[] {})*/;
        //praxFrameClass.addConstructor(praxFrameConstructor);
 
        praxFrameConstructor.insertAfter("localLabel25 = new de.matthiasmann.twl.Label(new StringBuilder().append(paramsuper.õo0000().o00000(null.Ô00000)).append(\"\").toString() + \"peanuts\");\n" 
                "localLabel26 = new de.matthiasmann.twl.Label(new StringBuilder().append(paramsuper.õo0000().o00000(null.class)).append(\"\").toString() + \"peanuts\");\n" +
                "localLabel27 = new de.matthiasmann.twl.Label(new StringBuilder().append(paramsuper.õo0000().o00000(null.õ00000)).append(\"\").toString() + \"peanuts\");\n" +
                "localLabel28 = new de.matthiasmann.twl.Label(new StringBuilder().append(paramsuper.õo0000().o00000(null.ÓO0000)).append(\"\").toString() + \"peanuts\");\n" +
                "localLabel29 = new de.matthiasmann.twl.Label(new StringBuilder().append(paramsuper.õo0000().o00000(null.interface)).append(\"\").toString() + \"peanuts\");\n" +
                "localLabel25.setTheme(\"label-value\");\n" +
                "localLabel26.setTheme(\"label-value\");\n" +
                "localLabel27.setTheme(\"label-value\");\n" +
                "localLabel28.setTheme(\"label-value\");\n" +
                "localLabel29.setTheme(\"label-value\");");
        //praxFrameClass.toBytecode();
        praxFrameClass.writeFile(".");
 
    }
}

 

 

When running it, I get this :

Exception in thread "main" javassist.CannotCompileException: [source error] no such field: localLabel25
    at javassist.CtBehavior.insertAfter(CtBehavior.java:873)
    at javassist.CtBehavior.insertAfter(CtBehavior.java:788)
    at StaticPraxMMod.addEVLink(StaticPraxMMod.java:27)
    at StaticPraxMMod.main(StaticPraxMMod.java:18)
Caused by: compile error: no such field: localLabel25
    at javassist.compiler.TypeChecker.fieldAccess(TypeChecker.java:819)
    at javassist.compiler.TypeChecker.atFieldAssign(TypeChecker.java:271)
    at javassist.compiler.JvstTypeChecker.atFieldAssign(JvstTypeChecker.java:84)
    at javassist.compiler.TypeChecker.atAssignExpr(TypeChecker.java:230)
    at javassist.compiler.ast.AssignExpr.accept(AssignExpr.java:39)
    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.Javac.compileStmnt(Javac.java:569)
    at javassist.CtBehavior.insertAfterAdvice(CtBehavior.java:888)
    at javassist.CtBehavior.insertAfter(CtBehavior.java:847)

 

 

I tried adding the full package path to labels but it doesn't change a thing.
I had the same issue with Label when I did "de.matthiasmann.twl.Label localLabel25 = new de.matthiasmann.twl.Label[...]" with and without the full package path.

 

I would also like to know if injecting something like

localLabel25.setText(localLabel25.getText().append(\" peanuts\"));

would work.

 

Thanks for your help.

Reply to this message by going to Community

Start a new discussion in Javassist at Community