Skip to content

Commit

Permalink
Fix NPE in testBug424138_001 and others; bad synchronized block
Browse files Browse the repository at this point in the history
Signed-off-by: Rob Stryker <[email protected]>
  • Loading branch information
Rob Stryker authored and robstryker committed Dec 11, 2024
1 parent 07e9fa1 commit 3fdb0c2
Showing 1 changed file with 18 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,8 @@
import com.sun.tools.javac.api.JavacTaskImpl;
import com.sun.tools.javac.api.JavacTrees;
import com.sun.tools.javac.code.Attribute;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Symtab;
import com.sun.tools.javac.code.TypeTag;
import com.sun.tools.javac.code.Types;
import com.sun.tools.javac.code.Attribute.Compound;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Symbol.ClassSymbol;
import com.sun.tools.javac.code.Symbol.MethodSymbol;
import com.sun.tools.javac.code.Symbol.ModuleSymbol;
Expand All @@ -60,6 +57,7 @@
import com.sun.tools.javac.code.Symbol.TypeSymbol;
import com.sun.tools.javac.code.Symbol.TypeVariableSymbol;
import com.sun.tools.javac.code.Symbol.VarSymbol;
import com.sun.tools.javac.code.Symtab;
import com.sun.tools.javac.code.Type.ArrayType;
import com.sun.tools.javac.code.Type.ClassType;
import com.sun.tools.javac.code.Type.ErrorType;
Expand All @@ -71,8 +69,9 @@
import com.sun.tools.javac.code.Type.ModuleType;
import com.sun.tools.javac.code.Type.PackageType;
import com.sun.tools.javac.code.Type.TypeVar;
import com.sun.tools.javac.code.TypeTag;
import com.sun.tools.javac.code.Types;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.TreeInfo;
import com.sun.tools.javac.tree.JCTree.JCAnnotatedType;
import com.sun.tools.javac.tree.JCTree.JCAnnotation;
import com.sun.tools.javac.tree.JCTree.JCArrayTypeTree;
Expand All @@ -96,6 +95,7 @@
import com.sun.tools.javac.tree.JCTree.JCTypeParameter;
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
import com.sun.tools.javac.tree.JCTree.JCWildcard;
import com.sun.tools.javac.tree.TreeInfo;
import com.sun.tools.javac.util.Context;

/**
Expand All @@ -104,7 +104,7 @@
*/
public class JavacBindingResolver extends BindingResolver {

private JavacTask javac; // TODO evaluate memory cost of storing the instance
private JavacTask javacTask; // TODO evaluate memory cost of storing the instance
// it will probably be better to run the `Enter` and then only extract interesting
// date from it.
public final Context context;
Expand Down Expand Up @@ -427,7 +427,7 @@ public IBinding getBinding(String key) {
private List<JCCompilationUnit> javacCompilationUnits;

public JavacBindingResolver(IJavaProject javaProject, JavacTask javacTask, Context context, JavacConverter converter, WorkingCopyOwner owner, List<JCCompilationUnit> javacCompilationUnits) {
this.javac = javacTask;
this.javacTask = javacTask;
this.context = context;
this.javaProject = javaProject;
this.converter = converter;
Expand All @@ -440,14 +440,21 @@ private void resolve() {
// already done and ready
return;
}
synchronized (this.javac) { // prevents from multiple `analyze` for the same task
final JavacTask tmpTask = this.javacTask;
if( tmpTask == null ) {
return;
}
synchronized (tmpTask) { // prevents from multiple `analyze` for the same task
if( this.javacTask == null ) {
return;
}
boolean alreadyAnalyzed = this.converter.domToJavac.values().stream().map(TreeInfo::symbolFor).anyMatch(Objects::nonNull);
if (!alreadyAnalyzed) {
// symbols not already present: analyze
try {
Iterable<? extends Element> elements;
// long start = System.currentTimeMillis();
if (this.javac instanceof JavacTaskImpl javacTaskImpl) {
if (this.javacTask instanceof JavacTaskImpl javacTaskImpl) {
if (javacCompilationUnits != null && !javacCompilationUnits.isEmpty()) {
Iterable<? extends CompilationUnitTree> trees = javacCompilationUnits;
elements = javacTaskImpl.enter(trees);
Expand All @@ -462,7 +469,7 @@ private void resolve() {
// ILog.get().info("enter/analyze elements=" + count + ", took: "
// + (System.currentTimeMillis() - start) + ", first=" + name);
} else {
elements = this.javac.analyze();
elements = this.javacTask.analyze();
// long count = StreamSupport.stream(elements.spliterator(), false).count();
// String name = elements.iterator().hasNext()
// ? elements.iterator().next().getSimpleName().toString()
Expand All @@ -477,7 +484,7 @@ private void resolve() {
// some cleanups to encourage garbage collection
JavacCompilationUnitResolver.cleanup(context);
}
this.javac = null;
this.javacTask = null;
synchronized (this) {
if (this.symbolToDeclaration == null) {
Map<Symbol, ASTNode> wipSymbolToDeclaration = new HashMap<>();
Expand Down

0 comments on commit 3fdb0c2

Please sign in to comment.