Helping to find the usefulness of a proposal

Mark Mahieu markmahieu at googlemail.com
Thu Apr 2 06:25:15 PDT 2009


Wow.

http://www.geekherocomic.com/comics-highres/2009-02-25-coding-overkill.png


Here are the important bits from my quick hack.  Took under half an hour to
get it finding cases where Auto-assignment Parameters would and wouldn't
work across large source trees.

Of course, javac makes much more than parsing available if you need it...



import com.sun.tools.javac.file.JavacFileManager;
import com.sun.tools.javac.parser.JavacParser;
import com.sun.tools.javac.parser.ParserFactory;
import com.sun.tools.javac.tree.TreeScanner;
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.List;

...

// create a TreeScanner to do your analysis
TreeScanner scanner = new TreeScanner() {

@Override
public void visitMethodDef(JCMethodDecl tree) {
 // look at a method's parameters
for (List<JCVariableDecl> l = params; l.nonEmpty(); l = l.tail) {
JCVariableDecl param = l.head;
 // examine param or whatever
}
 // continue recursively scanning the method body etc
super.visitMethodDef(tree);
}
};

...

Context context = new Context();
JavacFileManager.preRegister(context);
ParserFactory factory = ParserFactory.instance(context);

String sourceCode = readSourceFromFile(...);

JavacParser parser = (JavacParser) factory.newParser(sourceCode, false,
false, false);
JCCompilationUnit cu = parser.parseCompilationUnit();

// do your analysis
scanner.scan(cu);



More information about the coin-dev mailing list