1. Artem Melentyev
  2. juast

Wiki

Clone wiki

juast / Demo

Demo

Verification

Lets demonstrate simple verificators in juast.

Assume we have Verification.java:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

public class Verification {
	void goodMethod() throws Exception {
		BufferedReader br = new BufferedReader(new FileReader(new File("pom.xml")));
		try {
			System.out.println( br.readLine() );
		} finally {
			br.close();
		}
	}
	
	void badMethod() throws Exception {
		// warning: Closable not properly closed
		BufferedReader br = new BufferedReader(new FileReader(new File("pom.xml")));
		System.out.println( br.readLine() );
		br.close();
	}
	
	void outMethod() {
		// warning: println is bad practice
		System.out.println(" hi ");
	}
}

Note that #badMethod not property closes BufferedReader in finally scope as in #goodMethod.

Lets compile it with juast.jar on javac:

~/src/my/juast/package/demo $ javac -version -cp juast.jar Verification.java
javac 1.6.0_19
Note: juast: found Sun Java Compiler
Note: juast: Processing CompilationUnits
Note: juast: loading JuastProcessors via ServiceLoader
Note: juast: Run processor juast.processors.SystemOutPrintlnProcessor@1629ce8c
Verification.java:10: warning: using System.out.println is bad practice
			System.out.println( br.readLine() );
			                  ^
Verification.java:19: warning: using System.out.println is bad practice
		System.out.println( br.readLine() );
		                  ^
Verification.java:25: warning: using System.out.println is bad practice
		System.out.println(" hi ");
		                  ^
Note: juast: Run processor juast.processors.EnsureCloseProcessor@661736e
Verification.java:18: warning: Closable not properly closed
		BufferedReader br = new BufferedReader(new FileReader(new File("pom.xml")));
		               ^

Note warnings in standard javac format, generated by juast processors.

The same on Eclipse Java Compiler:

~/src/my/juast/package/demo $ ecj -version
Eclipse Java Compiler 0.972_R35x, 3.5.1 release, Copyright IBM Corp 2000, 2009. All rights reserved.
~/src/my/juast/package/demo $ ecj -source 1.6 -cp juast.jar Verification.java 
----------
1. WARNING in Verification.java (at line 10)
	System.out.println( br.readLine() );
	^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
using System.out.println is bad practice
----------
2. WARNING in Verification.java (at line 18)
	BufferedReader br = new BufferedReader(new FileReader(new File("pom.xml")));
	               ^^
Closable not properly closed
----------
3. WARNING in Verification.java (at line 19)
	System.out.println( br.readLine() );
	^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
using System.out.println is bad practice
----------
4. WARNING in Verification.java (at line 25)
	System.out.println(" hi ");
	^^^^^^^^^^^^^^^^^^^^^^^^^^
using System.out.println is bad practice
----------
...

AST Modification

Now lets demonstrate operator overloading for BigInteger/BigDecimal, implemented with juast as proof of concept. As you know, Java does not support operator overloading, but you can enable it just rewriting operators to methods in AST with juast (see juast.processors.OperatorOverload).

Assume we have Operator.java:

import java.math.BigInteger;
import static java.math.BigInteger.*;
import static java.lang.System.out;

public class Operator {
    public static void main(String[] args) {
        BigInteger
            a = valueOf(1),
            b = valueOf(2),
            c = valueOf(3),
            d1 = a.add( b.multiply(c) ),
            d2 = a + b * c;
        out.println(d1);
        out.println(d2);
    }
}

Lets compile it without juast, just to demonstrate compile error:

 ~/src/my/juast/package/demo $ javac Operator.java 
Operator.java:13: operator * cannot be applied to java.math.BigInteger,java.math.BigInteger
			d2 = a + b * c;
			           ^
Operator.java:13: operator + cannot be applied to java.math.BigInteger,<any>
			d2 = a + b * c;
			     ^
2 errors

Now compile and run it with juast:

 ~/src/my/juast/package/demo $ javac -cp juast.jar Operator.java 
Note: juast: found Sun Java Compiler
Note: juast: Processing CompilationUnits
Note: juast: loading JuastProcessors via ServiceLoader
Note: juast: Run processor juast.processors.SystemOutPrintlnProcessor@579d75ee
Note: juast: Run processor juast.processors.EnsureCloseProcessor@7b7a4989
Note: juast: Run processor juast.processors.OperatorOverload@68de462
Note: juast: found Sun Java Compiler
 ~/src/my/juast/package/demo $ java Operator 
7
7

Works as expected. "+" rewrites to a.add(), "*" rewrites to b.multiply(c).

Updated