Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report PMD processing errors #1185

Merged
merged 1 commit into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
192 changes: 192 additions & 0 deletions qulice-pmd/src/main/java/com/qulice/pmd/PmdError.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
/*
* Copyright (c) 2011-2023 Qulice.com
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met: 1) Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer. 2) Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution. 3) Neither the name of the Qulice.com nor
* the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.qulice.pmd;

import net.sourceforge.pmd.Report.ConfigurationError;
import net.sourceforge.pmd.Report.ProcessingError;
import net.sourceforge.pmd.RuleViolation;

/**
* Represents one PMD error (usually it will be violation).
*
* @since 1.0
*/
public interface PmdError {
/**
* Returns error name which is short, fixed, human-readable category of
* the error.
* @return Error name.
*/
String name();

/**
* Returns file name which caused this error.
* May return sentinel value if file information is not available.
* @return File name.
*/
String fileName();

/**
* Returns formatted line range which cause this error.
* May return sentinel value if line information is not available.
* @return Formatted line range.
*/
String lines();

/**
* Returns error description.
* @return Description.
*/
String description();

/**
* PmdError backed by a RuleViolation.
* @since 1.0
*/
final class OfRuleViolation implements PmdError {
/**
* Internal RuleViolation.
*/
private final RuleViolation violation;

/**
* Creates a new PmdError, representing given RuleViolation.
* @param violation Internal RuleViolation.
*/
public OfRuleViolation(final RuleViolation violation) {
this.violation = violation;
}

@Override
public String name() {
return this.violation.getRule().getName();
}

@Override
public String fileName() {
return this.violation.getFilename();
}

@Override
public String lines() {
return String.format(
"%d-%d",
this.violation.getBeginLine(), this.violation.getEndLine()
);
}

@Override
public String description() {
return this.violation.getDescription();
}
}

/**
* PmdError backed by a ProcessingError.
* @since 1.0
*/
final class OfProcessingError implements PmdError {
/**
* Internal ProcessingError.
*/
private final ProcessingError error;

/**
* Creates a new PmdError, representing given ProcessingError.
* @param error Internal ProcessingError.
*/
public OfProcessingError(final ProcessingError error) {
this.error = error;
}

@Override
public String name() {
return "ProcessingError";
}

@Override
public String fileName() {
return this.error.getFile();
}

@Override
public String lines() {
return "unknown";
}

@Override
public String description() {
return new StringBuilder()
.append(this.error.getMsg())
.append(": ")
.append(this.error.getDetail())
.toString();
}
}

/**
* PmdError backed by a ConfigError.
* @since 1.0
*/
final class OfConfigError implements PmdError {
/**
* Internal ConfigError.
*/
private final ConfigurationError error;

/**
* Creates a new PmdError, representing given ProcessingError.
* @param error Internal ProcessingError.
*/
public OfConfigError(final ConfigurationError error) {
this.error = error;
}

@Override
public String name() {
return "ProcessingError";
}

@Override
public String fileName() {
return "unknown";
}

@Override
public String lines() {
return "unknown";
}

@Override
public String description() {
return this.error.issue();
}
}
}
36 changes: 29 additions & 7 deletions qulice-pmd/src/main/java/com/qulice/pmd/PmdListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import net.sourceforge.pmd.Report.ConfigurationError;
import net.sourceforge.pmd.Report.ProcessingError;
import net.sourceforge.pmd.RuleViolation;
import net.sourceforge.pmd.ThreadSafeReportListener;
import net.sourceforge.pmd.stat.Metric;
Expand All @@ -50,16 +52,17 @@ final class PmdListener implements ThreadSafeReportListener {
private final Environment env;

/**
* Violations.
* All errors spotted (mostly violations, but also processing
* and config errors).
*/
private final Collection<RuleViolation> violations;
private final Collection<PmdError> errors;

/**
* Public ctor.
* @param environ Environment
*/
PmdListener(final Environment environ) {
this.violations = new LinkedList<>();
this.errors = new LinkedList<>();
this.env = environ;
}

Expand All @@ -74,16 +77,35 @@ public void ruleViolationAdded(final RuleViolation violation) {
this.env.basedir().toString().length()
);
if (!this.env.exclude("pmd", name)) {
this.violations.add(violation);
this.errors.add(new PmdError.OfRuleViolation(violation));
}
}

/**
* Registers a new ProcessingError.
* @param error A processing error that needs to be reported.
* @todo #1129 If was added to avoid failing build, but there should be
* better place for this check.
*/
public void onProcessingError(final ProcessingError error) {
if (error.getFile().endsWith(".java")) {
this.errors.add(new PmdError.OfProcessingError(error));
}
}

/**
* Registers a new ConfigurationError.
* @param error A configuration error that needs to be reported.
*/
public void onConfigError(final ConfigurationError error) {
this.errors.add(new PmdError.OfConfigError(error));
}

/**
* Get list of violations.
* @return List of violations
*/
public Collection<RuleViolation> getViolations() {
return Collections.unmodifiableCollection(this.violations);
public Collection<PmdError> errors() {
return Collections.unmodifiableCollection(this.errors);
}

}
131 changes: 131 additions & 0 deletions qulice-pmd/src/main/java/com/qulice/pmd/PmdRenderer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* Copyright (c) 2011-2023 Qulice.com
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met: 1) Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer. 2) Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution. 3) Neither the name of the Qulice.com nor
* the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.qulice.pmd;

import java.io.Writer;
import net.sourceforge.pmd.Report;
import net.sourceforge.pmd.properties.AbstractPropertySource;
import net.sourceforge.pmd.renderers.Renderer;
import net.sourceforge.pmd.util.datasource.DataSource;

/**
* Renderer implementation which keeps track of all pmd-generated report.
*
* @since 1.0
*/
final class PmdRenderer extends AbstractPropertySource implements Renderer {
/**
* This variable is union of all observed reports.
*/
private final Report accumulator = new Report();

@Override
public String getName() {
return "qulice";
}

@Override
public void setName(final String name) {
throw new UnsupportedOperationException("Unimplemented method 'setName'");
}

@Override
public String getDescription() {
return "TODO";
}

@Override
public String defaultFileExtension() {
throw new UnsupportedOperationException("Unimplemented defaultFileExtension");
}

@Override
public void setDescription(final String description) {
throw new UnsupportedOperationException("Unimplemented setDescription");
}

@Override
public boolean isShowSuppressedViolations() {
throw new UnsupportedOperationException("Unimplemented isShowSuppressedViolations");
}

@Override
public void setShowSuppressedViolations(final boolean show) {
throw new UnsupportedOperationException("Unimplemented setShowSuppressedViolations");
}

@Override
public Writer getWriter() {
throw new UnsupportedOperationException("Unimplemented getWriter");
}

@Override
public void setWriter(final Writer writer) {
throw new UnsupportedOperationException("Unimplemented setWriter");
}

@Override
public void start() {
// ignore it
}

@Override
public void startFileAnalysis(final DataSource source) {
// ignore it
}

@Override
public void renderFileReport(final Report report) {
this.accumulator.merge(report);
}

@Override
public void end() {
// ignore it
}

@Override
public void flush() {
// ignore it
}

@Override
public String getPropertySourceType() {
throw new UnsupportedOperationException("Unimplemented method 'getPropertySourceType'");
}

/**
* Merges all collected errors into the provided target.
* @param target A Report instance which is updated
*/
void exportTo(final Report target) {
target.merge(this.accumulator);
}
}