background preloader

Javac

Facebook Twitter

Java 6.0 Articles,Books,Tutorials,SourceCode and Example Program. Compiler Tree API. A. Sundararajan's Weblog. I wrote about javac's APIs in my last blog entry. But, I did not mention an example for Javac's Tree API. How about a simple example that uses javac's tree API? Here it is... In the following example, we implement a checker for empty catch blocks (because "eating exceptions silently" is considered bad) and warn the user. We implement EmptyCatchChecker as a JSR-269 processor. Update: I had missed HTML escaping < characters in source code [for generics] and so the code below was not compiling. To compile the above code, you can use the following command: javac -classpath $JDK_HOME/lib/tools.jar EmptyCatchChecker.java To use this empty catch checker, you can use the following command: javac -processor EmptyCatchChecker YourClass.java Note that the above checker can be fooled by adding a simple ';' in catch block - because ';' is a statement.

[Hint: EmptyStatementTree] Erni08b. Source Code Analysis Using Java 6 APIs. Have you ever thought of how tools like " or " perform a static code analysis, or how Integrated Development Environments (IDEs) like NetBeans or " execute quick code fixes or find the exact references of a field declared in your code? In many cases, IDEs have their own APIs to parse the source code and generate a standard tree structure, called an Abstract Syntax Tree (AST) or "parse tree," which can be used for deeper analysis of the source elements. The good news is that it is now possible to accomplish the said tasks plus a lot more with the help of three new APIs introduced in Java as part of the Java Standard Edition 6 release. The APIs that might be of interest to developers of Java applications that need to perform source code analysis are the " Compiler API (JSR 199), the "

Com.sun.tools.javac.util: public static interface: JCDiagnostic.DiagnosticPosition. Generating java byte code by building AST trees - Yang Jiang. There are several ways to generate byte code to run on JVM. You can write a .java file then compile it with javac, or write ASM similar code directly then compile it with tools like JASML, or with tools like BCEL it is even possible to generate your own class in runtime. Aside from these, a quite interesting approach is to construct AST nodes representing the structure of the java code, then generate byte code from that. Actually, this is what the javac does. When javac compiles .java files into .class files, there is a two step process involved. These two steps are quite independent from each other, which makes it possible to replace either of the two without affecting the other. The javac source is located on the OpenJDK langtools repository, which hosts a series of tools like javadoc, javah etc..

After getting the code, let's try to make a very simple javac tree for this file [Test.java] public class Test{ public static void main(String[] args){ System.out.println("Hello! ")