Message-ID: <1072304293.397.1419002427830.JavaMail.firstname.lastname@example.org> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_396_1661370541.1419002427829" ------=_Part_396_1661370541.1419002427829 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
The JavaSourceClassLo= ader extends JavaTM's java.lang.ClassLoader class with the a= bility 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 specified= by a given "source path", reads, scans, parses and compiles it a= nd defines the resulting classes in the JVM. As necessary, more classes are= loaded through the parent class loader and/or through the source path. 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 ResourceFinder ins= tead 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.
A BASH shell script named "bin/janino" is provided that wraps = the JavaSourceClassLoade= r in a JAVAC-like command line interface:
The Compiler class mimics the = behavior of SUN's javac tool. It compiles a set of "compilation units&= quot; (i.e. JavaTM source files) into a set of class 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:
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 follwi= ng init parameter section to the JSP servlet definition in TOMCAT's "c= onf/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 Traverse= r walks through all nodes of the AST, and derived classes can do all ki= nds 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 class demostrates how to do t= his.
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 t=
he javax.tools API (av=
ailable since JDK 1.6), and is shipped as part of the JANINO distribution: =
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.ja= rmust always be on the classpath, because it conta= ins the basic classes that every implementation requires.)=
org.codehaus.commons.compiler.CompilerFactoryFactory.getDefau= ltFactory().newExpressionEvaluator()and compile only against
= commons-compiler.jar(and no concrete implementatio= n). At runtime, add one implementation (
janino.jar= code> orwill find it at runtime.
commons-compiler-jdk.jar) to the class path, and