-
Notifications
You must be signed in to change notification settings - Fork 86
/
ReorgCorrectionsBaseSubProcessor.java
330 lines (272 loc) · 15 KB
/
ReorgCorrectionsBaseSubProcessor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*******************************************************************************
* Copyright (c) 2024 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
* Matt Chapman, mpchapman@gmail.com - 89977 Make JDT .java agnostic
*******************************************************************************/
package org.eclipse.jdt.internal.ui.text.correction;
import java.util.Collection;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.resources.IProject;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.CompositeChange;
import org.eclipse.jdt.core.ClasspathContainerInitializer;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageDeclaration;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaConventions;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.dom.ASTNode;
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.Modifier;
import org.eclipse.jdt.core.dom.Name;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.Type;
import org.eclipse.jdt.internal.core.manipulation.JavaElementLabelsCore;
import org.eclipse.jdt.internal.core.manipulation.util.BasicElementLabels;
import org.eclipse.jdt.internal.corext.fix.CleanUpConstants;
import org.eclipse.jdt.internal.corext.fix.IProposableFix;
import org.eclipse.jdt.internal.corext.fix.UnusedCodeFixCore;
import org.eclipse.jdt.internal.corext.refactoring.changes.CreatePackageChange;
import org.eclipse.jdt.internal.corext.refactoring.changes.MoveCompilationUnitChange;
import org.eclipse.jdt.internal.corext.refactoring.changes.RenameCompilationUnitChange;
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
import org.eclipse.jdt.internal.corext.util.Messages;
import org.eclipse.jdt.ui.cleanup.CleanUpOptions;
import org.eclipse.jdt.internal.ui.fix.UnusedCodeCleanUp;
import org.eclipse.jdt.internal.ui.text.correction.proposals.CorrectPackageDeclarationProposalCore;
public abstract class ReorgCorrectionsBaseSubProcessor<T> {
protected ReorgCorrectionsBaseSubProcessor() {
}
public void addWrongTypeNameProposals(IInvocationContextCore context, IProblemLocationCore problem, Collection<T> proposals) {
ICompilationUnit cu= context.getCompilationUnit();
boolean isLinked= cu.getResource().isLinked();
IJavaProject javaProject= cu.getJavaProject();
String sourceLevel= javaProject.getOption(JavaCore.COMPILER_SOURCE, true);
String compliance= javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true);
String previewEnabled= javaProject.getOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, true);
CompilationUnit root= context.getASTRoot();
ASTNode coveredNode= problem.getCoveredNode(root);
if (!(coveredNode instanceof SimpleName simpleCovered))
return;
ASTNode parentType= coveredNode.getParent();
if (!(parentType instanceof AbstractTypeDeclaration))
return;
String currTypeName= simpleCovered.getIdentifier();
String newTypeName= JavaCore.removeJavaLikeExtension(cu.getElementName());
boolean hasOtherPublicTypeBefore= false;
boolean found= false;
List<AbstractTypeDeclaration> types= root.types();
for (AbstractTypeDeclaration curr : types) {
if (parentType != curr) {
if (newTypeName.equals(curr.getName().getIdentifier())) {
return;
}
if (!found && Modifier.isPublic(curr.getModifiers())) {
hasOtherPublicTypeBefore= true;
}
} else {
found= true;
}
}
if (!JavaConventions.validateJavaTypeName(newTypeName, sourceLevel, compliance, previewEnabled).matches(IStatus.ERROR)) {
T prop1= createCorrectMainTypeNameProposal(cu, context, currTypeName, newTypeName, IProposalRelevance.RENAME_TYPE);
if (prop1 != null)
proposals.add(prop1);
}
if (!hasOtherPublicTypeBefore) {
String newCUName= JavaModelUtil.getRenamedCUName(cu, currTypeName);
ICompilationUnit newCU= ((IPackageFragment) (cu.getParent())).getCompilationUnit(newCUName);
if (!newCU.exists() && !isLinked && !JavaConventions.validateCompilationUnitName(newCUName, sourceLevel, compliance).matches(IStatus.ERROR)) {
RenameCompilationUnitChange change= new RenameCompilationUnitChange(cu, newCUName);
// rename CU
String label= Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_renamecu_description, BasicElementLabels.getResourceName(newCUName));
T p2= createRenameCUProposal(label, change, IProposalRelevance.RENAME_CU);
if( p2 != null )
proposals.add(p2);
}
}
}
public void addWrongPackageDeclNameProposals(IInvocationContextCore context, IProblemLocationCore problem, Collection<T> proposals) throws CoreException {
ICompilationUnit cu= context.getCompilationUnit();
boolean isLinked= cu.getResource().isLinked();
// correct package declaration
int relevance= cu.getPackageDeclarations().length == 0 ? IProposalRelevance.MISSING_PACKAGE_DECLARATION : IProposalRelevance.CORRECT_PACKAGE_DECLARATION; // bug 38357
if (CorrectPackageDeclarationProposalCore.isValidProposal(cu)) {
T p1= createCorrectPackageDeclarationProposal(cu, problem, relevance);
if (p1 != null)
proposals.add(p1);
}
// move to package
IPackageDeclaration[] packDecls= cu.getPackageDeclarations();
String newPackName= packDecls.length > 0 ? packDecls[0].getElementName() : ""; //$NON-NLS-1$
IPackageFragmentRoot root= JavaModelUtil.getPackageFragmentRoot(cu);
IPackageFragment newPack= root.getPackageFragment(newPackName);
ICompilationUnit newCU= newPack.getCompilationUnit(cu.getElementName());
if (!newCU.exists() && !isLinked) {
String label;
if (newPack.isDefaultPackage()) {
label= Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_movecu_default_description, BasicElementLabels.getFileName(cu));
} else {
String packageLabel= JavaElementLabelsCore.getElementLabel(newPack, JavaElementLabelsCore.ALL_DEFAULT);
label= Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_movecu_description, new Object[] { BasicElementLabels.getFileName(cu), packageLabel });
}
CompositeChange composite= new CompositeChange(label);
composite.add(new CreatePackageChange(newPack));
composite.add(new MoveCompilationUnitChange(cu, newPack));
T p1= createMoveToNewPackageProposal(label, composite, IProposalRelevance.MOVE_CU_TO_PACKAGE);
if (p1 != null)
proposals.add(p1);
}
}
public void addRemoveImportStatementProposals(IInvocationContextCore context, IProblemLocationCore problem, Collection<T> proposals) {
IProposableFix fix= UnusedCodeFixCore.createRemoveUnusedImportFix(context.getASTRoot(), problem);
if (fix != null) {
Map<String, String> options= new Hashtable<>();
options.put(CleanUpConstants.REMOVE_UNUSED_CODE_IMPORTS, CleanUpOptions.TRUE);
T proposal= createRemoveUnusedImportProposal(fix, new UnusedCodeCleanUp(options), IProposalRelevance.REMOVE_UNUSED_IMPORT, context);
if (proposal != null)
proposals.add(proposal);
}
final ICompilationUnit cu= context.getCompilationUnit();
String name= CorrectionMessages.ReorgCorrectionsSubProcessor_organizeimports_description;
T proposal= createOrganizeImportsProposal(name, null, cu, IProposalRelevance.ORGANIZE_IMPORTS);
if (proposal != null)
proposals.add(proposal);
}
public void addProjectSetupFixProposals(IInvocationContextCore context, IProblemLocationCore problem, String missingType, Collection<T> proposals) {
T prop= createProjectSetupFixProposal(context, problem, missingType, proposals);
if (prop != null) {
proposals.add(prop);
}
}
/**
* Adds a proposal to increase the compiler compliance level
* @param context the context
* @param problem the current problem
* @param proposals the resulting proposals
* @param requiredVersion the minimal required Java compiler version
*/
public void addNeedHigherComplianceProposals(IInvocationContextCore context, IProblemLocationCore problem, Collection<T> proposals, String requiredVersion) {
addNeedHigherComplianceProposals(context, problem, proposals, false, requiredVersion);
}
public void addNeedHigherComplianceProposals(IInvocationContextCore context, IProblemLocationCore problem, Collection<T> proposals) {
String[] args= problem.getProblemArguments();
if (args != null && args.length == 2) {
addNeedHigherComplianceProposals(context, problem, proposals, false, args[1]);
}
}
/**
* Adds a proposal to increase the compiler compliance level as well as set --enable-previews
* option.
*
* @param context the context
* @param problem the current problem
* @param proposals the resulting proposals
* @param enablePreviews --enable-previews option will be enabled if set to true
* @param requiredVersion the minimal required Java compiler version
*/
protected void addNeedHigherComplianceProposals(IInvocationContextCore context, IProblemLocationCore problem, Collection<T> proposals, boolean enablePreviews, String requiredVersion) {
IJavaProject project= context.getCompilationUnit().getJavaProject();
String label1= Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_change_project_compliance_description, requiredVersion);
if (enablePreviews) {
label1= Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_combine_two_quickfixes, new String[] {label1, CorrectionMessages.PreviewFeaturesSubProcessor_enable_preview_features});
proposals.add(createChangeToRequiredCompilerComplianceProposal(label1, project, false, requiredVersion, enablePreviews, IProposalRelevance.CHANGE_PROJECT_COMPLIANCE));
} else {
proposals.add(createChangeToRequiredCompilerComplianceProposal(label1, project, false, requiredVersion, IProposalRelevance.CHANGE_PROJECT_COMPLIANCE));
}
if (project.getOption(JavaCore.COMPILER_COMPLIANCE, false) == null) {
String label2= Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_change_workspace_compliance_description, requiredVersion);
if (enablePreviews) {
label2= Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_combine_two_quickfixes, new String[] {label2, CorrectionMessages.PreviewFeaturesSubProcessor_enable_preview_features_workspace});
}
T p1= createChangeToRequiredCompilerComplianceProposal(label2, project, true, requiredVersion, enablePreviews, IProposalRelevance.CHANGE_WORKSPACE_COMPLIANCE);
if (p1 != null)
proposals.add(p1);
}
}
/**
* Adds a proposal that opens the build path dialog
* @param context the context
* @param problem the current problem
* @param proposals the resulting proposals
*/
public void addIncorrectBuildPathProposals(IInvocationContextCore context, IProblemLocationCore problem, Collection<T> proposals) {
IProject project= context.getCompilationUnit().getJavaProject().getProject();
String label= CorrectionMessages.ReorgCorrectionsSubProcessor_configure_buildpath_label;
T proposal= createOpenBuildPathCorrectionProposal(project, label, IProposalRelevance.CONFIGURE_BUILD_PATH, null);
if (proposal != null)
proposals.add(proposal);
}
public void addAccessRulesProposals(IInvocationContextCore context, IProblemLocationCore problem, Collection<T> proposals) {
IBinding referencedElement= null;
ASTNode node= problem.getCoveredNode(context.getASTRoot());
if (node instanceof Type type) {
referencedElement= type.resolveBinding();
} else if (node instanceof Name name) {
referencedElement= name.resolveBinding();
}
if (referencedElement != null && canModifyAccessRules(referencedElement)) {
IProject project= context.getCompilationUnit().getJavaProject().getProject();
String label= CorrectionMessages.ReorgCorrectionsSubProcessor_accessrules_description;
T proposal= createOpenBuildPathCorrectionProposal(project, label, IProposalRelevance.CONFIGURE_ACCESS_RULES, referencedElement);
if (proposal != null)
proposals.add(proposal);
}
}
private static boolean canModifyAccessRules(IBinding binding) {
IJavaElement element= binding.getJavaElement();
if (element == null)
return false;
IPackageFragmentRoot root= JavaModelUtil.getPackageFragmentRoot(element);
if (root == null)
return false;
try {
IClasspathEntry classpathEntry= root.getRawClasspathEntry();
if (classpathEntry == null)
return false;
if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY)
return true;
if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
ClasspathContainerInitializer classpathContainerInitializer= JavaCore.getClasspathContainerInitializer(classpathEntry.getPath().segment(0));
IStatus status= classpathContainerInitializer.getAccessRulesStatus(classpathEntry.getPath(), root.getJavaProject());
return status.isOK();
}
} catch (JavaModelException e) {
return false;
}
return false;
}
public abstract T createRenameCUProposal(String label, RenameCompilationUnitChange change, int relevance);
public abstract T createCorrectMainTypeNameProposal(ICompilationUnit cu, IInvocationContextCore context, String currTypeName, String newTypeName, int relevance);
protected abstract T createCorrectPackageDeclarationProposal(ICompilationUnit cu, IProblemLocationCore problem, int relevance);
protected abstract T createMoveToNewPackageProposal(String label, CompositeChange composite, int relevance);
protected abstract T createOrganizeImportsProposal(String name, Change change, ICompilationUnit cu, int relevance);
protected abstract T createRemoveUnusedImportProposal(IProposableFix fix, UnusedCodeCleanUp unusedCodeCleanUp, int relevance, IInvocationContextCore context);
public abstract T createProjectSetupFixProposal(IInvocationContextCore context, IProblemLocationCore problem, String missingType, Collection<T> proposals);
/* answers false if the problem location is not an import declaration, and hence no proposal have been added. */
public abstract boolean addImportNotFoundProposals(IInvocationContextCore context, IProblemLocationCore problem, Collection<T> proposals) throws CoreException;
protected abstract T createChangeToRequiredCompilerComplianceProposal(String label1, IJavaProject project, boolean changeOnWorkspace, String requiredVersion, int relevance);
protected abstract T createChangeToRequiredCompilerComplianceProposal(String label2, IJavaProject project, boolean changeOnWorkspace, String requiredVersion, boolean enablePreviews,
int relevance);
protected abstract T createOpenBuildPathCorrectionProposal(IProject project, String label, int relevance, IBinding referencedElement);
}