Message-ID: <1212538672.71.1369149546147.JavaMail.firstname.lastname@example.org> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_70_347742937.1369149546146" ------=_Part_70_347742937.1369149546146 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
The Jav= aSourceClassLoader extends JavaTM's java.lang.ClassLoader cl= ass with the ability to load classes directly from source code.
To be precise, if a class is loaded through this ClassLoader, it searche= s for a matching ".java" file in any of the directories speci= fied by a given "source path", reads, scans, parses and compiles = it and defines the resulting classes in the JVM. As necessary, more class= es are loaded through the parent class loader and/or through the source p= ath. No intermediate files are created in the file system.
If the JavaTM source is not available in files, but from some= other storage (database, main memory, ...), you may specify a custom Resour= ceFinder instead of the directory-based source path.
If you have many source files and you want to reduce the compilation tim= e, you may want to use the CachingJavaSourceClassLoader, which uses a = cache provided by the application to store class files for repeated use.<= /p>
A BASH shell script named "bin/janino" is provided that wraps = the JavaSo= urceClassLoader in a JAVAC-like command line interface:
The Compiler cla= ss mimics the behavior of SUN's javac tool. It compiles a set of "co= mpilation units" (i.e. JavaTM source files) into a set of c= lass files.
Using the "-warn" option, Janino spits out some probably very = interesting warnings which may help you to "clean up" the source = code.
The BASH script "bin/janinoc" implements a drop-in replacement= for SUN's JAVAC utility:
You can plug JANINO into the ANT utility through the AntCompilerAdapter clas= s. Just make sure that janino.jar is on the class path, then run ANT with= the following command-line option:
If you want to use JANINO with TOMCAT, just copy the "janino.jar&qu= ot; file into TOMCAT's "common/lib" directory, and add the foll= wing init parameter section to the JSP servlet definition in TOMCAT's "= ;conf/web.xml" file:
Apart from compiling JavaTM code, JANINO can be used for stat= ic code analysis: Based on the AST ("abstract syntax tree") produ= ced by the parser, the Traverser walks through all nodes of the AST, and derived classes= can do all kinds of analyses on them, e.g. count declarations:
This is the basis for all these neat code metrics and style checking.
If, e.g., you want to read a JavaTM compilation unit into mem= ory, manipulate it, and then write it back to a file for compilation, then = all you have to do is:
The UnparseVisitor (http://dist.codehaus.org/janino/javadoc/org/codehaus= /janino/UnparseVisitor.html#main%28java.lang.String\[\]%29) class demostrat= es how to do this.
JANINO can be configured to use not its own Java=E2=84=A2 compiler, but =
an alternative implementation. Alternative implementations must basically i=
mplement the interface ICompilerFactory. One such alternative implementation=
is based on the javax.too=
ls API (available since JDK 1.6), and is shipped as part of the JANINO =
Basically there are two ways to switch implementations:
org.codehaus.commons.compiler.jdk.ExpressionEvaluator and consorts instead of
janino.jar=on your compile-time and runtime classpath. (
commons-compiler= .jarmust always be on the classpath, because it co= ntains the basic classes that every implementation require= s.)
org.codehaus.commons.compiler.CompilerFactoryFactory.getDe= faultFactory().newExpressionEvaluator()and compile only against
commons-compiler-jdk.jar) to the class path, and <= code>getDefaultFactory() will find it at runtime.= li>