1 Reply Latest reply on Jun 15, 2009 3:06 AM by baranowb

    Method body: introducing anonymous inner class

    baranowb

      Hi
      Is it possible to define anonymous inner class and pass parsing?
      Javassist is supposed to instrument method which in some cases must embed call into implementation of certain interface. generated method body is 100% correct, checked it with IDE (after removing $$ and other javassist magic words).

      In short body geenration looks as follows:

      if(System.getSecurityManager() == null)
      {
      // doCall
      }else
      {
      PriviledgedAction action = new PriviledgedAction(){ ... HERE GOES RUN... };
      //doAction
      }

      String returnStatement = method.getReturnType().equals(CtClass.voidType) ? "" : "return ($r)";

      String body= "...
      "if(System.getSecurityManager()==null){"
      + ""
      + returnStatement + interceptorAccess +'.'+ method.getName()+"($$);"
      +"}else"
      +"{" +
      PrivilegedExceptionAction.class.getName()+" action = new "+PrivilegedExceptionAction.class.getName()+"() {" +
      "" +
      "public "+Object.class.getName()+" run() throws "+Exception.class.getName()+" {" +
      ""
      +returnStatement + interceptorAccess +'.'+ method.getName()+"($$);" ;
      if(method.getReturnType().equals(CtClass.voidType))
      {
      body+= "return null;";
      }
      body+= "}"
      +"};"
      +"; "+returnStatement+" " +AccessController.class.getName()+".doPrivileged(action);"

      +"}"


      However thing is parsers eat some of ther chars. If definition is as above it throws exception stating that it lacks ";". When inner class is defined like:
      AccessController.doPriviledged(new PriviledgedAction(){ .. here goes run });

      exception thrown indicates lack of ")".

      Is this expected limitation?

      Relevant stack trace for first case:
      Caused by: javassist.CannotCompileException: [source error] ; is missing
      at javassist.CtBehavior.setBody(CtBehavior.java:368)
      at javassist.CtBehavior.setBody(CtBehavior.java:334)
      Caused by: compile error: ; is missing
      at javassist.compiler.Parser.parseDeclarators(Parser.java:625)
      at javassist.compiler.Parser.parseDeclarationOrExpression(Parser.java:582)
      at javassist.compiler.Parser.parseStatement(Parser.java:276)
      at javassist.compiler.Parser.parseBlock(Parser.java:288)
      at javassist.compiler.Parser.parseStatement(Parser.java:242)
      at javassist.compiler.Parser.parseIf(Parser.java:310)
      at javassist.compiler.Parser.parseStatement(Parser.java:254)
      at javassist.compiler.Parser.parseBlock(Parser.java:288)
      at javassist.compiler.Parser.parseTry(Parser.java:472)
      at javassist.compiler.Parser.parseStatement(Parser.java:262)
      at javassist.compiler.Parser.parseBlock(Parser.java:288)
      at javassist.compiler.Parser.parseStatement(Parser.java:242)
      at javassist.compiler.Javac.compileBody(Javac.java:213)
      at javassist.CtBehavior.setBody(CtBehavior.java:360)