1 /* 2 * Copyright (c) 2011-2024 Qulice.com 3 * 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 1) Redistributions of source code must retain the above 9 * copyright notice, this list of conditions and the following 10 * disclaimer. 2) Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following 12 * disclaimer in the documentation and/or other materials provided 13 * with the distribution. 3) Neither the name of the Qulice.com nor 14 * the names of its contributors may be used to endorse or promote 15 * products derived from this software without specific prior written 16 * permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 20 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 21 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 22 * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 29 * OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 package com.qulice.pmd; 32 33 import com.qulice.spi.Environment; 34 import java.util.Collection; 35 import java.util.Collections; 36 import java.util.LinkedList; 37 import net.sourceforge.pmd.Report.ConfigurationError; 38 import net.sourceforge.pmd.Report.ProcessingError; 39 import net.sourceforge.pmd.RuleViolation; 40 41 /** 42 * Listener of PMD errors. 43 * 44 * @since 0.3 45 */ 46 @SuppressWarnings("deprecation") 47 final class PmdListener implements net.sourceforge.pmd.ThreadSafeReportListener { 48 49 /** 50 * Environment. 51 */ 52 private final Environment env; 53 54 /** 55 * All errors spotted (mostly violations, but also processing 56 * and config errors). 57 */ 58 private final Collection<PmdError> violations; 59 60 /** 61 * Public ctor. 62 * @param environ Environment 63 */ 64 PmdListener(final Environment environ) { 65 this.violations = new LinkedList<>(); 66 this.env = environ; 67 } 68 69 @Override 70 public void metricAdded(final net.sourceforge.pmd.stat.Metric metric) { 71 // ignore it 72 } 73 74 @Override 75 public void ruleViolationAdded(final RuleViolation violation) { 76 final String name = violation.getFilename().substring( 77 this.env.basedir().toString().length() 78 ); 79 if (!this.env.exclude("pmd", name)) { 80 this.violations.add(new PmdError.OfRuleViolation(violation)); 81 } 82 } 83 84 /** 85 * Registers a new ProcessingError. 86 * @param error A processing error that needs to be reported. 87 * @todo #1129 If was added to avoid failing build, but there should be 88 * better place for this check. 89 */ 90 public void onProcessingError(final ProcessingError error) { 91 if (error.getFile().endsWith(".java")) { 92 this.violations.add(new PmdError.OfProcessingError(error)); 93 } 94 } 95 96 /** 97 * Registers a new ConfigurationError. 98 * @param error A configuration error that needs to be reported. 99 */ 100 public void onConfigError(final ConfigurationError error) { 101 this.violations.add(new PmdError.OfConfigError(error)); 102 } 103 104 /** 105 * Get list of violations. 106 * @return List of violations 107 */ 108 public Collection<PmdError> errors() { 109 return Collections.unmodifiableCollection(this.violations); 110 } 111 }