View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ///////////////////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.api;
21  
22  import java.util.Map;
23  
24  import com.puppycrawl.tools.checkstyle.AbstractAutomaticBean;
25  
26  /**
27   * Serves as an abstract base class for all modules that report inspection
28   * findings. Such modules have a Severity level which is used for the
29   * {@link Violation violations} that are created by the module.
30   *
31   * @noinspection NoopMethodInAbstractClass
32   * @noinspectionreason NoopMethodInAbstractClass - we allow each check to
33   *      define these methods, as needed. They should be overridden only by demand in subclasses
34   */
35  public abstract class AbstractViolationReporter extends AbstractAutomaticBean {
36  
37      /** The severity level of any violations found. */
38      private SeverityLevel severityLevel = SeverityLevel.ERROR;
39  
40      /** The identifier of the reporter. */
41      private String id;
42  
43      /**
44       * Creates a new {@code AbstractViolationReporter} instance.
45       */
46      protected AbstractViolationReporter() {
47          // no code by default
48      }
49  
50      /**
51       * Returns the severity level of the violations generated by this module.
52       *
53       * @return the severity level
54       * @see SeverityLevel
55       * @see Violation#getSeverityLevel
56       * @noinspection WeakerAccess
57       * @noinspectionreason we avoid 'protected' when possible
58       */
59      public final SeverityLevel getSeverityLevel() {
60          return severityLevel;
61      }
62  
63      /**
64       * Sets the severity level.  The string should be one of the names
65       * defined in the {@code SeverityLevel} class.
66       *
67       * @param severity  The new severity level
68       * @see SeverityLevel
69       */
70      public final void setSeverity(String severity) {
71          severityLevel = SeverityLevel.getInstance(severity);
72      }
73  
74      /**
75       *  Get the severity level's name.
76       *
77       *  @return  the check's severity level name.
78       *  @noinspection WeakerAccess
79       *  @noinspectionreason WeakerAccess - we avoid 'protected' when possible
80       */
81      public final String getSeverity() {
82          return severityLevel.getName();
83      }
84  
85      /**
86       * Returns the identifier of the reporter. Can be null.
87       *
88       * @return the id
89       */
90      public final String getId() {
91          return id;
92      }
93  
94      /**
95       * Sets the identifier of the reporter. Can be null.
96       *
97       * @param id the id
98       */
99      public final void setId(final String id) {
100         this.id = id;
101     }
102 
103     /**
104      * Returns an unmodifiable map instance containing the custom messages for this configuration.
105      *
106      * @return unmodifiable map containing custom messages
107      */
108     protected Map<String, String> getCustomMessages() {
109         return getConfiguration().getMessages();
110     }
111 
112     /**
113      * Returns the message bundle name resource bundle that contains the messages
114      * used by this module.
115      *
116      * <p>
117      * The default implementation expects the resource files to be named
118      * messages.properties, messages_de.properties, etc. The file must
119      * be placed in the same package as the module implementation.
120      * </p>
121      *
122      * <p>
123      * Example: If you write com/foo/MyCoolCheck, create resource files
124      * com/foo/messages.properties, com/foo/messages_de.properties, etc.
125      * </p>
126      *
127      * @return name of a resource bundle that contains the messages used by this module.
128      */
129     protected String getMessageBundle() {
130         final String className = getClass().getName();
131         return getMessageBundle(className);
132     }
133 
134     /**
135      * For unit tests, especially with a class with no package name.
136      *
137      * @param className class name of the module.
138      * @return name of a resource bundle that contains the messages used by the module.
139      */
140     private static String getMessageBundle(final String className) {
141         final String messageBundle;
142         final int endIndex = className.lastIndexOf('.');
143         final String messages = "messages";
144         if (endIndex == -1) {
145             messageBundle = messages;
146         }
147         else {
148             final String packageName = className.substring(0, endIndex);
149             messageBundle = packageName + "." + messages;
150         }
151         return messageBundle;
152     }
153 
154     @Override
155     protected void finishLocalSetup() throws CheckstyleException {
156         // No code by default
157     }
158 
159     /**
160      * Log a message that has no column information.
161      *
162      * @param line the line number where the audit event was found
163      * @param key the message that describes the audit event
164      * @param args the details of the message
165      *
166      * @see java.text.MessageFormat
167      */
168     // -@cs[CustomDeclarationOrder] CustomDeclarationOrder does not treat groups of
169     // overloaded methods. See https://github.com/sevntu-checkstyle/sevntu.checkstyle/issues/414
170     public abstract void log(int line, String key, Object... args);
171 
172     /**
173      * Log a message that has column information.
174      *
175      * @param line the line number where the audit event was found
176      * @param col the column number where the audit event was found
177      * @param key the message that describes the audit event
178      * @param args the details of the message
179      *
180      * @see java.text.MessageFormat
181      */
182     // -@cs[CustomDeclarationOrder] CustomDeclarationOrder does not treat groups of
183     // overloaded methods. See https://github.com/sevntu-checkstyle/sevntu.checkstyle/issues/414
184     public abstract void log(int line, int col, String key,
185             Object... args);
186 
187 }