0 Replies Latest reply on Apr 10, 2008 2:47 PM by coloss

    Final fields, paramters and local variables

    coloss

      Hello,

      I am using Javassist only since a few weeks and I have tried out to create my own classes at runtime with final fields, function parameters and variables.

      The surprising thing was that i wasn't able at all to have this working (for parameters I have found out that Javassist doesn't seem to allow me to specify this kind of propriety. Am I right?).

      For local variables and fields the code compiles (thing that is not done with parameters) but the final propriety is not really applied (I am still able to change this variable many times).

      Is this a bug or am I doing something wrong?

      Compilation fails:

      CtMethod m = CtMethod.make("private void printString (final String s)
      { System.out.print(s); }",newClass);
      newClass.addMethod(m);
      


      Code that compiles but doesn't apply the final propriety (LV)
      CtMethod m = CtMethod.make("private void printString (String s) {
      final String prefix = \"> \"; prefix = \">>\";
      System.out.print(prefix + s); }",newClass);
      newClass.addMethod(m);
      


      Code that compiles but doesn't apply the final propriety (Fields)
      CtField field = new CtField(CtClass.intType, "x",newClass);
      field.setModifiers(Modifier.FINAL);
      newClass.addField(field);
      
      CtMethod m = CtMethod.make("private void setX (int x)
      { this.x = x; this.x++; }",newClass);
      newClass.addMethod(m);
      



      Thanks in advance for the answer.