Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions org.eclipse.jdt.core.manipulation/.settings/.api_filters
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@
</message_arguments>
</filter>
</resource>
<resource path="common/org/eclipse/jdt/core/manipulation/OrganizeImportsOperation.java" type="org.eclipse.jdt.core.manipulation.OrganizeImportsOperation">
<filter comment="Use of import module modifiers" id="640712815">
<message_arguments>
<message_argument value="ImportDeclaration"/>
<message_argument value="OrganizeImportsOperation"/>
<message_argument value="getModifiers()"/>
</message_arguments>
</filter>
<filter comment="Use of import module APIs" id="640712815">
<message_arguments>
<message_argument value="Modifier"/>
<message_argument value="OrganizeImportsOperation"/>
<message_argument value="isModule(int)"/>
</message_arguments>
</filter>
</resource>
<resource path="core extension/org/eclipse/jdt/internal/corext/dom/ASTFlattener.java" type="org.eclipse.jdt.internal.corext.dom.ASTFlattener">
<filter comment="Java preview feature API" id="640712815">
<message_arguments>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2024 IBM Corporation and others.
* Copyright (c) 2000, 2025 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -42,14 +42,20 @@
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.ISourceRange;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.Signature;
import org.eclipse.jdt.core.SourceRange;
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.AbstractTypeDeclaration;
import org.eclipse.jdt.core.dom.CompilationUnit;
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.MethodInvocation;
Expand All @@ -72,7 +78,9 @@
import org.eclipse.jdt.internal.core.manipulation.util.Strings;
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
import org.eclipse.jdt.internal.corext.dom.Bindings;
import org.eclipse.jdt.internal.corext.dom.IASTSharedValues;
import org.eclipse.jdt.internal.corext.dom.ScopeAnalyzer;
import org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser;
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
import org.eclipse.jdt.internal.corext.util.JdtFlags;
import org.eclipse.jdt.internal.corext.util.StaticImportFavoritesCompletionInvoker;
Expand Down Expand Up @@ -220,6 +228,7 @@

private Set<String> fOldSingleImports;
private Set<String> fOldDemandImports;
private Map<String, Set<String>> fOldModuleImports;

private Set<String> fImplicitImports;

Expand All @@ -235,13 +244,16 @@

private Map<String, UnresolvedTypeData> fUnresolvedTypes;
private Set<String> fImportsAdded;
private Set<String> fModuleImportsAdded;
private TypeNameMatch[][] fOpenChoices;
private SourceRange[] fSourceRanges;


public TypeReferenceProcessor(Set<String> oldSingleImports, Set<String> oldDemandImports, CompilationUnit root, ImportRewrite impStructure, boolean ignoreLowerCaseNames, UnresolvableImportMatcher unresolvableImportMatcher) {
public TypeReferenceProcessor(Set<String> oldSingleImports, Set<String> oldDemandImports, Map<String, Set<String>> oldModuleImports,
CompilationUnit root, ImportRewrite impStructure, boolean ignoreLowerCaseNames, UnresolvableImportMatcher unresolvableImportMatcher) {
fOldSingleImports= oldSingleImports;
fOldDemandImports= oldDemandImports;
fOldModuleImports= oldModuleImports;
fImpStructure= impStructure;
fDoIgnoreLowerCaseNames= ignoreLowerCaseNames;
fUnresolvableImportMatcher= unresolvableImportMatcher;
Expand All @@ -253,11 +265,46 @@
fImplicitImports.add("java.lang"); //$NON-NLS-1$
fImplicitImports.add(cu.getParent().getElementName());

if (root.getAST().apiLevel() >= AST.JLS24) {
try {
IType[] types= cu.getAllTypes();
if (types.length > 0 && types[0].isImplicitlyDeclared()) {
ASTParser parser = ASTParser.newParser(IASTSharedValues.SHARED_AST_LEVEL);
Map<String, String> compilerOptions= RefactoringASTParser.getCompilerOptions(cu);
if (root.getAST().isPreviewEnabled()) {
compilerOptions.put(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED);
JavaCore.setComplianceOptions(Integer.toString(root.getAST().apiLevel()), compilerOptions);
}
parser.setCompilerOptions(compilerOptions);
parser.setEnvironment(null, null, null, true);
parser.setUnitName("A.java"); //$NON-NLS-1$
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource("import module java.base; class A { BigDecimal a; }".toCharArray()); //$NON-NLS-1$
parser.setResolveBindings(true);
CompilationUnit astRoot = (CompilationUnit) parser.createAST(null);
List<ImportDeclaration> importDecls= astRoot.imports();
if (!importDecls.isEmpty()) {
ImportDeclaration importDecl= importDecls.get(0);
if (importDecl.resolveBinding() instanceof IModuleBinding moduleBinding) {
IPackageBinding[] packageBindings= moduleBinding.getExportedPackages();
for (IPackageBinding packageBinding : packageBindings) {
fImplicitImports.add(packageBinding.getName());
}
}
}
}
} catch (JavaModelException e) {
// ignore
}
}


fAnalyzer= new ScopeAnalyzer(root);

fCurrPackage= (IPackageFragment) cu.getParent();

fImportsAdded= new HashSet<>();
fModuleImportsAdded= new HashSet<>();
fUnresolvedTypes= new HashMap<>();
}

Expand Down Expand Up @@ -323,6 +370,27 @@
}
typeBinding= typeBinding.getTypeDeclaration();
if (!typeBinding.isRecovered()) {
String qualifiedTypeName= typeBinding.getQualifiedName();
if (qualifiedTypeName.indexOf('.') > 0) {
String qualifier= qualifiedTypeName.substring(0, qualifiedTypeName.lastIndexOf('.'));
if (moduleImportsContains(qualifier)) {
for (Entry<String, Set<String>> entry : fOldModuleImports.entrySet()) {
if (entry.getValue().contains(qualifier)) {
fImportsAdded.add(typeName);
if (fModuleImportsAdded.contains(entry.getKey())) {
return;
}
fImpStructure.addModuleImport(entry.getKey(), new ArrayList<>(entry.getValue()));

Check warning on line 383 in org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/core/manipulation/OrganizeImportsOperation.java

View check run for this annotation

Jenkins - Eclipse JDT / Compiler

Member

ERROR: The method addModuleImport(String, new ArrayList<>(entry.getValue())) is undefined for the type ImportRewrite
fModuleImportsAdded.add(entry.getKey());
break;
}
}
return;
}
if (fImplicitImports.contains(qualifier)) {
return;
}
}
if (needsImport(typeBinding, ref)) {
fImpStructure.addImport(typeBinding);
fImportsAdded.add(typeName);
Expand Down Expand Up @@ -408,7 +476,9 @@
return null;
} else if (nFound == 1) {
TypeNameMatch typeRef= typeRefsFound.get(0);
fImpStructure.addImport(typeRef.getFullyQualifiedName());
if (!moduleImportsContains(typeRef.getTypeContainerName())) {
fImpStructure.addImport(typeRef.getFullyQualifiedName());
}
return null;
} else {
String typeToImport= null;
Expand All @@ -429,6 +499,8 @@
} else { // more than one import-on-demand
ambiguousImports= true;
}
} else if (moduleImportsContains(containerName)) {
return null; // we don't reimport
}
}

Expand All @@ -441,6 +513,15 @@
}
}

private boolean moduleImportsContains(String typeContainerName) {
for (Set<String> packageNames : fOldModuleImports.values()) {
if (packageNames.contains(typeContainerName)) {
return true;
}
}
return false;
}

private boolean isOfKind(TypeNameMatch curr, int typeKinds) {
int flags= curr.getModifiers();
if (Flags.isAnnotation(flags)) {
Expand Down Expand Up @@ -587,10 +668,11 @@

Set<String> oldSingleImports= new HashSet<>();
Set<String> oldDemandImports= new HashSet<>();
Map<String, Set<String>> oldModulePackages= new HashMap<>();
List<SimpleName> typeReferences= new ArrayList<>();
List<SimpleName> staticReferences= new ArrayList<>();

if (!collectReferences(astRoot, typeReferences, staticReferences, oldSingleImports, oldDemandImports))
if (!collectReferences(astRoot, typeReferences, staticReferences, oldSingleImports, oldDemandImports, oldModulePackages))
return null;

subMonitor.split(1);
Expand All @@ -600,6 +682,7 @@
TypeReferenceProcessor processor= new TypeReferenceProcessor(
oldSingleImports,
oldDemandImports,
oldModulePackages,
astRoot,
importsRewrite,
fIgnoreLowerCaseNames,
Expand Down Expand Up @@ -650,6 +733,7 @@
List<String> importsAdded= new ArrayList<>(importsStructure.getCreatedImports().length + importsStructure.getCreatedStaticImports().length);
importsAdded.addAll(Arrays.asList(importsStructure.getCreatedImports()));
importsAdded.addAll(Arrays.asList(importsStructure.getCreatedStaticImports()));
importsAdded.addAll(Arrays.asList(importsStructure.getCreatedModuleImports()));

Check warning on line 736 in org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/core/manipulation/OrganizeImportsOperation.java

View check run for this annotation

Jenkins - Eclipse JDT / Compiler

Member

ERROR: The method getCreatedModuleImports() is undefined for the type ImportRewrite

for (Object element : oldSingleImports.toArray()) {
String importName= (String) element;
Expand Down Expand Up @@ -734,7 +818,8 @@


// find type references in a compilation unit
private boolean collectReferences(CompilationUnit astRoot, List<SimpleName> typeReferences, List<SimpleName> staticReferences, Set<String> oldSingleImports, Set<String> oldDemandImports) {
private boolean collectReferences(CompilationUnit astRoot, List<SimpleName> typeReferences, List<SimpleName> staticReferences, Set<String> oldSingleImports,
Set<String> oldDemandImports, Map<String, Set<String>> oldModuleImports) {
if (!fAllowSyntaxErrors) {
for (IProblem curr : astRoot.getProblems()) {
if (curr.isError() && (curr.getID() & IProblem.Syntax) != 0) {
Expand All @@ -746,7 +831,16 @@
List<ImportDeclaration> imports= astRoot.imports();
for (ImportDeclaration curr : imports) {
String id= ASTResolving.getFullName(curr.getName());
if (curr.isOnDemand()) {
if (Modifier.isModule(curr.getModifiers())) {
Set<String> oldModulePackages= new HashSet<>();
oldModuleImports.put(id, oldModulePackages);
if (curr.resolveBinding() instanceof IModuleBinding binding) {
IPackageBinding[] packageBindings= binding.getExportedPackages();
for (IPackageBinding packageBinding : packageBindings) {
oldModulePackages.add(packageBinding.getName());
}
}
} else if (curr.isOnDemand()) {
oldDemandImports.add(id);
} else {
oldSingleImports.add(id);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2018 IBM Corporation and others.
* Copyright (c) 2000, 2025 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -35,9 +35,9 @@
import org.eclipse.jdt.core.dom.NodeFinder;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext;
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite.TypeLocation;
import org.eclipse.jdt.core.manipulation.ImportReferencesCollector;
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext;

import org.eclipse.jdt.internal.corext.dom.ScopeAnalyzer;
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
Expand Down Expand Up @@ -137,6 +137,15 @@
}
}

// check if qualifier matches a package provided by an import module statement
for (String moduleName : fImportRewrite.getAddedModuleImports()) {

Check warning on line 141 in org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/codemanipulation/ContextSensitiveImportRewriteContext.java

View check run for this annotation

Jenkins - Eclipse JDT / Compiler

Member

ERROR: The method getAddedModuleImports() is undefined for the type ImportRewrite
for (String packageName : fImportRewrite.getAddedModuleExportedPackages(moduleName)) {

Check warning on line 142 in org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/codemanipulation/ContextSensitiveImportRewriteContext.java

View check run for this annotation

Jenkins - Eclipse JDT / Compiler

Member

ERROR: The method getAddedModuleExportedPackages(String) is undefined for the type ImportRewrite
if (qualifier.equals(packageName)) {
return RES_NAME_FOUND;
}
}
}

String qualifiedName= JavaModelUtil.concatenateName(qualifier, name);
for (String addedImport : fImportRewrite.getAddedImports()) {
if (qualifiedName.equals(addedImport)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public class JavaProjectHelper {
public static final IPath RT_STUBS21= new Path("testresources/rtstubs_21.jar");
public static final IPath RT_STUBS22= new Path("testresources/rtstubs_22.jar");
public static final IPath RT_STUBS23= new Path("testresources/rtstubs_23.jar");
public static final IPath RT_STUBS24= new Path("testresources/rtstubs_24.jar");
public static final IPath JUNIT_SRC_381= new Path("testresources/junit381-noUI-src.zip");
public static final String JUNIT_SRC_ENCODING= "ISO-8859-1";

Expand Down Expand Up @@ -400,6 +401,23 @@ public static void set23CompilerOptions(IJavaProject project, boolean enable_pre
project.setOptions(options);
}

/**
* Sets the compiler options to 24 for the given project.
*
* @param project the java project
* @param enable_preview_feature sets enable-preview compliance project option based on the
* value specified.
*/
public static void set24CompilerOptions(IJavaProject project, boolean enable_preview_feature) {
Map<String, String> options= project.getOptions(false);
set24_CompilerOptions(options);
if (enable_preview_feature) {
options.put(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED);
options.put(JavaCore.COMPILER_PB_REPORT_PREVIEW_FEATURES, JavaCore.IGNORE);
}
project.setOptions(options);
}

/**
* Sets the compiler options to 9.
*
Expand Down Expand Up @@ -508,6 +526,15 @@ public static void set23_CompilerOptions(Map<String, String> options) {
JavaCore.setComplianceOptions(JavaCore.VERSION_23, options);
}

/**
* Sets the compiler options to 24.
*
* @param options the compiler options to configure
*/
public static void set24_CompilerOptions(Map<String, String> options) {
JavaCore.setComplianceOptions(JavaCore.VERSION_24, options);
}

/**
* Sets the compiler options to 1.8
*
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
HierarchicalASTVisitorTest.class,
ImportOrganizeTest.class,
ImportOrganizeTest1d8.class,
ImportOrganizeTest24.class,
JavaElementLabelsTest.class,
JavaElementLabelsTest1d8.class,
BindingLabelsTest.class,
Expand Down
Loading
Loading