View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2011-2025 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.qulice.pmd;
6   
7   import com.qulice.spi.Environment;
8   import java.util.Collection;
9   import java.util.Collections;
10  import java.util.LinkedList;
11  import net.sourceforge.pmd.Report.ConfigurationError;
12  import net.sourceforge.pmd.Report.ProcessingError;
13  import net.sourceforge.pmd.RuleViolation;
14  
15  /**
16   * Listener of PMD errors.
17   *
18   * @since 0.3
19   */
20  @SuppressWarnings("deprecation")
21  final class PmdListener implements net.sourceforge.pmd.ThreadSafeReportListener {
22  
23      /**
24       * Environment.
25       */
26      private final Environment env;
27  
28      /**
29       * All errors spotted (mostly violations, but also processing
30       * and config errors).
31       */
32      private final Collection<PmdError> violations;
33  
34      /**
35       * Public ctor.
36       * @param environ Environment
37       */
38      PmdListener(final Environment environ) {
39          this.violations = new LinkedList<>();
40          this.env = environ;
41      }
42  
43      @Override
44      public void metricAdded(final net.sourceforge.pmd.stat.Metric metric) {
45          // ignore it
46      }
47  
48      @Override
49      public void ruleViolationAdded(final RuleViolation violation) {
50          final String name = violation.getFilename().substring(
51              this.env.basedir().toString().length()
52          );
53          if (!this.env.exclude("pmd", name)) {
54              this.violations.add(new PmdError.OfRuleViolation(violation));
55          }
56      }
57  
58      /**
59       * Registers a new ProcessingError.
60       * @param error A processing error that needs to be reported.
61       * @todo #1129 If was added to avoid failing build, but there should be
62       *  better place for this check.
63       */
64      public void onProcessingError(final ProcessingError error) {
65          if (error.getFile().endsWith(".java")) {
66              this.violations.add(new PmdError.OfProcessingError(error));
67          }
68      }
69  
70      /**
71       * Registers a new ConfigurationError.
72       * @param error A configuration error that needs to be reported.
73       */
74      public void onConfigError(final ConfigurationError error) {
75          this.violations.add(new PmdError.OfConfigError(error));
76      }
77  
78      /**
79       * Get list of violations.
80       * @return List of violations
81       */
82      public Collection<PmdError> errors() {
83          return Collections.unmodifiableCollection(this.violations);
84      }
85  }