Skip to content

Commit

Permalink
Build auxPath correctly, fixes #15
Browse files Browse the repository at this point in the history
Signed-off-by: Yannick da Silva Bröker <ybroeker@techfak.uni-bielefeld.de>
  • Loading branch information
ybroeker committed Jan 10, 2021
1 parent adad953 commit e0ae918
Show file tree
Hide file tree
Showing 10 changed files with 211 additions and 118 deletions.
10 changes: 1 addition & 9 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,9 @@
# pmd-idea Changelog

## [Unreleased]
### Added

### Changed

### Deprecated

### Removed

### Fixed
- Remove non-existing files from aux-classpath, see [#15](https://github.com/ybroeker/pmd-idea/issues/15)

### Security
## [1.3.2]
### Fixed
- Fix IndexOutOfBoundsException while running inspection
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

pluginGroup = com.github.ybroeker.pmdidea
pluginName_ = pmd-idea
pluginVersion = 1.3.2
pluginVersion = 1.3.3
pluginSinceBuild = 193
pluginUntilBuild = 203.*
# Plugin Verifier integration -> https://github.com/JetBrains/gradle-intellij-plugin#plugin-verifier-dsl
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package com.github.ybroeker.pmdidea.actions.scan;

import java.io.File;
import java.nio.file.*;
import java.util.*;
import java.util.ArrayList;
import java.util.List;

import com.github.ybroeker.pmdidea.config.PmdConfigurationService;
import com.github.ybroeker.pmdidea.pmd.*;
import com.github.ybroeker.pmdidea.toolwindow.PmdToolPanel;
import com.github.ybroeker.pmdidea.toolwindow.PmdToolWindowFactory;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.application.*;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowManager;
Expand All @@ -35,59 +34,54 @@ public void update(@NotNull final AnActionEvent event) {
return;
}

final Optional<Path> rules = getRules(project);
final boolean hasValidRules = project.getService(RulesService.class).hasValidRuleSet();

final Presentation presentation = event.getPresentation();
if (rules.isPresent() != presentation.isEnabled()) {
presentation.setEnabled(rules.isPresent());
if (hasValidRules != presentation.isEnabled()) {
presentation.setEnabled(hasValidRules);
}
}

private Optional<Path> getRules(final Project project) {
final PmdConfigurationService service = project.getService(PmdConfigurationService.class);
final String pathName = service.getState().getRulesPath();
if (pathName == null || pathName.isEmpty()) {
return Optional.empty();
private boolean isEnabled(final Project project) {
if (project == null) {
return false;
}
final Path rulesPath = Paths.get(pathName);
if (!Files.exists(rulesPath)) {
return Optional.empty();

if (!project.getService(RulesService.class).hasValidRuleSet()) {
return false;
}
return Optional.of(rulesPath);

return true;
}

@Override
public final void actionPerformed(final AnActionEvent event) {
final Project project = event.getData(CommonDataKeys.PROJECT);
if (project == null) {
return;
}

final PmdConfigurationService service = project.getService(PmdConfigurationService.class);

final Optional<Path> rulesPath = getRules(project);
if (!rulesPath.isPresent()) {
if (!isEnabled(project)) {
return;
}

final ToolWindow toolWindow = ToolWindowManager.getInstance(project).getToolWindow(PmdToolWindowFactory.TOOL_WINDOW_ID);

final PmdToolPanel toolPanel = (PmdToolPanel) toolWindow.getContentManager().getContent(0).getComponent();
final PmdRunListener pmdRunListener = createRunListener(project);

final List<ScannableFile> files = getScannableFiles(project);
final PmdConfigurationFactory configurationFactory = project.getService(PmdConfigurationFactory.class);
final PmdConfiguration configuration = configurationFactory.getPmdConfiguration(files, pmdRunListener);

final PmdRunListener pmdRunListener = new PmdRunListenerAdapter(toolPanel);

PmdAdapter pmdAdapter = project.getService(PmdAdapterDelegate.class);

final PmdOptions pmdOptions = new PmdOptions(service.getState().getJdkVersion().toString(), service.getState().getPmdVersion());
PmdConfiguration configuration = new PmdConfiguration(project, files, rulesPath.get().toFile().getAbsolutePath(), pmdOptions, pmdRunListener);

final PmdAdapter pmdAdapter = project.getService(PmdAdapterDelegate.class);
//TODO: check if real files are needed
//TODO: ReadAction needed?
ApplicationManager.getApplication().saveAll();
ApplicationManager.getApplication().executeOnPooledThread(() -> pmdAdapter.runPmd(configuration));
}

private PmdRunListener createRunListener(final Project project) {
final ToolWindow toolWindow = ToolWindowManager.getInstance(project).getToolWindow(PmdToolWindowFactory.TOOL_WINDOW_ID);

final PmdToolPanel toolPanel = (PmdToolPanel) toolWindow.getContentManager().getContent(0).getComponent();

return new PmdRunListenerAdapter(toolPanel);
}


}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.github.ybroeker.pmdidea.inspection;

import java.nio.file.*;
import java.util.*;

import com.github.ybroeker.pmdidea.config.PmdConfigurationService;
Expand All @@ -18,19 +17,6 @@ public class PmdInspection extends LocalInspectionTool {

private static final Logger LOGGER = LoggerFactory.getLogger(PmdInspection.class);

private Optional<Path> getRules(final Project project) {
final PmdConfigurationService service = project.getService(PmdConfigurationService.class);
final String pathName = service.getState().getRulesPath();
if (pathName == null || pathName.isEmpty()) {
return Optional.empty();
}
final Path rulesPath = Paths.get(pathName);
if (!Files.exists(rulesPath)) {
return Optional.empty();
}
return Optional.of(rulesPath);
}

@Override
public ProblemDescriptor[] checkFile(@NotNull final PsiFile psiFile,
@NotNull final InspectionManager manager,
Expand All @@ -48,27 +34,24 @@ public ProblemDescriptor[] checkFile(@NotNull final PsiFile psiFile,
return ProblemDescriptor.EMPTY_ARRAY;
}

final Optional<Path> rulesPath = getRules(project);
if (!rulesPath.isPresent()) {
if (!project.getService(RulesService.class).hasValidRuleSet()) {
LOGGER.trace("Skip inspection, no rules configured");
return ProblemDescriptor.EMPTY_ARRAY;
}

final ScannablePsiFile scannableFile = new ScannablePsiFile(psiFile);

final List<PmdRuleViolation> violations = runPmd(project, scannableFile, rulesPath.get());
final List<PmdRuleViolation> violations = runPmd(project, scannableFile);
return getProblemDescriptors(manager, psiFile, violations);
}

private List<PmdRuleViolation> runPmd(final Project project, final ScannablePsiFile scannableFile, final Path rules) {
private List<PmdRuleViolation> runPmd(final Project project, final ScannablePsiFile scannableFile) {
LOGGER.trace("Inspect File: '{}'", scannableFile.getDisplayName());
final PmdConfigurationService service = project.getService(PmdConfigurationService.class);

final PmdOptions pmdOptions = new PmdOptions(service.getState().getJdkVersion().toString(), service.getState().getPmdVersion());

final ViolationCollector pmdRunListener = new ViolationCollector();

final PmdConfiguration configuration = new PmdConfiguration(project, Collections.singletonList(scannableFile), rules.toFile().getAbsolutePath(), pmdOptions, pmdRunListener);
final PmdConfigurationFactory configurationFactory = project.getService(PmdConfigurationFactory.class);
final PmdConfiguration configuration = configurationFactory.getPmdConfiguration(Collections.singletonList(scannableFile), pmdRunListener);

final PmdAdapterDelegate pmdAdapter = project.getService(PmdAdapterDelegate.class);
final long start = System.currentTimeMillis();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.github.ybroeker.pmdidea.pmd;

import java.io.File;
import java.nio.file.*;
import java.util.*;

import com.intellij.openapi.components.Service;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.OrderEnumerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


@Service
public final class AuxClassPathFactory {

private static final Logger LOGGER = LoggerFactory.getLogger(AuxClassPathFactory.class);

private final Project project;

public AuxClassPathFactory(final Project project) {
this.project = project;
}


public String getClassPath() {
final List<Path> paths = getClassPathEntries();

return joinClassPathEntries(paths);
}

private List<Path> getClassPathEntries() {
final Module[] modules = ModuleManager.getInstance(project).getModules();

final List<Path> paths = new ArrayList<>();

for (final Module module : modules) {
final List<String> pathList = OrderEnumerator.orderEntries(module).recursively().getPathsList().getPathList();
for (final String pathString : pathList) {
final Path path = Paths.get(pathString);
if (Files.exists(path)) {
paths.add(path);
LOGGER.trace("Include in Classpath: {}", path);
} else {
LOGGER.trace("Exclude from Classpath: {}", path);
}
}
}
return paths;
}

private String joinClassPathEntries(final List<Path> paths) {
final StringJoiner joiner = new StringJoiner(File.pathSeparator);
for (final Path path : paths) {
joiner.add(path.toAbsolutePath().toString());
}
return joiner.toString();
}


}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.github.ybroeker.pmdidea.pmd;

import java.io.File;
import java.util.List;

import com.intellij.openapi.project.Project;
Expand All @@ -17,12 +16,15 @@ public class PmdConfiguration {

private final PmdRunListener pmdRunListener;

public PmdConfiguration(final Project project, final List<ScannableFile> files, final String ruleSets, final PmdOptions pmdOptions, final PmdRunListener pmdRunListener) {
private final String auxClassPath;

public PmdConfiguration(final Project project, final List<ScannableFile> files, final String ruleSets, final PmdOptions pmdOptions, final PmdRunListener pmdRunListener, final String auxClassPath) {
this.project = project;
this.files = files;
this.ruleSets = ruleSets;
this.pmdOptions = pmdOptions;
this.pmdRunListener = pmdRunListener;
this.auxClassPath = auxClassPath;
}

public Project getProject() {
Expand All @@ -44,4 +46,8 @@ public PmdOptions getPmdOptions() {
public PmdRunListener getPmdRunListener() {
return pmdRunListener;
}

public String getAuxClassPath() {
return auxClassPath;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.github.ybroeker.pmdidea.pmd;

import java.util.List;

import com.github.ybroeker.pmdidea.config.PmdConfigurationService;
import com.intellij.openapi.components.Service;
import com.intellij.openapi.project.Project;


@Service
public final class PmdConfigurationFactory {

private final Project project;

public PmdConfigurationFactory(final Project project) {
this.project = project;
}

public PmdConfiguration getPmdConfiguration(final List<ScannableFile> files, final PmdRunListener pmdRunListener) {
final RulesService rulesService = project.getService(RulesService.class);
final PmdConfigurationService service = project.getService(PmdConfigurationService.class);
final PmdOptions pmdOptions = new PmdOptions(service.getState().getJdkVersion().toString(), service.getState().getPmdVersion());
final String classPath = project.getService(AuxClassPathFactory.class).getClassPath();

return new PmdConfiguration(project, files, rulesService.getRulesPath(), pmdOptions, pmdRunListener, classPath);
}

}
42 changes: 42 additions & 0 deletions src/main/java/com/github/ybroeker/pmdidea/pmd/RulesService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.github.ybroeker.pmdidea.pmd;

import java.nio.file.*;
import java.util.Optional;

import com.github.ybroeker.pmdidea.config.PmdConfigurationService;
import com.intellij.openapi.components.Service;
import com.intellij.openapi.project.Project;

@Service
public final class RulesService {

private final Project project;

public RulesService(final Project project) {
this.project = project;
}


public String getRulesPath() {
return getRules().orElseThrow(() -> new IllegalStateException("No valid ruleset found!"));
}

public boolean hasValidRuleSet() {
return getRules().isPresent();
}


private Optional<String> getRules() {
final PmdConfigurationService service = project.getService(PmdConfigurationService.class);
final String pathName = service.getState().getRulesPath();
if (pathName == null || pathName.isEmpty()) {
return Optional.empty();
}
final Path rulesPath = Paths.get(pathName);
if (!Files.exists(rulesPath)) {
return Optional.empty();
}
return Optional.of(pathName);
}

}
Loading

0 comments on commit e0ae918

Please sign in to comment.