0 Replies Latest reply: Aug 10, 2011 9:17 AM by Ian Clough-Oracle RSS

    Resolve full qualified type from compiler syntax tree

    Ian Clough-Oracle
      I'm writting a program to perform some analysis on arbitrary Java source code and I need to be able to resolve types that are imported. This is relatively easy if the type is explicitly imported e.g.

      import java.util.List;

      List x = null;

      then the type of x can be determined to be List.

      However if wildcard imports are used e.g.

      import java.util.*;
      import java.io.*;

      List x = nulll;

      How can I determine what the type of x actually is? I'm using the java compiler API to parse the source code:

      CompilationTask task = compiler.getTask(null, null, null, options, null, files);
      JavacTask javacTask = (JavacTask) task;

      Iterable<? extends CompilationUnitTree> tree = javacTask.parse();

      and then scanning the CompilationUnitTree for items of interest.

      I suspect I need to execute the analyze phase of the compiler but I can't see that the output of that phase is immediatly useful.

      Ian Clough