[jboss-user] [Javassist] - Generating a main method
Rafael Nagure
do-not-reply at jboss.com
Sun Jun 6 20:07:16 EDT 2010
Rafael Nagure [http://community.jboss.org/people/nagure] created the discussion
"Generating a main method"
To view the discussion, visit: http://community.jboss.org/message/546375#546375
--------------------------------------------------------------
Hi there, folks. Sorry if this is a dumb question, but I couldn't find any good explanation so far.
I have the following code, a plain and simple *HelloWorld*:
ClassPool pool = ClassPool.getDefault();
CtClass cc = pool.makeClass("HelloWorld");
CtMethod mt = CtNewMethod.make("public static void main(String[] args) { System.out.println(\"Hello World!\"); }", cc);
cc.addMethod(mt);
cc.writeFile();
As you see, there's a main method in this new generated class. So far, so good. It works like a charm.
I'd like to create this very same method using the other way:
CtClass cc = pool.makeClass("HelloWorld");
CtMethod m = new CtMethod(CtClass.voidType, "main", new CtClass[] { } , cc);
m.setModifiers(Modifier.PUBLIC & Modifier.STATIC);
cc.addMethod(m);
m.setBody("{ System.out.println(\"Hello World!\"); }");
cc.setModifiers(cc.getModifiers() & ~Modifier.ABSTRACT);
cc.writeFile();
Unfortunately, this code fails. I couldn't figure out how I'm supposed to handle String args[] with CtClass.TYPE, as it seems there are only primitive types.
Thanks a lot!
--------------------------------------------------------------
Reply to this message by going to Community
[http://community.jboss.org/message/546375#546375]
Start a new discussion in Javassist at Community
[http://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2062]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/jboss-user/attachments/20100606/cfcc8880/attachment.html
More information about the jboss-user
mailing list