[jboss-user] [Javassist user questions] - Method body: introducing anonymous inner class

baranowb do-not-reply at jboss.com
Mon Jun 15 02:48:58 EDT 2009

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
   PriviledgedAction action = new PriviledgedAction(){  ... HERE GOES RUN... };

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

String body= "...
			+		""
			+ 			returnStatement + interceptorAccess +'.'+ method.getName()+"($$);" 
					+"{" +
							PrivilegedExceptionAction.class.getName()+" action = new  "+PrivilegedExceptionAction.class.getName()+"() {" +
								"" +
								"public "+Object.class.getName()+" run() throws "+Exception.class.getName()+" {" +
								+returnStatement + interceptorAccess +'.'+ method.getName()+"($$);" ;
									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)

View the original post : http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4237541#4237541

Reply to the post : http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=4237541

More information about the jboss-user mailing list