Helping to find the usefulness of a proposal

Michael Nascimento misterm at gmail.com
Thu Apr 2 06:34:05 PDT 2009


Using JDK 6, you can write an annotation processor for all sources
that uses the Tree API for that.

Regards,
Michael Nascimento Santos
https://genesis.dev.java.net/
https://jsr-310.dev.java.net/

On Thu, Apr 2, 2009 at 10:25 AM, Mark Mahieu <markmahieu at googlemail.com> wrote:
> 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