Skip to content

Commit ad4ea63

Browse files
authored
Ensure unused suppress warnings cleanup uses it own compiler options (eclipse-jdt#2124)
* Ensure unused suppress warnings cleanup uses it own compiler options - modify CleanUpRequirements to have a new boolean setting regarding separate compiler options and add new constructor that sets it explicitly (otherwise, it defaults to false) - modify CleanUpRefactoring.CleanUpFixpointIterator to not merge options from a cleanup that requires its own options and also modify the next() method to use these special options for such a cleanup which must also request a fresh AST and must be at the end of the cleanup list with others that do the same - fixes eclipse-jdt#2123
1 parent 4cfcabe commit ad4ea63

File tree

4 files changed

+63
-12
lines changed

4 files changed

+63
-12
lines changed

org.eclipse.jdt.core.manipulation/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.core.manipulation
33
Bundle-ManifestVersion: 2
44
Bundle-Name: %pluginName
55
Bundle-SymbolicName: org.eclipse.jdt.core.manipulation; singleton:=true
6-
Bundle-Version: 1.22.100.qualifier
6+
Bundle-Version: 1.23.0.qualifier
77
Bundle-Vendor: %providerName
88
Bundle-Activator: org.eclipse.jdt.internal.core.manipulation.JavaManipulationPlugin
99
Bundle-Localization: plugin

org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/ui/fix/UnusedSuppressWarningsCleanUp.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*******************************************************************************/
1414
package org.eclipse.jdt.internal.ui.fix;
1515

16+
import java.util.Hashtable;
1617
import java.util.Map;
1718

1819
import org.eclipse.core.runtime.CoreException;
@@ -60,7 +61,7 @@ public CleanUpRequirements getRequirements() {
6061
boolean requireAST= requireAST();
6162
Map<String, String> requiredOptions= requireAST ? getRequiredOptions() : null;
6263
// ask for fresh AST as we are setting all default options on and we run as last cleanup
63-
return new CleanUpRequirements(requireAST, requireAST, false, requiredOptions);
64+
return new CleanUpRequirements(requireAST, requireAST, false, requireAST, requiredOptions);
6465
}
6566

6667
private boolean requireAST() {
@@ -69,7 +70,7 @@ private boolean requireAST() {
6970

7071
@Override
7172
protected ICleanUpFix createFix(CompilationUnit compilationUnit) throws CoreException {
72-
if (compilationUnit == null)
73+
if (compilationUnit == null || !isEnabled(CleanUpConstants.REMOVE_UNNECESSARY_SUPPRESS_WARNINGS))
7374
return null;
7475

7576
ICleanUpFix coreFix= fLiteral != null ? UnusedSuppressWarningsFixCore.createAllFix(fSavedCompilationUnit == null ? compilationUnit : fSavedCompilationUnit,
@@ -79,20 +80,18 @@ protected ICleanUpFix createFix(CompilationUnit compilationUnit) throws CoreExce
7980

8081
@Override
8182
protected ICleanUpFix createFix(CompilationUnit compilationUnit, IProblemLocation[] problems) throws CoreException {
82-
if (compilationUnit == null)
83+
if (compilationUnit == null || !isEnabled(CleanUpConstants.REMOVE_UNNECESSARY_SUPPRESS_WARNINGS))
8384
return null;
8485

8586
ICleanUpFix coreFix= UnusedSuppressWarningsFixCore.createAllFix(compilationUnit, fLiteral);
8687
return coreFix;
8788
}
8889

8990
private Map<String, String> getRequiredOptions() {
90-
// This cleanup is run last and can require using all default options to maximize
91-
// chance of finding an unused suppress warnings annotation.
92-
// We ask for a fresh AST so all other cleanups are complete before we do this
93-
Map<String, String> result= JavaCore.getOptions();
91+
Map<String, String> result= new Hashtable<>();
9492

9593
if (isEnabled(CleanUpConstants.REMOVE_UNNECESSARY_SUPPRESS_WARNINGS)) {
94+
result.putAll(JavaCore.getDefaultOptions());
9695
result.put(JavaCore.COMPILER_PB_SUPPRESS_WARNINGS, JavaCore.ENABLED);
9796
result.put(JavaCore.COMPILER_PB_SUPPRESS_OPTIONAL_ERRORS, JavaCore.ENABLED);
9897
result.put(JavaCore.COMPILER_PB_UNUSED_WARNING_TOKEN, JavaCore.ERROR); // do not change to WARNING

org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/ui/cleanup/CleanUpRequirements.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public final class CleanUpRequirements {
3737

3838
protected final boolean fRequiresChangedRegions;
3939

40+
protected final boolean fRequiresSeparateOptions;
4041

4142
/**
4243
* Create a new instance
@@ -47,11 +48,28 @@ public final class CleanUpRequirements {
4748
* @param compilerOptions map of compiler options or <code>null</code> if no requirements
4849
*/
4950
public CleanUpRequirements(boolean requiresAST, boolean requiresFreshAST, boolean requiresChangedRegions, Map<String, String> compilerOptions) {
51+
this(requiresAST, requiresFreshAST, requiresChangedRegions, false, compilerOptions);
52+
}
53+
54+
/**
55+
* Create a new instance
56+
*
57+
* @param requiresAST <code>true</code> if an AST is required
58+
* @param requiresFreshAST <code>true</code> if a fresh AST is required
59+
* @param requiresChangedRegions <code>true</code> if changed regions are required
60+
* @param requiresSeparateOptions <code>true</code> if clean up has options that should not be shared
61+
* @param compilerOptions map of compiler options or <code>null</code> if no requirements
62+
* @since 1.23
63+
*/
64+
public CleanUpRequirements(boolean requiresAST, boolean requiresFreshAST, boolean requiresChangedRegions,
65+
boolean requiresSeparateOptions, Map<String, String> compilerOptions) {
5066
Assert.isLegal(!requiresFreshAST || requiresAST, "Must not request fresh AST if no AST is required"); //$NON-NLS-1$
5167
Assert.isLegal(compilerOptions == null || requiresAST, "Must not provide options if no AST is required"); //$NON-NLS-1$
68+
Assert.isLegal(!requiresSeparateOptions || requiresFreshAST, "Must not require separate options if fresh AST not required"); //$NON-NLS-1$
5269
fRequiresAST= requiresAST;
5370
fRequiresFreshAST= requiresFreshAST;
5471
fRequiresChangedRegions= requiresChangedRegions;
72+
fRequiresSeparateOptions= requiresSeparateOptions;
5573

5674
fCompilerOptions= compilerOptions;
5775
// Make sure that compile warnings are not suppressed since some clean ups work on reported warnings
@@ -86,6 +104,17 @@ public boolean requiresFreshAST() {
86104
return fRequiresFreshAST;
87105
}
88106

107+
/**
108+
* Tells whether separate compiler options are required as the options should not be shared with
109+
* other cleanups.
110+
*
111+
* @return <code>true</code> if the cleanup has its own options.
112+
* @since 1.23
113+
*/
114+
public boolean requiresSeparateOptions() {
115+
return fRequiresSeparateOptions;
116+
}
117+
89118
/**
90119
* Required compiler options.
91120
*

org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/fix/CleanUpRefactoring.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,16 @@
8282
import org.eclipse.jdt.ui.JavaElementLabels;
8383
import org.eclipse.jdt.ui.cleanup.CleanUpContext;
8484
import org.eclipse.jdt.ui.cleanup.CleanUpOptions;
85+
import org.eclipse.jdt.ui.cleanup.CleanUpRequirements;
8586
import org.eclipse.jdt.ui.cleanup.ICleanUp;
8687
import org.eclipse.jdt.ui.cleanup.ICleanUpFix;
88+
import org.eclipse.jdt.ui.text.java.IProblemLocation;
8789

8890
import org.eclipse.jdt.internal.ui.IJavaStatusConstants;
8991
import org.eclipse.jdt.internal.ui.JavaPlugin;
9092
import org.eclipse.jdt.internal.ui.fix.IMultiFix.MultiFixContext;
9193
import org.eclipse.jdt.internal.ui.fix.MapCleanUpOptions;
9294
import org.eclipse.jdt.internal.ui.refactoring.IScheduledRefactoring;
93-
import org.eclipse.jdt.ui.text.java.IProblemLocation;
9495
import org.eclipse.jdt.internal.ui.util.Progress;
9596

9697
public class CleanUpRefactoring extends Refactoring implements IScheduledRefactoring {
@@ -329,6 +330,7 @@ private class CleanUpFixpointIterator {
329330
private final Hashtable<ICompilationUnit, List<CleanUpChange>> fSolutions;
330331
private final Hashtable<ICompilationUnit, ICompilationUnit> fWorkingCopies; // map from primary to working copy
331332
private final Map<String, String> fCleanUpOptions;
333+
private final Map<String, String> fSeparateOptions;
332334
private final int fSize;
333335
private int fIndex;
334336

@@ -343,11 +345,15 @@ public CleanUpFixpointIterator(CleanUpTarget[] targets, ICleanUp[] cleanUps) {
343345

344346
fCleanUpOptions= new Hashtable<>();
345347
for (ICleanUp cleanUp : cleanUps) {
346-
Map<String, String> currentCleanUpOption= cleanUp.getRequirements().getCompilerOptions();
347-
if (currentCleanUpOption != null)
348+
CleanUpRequirements requirements= cleanUp.getRequirements();
349+
Map<String, String> currentCleanUpOption= requirements.getCompilerOptions();
350+
boolean needsSeparateOptions= requirements.requiresSeparateOptions();
351+
if (currentCleanUpOption != null && !needsSeparateOptions)
348352
fCleanUpOptions.putAll(currentCleanUpOption);
349353
}
350354

355+
fSeparateOptions= new Hashtable<>();
356+
351357
fSize= targets.length;
352358
fIndex= 1;
353359
}
@@ -390,7 +396,11 @@ protected ASTParser createParser(IJavaProject project) {
390396
result.setProject(project);
391397

392398
Map<String, String> options= RefactoringASTParser.getCompilerOptions(project);
393-
options.putAll(fCleanUpOptions);
399+
if (!fSeparateOptions.isEmpty()) {
400+
options.putAll(fSeparateOptions);
401+
} else {
402+
options.putAll(fCleanUpOptions);
403+
}
394404
result.setCompilerOptions(options);
395405
return result;
396406
}
@@ -403,6 +413,8 @@ protected ASTParser createParser(IJavaProject project) {
403413
}
404414
}
405415

416+
fSeparateOptions.clear();
417+
406418
for (ICompilationUnit cu : sourceList) {
407419
monitor.worked(1);
408420

@@ -413,6 +425,17 @@ protected ASTParser createParser(IJavaProject project) {
413425
}
414426

415427
fParseList= requestor.getUndoneElements();
428+
// check if undone cleanup requires separate options in which case, set up special options.
429+
if (fParseList != null && !fParseList.isEmpty()) {
430+
ParseListElement element= fParseList.get(0);
431+
if (element.getCleanUps() != null && element.getCleanUps().length > 0) {
432+
ICleanUp cleanUp= element.getCleanUps()[0];
433+
if (cleanUp.getRequirements().requiresSeparateOptions()) {
434+
fSeparateOptions.putAll(cleanUp.getRequirements().getCompilerOptions());
435+
}
436+
}
437+
}
438+
416439
fIndex= cuMonitor.getIndex();
417440
} finally {
418441
}

0 commit comments

Comments
 (0)