Skip to content

Commit

Permalink
Merge pull request #171 from martenbohlin/pmd7
Browse files Browse the repository at this point in the history
Upgraded to PMD 7.1.0 (Issue #161)
  • Loading branch information
amitdev authored May 26, 2024
2 parents 5c251a2 + f5ea245 commit be6f449
Show file tree
Hide file tree
Showing 29 changed files with 195 additions and 197 deletions.
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

fun properties(key: String) = project.findProperty(key).toString()

val pmdVersion = "6.55.0"
val pmdVersion = "7.1.0"

plugins {
id("java")
id("org.jetbrains.kotlin.jvm") version "1.6.20"
id("org.jetbrains.kotlin.jvm") version "1.9.23"
id("org.jetbrains.intellij") version "1.17.3"
id("org.jetbrains.changelog") version "1.1.2"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ private void validateJavaVersion(String versionInput, int row, int column, Objec
if (versionInput.equals(orig)) {
return;
}
Language java = LanguageRegistry.findLanguageByTerseName("java");
Language java = LanguageRegistry.PMD.getLanguageById("java");
boolean isRegistered = java.hasVersion(versionInput);
if (isRegistered) {
String registeredVersion = java.getVersion(versionInput).getVersion();
Expand Down
68 changes: 39 additions & 29 deletions src/main/java/com/intellij/plugins/bodhi/pmd/PMDInvoker.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.plugins.bodhi.pmd.core.PMDProgressRenderer;
import com.intellij.plugins.bodhi.pmd.core.PMDResultCollector;
import com.intellij.plugins.bodhi.pmd.handlers.PMDCheckinHandler;
import com.intellij.plugins.bodhi.pmd.tree.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jetbrains.annotations.NotNull;

import java.io.File;
Expand All @@ -35,6 +38,7 @@
* @version 1.0
*/
public class PMDInvoker {
private static final Log log = LogFactory.getLog(PMDInvoker.class);

/**
* The delimiter used for delimiting multiple rules.
Expand Down Expand Up @@ -162,44 +166,50 @@ public void run(@NotNull ProgressIndicator indicator) {
String[] ruleSetPathArray = ruleSetPaths.split(RULE_DELIMITER);
PMDResultPanel resultPanel = projectComponent.getResultPanel();

System.setProperty("pmd.error_recovery", "true"); //Recover from errors on single files
PMDRootNode rootNode = resultPanel.getRootNode();
resultPanel.createProcessingErrorNode();
PMDResultCollector.clearReport();
rootNode.setFileCount(files.size());
rootNode.setRuleSetCount(ruleSetPathArray.length);
rootNode.setRunning(true);
PMDProgressRenderer progressRenderer = new PMDProgressRenderer(progress, files.size() * ruleSetPathArray.length);
for (String ruleSetPath : ruleSetPathArray) {
progress.setText("Running : " + ruleSetPath + " on " + files.size() + " file(s)");

//Create a result collector to get results
PMDResultCollector collector = new PMDResultCollector();

//Get the tree nodes from result collector
List<PMDRuleSetEntryNode> resultRuleNodes = collector.runPMDAndGetResults(files, ruleSetPath, projectComponent, progressRenderer);
// sort rules by priority, rule and suppressed nodes are comparable
resultRuleNodes.sort(null);

if (!resultRuleNodes.isEmpty()) {
String ruleSetName = PMDUtil.getBareFileNameFromPath(ruleSetPath);
String desc = PMDResultCollector.getRuleSetDescription(ruleSetPath);
PMDRuleSetNode ruleSetNode = resultPanel.addCreateRuleSetNodeAtRoot(ruleSetName);
ruleSetNode.setToolTip(desc);
//Add all rule nodes to the tree
for (PMDRuleSetEntryNode resultRuleNode : resultRuleNodes) {
resultPanel.addNode(ruleSetNode, resultRuleNode);
try {
for (String ruleSetPath : ruleSetPathArray) {
progress.setText("Running : " + ruleSetPath + " on " + files.size() + " file(s)");

//Create a result collector to get results
PMDResultCollector collector = new PMDResultCollector();

//Get the tree nodes from result collector
List<PMDRuleSetEntryNode> resultRuleNodes = collector.runPMDAndGetResults(files, ruleSetPath, projectComponent, progressRenderer);
// sort rules by priority, rule and suppressed nodes are comparable
resultRuleNodes.sort(null);

if (!resultRuleNodes.isEmpty()) {
String ruleSetName = PMDUtil.getBareFileNameFromPath(ruleSetPath);
String desc = PMDResultCollector.getRuleSetDescription(ruleSetPath);
PMDRuleSetNode ruleSetNode = resultPanel.addCreateRuleSetNodeAtRoot(ruleSetName);
ruleSetNode.setToolTip(desc);
//Add all rule nodes to the tree
for (PMDRuleSetEntryNode resultRuleNode : resultRuleNodes) {
resultPanel.addNode(ruleSetNode, resultRuleNode);
}
rootNode.calculateCounts();
resultPanel.reloadResultTree();
}
if (progress.isCanceled()) {
break;
}
rootNode.calculateCounts();
resultPanel.reloadResultTree();
}
if (progress.isCanceled()) {
break;
}
resultPanel.addProcessingErrorsNodeToRootIfHasAny(); // as last node
rootNode.calculateCounts();
} catch (Throwable t) {
rootNode.setRuleSetErrorMsg(t.getMessage());
log.error("Error running PMD", t);
} finally {
rootNode.setRunning(false);
resultPanel.reloadResultTree();
}
resultPanel.addProcessingErrorsNodeToRootIfHasAny(); // as last node
rootNode.calculateCounts();
rootNode.setRunning(false);
resultPanel.reloadResultTree();
}
});
}
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/com/intellij/plugins/bodhi/pmd/PMDResultPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
import com.intellij.ui.components.JBScrollPane;
import com.intellij.usageView.UsageViewBundle;
import com.intellij.util.ui.tree.TreeUtil;
import net.sourceforge.pmd.Report;
import net.sourceforge.pmd.Rule;
import net.sourceforge.pmd.lang.rule.Rule;
import net.sourceforge.pmd.renderers.HTMLRenderer;
import net.sourceforge.pmd.reporting.Report;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -41,6 +41,7 @@
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;
import java.util.*;
Expand Down Expand Up @@ -143,7 +144,7 @@ public void actionPerformed(ActionEvent e) {
// suppress all selected violations, max 1 per file+line
Map<String, PMDViolation> uniqueViolationsMap = new HashMap<>();
for (PMDViolation violation : violations) {
uniqueViolationsMap.put(violation.getFilename() + ":" + violation.getBeginLine(), violation);
uniqueViolationsMap.put(violation.getFilePath() + ":" + violation.getBeginLine(), violation);
}
for (PMDViolation violation : uniqueViolationsMap.values()) {
//Suppress the violation
Expand Down Expand Up @@ -403,7 +404,7 @@ public void removeSettingsChangedListener(ChangeListener listener) {
HTMLRenderer renderer = new HTMLRenderer();
StringWriter w = new StringWriter();
try {
renderer.renderBody(w, r);
renderer.renderBody(new PrintWriter(w), r);
return w.getBuffer().toString();
} catch (IOException e) {
e.printStackTrace();
Expand Down Expand Up @@ -463,7 +464,7 @@ public void highlightFindingInEditor(HasPositionInFile finding) {
private Editor openEditor(HasPositionInFile finding) {
FileEditorManager fileEditorManager = FileEditorManager.getInstance(projectComponent.getCurrentProject());
final VirtualFile virtualFile = LocalFileSystem.getInstance().findFileByPath(
finding.getFilename().replace(File.separatorChar, '/'));
finding.getFilePath().replace(File.separatorChar, '/'));
if (virtualFile != null) {
return fileEditorManager.openTextEditor(new OpenFileDescriptor(
projectComponent.getCurrentProject(),
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/com/intellij/plugins/bodhi/pmd/PMDUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileFilter;
import com.intellij.openapi.vfs.VirtualFileVisitor;
import com.intellij.util.containers.OrderedSet;
import com.intellij.plugins.bodhi.pmd.core.PMDResultCollector;
import org.jetbrains.annotations.NotNull;

Expand Down Expand Up @@ -110,9 +111,14 @@ public static List<Module> getProjectModules(Project project) {

public static String getFullClassPathForAllModules(Project project) {
List<Module> modules = getProjectModules(project);
StringJoiner joiner = new StringJoiner(File.pathSeparator);
OrderedSet<String> uniqPaths = new OrderedSet<>();
for (Module module : modules) {
joiner.add(OrderEnumerator.orderEntries(module).recursively().getPathsList().getPathsString());
uniqPaths.addAll(OrderEnumerator.orderEntries(module).recursively().getPathsList().getPathList());
}

StringJoiner joiner = new StringJoiner(File.pathSeparator);
for (String path : uniqPaths) {
joiner.add(path);
}
return joiner.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
import com.intellij.plugins.bodhi.pmd.PMDInvoker;
import com.intellij.plugins.bodhi.pmd.PMDProjectComponent;
import com.intellij.plugins.bodhi.pmd.PMDUtil;
import net.sourceforge.pmd.util.ResourceLoader;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.util.Properties;

/**
Expand All @@ -31,7 +29,7 @@ public class PreDefinedMenuGroup extends ActionGroup {
private PMDProjectComponent component;

//The ruleset property file which lists all the predefined rulesets
public static final String RULESETS_PROPERTY_FILE = "rulesets/java/rulesets.properties";
public static final String RULESETS_PROPERTY_FILE = "category/java/categories.properties";
public static final String RULESETS_FILENAMES_KEY = "rulesets.filenames";

/**
Expand All @@ -47,7 +45,7 @@ public void actionPerformed(AnActionEvent e) {
Properties props = new Properties();
try {
//Load the property file which has all the rulesets.
props.load(new ResourceLoader(getClass().getClassLoader()).loadResourceAsStream(RULESETS_PROPERTY_FILE));
props.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(RULESETS_PROPERTY_FILE));
String[] rulesetFilenames = props.getProperty(RULESETS_FILENAMES_KEY).split(PMDInvoker.RULE_DELIMITER);

//We have 'All' rules in addition to the rulesets
Expand All @@ -68,17 +66,14 @@ public void actionPerformed(AnActionEvent e) {
};
children.add(ruleAction);
}
} catch (IOException e) {
//Should not happen
//e.printStackTrace();
} catch (Exception e) {
//Should not happen
//e.printStackTrace();
}
}

public AnAction[] getChildren(@Nullable AnActionEvent event) {
return new AnAction[] { this.children };
return new AnAction[]{this.children};
}

public void setComponent(PMDProjectComponent component) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.intellij.plugins.bodhi.pmd.annotator;

import com.intellij.openapi.editor.Document;
import net.sourceforge.pmd.Report;
import net.sourceforge.pmd.lang.document.TextFile;
import net.sourceforge.pmd.renderers.AbstractRenderer;
import net.sourceforge.pmd.util.datasource.DataSource;
import net.sourceforge.pmd.reporting.Report;

import java.io.IOException;

Expand All @@ -25,13 +25,17 @@ public void start() {
}

@Override
public void startFileAnalysis(DataSource dataSource) {
public void startFileAnalysis(TextFile dataSource) {

}

@Override
public void renderFileReport(Report report) throws IOException {
this.report = report;
public void renderFileReport(Report report) {
if (this.report == null) {
this.report = report;
} else {
this.report = this.report.union(report);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.intellij.plugins.bodhi.pmd.annotator;

import com.intellij.openapi.editor.Document;
import net.sourceforge.pmd.Report;
import net.sourceforge.pmd.reporting.Report;

public class PMDAnnotations {
private final Report report;
Expand Down
Loading

0 comments on commit be6f449

Please sign in to comment.