Skip to content

Commit a8e6a1b

Browse files
authored
Make correct package declaration a common applicable fix (#2259)
* Make correct package declaration a common applicable fix - create new CorrectPackageDeclarationFixCore which implements IProposableFix - create new CorrectPackageDeclarationCleanUpCore which extends AbstractMultiFix - change ReorgCorrectionBaseSubProcessor.addWrongPackageNameProposals() to try and create a CorrectPackageDeclarationFixCore and if successful, then to call createCorrectPackageDeclarationProposal() - change createCorrectPackageDeclarationProposal() method in ReorgCorrectionSubProcessor to create a FixCorrectionProposal using the fix from the base subprocessor and a newly created CorrectPackageDeclarationCleanUpCore - remove unused CorrectPackageDeclarationProposal and CorrectPackageDeclarationProposalCore classes - increase the relevance of a package correction to 6 - fixes #2081
1 parent dfde4e2 commit a8e6a1b

File tree

10 files changed

+236
-205
lines changed

10 files changed

+236
-205
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Red Hat Inc. and others.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* Red Hat Inc. - initial API and implementation
13+
*******************************************************************************/
14+
package org.eclipse.jdt.internal.ui.fix;
15+
16+
import org.eclipse.core.runtime.CoreException;
17+
18+
import org.eclipse.jdt.core.ICompilationUnit;
19+
import org.eclipse.jdt.core.compiler.IProblem;
20+
import org.eclipse.jdt.core.dom.CompilationUnit;
21+
22+
import org.eclipse.jdt.internal.corext.fix.CorrectPackageDeclarationFixCore;
23+
24+
import org.eclipse.jdt.ui.cleanup.CleanUpRequirements;
25+
import org.eclipse.jdt.ui.cleanup.ICleanUpFix;
26+
import org.eclipse.jdt.ui.text.java.IProblemLocation;
27+
28+
import org.eclipse.jdt.internal.ui.text.correction.ProblemLocation;
29+
30+
public class CorrectPackageDeclarationCleanUpCore extends AbstractMultiFix {
31+
32+
@Override
33+
public boolean canFix(ICompilationUnit compilationUnit, IProblemLocation problem) {
34+
if (problem.getProblemId() == IProblem.PackageIsNotExpectedPackage) {
35+
return true;
36+
}
37+
return false;
38+
}
39+
40+
@Override
41+
protected ICleanUpFix createFix(CompilationUnit unit) throws CoreException {
42+
IProblem[] problems= unit.getProblems();
43+
for (IProblem problem : problems) {
44+
ProblemLocation location= new ProblemLocation(problem);
45+
if (location.getProblemId() == IProblem.PackageIsNotExpectedPackage) {
46+
return createFix(unit, new IProblemLocation[] { location });
47+
}
48+
}
49+
return null;
50+
}
51+
52+
@Override
53+
protected ICleanUpFix createFix(CompilationUnit unit, IProblemLocation[] problems) throws CoreException {
54+
for (IProblemLocation problem : problems) {
55+
if (problem.getProblemId() == IProblem.PackageIsNotExpectedPackage) {
56+
return CorrectPackageDeclarationFixCore.create(unit, problem);
57+
}
58+
}
59+
return null;
60+
}
61+
62+
@Override
63+
public CleanUpRequirements getRequirements() {
64+
return new CleanUpRequirements(true, false, false, null);
65+
}
66+
}

org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/ui/text/correction/IProposalRelevance.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ public interface IProposalRelevance {
142142
int CONVERT_METHOD_REFERENCE_TO_LAMBDA= 6;
143143
int CONVERT_TO_METHOD_REFERENCE= 6;
144144
int CONVERT_PATTERN_INSTANCEOF_TO_SWITCH= 6;
145+
int CORRECT_PACKAGE_DECLARATION= 6;
145146
int CREATE_NEW_SUB_TYPE= 5;
146147

147148
int ADD_ALL_MISSING_TAGS= 5;
@@ -198,7 +199,6 @@ public interface IProposalRelevance {
198199
int ADD_CONSTRUCTOR_FROM_SUPER_CLASS= 5;
199200
int GETTER_SETTER_UNQUALIFIED_FIELD_ACCESS= 5;
200201
int RENAME_TYPE= 5;
201-
int CORRECT_PACKAGE_DECLARATION= 5;
202202
int TYPE_ARGUMENTS_FROM_CONTEXT= 5;
203203
int REMOVE_REDUNDANT_NULLNESS_ANNOTATION= 5;
204204
int ADD_MISSING_NULLNESS_ANNOTATION= 5;

org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/ui/text/correction/ReorgCorrectionsBaseSubProcessor.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2024 IBM Corporation and others.
2+
* Copyright (c) 2024, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -55,6 +55,7 @@
5555
import org.eclipse.jdt.internal.core.manipulation.util.BasicElementLabels;
5656
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
5757
import org.eclipse.jdt.internal.corext.fix.CleanUpConstants;
58+
import org.eclipse.jdt.internal.corext.fix.CorrectPackageDeclarationFixCore;
5859
import org.eclipse.jdt.internal.corext.fix.IProposableFix;
5960
import org.eclipse.jdt.internal.corext.fix.UnusedCodeFixCore;
6061
import org.eclipse.jdt.internal.corext.refactoring.changes.CreatePackageChange;
@@ -68,7 +69,6 @@
6869
import org.eclipse.jdt.ui.text.java.IProblemLocation;
6970

7071
import org.eclipse.jdt.internal.ui.fix.UnusedCodeCleanUpCore;
71-
import org.eclipse.jdt.internal.ui.text.correction.proposals.CorrectPackageDeclarationProposalCore;
7272

7373
public abstract class ReorgCorrectionsBaseSubProcessor<T> {
7474

@@ -142,8 +142,9 @@ public void addWrongPackageDeclNameProposals(IInvocationContext context, IProble
142142

143143
// correct package declaration
144144
int relevance= cu.getPackageDeclarations().length == 0 ? IProposalRelevance.MISSING_PACKAGE_DECLARATION : IProposalRelevance.CORRECT_PACKAGE_DECLARATION; // bug 38357
145-
if (CorrectPackageDeclarationProposalCore.isValidProposal(cu)) {
146-
T p1= createCorrectPackageDeclarationProposal(cu, problem, relevance);
145+
IProposableFix fix= CorrectPackageDeclarationFixCore.create(context.getASTRoot(), problem);
146+
if (fix != null) {
147+
T p1= createCorrectPackageDeclarationProposal(fix, context, relevance);
147148
if (p1 != null)
148149
proposals.add(p1);
149150
}
@@ -347,7 +348,7 @@ private static boolean canModifyAccessRules(IBinding binding) {
347348

348349
public abstract T createCorrectMainTypeNameProposal(ICompilationUnit cu, IInvocationContext context, String currTypeName, String newTypeName, int relevance);
349350

350-
protected abstract T createCorrectPackageDeclarationProposal(ICompilationUnit cu, IProblemLocation problem, int relevance);
351+
protected abstract T createCorrectPackageDeclarationProposal(IProposableFix fix, IInvocationContext context, int relevance);
351352

352353
protected abstract T createMoveToNewPackageProposal(String label, CompositeChange composite, int relevance);
353354

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Red Hat Inc. and others.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* Red Hat Inc. - initial API and implementation
13+
*******************************************************************************/
14+
package org.eclipse.jdt.internal.corext.fix;
15+
16+
import org.eclipse.core.runtime.CoreException;
17+
import org.eclipse.core.runtime.IProgressMonitor;
18+
import org.eclipse.core.runtime.IStatus;
19+
20+
import org.eclipse.text.edits.DeleteEdit;
21+
import org.eclipse.text.edits.InsertEdit;
22+
import org.eclipse.text.edits.MultiTextEdit;
23+
import org.eclipse.text.edits.ReplaceEdit;
24+
25+
import org.eclipse.jdt.core.ICompilationUnit;
26+
import org.eclipse.jdt.core.IJavaProject;
27+
import org.eclipse.jdt.core.IModuleDescription;
28+
import org.eclipse.jdt.core.IPackageDeclaration;
29+
import org.eclipse.jdt.core.IPackageFragment;
30+
import org.eclipse.jdt.core.ISourceRange;
31+
import org.eclipse.jdt.core.JavaModelException;
32+
import org.eclipse.jdt.core.dom.CompilationUnit;
33+
import org.eclipse.jdt.core.refactoring.CompilationUnitChange;
34+
35+
import org.eclipse.jdt.internal.core.manipulation.JavaManipulationPlugin;
36+
import org.eclipse.jdt.internal.core.manipulation.StubUtility;
37+
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
38+
39+
import org.eclipse.jdt.ui.text.java.IProblemLocation;
40+
41+
import org.eclipse.jdt.internal.ui.text.correction.CorrectionMessages;
42+
43+
public class CorrectPackageDeclarationFixCore implements IProposableFix {
44+
45+
private final ICompilationUnit fCompilationUnit;
46+
private final IProblemLocation fLocation;
47+
48+
private CorrectPackageDeclarationFixCore(CompilationUnit compilationUnit, IProblemLocation location) {
49+
this.fCompilationUnit= (ICompilationUnit)compilationUnit.getJavaElement();
50+
this.fLocation= location;
51+
}
52+
53+
public static CorrectPackageDeclarationFixCore create(CompilationUnit cu, IProblemLocation location) {
54+
if (!isValidProposal((ICompilationUnit) cu.getJavaElement())) {
55+
return null;
56+
}
57+
return new CorrectPackageDeclarationFixCore(cu, location);
58+
}
59+
60+
private static boolean isValidProposal(ICompilationUnit cu) {
61+
boolean isValid= true;
62+
IPackageFragment parentPack= (IPackageFragment) cu.getParent();
63+
try {
64+
IPackageDeclaration[] decls= cu.getPackageDeclarations();
65+
if (parentPack.isDefaultPackage() && decls.length > 0) {
66+
IJavaProject jProject = parentPack.getJavaProject();
67+
if (jProject != null && JavaModelUtil.is9OrHigher(jProject)) {
68+
try {
69+
IModuleDescription desc= jProject.getModuleDescription();
70+
if (desc!= null && desc.exists()) {
71+
isValid= false;
72+
}
73+
} catch (JavaModelException e) {
74+
// TODO Auto-generated catch block
75+
e.printStackTrace();
76+
}
77+
}
78+
}
79+
} catch(JavaModelException e) {
80+
JavaManipulationPlugin.log(e);
81+
}
82+
return isValid;
83+
}
84+
85+
@Override
86+
public CompilationUnitChange createChange(IProgressMonitor progressMonitor) throws CoreException {
87+
88+
IPackageFragment parentPack= (IPackageFragment) fCompilationUnit.getParent();
89+
IPackageDeclaration[] decls= fCompilationUnit.getPackageDeclarations();
90+
91+
CompilationUnitChange change= new CompilationUnitChange(getDisplayString(), fCompilationUnit);
92+
MultiTextEdit root= new MultiTextEdit();
93+
94+
if (parentPack.isDefaultPackage() && decls.length > 0) {
95+
for (IPackageDeclaration decl : decls) {
96+
ISourceRange range= decl.getSourceRange();
97+
root.addChild(new DeleteEdit(range.getOffset(), range.getLength()));
98+
}
99+
} else if (!parentPack.isDefaultPackage() && decls.length == 0) {
100+
String lineDelim= StubUtility.getLineDelimiterUsed(fCompilationUnit);
101+
String str= "package " + parentPack.getElementName() + ';' + lineDelim + lineDelim; //$NON-NLS-1$
102+
root.addChild(new InsertEdit(0, str));
103+
} else {
104+
root.addChild(new ReplaceEdit(fLocation.getOffset(), fLocation.getLength(), parentPack.getElementName()));
105+
}
106+
change.setEdit(root);
107+
return change;
108+
}
109+
110+
@Override
111+
public String getDisplayString() {
112+
return CorrectionMessages.CorrectPackageDeclarationProposal_name;
113+
}
114+
115+
@Override
116+
public String getAdditionalProposalInfo() {
117+
return null;
118+
}
119+
120+
@Override
121+
public IStatus getStatus() {
122+
return null;
123+
}
124+
125+
}

org.eclipse.jdt.core.manipulation/proposals/org/eclipse/jdt/internal/ui/text/correction/proposals/CorrectPackageDeclarationProposalCore.java

Lines changed: 0 additions & 121 deletions
This file was deleted.

org.eclipse.jdt.ui.tests/META-INF/MANIFEST.MF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Automatic-Module-Name: org.eclipse.jdt.ui.tests
33
Bundle-ManifestVersion: 2
44
Bundle-Name: %Plugin.name
55
Bundle-SymbolicName: org.eclipse.jdt.ui.tests; singleton:=true
6-
Bundle-Version: 3.16.100.qualifier
6+
Bundle-Version: 3.16.200.qualifier
77
Bundle-Activator: org.eclipse.jdt.testplugin.JavaTestPlugin
88
Bundle-Vendor: %Plugin.providerName
99
Bundle-Localization: plugin

org.eclipse.jdt.ui.tests/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
</parent>
2121
<groupId>org.eclipse.jdt</groupId>
2222
<artifactId>org.eclipse.jdt.ui.tests</artifactId>
23-
<version>3.16.100-SNAPSHOT</version>
23+
<version>3.16.200-SNAPSHOT</version>
2424
<packaging>eclipse-test-plugin</packaging>
2525
<properties>
2626
<defaultSigning-excludeInnerJars>true</defaultSigning-excludeInnerJars>

0 commit comments

Comments
 (0)