Skip to content

Commit 7944eef

Browse files
committed
Merge pull request #156 from adangel:fix-deprecations
Cleanup and fix deprecations #156
2 parents a3b5a6e + 8ae6be1 commit 7944eef

File tree

274 files changed

+2330
-3398
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

274 files changed

+2330
-3398
lines changed

ReleaseNotes.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ This is a minor release.
2020

2121
### API Changes
2222

23+
#### Deprecated for removal
24+
25+
* `net.sourceforge.pmd.eclipse.plugin.EclipseUtil`
26+
* `net.sourceforge.pmd.eclipse.plugin.PMDPlugin.getPluginFolder()`
27+
* `net.sourceforge.pmd.eclipse.runtime.cmd.BaseVisitor.isUseTaskMarker()`
28+
* `net.sourceforge.pmd.eclipse.runtime.cmd.BaseVisitor.setUseTaskMarker(boolean)`
29+
* `net.sourceforge.pmd.eclipse.ui.preferences.br.FilterManager`
30+
* `net.sourceforge.pmd.eclipse.util.IOUtil`
31+
* All classes that couldn't be instantiated because they had a private constructor only
32+
are now also `final`.
33+
2334
### External Contributions
2435

2536
## 26-June-2021: 4.25.0.v20210626-0908

net.sourceforge.pmd.eclipse.plugin.test.fragment/src/main/java/test/RuleSetsExtension.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class RuleSetsExtension implements IRuleSetsExtension {
3131
* Replace the core plugin fragment with our own rulesets
3232
* @see net.sourceforge.pmd.eclipse.core.IRuleSetsExtension#registerRuleSets(java.util.Set)
3333
*/
34+
@Override
3435
public void registerRuleSets(Set<RuleSet> registeredRuleSets) {
3536
try {
3637
RuleSet ruleSet1 = getRuleSet1();
@@ -49,6 +50,7 @@ public void registerRuleSets(Set<RuleSet> registeredRuleSets) {
4950
* (for instance when creating a new workspace)
5051
* @see net.sourceforge.pmd.eclipse.core.IRuleSetsExtension#registerDefaultRuleSets(java.util.Set)
5152
*/
53+
@Override
5254
public void registerDefaultRuleSets(Set<RuleSet> defaultRuleSets) {
5355
try {
5456
RuleSet ruleSet1 = getRuleSet1();

net.sourceforge.pmd.eclipse.plugin.test/src/main/java/net/sourceforge/pmd/eclipse/EclipseUtils.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -142,19 +142,18 @@ public static IFile createTestSourceFile(final IProject project)
142142
throws JavaModelException, CoreException, IOException {
143143

144144
// 1. Locate the test java source template
145-
final InputStream is = EclipseUtils.class.getResourceAsStream("/test.template");
146-
147-
// 2. Copy the template inside the source directory
148-
final IFile sourceFile = project.getFile("/src/Test.java");
149-
if (sourceFile.exists() && sourceFile.isAccessible()) {
150-
sourceFile.setContents(is, true, false, null);
151-
} else {
152-
sourceFile.create(is, true, null);
145+
try (InputStream is = EclipseUtils.class.getResourceAsStream("/test.template")) {
146+
// 2. Copy the template inside the source directory
147+
final IFile sourceFile = project.getFile("/src/Test.java");
148+
if (sourceFile.exists() && sourceFile.isAccessible()) {
149+
sourceFile.setContents(is, true, false, null);
150+
} else {
151+
sourceFile.create(is, true, null);
152+
}
153+
project.refreshLocal(IResource.DEPTH_INFINITE, null);
154+
155+
return sourceFile;
153156
}
154-
is.close();
155-
project.refreshLocal(IResource.DEPTH_INFINITE, null);
156-
157-
return sourceFile;
158157
}
159158

160159
/**
@@ -192,7 +191,7 @@ public static boolean removePMDNature(final IProject project) throws CoreExcepti
192191
final String[] natureIds = description.getNatureIds();
193192
final String[] newNatureIds = new String[natureIds.length - 1];
194193
for (int i = 0, j = 0; i < natureIds.length; i++) {
195-
if (!natureIds[i].equals(PMDNature.PMD_NATURE)) {
194+
if (!PMDNature.PMD_NATURE.equals(natureIds[i])) {
196195
newNatureIds[j++] = natureIds[i];
197196
}
198197
}
@@ -265,7 +264,7 @@ private static boolean propertiesMatchFor(final Rule ruleA, final Rule ruleB) {
265264
// simple equals doesn't work for RegexProperties whose value type is java.util.regex.Pattern...
266265
//return ruleAProperties.equals(ruleBProperties);
267266

268-
if (ruleAProperties == ruleBProperties) {
267+
if (ruleAProperties == ruleBProperties) { // NOPMD: CompareObjectsWithEquals is needed here
269268
return true;
270269
}
271270

net.sourceforge.pmd.eclipse.plugin.test/src/main/java/net/sourceforge/pmd/eclipse/core/RuleSetManagerTest.java

Lines changed: 17 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ public void testDuplicateUnregister() {
7474
}
7575

7676
/**
77-
* Unregistering twice the same Default rule set has no effect
78-
*
77+
* Unregistering twice the same Default rule set has no effect.
7978
*/
8079
@Test
8180
public void testDuplicateUnregisterDefault() {
@@ -88,8 +87,7 @@ public void testDuplicateUnregisterDefault() {
8887
}
8988

9089
/**
91-
* Test the register default ruleset
92-
*
90+
* Test the register default ruleset.
9391
*/
9492
@Test
9593
public void testRegisterDefaultRuleSet() {
@@ -99,36 +97,23 @@ public void testRegisterDefaultRuleSet() {
9997
}
10098

10199
/**
102-
* Test the registration of a null ruleset
103-
*
100+
* Test the registration of a null ruleset.
104101
*/
105-
@Test
102+
@Test(expected = IllegalArgumentException.class)
106103
public void testRegisterNullDefaultRuleSet() {
107-
try {
108-
this.ruleSetManager.registerDefaultRuleSet(null);
109-
Assert.fail("Should return an IllegalArgumentException");
110-
} catch (final IllegalArgumentException e) {
111-
// cool
112-
}
104+
this.ruleSetManager.registerDefaultRuleSet(null);
113105
}
114106

115107
/**
116-
* Test the registration of a null ruleset
117-
*
108+
* Test the registration of a null ruleset.
118109
*/
119-
@Test
110+
@Test(expected = IllegalArgumentException.class)
120111
public void testRegisterNullRuleSet() {
121-
try {
122-
this.ruleSetManager.registerRuleSet(null);
123-
Assert.fail("Should return an IllegalArgumentException");
124-
} catch (final IllegalArgumentException e) {
125-
// cool
126-
}
112+
this.ruleSetManager.registerRuleSet(null);
127113
}
128114

129115
/**
130-
* Test the register ruleset
131-
*
116+
* Test the register ruleset.
132117
*/
133118
@Test
134119
public void testRegisterRuleSet() {
@@ -138,8 +123,7 @@ public void testRegisterRuleSet() {
138123
}
139124

140125
/**
141-
* Test unregistration default
142-
*
126+
* Test unregistration default.
143127
*/
144128
@Test
145129
public void testUnregisterDefaultRuleSet() {
@@ -152,36 +136,23 @@ public void testUnregisterDefaultRuleSet() {
152136
}
153137

154138
/**
155-
* Unregistering a null default ruleset is illegal
156-
*
139+
* Unregistering a null default ruleset is illegal.
157140
*/
158-
@Test
141+
@Test(expected = RuntimeException.class)
159142
public void testUnregisterNullDefaultRuleSet() {
160-
try {
161-
this.ruleSetManager.unregisterDefaultRuleSet(null);
162-
Assert.fail("An IllegalArgumentException should be returned");
163-
} catch (final RuntimeException e) {
164-
// cool
165-
}
143+
this.ruleSetManager.unregisterDefaultRuleSet(null);
166144
}
167145

168146
/**
169-
* Unregistering a null ruleset is illegal
170-
*
147+
* Unregistering a null ruleset is illegal.
171148
*/
172-
@Test
149+
@Test(expected = RuntimeException.class)
173150
public void testUnregisterNullRuleSet() {
174-
try {
175-
this.ruleSetManager.unregisterRuleSet(null);
176-
Assert.fail("An IllegalArgumentException should be returned");
177-
} catch (final RuntimeException e) {
178-
// cool
179-
}
151+
this.ruleSetManager.unregisterRuleSet(null);
180152
}
181153

182154
/**
183-
* Test unregistration
184-
*
155+
* Test unregistration.
185156
*/
186157
@Test
187158
public void testUnregisterRuleSet() {

net.sourceforge.pmd.eclipse.plugin.test/src/main/java/net/sourceforge/pmd/eclipse/internal/ResourceUtil.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
package net.sourceforge.pmd.eclipse.internal;
66

77
import java.io.File;
8-
import java.io.FileOutputStream;
98
import java.io.IOException;
109
import java.io.InputStream;
10+
import java.io.OutputStream;
11+
import java.nio.file.Files;
1112

12-
public class ResourceUtil {
13+
public final class ResourceUtil {
1314
private ResourceUtil() {
1415
// utility
1516
}
@@ -19,7 +20,7 @@ public static void copyResource(Object context, String resource, File target) th
1920
if (parent != null && !parent.exists()) {
2021
parent.mkdirs();
2122
}
22-
try (FileOutputStream out = new FileOutputStream(target);
23+
try (OutputStream out = Files.newOutputStream(target.toPath());
2324
InputStream in = context.getClass().getResourceAsStream(resource)) {
2425
int count;
2526
byte[] buffer = new byte[8192];

net.sourceforge.pmd.eclipse.plugin.test/src/main/java/net/sourceforge/pmd/eclipse/runtime/cmd/DetectCutAndPasteCmdTest.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@
2929
public class DetectCutAndPasteCmdTest {
3030
private IProject testProject;
3131

32-
/**
33-
* @see junit.framework.TestCase#setUp()
34-
*/
3532
@Before
3633
public void setUp() throws Exception {
3734
// 1. Create a Java project
@@ -41,9 +38,9 @@ public void setUp() throws Exception {
4138

4239
// 2. Create a test source file inside that project
4340
EclipseUtils.createTestSourceFile(this.testProject);
44-
final InputStream is = EclipseUtils.getResourceStream(this.testProject, "/src/Test.java");
45-
Assert.assertNotNull("Cannot find the test source file", is);
46-
is.close();
41+
try (InputStream is = EclipseUtils.getResourceStream(this.testProject, "/src/Test.java")) {
42+
Assert.assertNotNull("Cannot find the test source file", is);
43+
}
4744
}
4845

4946
@After

net.sourceforge.pmd.eclipse.plugin.test/src/main/java/net/sourceforge/pmd/eclipse/runtime/cmd/RenderReportCmdTest.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@
2929
public class RenderReportCmdTest {
3030
private IProject testProject;
3131

32-
/**
33-
* @see junit.framework.TestCase#setUp()
34-
*/
3532
@Before
3633
public void setUp() throws Exception {
3734

@@ -42,10 +39,9 @@ public void setUp() throws Exception {
4239

4340
// 2. Create a test source file inside that project
4441
EclipseUtils.createTestSourceFile(this.testProject);
45-
final InputStream is = EclipseUtils.getResourceStream(this.testProject, "/src/Test.java");
46-
Assert.assertNotNull("Cannot find the test source file", is);
47-
is.close();
48-
42+
try (InputStream is = EclipseUtils.getResourceStream(this.testProject, "/src/Test.java")) {
43+
Assert.assertNotNull("Cannot find the test source file", is);
44+
}
4945
}
5046

5147
@After

net.sourceforge.pmd.eclipse.plugin.test/src/main/java/net/sourceforge/pmd/eclipse/runtime/cmd/ReviewCmdTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ public void setUp() throws Exception {
5454
this.testProject != null && this.testProject.exists() && this.testProject.isAccessible());
5555

5656
// 2. Create a test source file inside that project
57-
final IFile testFile = EclipseUtils.createTestSourceFile(this.testProject);
58-
final InputStream is = EclipseUtils.getResourceStream(this.testProject, "/src/Test.java");
59-
Assert.assertNotNull("Cannot find the test source file", is);
60-
is.close();
57+
EclipseUtils.createTestSourceFile(this.testProject);
58+
try (InputStream is = EclipseUtils.getResourceStream(this.testProject, "/src/Test.java")) {
59+
Assert.assertNotNull("Cannot find the test source file", is);
60+
}
6161

6262
// 3. Enable PMD for the test project
6363
IProjectProperties properties = PMDPlugin.getDefault().getPropertiesManager()
@@ -93,7 +93,7 @@ public void testReviewCmdBasic() throws CoreException {
9393

9494
// We do not test PMD, only a non-empty report is enough
9595
Assert.assertNotNull(markers);
96-
Assert.assertTrue("Report size = " + markers.size(), markers.size() > 0);
96+
Assert.assertFalse("Report is empty", markers.isEmpty());
9797

9898
// test the marker types - they should be problem markers...
9999
final IFile sourceFile = this.testProject.getFile("/src/Test.java");

net.sourceforge.pmd.eclipse.plugin.test/src/main/java/net/sourceforge/pmd/eclipse/runtime/properties/ProjectPropertiesModelTest.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Iterator;
2020
import java.util.LinkedList;
2121
import java.util.List;
22+
import java.util.Objects;
2223
import java.util.Set;
2324

2425
import org.apache.commons.io.IOUtils;
@@ -125,7 +126,7 @@ public static void compareTwoRuleSets(RuleSet ruleSet1, RuleSet ruleSet2) {
125126
Rule pluginRule = it1.next();
126127
Rule projectRule = it2.next();
127128

128-
if (pluginRule != projectRule) {
129+
if (!Objects.equals(pluginRule, projectRule)) {
129130
System.out.println("i=" + i + ": pluginRule=" + pluginRule + " projectRule=" + projectRule);
130131
System.out.println("plugin: " + pluginRule.getName() + " (" + pluginRule.getLanguage() + ")");
131132
System.out.println("project: " + projectRule.getName() + " (" + projectRule.getLanguage() + ")");
@@ -172,7 +173,7 @@ public void testBug() throws PropertiesException, RuleSetNotFoundException, Core
172173
RuleSet rereadProjectRuleSet = model.getProjectRuleSet();
173174
Assert.assertEquals("The rule count should 1 less", ruleCountBefore - 1, rereadProjectRuleSet.getRules().size());
174175
for (Rule r : rereadProjectRuleSet.getRules()) {
175-
if (r.getName().equals(removedRule.getName()) && r.getLanguage() == removedRule.getLanguage()) {
176+
if (r.getName().equals(removedRule.getName()) && Objects.equals(r.getLanguage(), removedRule.getLanguage())) {
176177
Assert.fail("The rule has not been removed!");
177178
}
178179
}
@@ -327,18 +328,12 @@ public String getName() {
327328
* It should not be possible to set to null a project ruleset
328329
*
329330
*/
330-
@Test
331+
@Test(expected = PropertiesException.class)
331332
public void testProjectRuleSetNull() throws PropertiesException {
332333
final IProjectPropertiesManager mgr = PMDPlugin.getDefault().getPropertiesManager();
333334
final IProjectProperties model = mgr.loadProjectProperties(this.testProject);
334335

335-
try {
336-
model.setProjectRuleSetList(null);
337-
Assert.fail("A ModelException must be raised when setting a project ruleset to null");
338-
} catch (final PropertiesException e) {
339-
// OK that's correct
340-
}
341-
336+
model.setProjectRuleSetList(null);
342337
}
343338

344339
/**
@@ -503,7 +498,8 @@ public void testNoRulesInProperties() throws PropertiesException, RuleSetNotFoun
503498
@Test(expected = PropertiesException.class)
504499
public void testInvalidProperties() throws PropertiesException, RuleSetNotFoundException, CoreException {
505500
final IProjectPropertiesManager mgr = PMDPlugin.getDefault().getPropertiesManager();
506-
IProjectProperties model = mgr.loadProjectProperties(this.testProject);
501+
// load the project properties to fill the cache
502+
mgr.loadProjectProperties(this.testProject);
507503
// remove PMD's cached project properties, so that we reload it from disk again
508504
mgr.removeProjectProperties(this.testProject);
509505

@@ -517,7 +513,7 @@ public void testInvalidProperties() throws PropertiesException, RuleSetNotFoundE
517513
file.setContents(propertiesStream, 0, null);
518514

519515
// reload the project properties
520-
model = mgr.loadProjectProperties(this.testProject);
516+
mgr.loadProjectProperties(this.testProject);
521517
}
522518

523519
private void dumpRuleSet(final RuleSet ruleSet) {
@@ -591,7 +587,7 @@ public void testProjectClasspath() throws Exception {
591587
file.setContents(IOUtils.toInputStream(newClasspathContent, "UTF-8"), 0, null);
592588
final IProjectPropertiesManager mgr = PMDPlugin.getDefault().getPropertiesManager();
593589
IProjectProperties model = mgr.loadProjectProperties(this.testProject);
594-
URLClassLoader auxClasspath = (URLClassLoader) model.getAuxClasspath();
590+
URLClassLoader auxClasspath = (URLClassLoader) model.getAuxClasspath(); // NOPMD: don't close auxclasspath in test, this should be done by the plugin
595591
Set<URI> urls = new HashSet<>();
596592
for (URL url : auxClasspath.getURLs()) {
597593
urls.add(url.toURI());

net.sourceforge.pmd.eclipse.plugin.test/src/main/java/net/sourceforge/pmd/eclipse/ui/actions/PMDCheckActionTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ public void setUp() throws Exception {
3636

3737
// 2. Create a test source file inside that project
3838
EclipseUtils.createTestSourceFile(this.testProject);
39-
final InputStream is = EclipseUtils.getResourceStream(this.testProject, "/src/Test.java");
40-
Assert.assertNotNull("Cannot find the test source file", is);
41-
is.close();
39+
try (InputStream is = EclipseUtils.getResourceStream(this.testProject, "/src/Test.java")) {
40+
Assert.assertNotNull("Cannot find the test source file", is);
41+
}
4242

4343
// 3. Enable PMD for the test project
4444
IProjectProperties properties = PMDPlugin.getDefault().getPropertiesManager()

0 commit comments

Comments
 (0)