Javassist Vs. Java Compiler API
In a project I'm currently working on, I need to generate Java classes at runtime. I also need to avoid using reflection when using these classes later on.
I've been search for current solutions to do this, and found Javassist and Java 6 Java Compiler API.
I'm confused though:
What does Javassist uses to generate classes? Does it uses reflection or something?
I've coded some tests and found it pretty easy to generate bytecode from source code, and then load classes from the generated bytecode. What are the advanta开发者_开发百科ges of using Javassist over this solution?
Javassist has several options, one being a small included compiler allowing you to convert Java snippets to byte code, making it easy to insert e.g. a "System.out.println(....)" or "log.debug(...)" statement in existing classes.
I wrote an article on this a while back, which shows how it can be done. See http://blogs.oracle.com/CoreJavaTechTips/entry/add_logging_at_class_load. Note: For a production setting this approach should not be used - then you should consider using Aspect Oriented Programming in your application.
精彩评论