Underscore is a keyword in Java 9, use this java code to check your code

Underscore (“_”) is a keyword in Java 9. If you use “_” as an identifier, javac of JDK 8+ issues a warning. javac of JDK 9, issues an error! To check and migrate your Java code to avoid using “_” as an identifier, you can use the following Java code.

Here I have created UnderscoreScanner class which extends TreeScanner class and overrides visitVariable method as we are only interested in this node. For each variable this function will be invoked and we check if the name of the variable is ‘_’. If yes, we are printing the file’s name along with row and column numbers.

You can read more about TreeScanner class here : Using TreeScanner class to scan Java code

package jdk9;
 
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
 
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
 
import com.sun.source.tree.CompilationUnitTree;
import com.sun.source.tree.VariableTree;
import com.sun.source.util.JavacTask;
import com.sun.source.util.SourcePositions;
import com.sun.source.util.TreeScanner;
import com.sun.source.util.Trees;
 
public class TreeScannerExample {
	public static void main(String[] args) throws IOException {
		String path = "C:\\Downloads\\workspace";
		scanFiles(path);
	}
 
	private static void scanFiles(String path) {
		try {
			Files.walk(Paths.get(path))
					.filter(p -> p.toString().endsWith(".java"))
					.forEach(TreeScannerExample::scanCode);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("Total java files scanned : " +count);
 
 
	}
 
	static int count = 0;
 
	public static void scanCode(Path path) {
		count++;
 
		JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
		StandardJavaFileManager fMgr = compiler.getStandardFileManager(null,
				null, null);
		Iterable<? extends JavaFileObject> units = fMgr.getJavaFileObjects(path
				.toFile().getAbsolutePath());
		JavacTask task = (JavacTask) compiler.getTask(null, fMgr, null, null,
				null, units);
		SourcePositions sourcePositions = Trees.instance(task)
				.getSourcePositions();
		TreeScanner<Object, Object> ts = new UnderscoreScanner(sourcePositions);
 
		try {
			for (CompilationUnitTree t : task.parse()) {
				t.accept(ts, null);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
 
}
 
class UnderscoreScanner extends TreeScanner<Object, Object> {
 
	private SourcePositions sourcePositions;
	private CompilationUnitTree unitTree;
 
	public UnderscoreScanner(SourcePositions sourcePositions) {
		this.sourcePositions = sourcePositions;
	}
 
	public Object visitVariable(VariableTree t, Object o) {
		if (t.getName().toString().equals("_")) {
			long pos = sourcePositions.getStartPosition(unitTree, t);
			long col = unitTree.getLineMap().getColumnNumber(pos);
			long row = unitTree.getLineMap().getLineNumber(pos);
			System.out.println("File : " + unitTree.getSourceFile().getName()
					+ ". Row : " + row + ". Col : " + col);
		}
		return super.visitVariable(t, o);
	}
 
	public Object visitCompilationUnit(CompilationUnitTree t, Object o) {
		this.unitTree = t;
		return super.visitCompilationUnit(t, o);
	}
 
}

Uday Ogra

Connect with me at http://facebook.com/tendulkarogra and lets have some healthy discussion :)

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *