Skip to content

Commit

Permalink
Fixed a bug in which compiling files that contain multiple top-level …
Browse files Browse the repository at this point in the history
…classes causes NullPointerExceptions when accessing symbol information.
  • Loading branch information
Eddie Aftandilian committed Mar 2, 2012
1 parent eeef918 commit 66f9b5b
Showing 1 changed file with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@

package com.google.errorprone;

import com.sun.source.tree.CompilationUnitTree;
import com.sun.source.tree.Tree;
import com.sun.source.util.TreePath;
import com.sun.source.util.TreePathScanner;
import com.sun.tools.javac.comp.AttrContext;
import com.sun.tools.javac.comp.Env;
import com.sun.tools.javac.main.JavaCompiler;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.Context.Factory;

import java.util.LinkedList;
import java.util.Queue;

/**
Expand Down Expand Up @@ -74,6 +78,33 @@ public void postFlow(Env<AttrContext> env) {
: env.toplevel.sourcefile);
VisitorState visitorState = new VisitorState(context, logReporter);
Scanner scanner = (Scanner) context.get(TreePathScanner.class);
scanner.scan(env.toplevel, visitorState);

/* Each env corresponds to a parse tree (I *think* always a top-level
* class), not necessarily to a file as we had previously thought. Here we
* walk up the env to generate a TreePath for this parse tree, then scan it.
*
* TODO(eaftan): Note that because we are scanning parse trees and not
* compilation units, we can never encounter file-level tree nodes, such as
* import statements. Problem?
*/

// create list of Tree nodes from current to top
LinkedList<Tree> pathList = new LinkedList<Tree>();
Env<AttrContext> envForPath = env;
pathList.addFirst(envForPath.tree);
while (envForPath.outer != null) {
envForPath = envForPath.outer;
pathList.addFirst(envForPath.tree);
}

// generate TreePath based on list
if (!(pathList.element() instanceof CompilationUnitTree)) {
throw new IllegalStateException("Expected top of tree to be a compilation unit");
}
TreePath path = new TreePath((CompilationUnitTree) pathList.remove());
for (Tree t : pathList) {
path = new TreePath(path, t);
}
scanner.scan(path, visitorState);
}
}

0 comments on commit 66f9b5b

Please sign in to comment.