Skip to content

Commit 769326c

Browse files
authored
Merge pull request #174 from adangel/upgrade-to-pmd7
Upgrade to PMD 7
2 parents b32a0cc + 4611235 commit 769326c

File tree

68 files changed

+952
-1466
lines changed

Some content is hidden

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

68 files changed

+952
-1466
lines changed

ReleaseNotes.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,18 @@ This is a major release.
2020
For Bugfixes in the PMD Eclipse Plugin only, a new version is released with the same major, minor, and patch,
2121
but a new qualifier only.
2222

23+
* The minimum required Java version is Java 8.
24+
25+
* The PMD Designer can't be started from within the plugin anymore. Please install
26+
[PMD Designer](https://github.com/pmd/pmd-designer) separately when needed.
27+
28+
* Updated to PMD 7.0.0-rc1
29+
2330
### Fixed Issues
2431

2532
* [#122](https://github.com/pmd/pmd-eclipse-plugin/issues/122): Update PMD 7 Logo in eclipse pmd plugin
2633
* [#172](https://github.com/pmd/pmd-eclipse-plugin/issues/172): Only support the last 4 eclipse releases
34+
* [#174](https://github.com/pmd/pmd-eclipse-plugin/pull/174): Upgrade to PMD 7
2735

2836
### API Changes
2937

do-release.sh

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ if [ ! -f pom.xml ] || [ ! -d ${PMD_GITHUB_IO_DIR} ]; then
1717
fi
1818

1919
RELEASE_VERSION=
20-
DEVELOPMENT_VERSION=
2120
CURRENT_BRANCH=
2221

2322
echo "-------------------------------------------"
@@ -37,30 +36,37 @@ else
3736
NEXT_MINOR="${MINOR}"
3837
NEXT_PATCH=$(("${PATCH}" + 1))
3938
fi
40-
DEVELOPMENT_VERSION="$MAJOR.$NEXT_MINOR.$NEXT_PATCH"
41-
DEVELOPMENT_VERSION="${DEVELOPMENT_VERSION}"
4239

43-
# allow to override the next version, e.g. via "NEXT_VERSION=7.0.0 ./do-release.sh"
44-
if [ "$NEXT_VERSION" != "" ]; then
45-
DEVELOPMENT_VERSION="${NEXT_VERSION}"
40+
# allow to override the next version, e.g. via "DEVELOPMENT_VERSION=7.0.0-SNAPSHOT ./do-release.sh"
41+
if [ "$DEVELOPMENT_VERSION" = "" ]; then
42+
DEVELOPMENT_VERSION="$MAJOR.$NEXT_MINOR.$NEXT_PATCH-SNAPSHOT"
43+
fi
44+
45+
# allow to override the build qualifier, e.g. via "BUILDQUALIFIER="$(date -u +v%Y%m%d-%H%M)-rc1" ./do-release.sh"
46+
if [ "$BUILDQUALIFIER" = "" ]; then
47+
# Pick a release BUILDQUALIFIER (e.g. v20170401-0001-r) and update versions
48+
# E.g. version is: "4.0.13" and BUILDQUALIFIER is "v20170401-0001-r".
49+
# The complete version of the plugin will be "4.0.13.v20170401-0001-r
50+
BUILDQUALIFIER="$(date -u +v%Y%m%d-%H%M)-r"
4651
fi
4752

4853
# http://stackoverflow.com/questions/1593051/how-to-programmatically-determine-the-current-checked-out-git-branch
4954
CURRENT_BRANCH=$(git symbolic-ref -q HEAD)
5055
CURRENT_BRANCH=${CURRENT_BRANCH##refs/heads/}
5156
CURRENT_BRANCH=${CURRENT_BRANCH:-HEAD}
5257

53-
# Pick a release BUILDQUALIFIER (e.g. v20170401-0001-r) and update versions
54-
# E.g. version is: "4.0.13" and BUILDQUALIFIER is "v20170401-0001-r".
55-
# The complete version of the plugin will be "4.0.13.v20170401-0001-r
56-
BUILDQUALIFIER="$(date -u +v%Y%m%d-%H%M)-r"
58+
export DEVELOPMENT_VERSION
5759
export BUILDQUALIFIER
5860

5961
echo "RELEASE_VERSION: ${RELEASE_VERSION}.${BUILDQUALIFIER} (this release)"
6062
echo "DEVELOPMENT_VERSION: ${DEVELOPMENT_VERSION} (the next version after the release)"
6163
echo "CURRENT_BRANCH: ${CURRENT_BRANCH}"
6264

63-
65+
echo
66+
echo "Is this correct?"
67+
echo
68+
echo "Press enter to continue... (or CTRL+C to cancel)"
69+
read -r
6470

6571
echo Update the ReleaseNotes with the release date and version:
6672
echo

net.sourceforge.pmd.eclipse.plugin.test.fragment/META-INF/MANIFEST.MF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ Bundle-SymbolicName: net.sourceforge.pmd.eclipse.plugin.test.fragment;singleton:
55
Bundle-Version: 7.0.0.qualifier
66
Bundle-Vendor: PMD Development Team
77
Fragment-Host: net.sourceforge.pmd.eclipse.plugin
8-
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
8+
Bundle-RequiredExecutionEnvironment: JavaSE-1.8

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

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@
66

77
import java.util.Set;
88

9+
import org.checkerframework.checker.nullness.qual.Nullable;
910
import org.eclipse.core.runtime.IStatus;
11+
import org.eclipse.core.runtime.Status;
12+
import org.slf4j.event.Level;
1013

14+
import net.sourceforge.pmd.PMDConfiguration;
1115
import net.sourceforge.pmd.RuleSet;
12-
import net.sourceforge.pmd.RuleSetFactory;
13-
import net.sourceforge.pmd.RuleSetNotFoundException;
14-
import net.sourceforge.pmd.RulesetsFactoryUtils;
16+
import net.sourceforge.pmd.RuleSetLoadException;
17+
import net.sourceforge.pmd.RuleSetLoader;
1518
import net.sourceforge.pmd.eclipse.core.IRuleSetsExtension;
1619
import net.sourceforge.pmd.eclipse.plugin.PMDPlugin;
20+
import net.sourceforge.pmd.util.log.MessageReporter;
1721

1822
/**
1923
* Sample of an RuleSets extension.
@@ -40,7 +44,7 @@ public void registerRuleSets(Set<RuleSet> registeredRuleSets) {
4044
// registeredRuleSets.clear(); // to remove all rulesets already registered
4145
registeredRuleSets.add(ruleSet1);
4246
registeredRuleSets.add(ruleSet2);
43-
} catch (RuleSetNotFoundException e) {
47+
} catch (RuleSetLoadException e) {
4448
PMDPlugin.getDefault().log(IStatus.ERROR, "Unable to load rulesets", e);
4549
}
4650
}
@@ -59,20 +63,22 @@ public void registerDefaultRuleSets(Set<RuleSet> defaultRuleSets) {
5963
// registeredRuleSets.clear(); // to remove all rulesets already registered
6064
defaultRuleSets.add(ruleSet1);
6165
defaultRuleSets.add(ruleSet2);
62-
} catch (RuleSetNotFoundException e) {
66+
} catch (RuleSetLoadException e) {
6367
PMDPlugin.getDefault().log(IStatus.ERROR, "Unable to load rulesets", e);
6468
}
6569
}
6670

6771
/**
6872
* Load the 1st ruleset
6973
* @return the 1st ruleset
70-
* @throws RuleSetNotFoundException
74+
* @throws RuleSetLoadException
7175
*/
72-
private RuleSet getRuleSet1() throws RuleSetNotFoundException {
76+
private RuleSet getRuleSet1() throws RuleSetLoadException {
7377
if (this.ruleSet1 == null) {
74-
RuleSetFactory factory = RulesetsFactoryUtils.defaultFactory();
75-
this.ruleSet1 = factory.createRuleSets("rulesets/extra1.xml").getAllRuleSets()[0];
78+
PMDConfiguration configuration = new PMDConfiguration();
79+
configuration.setReporter(new RulesetLoaderMessageReporter());
80+
RuleSetLoader ruleSetLoader = RuleSetLoader.fromPmdConfig(configuration);
81+
this.ruleSet1 = ruleSetLoader.loadFromResource("rulesets/extra1.xml");
7682
}
7783

7884
return this.ruleSet1;
@@ -81,15 +87,42 @@ private RuleSet getRuleSet1() throws RuleSetNotFoundException {
8187
/**
8288
* Load the 2nd ruleset
8389
* @return the 2nd ruleset
84-
* @throws RuleSetNotFoundException
90+
* @throws RuleSetLoadException
8591
*/
86-
private RuleSet getRuleSet2() throws RuleSetNotFoundException {
92+
private RuleSet getRuleSet2() throws RuleSetLoadException {
8793
if (this.ruleSet2 == null) {
88-
RuleSetFactory factory = RulesetsFactoryUtils.defaultFactory();
89-
this.ruleSet2 = factory.createRuleSets("rulesets/extra2.xml").getAllRuleSets()[0];
94+
PMDConfiguration configuration = new PMDConfiguration();
95+
configuration.setReporter(new RulesetLoaderMessageReporter());
96+
RuleSetLoader ruleSetLoader = RuleSetLoader.fromPmdConfig(configuration);
97+
this.ruleSet2 = ruleSetLoader.loadFromResource("rulesets/extra2.xml");
9098
}
9199

92100
return this.ruleSet2;
93101
}
94102

103+
private static class RulesetLoaderMessageReporter implements MessageReporter {
104+
private int errors = 0;
105+
106+
@Override
107+
public int numErrors() {
108+
return errors;
109+
}
110+
111+
@Override
112+
public void logEx(Level level, @Nullable String message, Object[] formatArgs, @Nullable Throwable error) {
113+
if (Level.ERROR == level) {
114+
errors++;
115+
PMDPlugin.getDefault().logError(Status.error(message));
116+
} else if (Level.WARN == level) {
117+
PMDPlugin.getDefault().logWarn(message);
118+
} else {
119+
PMDPlugin.getDefault().logInformation(message);
120+
}
121+
}
122+
123+
@Override
124+
public boolean isLoggable(Level level) {
125+
return true;
126+
}
127+
}
95128
}

net.sourceforge.pmd.eclipse.plugin.test.fragment/src/main/resources/rulesets/extra1.xml

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -242,22 +242,45 @@ Some for loops can be simplified to while loops - this makes them more concise.
242242
</example>
243243
</rule>
244244

245-
246245
<rule name="ExtraUnnecessaryConversionTemporary"
246+
language="java"
247+
since="0.1"
247248
message="Avoid unnecessary temporaries when converting primitives to Strings"
248-
class="net.sourceforge.pmd.lang.java.rule.errorprone.UnnecessaryConversionTemporaryRule">
249-
<description>
250-
Avoid unnecessary temporaries when converting primitives to Strings
251-
</description>
249+
class="net.sourceforge.pmd.lang.rule.XPathRule"
250+
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_errorprone.html#unnecessaryconversiontemporary">
251+
<description>
252+
Avoid the use temporary objects when converting primitives to Strings. Use the static conversion methods
253+
on the wrapper classes instead.
254+
</description>
252255
<priority>3</priority>
253-
<example>
254-
<![CDATA[public String convert(int x) {
255-
// this wastes an object
256-
String foo = new Integer(x).toString();
257-
// this is better
258-
return Integer.toString(x);
259-
}]]>
260-
</example>
256+
<properties>
257+
<property name="xpath">
258+
<value>
259+
<![CDATA[
260+
//MethodCall[@MethodName = 'toString']
261+
[ConstructorCall[position() = 1]
262+
[
263+
pmd-java:typeIs('java.lang.Integer')
264+
or pmd-java:typeIs('java.lang.Long')
265+
or pmd-java:typeIs('java.lang.Float')
266+
or pmd-java:typeIs('java.lang.Byte')
267+
or pmd-java:typeIs('java.lang.Double')
268+
or pmd-java:typeIs('java.lang.Short')
269+
]
270+
]
271+
]]>
272+
</value>
273+
</property>
274+
</properties>
275+
<example>
276+
<![CDATA[
277+
public String convert(int x) {
278+
String foo = new Integer(x).toString(); // this wastes an object
279+
280+
return Integer.toString(x); // preferred approach
281+
}
282+
]]>
283+
</example>
261284
</rule>
262285

263286
<rule name="ExtraOverrideBothEqualsAndHashcode"
@@ -317,6 +340,7 @@ not
317340
</rule>
318341

319342
<rule name="ExtraDoubleCheckedLocking"
343+
language="java"
320344
message="Double checked locking is not thread safe in Java."
321345
class="net.sourceforge.pmd.lang.java.rule.multithreading.DoubleCheckedLockingRule">
322346
<description>

net.sourceforge.pmd.eclipse.plugin.test/META-INF/MANIFEST.MF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Bundle-ManifestVersion: 2
33
Bundle-Version: 7.0.0.qualifier
44
Bundle-Name: PMD Test Plugin
55
Bundle-SymbolicName: net.sourceforge.pmd.eclipse.plugin.test;singleton:=true
6-
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
6+
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
77
Bundle-Vendor: PMD Development Team
88
Require-Bundle: org.eclipse.ui,
99
net.sourceforge.pmd.eclipse.plugin,

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

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,20 @@
88
import java.io.IOException;
99
import java.io.InputStream;
1010
import java.nio.charset.StandardCharsets;
11-
import java.util.ArrayList;
12-
import java.util.Collections;
11+
import java.util.Arrays;
1312
import java.util.List;
1413

1514
import org.junit.Assert;
1615
import org.junit.Test;
1716

18-
import net.sourceforge.pmd.PMD;
1917
import net.sourceforge.pmd.PMDConfiguration;
18+
import net.sourceforge.pmd.PmdAnalysis;
2019
import net.sourceforge.pmd.Report;
2120
import net.sourceforge.pmd.RuleSet;
2221
import net.sourceforge.pmd.RuleSetLoader;
2322
import net.sourceforge.pmd.RuleViolation;
2423
import net.sourceforge.pmd.lang.LanguageRegistry;
25-
import net.sourceforge.pmd.renderers.Renderer;
24+
import net.sourceforge.pmd.lang.java.JavaLanguageModule;
2625
import net.sourceforge.pmd.util.datasource.DataSource;
2726

2827
/**
@@ -69,24 +68,24 @@ public void testDefaulltRuleSets() {
6968

7069
private void runPmd(String javaVersion) {
7170
PMDConfiguration configuration = new PMDConfiguration();
72-
configuration.setDefaultLanguageVersion(LanguageRegistry.findLanguageByTerseName("java").getVersion(javaVersion));
73-
configuration.setRuleSets("category/java/codestyle.xml/UnnecessaryReturn");
71+
configuration.setDefaultLanguageVersion(
72+
LanguageRegistry.PMD.getLanguageById(JavaLanguageModule.TERSE_NAME).getVersion(javaVersion));
73+
configuration.setRuleSets(Arrays.asList("category/java/codestyle.xml/UnnecessaryReturn"));
7474
configuration.setIgnoreIncrementalAnalysis(true);
75-
RuleSetLoader rulesetLoader = RuleSetLoader.fromPmdConfig(configuration);
76-
List<RuleSet> rulesets = rulesetLoader.loadFromResources(configuration.getRuleSets());
7775

78-
List<DataSource> files = new ArrayList<>();
7976
final String sourceCode = "public class Foo {\n public void foo() {\nreturn;\n}}";
80-
files.add(new StringDataSource(sourceCode));
8177

82-
Report result = PMD.processFiles(configuration, rulesets, files,
83-
Collections.<Renderer>emptyList());
78+
try (PmdAnalysis pmd = PmdAnalysis.create(configuration)) {
79+
pmd.files().addSourceFile("Foo.java", sourceCode);
80+
Report result = pmd.performAnalysisAndCollectReport();
8481

85-
Assert.assertFalse("There should be at least one violation", result.getViolations().isEmpty());
82+
Assert.assertFalse("There should be at least one violation", result.getViolations().isEmpty());
8683

87-
final RuleViolation violation = result.getViolations().get(0);
88-
Assert.assertEquals(violation.getRule().getName(), "UnnecessaryReturn");
89-
Assert.assertEquals(3, violation.getBeginLine());
84+
final RuleViolation violation = result.getViolations().get(0);
85+
Assert.assertEquals(violation.getRule().getName(), "UnnecessaryReturn");
86+
Assert.assertEquals(3, violation.getBeginLine());
87+
}
88+
9089
}
9190

9291
/**

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -364,16 +364,22 @@ public static void createFolders(IProject testProject, String fullpath) throws C
364364
}
365365
}
366366

367-
public static void waitForJobs() throws InterruptedException {
367+
public static void waitForPMDJobs() throws InterruptedException {
368368
long start = System.currentTimeMillis();
369-
Thread.sleep(500);
370-
371-
while (!Job.getJobManager().isIdle()) {
369+
while (hasPMDJob(Job.getJobManager().find(null))) {
372370
Thread.sleep(500);
373-
374371
if (System.currentTimeMillis() - start > TimeUnit.SECONDS.toMillis(30)) {
375372
Assert.fail("Timeout while waiting for Jobs to finish");
376373
}
377374
}
378375
}
376+
377+
private static boolean hasPMDJob(Job[] jobs) {
378+
for (Job job : jobs) {
379+
if (job.getClass().getName().startsWith("net.sourceforge.pmd.eclipse.runtime.cmd.JobCommandProcessor")) {
380+
return true;
381+
}
382+
}
383+
return false;
384+
}
379385
}

0 commit comments

Comments
 (0)