Skip to content

Commit

Permalink
Merge pull request #182 from adangel:update-pmd-7.0.0-SNAPSHOT
Browse files Browse the repository at this point in the history
Update to PMD 7.0.0-rc4 #182
  • Loading branch information
adangel committed Sep 30, 2023
2 parents 04cdbee + 95ab0f9 commit c5e8aee
Show file tree
Hide file tree
Showing 39 changed files with 418 additions and 509 deletions.
12 changes: 12 additions & 0 deletions ReleaseNotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,27 @@ This is a minor release.

### New and noteworthy

* Updated to PMD 7.0.0-rc4
* Support for Eclipse 2023-06 added
* Support for Eclipse 2022-06 removed

### Fixed Issues

* [#181](https://github.com/pmd/pmd-eclipse-plugin/pull/181): Support eclipse 2023-06
* [#182](https://github.com/pmd/pmd-eclipse-plugin/pull/182): Update to PMD 7.0.0-rc4

### API Changes

* The deprecated field net.sourceforge.pmd.eclipse.runtime.PMDRuntimeConstants.MAX_VIOLATIONS_DESCRIPTOR has been
removed.
* The following deprecated classes have been removed
* net.sourceforge.pmd.eclipse.ui.preferences.br.FilterManager
* net.sourceforge.pmd.eclipse.ui.preferences.editors.FileEditorFactory
* net.sourceforge.pmd.eclipse.ui.preferences.editors.FloatEditorFactory
* The following packages are not exported anymore, as they don't exist anymore:
* net.sourceforge.pmd.cpd.renderer
* net.sourceforge.pmd.properties.constraints

### External Contributions

## 30-May-2023: 7.0.0.v20230530-1924-rc3
Expand Down
4 changes: 0 additions & 4 deletions net.sourceforge.pmd.eclipse.plugin/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@ Bundle-ClassPath: .,
target/lib/checker-qual.jar,
target/lib/common_2.13.jar,
target/lib/commons-lang3.jar,
target/lib/fastparse-v2_2.13.jar,
target/lib/geny_2.13.jar,
target/lib/groovy.jar,
target/lib/gson.jar,
target/lib/javax.activation-api.jar,
target/lib/jaxb-api.jar,
target/lib/jaxb-core.jar,
target/lib/jaxb-impl.jar,
target/lib/jcommander.jar,
target/lib/jsoup.jar,
target/lib/jul-to-slf4j.jar,
target/lib/lenses_2.13.jar,
Expand Down Expand Up @@ -96,7 +94,6 @@ Export-Package: ch.qos.logback.classic;x-friends:="net.sourceforge.pmd.eclipse.p
net.sourceforge.pmd.lang.rule.properties,
net.sourceforge.pmd.lang.dfa.report",
net.sourceforge.pmd.cpd,
net.sourceforge.pmd.cpd.renderer,
net.sourceforge.pmd.eclipse.core;uses:="net.sourceforge.pmd",
net.sourceforge.pmd.eclipse.core.impl;x-friends:="net.sourceforge.pmd.eclipse.plugin.test",
net.sourceforge.pmd.eclipse.plugin;
Expand Down Expand Up @@ -138,7 +135,6 @@ Export-Package: ch.qos.logback.classic;x-friends:="net.sourceforge.pmd.eclipse.p
net.sourceforge.pmd.lang.rule,
net.sourceforge.pmd.lang.rule.xpath,
net.sourceforge.pmd.properties,
net.sourceforge.pmd.properties.constraints,
net.sourceforge.pmd.renderers,
net.sourceforge.pmd.reporting,
net.sourceforge.pmd.util;uses:="net.sourceforge.pmd.lang.java.ast",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@
import net.sourceforge.pmd.eclipse.ui.priority.PriorityDescriptorCache;
import net.sourceforge.pmd.eclipse.ui.views.PriorityFilter;
import net.sourceforge.pmd.eclipse.util.ResourceManager;
import net.sourceforge.pmd.lang.LanguageRegistry;
import net.sourceforge.pmd.lang.LanguageVersion;
import net.sourceforge.pmd.lang.java.JavaLanguageModule;
import net.sourceforge.pmd.lang.rule.RuleReference;
Expand Down Expand Up @@ -172,7 +171,7 @@ public static LanguageVersion javaVersionFor(IProject project) {

if (jProject.exists()) {
String compilerCompliance = jProject.getOption(JavaCore.COMPILER_COMPLIANCE, true);
return LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getVersion(compilerCompliance);
return JavaLanguageModule.getInstance().getVersion(compilerCompliance);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,6 @@ public final class PMDRuntimeConstants {
public static final String[] ALL_MARKER_TYPES = new String[] { PMD_MARKER, PMD_DFA_MARKER, PMD_TASKMARKER,
PMD_MARKER_1, PMD_MARKER_2, PMD_MARKER_3, PMD_MARKER_4, PMD_MARKER_5 };

/**
* @deprecated not useful, as the property cannot occur on rules anyway. The default was used always,
* which was 1000 violations.
*/
@Deprecated
public static final net.sourceforge.pmd.properties.IntegerProperty MAX_VIOLATIONS_DESCRIPTOR =
new net.sourceforge.pmd.properties.IntegerProperty("maxviolations",
"Max allowable violations", 1, Integer.MAX_VALUE - 1, 1000, 0f);

public static final String ID_PERSPECTIVE = "net.sourceforge.pmd.eclipse.ui.views.pmdPerspective";

public static final String KEY_MARKERATT_RULENAME = "rulename";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ protected final void reviewResource(IResource resource) {
LanguageVersion languageVersion = languageDiscoverer.getDefaultLanguageVersionForFile(file.getName());
// in case it is java, select the correct java version
if (languageVersion != null
&& LanguageRegistry.getLanguage(JavaLanguageModule.NAME).equals(languageVersion.getLanguage())) {
&& JavaLanguageModule.getInstance().equals(languageVersion.getLanguage())) {
languageVersion = PMDPlugin.javaVersionFor(file.getProject());
}
if (languageVersion != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.sourceforge.pmd.cpd.Language;
import net.sourceforge.pmd.cpd.CpdCapableLanguage;
import net.sourceforge.pmd.eclipse.runtime.properties.PropertiesException;

/**
Expand All @@ -32,7 +32,7 @@ public class CPDVisitor implements IResourceVisitor {
private static final Logger LOG = LoggerFactory.getLogger(CPDVisitor.class);
private boolean includeDerivedFiles;
private ResourceWorkingSetFilter workingSetFilter;
private Language language;
private CpdCapableLanguage language;
private List<File> files;

/**
Expand All @@ -56,7 +56,7 @@ public void setWorkingSet(IWorkingSet workingSet) {
* @param language
* Only add files with that language
*/
public void setLanguage(Language language) {
public void setLanguage(CpdCapableLanguage language) {
this.language = language;
}

Expand Down Expand Up @@ -88,7 +88,8 @@ public boolean visit(IResource resource) throws CoreException {
File ioFile = file.getLocation().toFile();
try {
if (StringUtils.isNotBlank(file.getFileExtension())
&& language.getFileFilter().accept(ioFile, file.getName()) && isFileInWorkingSet(file)
&& language.hasExtension(file.getFileExtension())
&& isFileInWorkingSet(file)
&& (includeDerivedFiles || !file.isDerived())) {
LOG.debug("Add file " + resource.getName());
files.add(ioFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.core.resources.IFile;
Expand All @@ -25,16 +28,17 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.sourceforge.pmd.cpd.CPD;
import net.sourceforge.pmd.cpd.CPDConfiguration;
import net.sourceforge.pmd.cpd.CPDReport;
import net.sourceforge.pmd.cpd.Language;
import net.sourceforge.pmd.cpd.LanguageFactory;
import net.sourceforge.pmd.cpd.renderer.CPDReportRenderer;
import net.sourceforge.pmd.cpd.CPDReportRenderer;
import net.sourceforge.pmd.cpd.CpdAnalysis;
import net.sourceforge.pmd.cpd.CpdCapableLanguage;
import net.sourceforge.pmd.eclipse.plugin.PMDPlugin;
import net.sourceforge.pmd.eclipse.runtime.PMDRuntimeConstants;
import net.sourceforge.pmd.eclipse.runtime.cmd.internal.CpdResult;
import net.sourceforge.pmd.eclipse.runtime.properties.IProjectProperties;
import net.sourceforge.pmd.eclipse.runtime.properties.PropertiesException;
import net.sourceforge.pmd.lang.LanguageRegistry;

/**
* This command produces a report of the Cut And Paste detector.
Expand All @@ -44,7 +48,7 @@
*/
public class DetectCutAndPasteCmd extends AbstractProjectCommand {

private Language language;
private CpdCapableLanguage language;
private int minTileSize;
private CPDReportRenderer renderer;
private String reportName;
Expand All @@ -65,13 +69,13 @@ public DetectCutAndPasteCmd() {
listeners = new ArrayList<>();
}

private void notifyListeners(final CPDReport cpdResult) {
private void notifyListeners(final CpdResult cpdResult) {
// trigger event propertyChanged for all listeners
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
for (IPropertyListener listener : listeners) {
listener.propertyChanged(cpdResult.getMatches(), PMDRuntimeConstants.PROPERTY_CPD);
listener.propertyChanged(cpdResult, PMDRuntimeConstants.PROPERTY_CPD);
}
}
});
Expand All @@ -90,13 +94,15 @@ public void execute() {
setStepCount(files.size());
beginTask("Finding suspect Cut And Paste", getStepCount() * 2);

Consumer<CPDReport> renderer = null;
if (createReport) {
renderer = this::renderReport;
}

if (!isCanceled()) {
final CPDReport cpdResult = detectCutAndPaste(files);
final CpdResult cpdResult = detectCutAndPaste(files, renderer);

if (!isCanceled()) {
if (createReport) {
renderReport(cpdResult);
}
notifyListeners(cpdResult);
}
}
Expand Down Expand Up @@ -130,7 +136,7 @@ public void reset() {
* The language to set.
*/
public void setLanguage(String theLanguage) {
language = LanguageFactory.createLanguage(theLanguage);
language = (CpdCapableLanguage) LanguageRegistry.CPD.getLanguageById(theLanguage);
}

/**
Expand Down Expand Up @@ -206,39 +212,41 @@ private List<File> findCandidateFiles() throws PropertiesException, CoreExceptio
* @return the CPD itself for retrieving the matches.
* @throws CoreException
*/
private CPDReport detectCutAndPaste(final List<File> files) {
private CpdResult detectCutAndPaste(final List<File> files, final Consumer<CPDReport> renderer) {
LOG.debug("Searching for project files");

final CPD cpd = newCPD();
final AtomicReference<CpdResult> reportResult = new AtomicReference<>();

subTask("Collecting files for CPD");
final Iterator<File> fileIterator = files.iterator();
while (fileIterator.hasNext() && !isCanceled()) {
final File file = fileIterator.next();
try {
cpd.add(file);
CPDConfiguration config = new CPDConfiguration();
config.setMinimumTileSize(minTileSize);
config.setOnlyRecognizeLanguage(language);
config.setSourceEncoding(Charset.defaultCharset());

try (CpdAnalysis cpd = CpdAnalysis.create(config)) {
subTask("Collecting files for CPD");
final Iterator<File> fileIterator = files.iterator();
while (fileIterator.hasNext() && !isCanceled()) {
final File file = fileIterator.next();
cpd.files().addFile(file.toPath());
worked(1);
} catch (IOException e) {
LOG.warn("IOException when adding file " + file.getName() + " to CPD. Continuing.", e);
}
}

if (!isCanceled()) {
subTask("Performing CPD");
LOG.debug("Performing CPD");
cpd.go();
worked(getStepCount());
if (!isCanceled()) {
subTask("Performing CPD");
LOG.debug("Performing CPD");
cpd.performAnalysis(r -> {
if (renderer != null) {
renderer.accept(r);
}
reportResult.set(new CpdResult(r));
});
worked(getStepCount());
}
} catch (IOException e) {
LOG.error("IOException while executing CPD", e);
}

return cpd.toReport();
}

private CPD newCPD() {
CPDConfiguration config = new CPDConfiguration();
config.setMinimumTileSize(minTileSize);
config.setLanguage(language);
config.setSourceEncoding(System.getProperty("file.encoding"));
return new CPD(config);
return reportResult.get();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/


package net.sourceforge.pmd.eclipse.runtime.cmd.internal;

import net.sourceforge.pmd.cpd.Mark;

public class CpdMarkWithSourceCode {
private final Mark mark;
private final CharSequence sourceCode;

public CpdMarkWithSourceCode(CpdResult result, Mark mark) {
this.mark = mark;
sourceCode = result.getSourceCodeSlices().get(this.mark);
}

public Mark getMark() {
return mark;
}

public CharSequence getSourceCode() {
return sourceCode;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/


package net.sourceforge.pmd.eclipse.runtime.cmd.internal;

import java.util.HashMap;
import java.util.Map;

import net.sourceforge.pmd.cpd.Mark;
import net.sourceforge.pmd.cpd.Match;

public class CpdMatchWithSourceCode {
private final Match match;
private final Map<Mark, CharSequence> sourceCodeSlices = new HashMap<>();

public CpdMatchWithSourceCode(CpdResult result, Match match) {
this.match = match;
for (Mark mark : this.match.getMarkSet()) {
sourceCodeSlices.put(mark, result.getSourceCodeSlices().get(mark));
}
}

public Match getMatch() {
return match;
}

public Map<Mark, CharSequence> getSourceCodeSlices() {
return sourceCodeSlices;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/


package net.sourceforge.pmd.eclipse.runtime.cmd.internal;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sourceforge.pmd.cpd.CPDReport;
import net.sourceforge.pmd.cpd.Mark;
import net.sourceforge.pmd.cpd.Match;

public class CpdResult {
private List<Match> matches = new ArrayList<>();
private Map<Mark, CharSequence> sourceCodeSlices = new HashMap<>();

public CpdResult(CPDReport report) {
matches.addAll(report.getMatches());
for (Match match : matches) {
for (Mark mark : match.getMarkSet()) {
sourceCodeSlices.put(mark, report.getSourceCodeSlice(mark));
}
}
}

public List<Match> getMatches() {
return matches;
}

public Map<Mark, CharSequence> getSourceCodeSlices() {
return sourceCodeSlices;
}
}
Loading

0 comments on commit c5e8aee

Please sign in to comment.