0 Replies Latest reply on Dec 9, 2012 4:26 PM by markasllen

    insertAfter yields "No such field" error

    markasllen Newbie

      I have to edit Labels that are being defined inside the constructor of a class. 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 also tried this :

       

      praxFrameConstructor.insertAfter("de.matthiasmann.twl.Label localLabel25 = new de.matthiasmann.twl.Label(new StringBuilder().append(paramsuper.õo0000().o00000(null.Ô00000)).append(\"\").toString() + \"peanuts\");\n");
      praxFrameConstructor.insertAfter("de.matthiasmann.twl.Label localLabel26 = new de.matthiasmann.twl.Label(new StringBuilder().append(paramsuper.õo0000().o00000(null.class)).append(\"\").toString() + \"peanuts\");");
      praxFrameConstructor.insertAfter("de.matthiasmann.twl.Label localLabel27 = new de.matthiasmann.twl.Label(new StringBuilder().append(paramsuper.õo0000().o00000(null.õ00000)).append(\"\").toString() + \"peanuts\");");
      praxFrameConstructor.insertAfter("de.matthiasmann.twl.Label localLabel28 = new de.matthiasmann.twl.Label(new StringBuilder().append(paramsuper.õo0000().o00000(null.ÓO0000)).append(\"\").toString() + \"peanuts\");");
      praxFrameConstructor.insertAfter("de.matthiasmann.twl.Label localLabel29 = new de.matthiasmann.twl.Label(new StringBuilder().append(paramsuper.õo0000().o00000(null.interface)).append(\"\").toString() + \"peanuts\");");
      praxFrameConstructor.insertAfter("localLabel25.setTheme(\"label-value\");");
      praxFrameConstructor.insertAfter("localLabel26.setTheme(\"label-value\");");
      praxFrameConstructor.insertAfter("localLabel27.setTheme(\"label-value\");");
      praxFrameConstructor.insertAfter("localLabel28.setTheme(\"label-value\");");
      praxFrameConstructor.insertAfter("localLabel29.setTheme(\"label-value\");");
      

       

      Which yields a "no such class: de.matthiasmann.twl.Label" error.

       

      Basically, I barely have an idea how I should achieve this.

       

      Thanks for your help.