-
Notifications
You must be signed in to change notification settings - Fork 86
/
ReorgCorrectionsSubProcessor.java
616 lines (547 loc) · 27.4 KB
/
ReorgCorrectionsSubProcessor.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
/*******************************************************************************
* Copyright (c) 2000, 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.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.text.edits.TextEdit;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.operation.IRunnableContext;
import org.eclipse.jface.text.IDocument;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.dialogs.PreferencesUtil;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.ui.progress.IProgressService;
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.IPackageFragmentRoot;
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.IBinding;
import org.eclipse.jdt.core.dom.ImportDeclaration;
import org.eclipse.jdt.core.dom.Name;
import org.eclipse.jdt.core.dom.QualifiedName;
import org.eclipse.jdt.core.dom.Type;
import org.eclipse.jdt.core.manipulation.TypeKinds;
import org.eclipse.jdt.internal.core.manipulation.util.BasicElementLabels;
import org.eclipse.jdt.internal.corext.codemanipulation.AddImportsOperation;
import org.eclipse.jdt.internal.corext.codemanipulation.AddImportsOperation.IChooseImportQuery;
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
import org.eclipse.jdt.internal.corext.fix.IProposableFix;
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.launching.IVMInstall;
import org.eclipse.jdt.launching.IVMInstall2;
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipse.jdt.launching.environments.IExecutionEnvironment;
import org.eclipse.jdt.ui.actions.OrganizeImportsAction;
import org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal;
import org.eclipse.jdt.ui.text.java.correction.ChangeCorrectionProposal;
import org.eclipse.jdt.ui.text.java.correction.ICommandAccess;
import org.eclipse.jdt.internal.ui.JavaPlugin;
import org.eclipse.jdt.internal.ui.JavaPluginImages;
import org.eclipse.jdt.internal.ui.actions.WorkbenchRunnableAdapter;
import org.eclipse.jdt.internal.ui.fix.UnusedCodeCleanUp;
import org.eclipse.jdt.internal.ui.javaeditor.AddImportOnSelectionAction;
import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
import org.eclipse.jdt.internal.ui.preferences.BuildPathsPropertyPage;
import org.eclipse.jdt.internal.ui.text.correction.proposals.CorrectMainTypeNameProposal;
import org.eclipse.jdt.internal.ui.text.correction.proposals.CorrectPackageDeclarationProposal;
import org.eclipse.jdt.internal.ui.text.correction.proposals.FixCorrectionProposal;
import org.eclipse.jdt.internal.ui.util.BusyIndicatorRunnableContext;
import org.eclipse.jdt.internal.ui.util.ClasspathVMUtil;
import org.eclipse.jdt.internal.ui.util.CoreUtility;
import org.eclipse.jdt.internal.ui.util.Progress;
import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElement;
import org.eclipse.jdt.internal.ui.wizards.buildpaths.ClasspathFixSelectionDialog;
public class ReorgCorrectionsSubProcessor extends ReorgCorrectionsBaseSubProcessor<ICommandAccess> {
public static void getWrongTypeNameProposals(IInvocationContextCore context, IProblemLocationCore problem, Collection<ICommandAccess> proposals) {
new ReorgCorrectionsSubProcessor().addWrongTypeNameProposals(context, problem, proposals);
}
public static void getWrongPackageDeclNameProposals(IInvocationContextCore context, IProblemLocationCore problem, Collection<ICommandAccess> proposals) throws CoreException {
new ReorgCorrectionsSubProcessor().addWrongPackageDeclNameProposals(context, problem, proposals);
}
public static void removeImportStatementProposals(IInvocationContextCore context, IProblemLocationCore problem, Collection<ICommandAccess> proposals) {
new ReorgCorrectionsSubProcessor().addRemoveImportStatementProposals(context, problem, proposals);
}
public static class ClasspathFixCorrectionProposal extends CUCorrectionProposal {
private final int fOffset;
private final int fLength;
private final String fMissingType;
private TextEdit fResultingEdit;
public ClasspathFixCorrectionProposal(ICompilationUnit cu, int offset, int length, String missingType) {
super(CorrectionMessages.ReorgCorrectionsSubProcessor_project_seup_fix_description, cu, -10, JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE));
fOffset= offset;
fLength= length;
fMissingType= missingType;
}
@Override
public void apply(IDocument document) {
IRunnableContext context= JavaPlugin.getActiveWorkbenchWindow();
if (context == null) {
context= new BusyIndicatorRunnableContext();
}
Shell shell= JavaPlugin.getActiveWorkbenchShell();
if (ClasspathFixSelectionDialog.openClasspathFixSelectionDialog(shell, getCompilationUnit().getJavaProject(), fMissingType, context)) {
if (fMissingType.indexOf('.') == -1) {
try {
IChooseImportQuery query= AddImportOnSelectionAction.newDialogQuery(shell);
AddImportsOperation op= new AddImportsOperation(getCompilationUnit(), fOffset, fLength, query, false, false);
IProgressService progressService= PlatformUI.getWorkbench().getProgressService();
progressService.runInUI(context, new WorkbenchRunnableAdapter(op, op.getScheduleRule()), op.getScheduleRule());
fResultingEdit= op.getResultingEdit();
super.apply(document);
} catch (InvocationTargetException e) {
JavaPlugin.log(e);
} catch (InterruptedException e) {
// ignore
}
}
}
}
@Override
protected boolean useDelegateToCreateTextChange() {
return false;
}
@Override
protected void addEdits(IDocument document, TextEdit editRoot) throws CoreException {
if (fResultingEdit != null) {
editRoot.addChild(fResultingEdit);
}
}
@Override
public Object getAdditionalProposalInfo(IProgressMonitor monitor) {
return Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_project_seup_fix_info, BasicElementLabels.getJavaElementName(fMissingType));
}
}
public static void addProjectSetupFixProposal(IInvocationContextCore context, IProblemLocationCore problem, String missingType, Collection<ICommandAccess> proposals) {
new ReorgCorrectionsSubProcessor().addProjectSetupFixProposals(context, problem, missingType, proposals);
}
/* answers false if the problem location is not an import declaration, and hence no proposal have been added. */
// This should be part of the base class but it has circular refs with UnresolvedElementsSubProcessor
public static boolean importNotFoundProposals(IInvocationContextCore context, IProblemLocationCore problem, Collection<ICommandAccess> proposals) throws CoreException {
ICompilationUnit cu= context.getCompilationUnit();
ASTNode selectedNode= problem.getCoveringNode(context.getASTRoot());
if (selectedNode == null) {
return false;
}
ImportDeclaration importDeclaration= (ImportDeclaration) ASTNodes.getParent(selectedNode, ASTNode.IMPORT_DECLARATION);
if (importDeclaration == null) {
return false;
}
if (!importDeclaration.isOnDemand()) {
Name name= importDeclaration.getName();
if (importDeclaration.isStatic() && name.isQualifiedName()) {
name= ((QualifiedName) name).getQualifier();
}
int kind= JavaModelUtil.is50OrHigher(cu.getJavaProject()) ? TypeKinds.REF_TYPES : TypeKinds.CLASSES | TypeKinds.INTERFACES;
UnresolvedElementsSubProcessor.addRequiresModuleProposals(cu, name, IProposalRelevance.IMPORT_NOT_FOUND_ADD_REQUIRES_MODULE, proposals, false);
UnresolvedElementsSubProcessor.addNewTypeProposals(cu, name, kind, IProposalRelevance.IMPORT_NOT_FOUND_NEW_TYPE, proposals);
} else {
Name name= importDeclaration.getName();
UnresolvedElementsSubProcessor.addRequiresModuleProposals(cu, name, IProposalRelevance.IMPORT_NOT_FOUND_ADD_REQUIRES_MODULE, proposals, true);
}
String name= ASTNodes.asString(importDeclaration.getName());
if (importDeclaration.isOnDemand()) {
name= JavaModelUtil.concatenateName(name, "*"); //$NON-NLS-1$
}
addProjectSetupFixProposal(context, problem, name, proposals);
return true;
}
private static final class OpenBuildPathCorrectionProposal extends ChangeCorrectionProposal {
private final IProject fProject;
private final IBinding fReferencedType;
private OpenBuildPathCorrectionProposal(IProject project, String label, int relevance, IBinding referencedType) {
super(label, null, relevance, null);
fProject= project;
fReferencedType= referencedType;
setImage(JavaPluginImages.get(JavaPluginImages.IMG_OBJS_ACCESSRULES_ATTRIB));
}
@Override
public void apply(IDocument document) {
Map<Object, Object> data= null;
if (fReferencedType != null) {
IJavaElement elem= fReferencedType.getJavaElement();
if (elem != null) {
IPackageFragmentRoot root= (IPackageFragmentRoot) elem.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
if (root != null) {
try {
IClasspathEntry entry= root.getRawClasspathEntry();
if (entry != null) {
data= new HashMap<>(1);
data.put(BuildPathsPropertyPage.DATA_REVEAL_ENTRY, entry);
if (entry.getEntryKind() != IClasspathEntry.CPE_CONTAINER) {
data.put(BuildPathsPropertyPage.DATA_REVEAL_ATTRIBUTE_KEY, CPListElement.ACCESSRULES);
}
}
} catch (JavaModelException e) {
// ignore
}
}
}
}
PreferencesUtil.createPropertyDialogOn(JavaPlugin.getActiveWorkbenchShell(), fProject, BuildPathsPropertyPage.PROP_ID, null, data).open();
}
/*
* @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension5#getAdditionalProposalInfo(org.eclipse.core.runtime.IProgressMonitor)
* @since 3.5
*/
@Override
public Object getAdditionalProposalInfo(IProgressMonitor monitor) {
return Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_configure_buildpath_description, BasicElementLabels.getResourceName(fProject));
}
}
private static final class ChangeToRequiredCompilerCompliance extends ChangeCorrectionProposal implements IWorkspaceRunnable {
private final IJavaProject fProject;
private final boolean fChangeOnWorkspace;
private final String fRequiredVersion;
private final boolean fEnablePreviews;
private Job fUpdateJob;
private boolean fRequiredJREFound;
public ChangeToRequiredCompilerCompliance(String name, IJavaProject project, boolean changeOnWorkspace, String requiredVersion, int relevance) {
this(name, project, changeOnWorkspace, requiredVersion, false, relevance);
}
public ChangeToRequiredCompilerCompliance(String name, IJavaProject project, boolean changeOnWorkspace, String requiredVersion, boolean enablePreviews, int relevance) {
super(name, null, relevance, JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE));
fProject= project;
fChangeOnWorkspace= changeOnWorkspace;
fRequiredVersion= requiredVersion;
fUpdateJob= null;
fRequiredJREFound= false;
fEnablePreviews= enablePreviews;
}
private boolean isRequiredOrGreaterVMInstall(IVMInstall install) {
if (install instanceof IVMInstall2) {
String compliance= JavaModelUtil.getCompilerCompliance((IVMInstall2) install, JavaCore.VERSION_1_3);
return !JavaModelUtil.isVersionLessThan(compliance, fRequiredVersion);
}
return false;
}
@Override
public void run(IProgressMonitor monitor) throws CoreException {
boolean needsBuild= updateJRE(monitor);
if (needsBuild) {
fUpdateJob= CoreUtility.getBuildJob(fChangeOnWorkspace ? null : fProject.getProject());
}
}
private boolean updateJRE( IProgressMonitor monitor) throws CoreException, JavaModelException {
// Caveat: Returns true iff the classpath has not been changed.
// If the classpath is changed, JDT Core triggers a build for free.
// If the classpath is not changed, we have to trigger a build because we changed
// the compiler compliance in #apply(IDocument).
try {
if (fChangeOnWorkspace) {
IVMInstall vmInstall= ClasspathVMUtil.findRequiredOrGreaterVMInstall(fRequiredVersion, false, false);
fRequiredJREFound= vmInstall != null;
if (vmInstall != null) {
IVMInstall install= JavaRuntime.getVMInstall(fProject); // can be null
monitor.beginTask(CorrectionMessages.ReorgCorrectionsSubProcessor_50_compliance_operation, 4);
IVMInstall defaultVM= JavaRuntime.getDefaultVMInstall(); // can be null
if (defaultVM != null && !defaultVM.equals(install)) {
IPath newPath= new Path(JavaRuntime.JRE_CONTAINER);
ClasspathVMUtil.updateClasspath(newPath, fProject, Progress.subMonitor(monitor, 1));
} else {
monitor.worked(1);
}
if (defaultVM == null || !isRequiredOrGreaterVMInstall(defaultVM)) {
JavaRuntime.setDefaultVMInstall(vmInstall, Progress.subMonitor(monitor, 3), true);
return false;
}
return true;
}
} else {
IExecutionEnvironment bestEE= ClasspathVMUtil.findBestMatchingEE(fRequiredVersion);
fRequiredJREFound= bestEE != null;
if (bestEE != null) {
IPath newPath= JavaRuntime.newJREContainerPath(bestEE);
boolean classpathUpdated= ClasspathVMUtil.updateClasspath(newPath, fProject, monitor);
return !classpathUpdated;
}
}
} finally {
monitor.done();
}
return true;
}
@Override
public Object getAdditionalProposalInfo(IProgressMonitor monitor) {
StringBuilder message= new StringBuilder();
if (fChangeOnWorkspace) {
message.append(Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_required_compliance_changeworkspace_description, fRequiredVersion));
} else {
message.append(Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_required_compliance_changeproject_description, fRequiredVersion));
}
try {
IVMInstall install= JavaRuntime.getVMInstall(fProject); // can be null
if (fChangeOnWorkspace) {
IVMInstall vmInstall= ClasspathVMUtil.findRequiredOrGreaterVMInstall(fRequiredVersion, false, false);
if (vmInstall != null) {
IVMInstall defaultVM= JavaRuntime.getDefaultVMInstall(); // can be null
if (defaultVM != null && !defaultVM.equals(install)) {
message.append(CorrectionMessages.ReorgCorrectionsSubProcessor_50_compliance_changeProjectJREToDefault_description);
}
if (defaultVM == null || !isRequiredOrGreaterVMInstall(defaultVM)) {
message.append(Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_50_compliance_changeWorkspaceJRE_description, vmInstall.getName()));
}
}
} else {
IExecutionEnvironment bestEE= ClasspathVMUtil.findBestMatchingEE(fRequiredVersion);
if (bestEE != null) {
if (install == null || !isEEOnClasspath(bestEE)) {
message.append(Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_50_compliance_changeProjectJRE_description, bestEE.getId()));
}
}
}
} catch (CoreException e) {
// ignore
}
if (fEnablePreviews) {
if (fChangeOnWorkspace) {
message.append(CorrectionMessages.PreviewFeaturesSubProcessor_enable_preview_features_workspace_info);
} else {
message.append(CorrectionMessages.PreviewFeaturesSubProcessor_enable_preview_features_info);
}
}
return message.toString();
}
private boolean isEEOnClasspath(IExecutionEnvironment ee) throws JavaModelException {
IPath eePath= JavaRuntime.newJREContainerPath(ee);
for (IClasspathEntry entry: fProject.getRawClasspath()) {
if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER && entry.getPath().equals(eePath))
return true;
}
return false;
}
@Override
public void apply(IDocument document) {
if (fChangeOnWorkspace) {
Hashtable<String, String> map= JavaCore.getOptions();
JavaModelUtil.setComplianceOptions(map, fRequiredVersion);
if (fEnablePreviews) {
map.put(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED);
}
JavaCore.setOptions(map);
} else {
Map<String, String> map= fProject.getOptions(false);
int optionsCount= map.size();
JavaModelUtil.setComplianceOptions(map, fRequiredVersion);
if (map.size() > optionsCount) {
// options have been added -> ensure that all compliance options from preference page set
JavaModelUtil.setDefaultClassfileOptions(map, fRequiredVersion);
}
if (fEnablePreviews) {
map.put(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED);
}
fProject.setOptions(map);
}
try {
IProgressService progressService= PlatformUI.getWorkbench().getProgressService();
progressService.run(true, true, new WorkbenchRunnableAdapter(this));
} catch (InvocationTargetException e) {
JavaPlugin.log(e);
} catch (InterruptedException e) {
return;
}
if (fUpdateJob != null) {
fUpdateJob.schedule();
}
if (!fRequiredJREFound) {
MessageDialog.openInformation(JavaPlugin.getActiveWorkbenchShell(),
Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_no_required_jre_title, fRequiredVersion),
Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_no_required_jre_message, fRequiredVersion));
}
}
}
/**
* 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 static void getNeedHigherComplianceProposals(IInvocationContextCore context, IProblemLocationCore problem, Collection<ICommandAccess> proposals, String requiredVersion) {
getNeedHigherComplianceProposals(context, problem, proposals, false, requiredVersion);
}
public static void getNeedHigherComplianceProposals(IInvocationContextCore context, IProblemLocationCore problem, Collection<ICommandAccess> proposals) {
String[] args= problem.getProblemArguments();
if (args != null && args.length == 2) {
ReorgCorrectionsSubProcessor.getNeedHigherComplianceProposals(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
*/
static void getNeedHigherComplianceProposals(IInvocationContextCore context, IProblemLocationCore problem, Collection<ICommandAccess> 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(new ChangeToRequiredCompilerCompliance(label1, project, false, requiredVersion, enablePreviews, IProposalRelevance.CHANGE_PROJECT_COMPLIANCE));
} else {
proposals.add(new ChangeToRequiredCompilerCompliance(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});
}
proposals.add(new ChangeToRequiredCompilerCompliance(label2, project, true, requiredVersion, enablePreviews, IProposalRelevance.CHANGE_WORKSPACE_COMPLIANCE));
}
}
/**
* Adds a proposal that opens the build path dialog
* @param context the context
* @param problem the current problem
* @param proposals the resulting proposals
*/
public static void getIncorrectBuildPathProposals(IInvocationContextCore context, IProblemLocationCore problem, Collection<ICommandAccess> proposals) {
IProject project= context.getCompilationUnit().getJavaProject().getProject();
String label= CorrectionMessages.ReorgCorrectionsSubProcessor_configure_buildpath_label;
OpenBuildPathCorrectionProposal proposal= new OpenBuildPathCorrectionProposal(project, label, IProposalRelevance.CONFIGURE_BUILD_PATH, null);
proposals.add(proposal);
}
public static void getAccessRulesProposals(IInvocationContextCore context, IProblemLocationCore problem, Collection<ICommandAccess> proposals) {
IBinding referencedElement= null;
ASTNode node= problem.getCoveredNode(context.getASTRoot());
if (node instanceof Type) {
referencedElement= ((Type) node).resolveBinding();
} else if (node instanceof Name) {
referencedElement= ((Name) node).resolveBinding();
}
if (referencedElement != null && canModifyAccessRules(referencedElement)) {
IProject project= context.getCompilationUnit().getJavaProject().getProject();
String label= CorrectionMessages.ReorgCorrectionsSubProcessor_accessrules_description;
OpenBuildPathCorrectionProposal proposal= new OpenBuildPathCorrectionProposal(project, label, IProposalRelevance.CONFIGURE_ACCESS_RULES, referencedElement);
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;
}
ReorgCorrectionsSubProcessor() {
}
@Override
public ICommandAccess createRenameCUProposal(String label, RenameCompilationUnitChange change, int renameCu) {
return new ChangeCorrectionProposal(label, change, IProposalRelevance.RENAME_CU, JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_RENAME));
}
@Override
public ICommandAccess createCorrectMainTypeNameProposal(ICompilationUnit cu, IInvocationContextCore context, String currTypeName, String newTypeName, int renameType) {
return new CorrectMainTypeNameProposal(cu, context, currTypeName, newTypeName, IProposalRelevance.RENAME_TYPE);
}
@Override
protected ICommandAccess createCorrectPackageDeclarationProposal(ICompilationUnit cu, IProblemLocationCore problem, int relevance) {
return new CorrectPackageDeclarationProposal(cu, problem, relevance);
}
@Override
protected ICommandAccess createMoveToNewPackageProposal(String label, CompositeChange composite, int moveCuToPackage) {
return new ChangeCorrectionProposal(label, composite, IProposalRelevance.MOVE_CU_TO_PACKAGE, JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_MOVE));
}
@Override
protected ICommandAccess createOrganizeImportsProposal(String name, Object object, ICompilationUnit cu, int organizeImports) {
ChangeCorrectionProposal proposal= new ChangeCorrectionProposal(name, null, IProposalRelevance.ORGANIZE_IMPORTS, JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE)) {
@Override
public void apply(IDocument document) {
IEditorInput input= new FileEditorInput((IFile) cu.getResource());
IWorkbenchPage p= JavaPlugin.getActivePage();
if (p == null) {
return;
}
IEditorPart part= p.findEditor(input);
if (part instanceof JavaEditor) {
OrganizeImportsAction action= new OrganizeImportsAction((JavaEditor) part);
action.run(cu);
}
}
};
return proposal;
}
@Override
protected ICommandAccess createRemoveUnusedImportProposal(IProposableFix fix, UnusedCodeCleanUp unusedCodeCleanUp, int removeUnusedImport, IInvocationContextCore context) {
Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_DELETE_IMPORT);
FixCorrectionProposal proposal= new FixCorrectionProposal(fix, unusedCodeCleanUp, IProposalRelevance.REMOVE_UNUSED_IMPORT, image, context);
return proposal;
}
@Override
public ICommandAccess createProjectSetupFixProposal(IInvocationContextCore context, IProblemLocationCore problem, String missingType, Collection<ICommandAccess> proposals) {
return new ClasspathFixCorrectionProposal(context.getCompilationUnit(), problem.getOffset(), problem.getLength(), missingType);
}
@Override
public boolean addImportNotFoundProposals(IInvocationContextCore context, IProblemLocationCore problem, Collection<ICommandAccess> proposals) throws CoreException {
return ReorgCorrectionsSubProcessor.importNotFoundProposals(context, problem, proposals);
}
@Override
protected ICommandAccess createChangeToRequiredCompilerComplianceProposal(String label1, IJavaProject project, boolean b, String requiredVersion, int changeProjectCompliance) {
return new ChangeToRequiredCompilerCompliance(label1, project, b, requiredVersion, changeProjectCompliance);
}
@Override
protected ICommandAccess createChangeToRequiredCompilerComplianceProposal(String label2, IJavaProject project, boolean b, String requiredVersion, boolean enablePreviews,
int changeWorkspaceCompliance) {
return new ChangeToRequiredCompilerCompliance(label2, project, b, requiredVersion, enablePreviews, changeWorkspaceCompliance);
}
@Override
protected ICommandAccess createOpenBuildPathCorrectionProposal(IProject project, String label, int relevance, IBinding referencedElement) {
// TODO Auto-generated method stub
return new OpenBuildPathCorrectionProposal(project, label, IProposalRelevance.CONFIGURE_BUILD_PATH, null);
}
}