Skip to content

Commit

Permalink
Prevent classpath refresh when resolve is not required
Browse files Browse the repository at this point in the history
Should fix CompletionTests9.test486988_0015
  • Loading branch information
mickaelistria committed Mar 25, 2024
1 parent 3720a0f commit 3c2413d
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,14 @@
*******************************************************************************/
package org.eclipse.jdt.core.tests.dom;

import junit.framework.Test;

import org.eclipse.jdt.core.dom.*;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
import org.eclipse.jdt.internal.core.JrtPackageFragmentRoot;
import org.eclipse.jdt.internal.core.SourceModule;

import java.util.List;
import java.util.function.Consumer;

import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IClasspathAttribute;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
Expand All @@ -38,6 +31,43 @@
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.compiler.IProblem;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.ASTRequestor;
import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.ExportsDirective;
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.FieldAccess;
import org.eclipse.jdt.core.dom.IAnnotationBinding;
import org.eclipse.jdt.core.dom.IBinding;
import org.eclipse.jdt.core.dom.IModuleBinding;
import org.eclipse.jdt.core.dom.IPackageBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.ImportDeclaration;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.ModuleDeclaration;
import org.eclipse.jdt.core.dom.ModuleDirective;
import org.eclipse.jdt.core.dom.Name;
import org.eclipse.jdt.core.dom.PackageDeclaration;
import org.eclipse.jdt.core.dom.ProvidesDirective;
import org.eclipse.jdt.core.dom.QualifiedName;
import org.eclipse.jdt.core.dom.RequiresDirective;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.SuperFieldAccess;
import org.eclipse.jdt.core.dom.TryStatement;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.core.dom.TypeParameter;
import org.eclipse.jdt.core.dom.UsesDirective;
import org.eclipse.jdt.core.dom.VariableDeclarationExpression;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
import org.eclipse.jdt.internal.core.JavaProject;
import org.eclipse.jdt.internal.core.JrtPackageFragmentRoot;
import org.eclipse.jdt.internal.core.SourceModule;

import junit.framework.Test;

@SuppressWarnings({"rawtypes"})
public class ASTConverter9Test extends ConverterTestSetup {
Expand Down Expand Up @@ -1475,5 +1505,37 @@ public void testBug542795() throws Exception {
deleteProject(p);
}
}

public void testModulePathRefresh() throws CoreException {
IJavaProject project1 = createJavaProject("Completion9_1", new String[] {"src"}, new String[] {"JCL18_LIB"}, "bin", "9");
IJavaProject project2 = createJavaProject("Completion9_2", new String[] {"src"}, new String[] {"JCL18_LIB"}, "bin", "9");
try {
project1.open(null);
createFile("/Completion9_1/src/module-info.java", """
module first {
requires second;
}
""");
addClasspathEntry(project1, JavaCore.newContainerEntry(new Path("org.eclipse.jdt.MODULE_PATH")));

createFile("/Completion9_2/src/module-info.java", """
module second {
exports pack21 to first;
exports pack22 to first;
}
""");
addClasspathEntry(project1, JavaCore.newContainerEntry(project2.getPath()));
assertEquals(2, ((JavaProject)project1).getResolvedClasspath().length);

project1.close();
project1.open(null);

IClasspathEntry[] resolvedClasspath = ((JavaProject)project1).getResolvedClasspath();
assertEquals(2, resolvedClasspath.length);
} finally {
deleteProject(project1);
deleteProject(project2);
}
}
// Add new tests here
}
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,13 @@ protected boolean buildStructure(OpenableElementInfo info, final IProgressMonito
ASTParser astParser = ASTParser.newParser(info instanceof ASTHolderCUInfo astHolder && astHolder.astLevel > 0 ? astHolder.astLevel : AST.getJLSLatest());
astParser.setWorkingCopyOwner(getOwner());
astParser.setSource(this instanceof ClassFileWorkingCopy ? source : this);
astParser.setProject(getJavaProject());
if (resolveBindings || computeProblems) {
astParser.setProject(getJavaProject());
} else {
// workaround https://github.com/eclipse-jdt/eclipse.jdt.core/issues/2204
// skips many operations, and prevents from conflicting classpath computation
astParser.setProject(null);
}
astParser.setStatementsRecovery((reconcileFlags & ICompilationUnit.ENABLE_STATEMENTS_RECOVERY) != 0);
astParser.setResolveBindings(computeProblems || resolveBindings);
astParser.setBindingsRecovery((reconcileFlags & ICompilationUnit.ENABLE_BINDINGS_RECOVERY) != 0);
Expand Down Expand Up @@ -259,8 +265,6 @@ protected boolean buildStructure(OpenableElementInfo info, final IProgressMonito
astHolder.ast = newAST;
}
newAST.accept(new DOMToModelPopulator(newElements, this, unitInfo));
// unitInfo.setModule();
// unitInfo.setSourceLength(newSourceLength);
boolean structureKnown = true;
for (IProblem problem : newAST.getProblems()) {
structureKnown &= (IProblem.Syntax & problem.getID()) == 0;
Expand All @@ -271,9 +275,9 @@ protected boolean buildStructure(OpenableElementInfo info, final IProgressMonito
(reconcileFlags & ICompilationUnit.ENABLE_BINDINGS_RECOVERY) != 0 &&
(reconcileFlags & ICompilationUnit.IGNORE_METHOD_BODIES) == 0) {
// most complete possible AST
this.ast = newAST;
this.ast = newAST;
} else {
this.ast = null;
this.ast = null;
}
}
} else {
Expand Down Expand Up @@ -531,7 +535,7 @@ org.eclipse.jdt.core.dom.CompilationUnit getOrBuildAST(WorkingCopyOwner workingC
boolean storeAST = isConsistent() &&
workingCopyOwner == getOwner() &&
isWorkingCopy() &&
!hasUnsavedChanges();
!hasUnsavedChanges();
if (this.ast != null && storeAST) {
return this.ast;
}
Expand Down

0 comments on commit 3c2413d

Please sign in to comment.