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
34   *      by demand in subclasses
35   */
36  public abstract class AbstractViolationReporter
37      extends AbstractAutomaticBean {
38  
39      /** The severity level of any violations found. */
40      private SeverityLevel severityLevel = SeverityLevel.ERROR;
41  
42      /** The identifier of the reporter. */
43      private String id;
44  
45      /**
46       * Creates a new {@code AbstractViolationReporter} instance.
47       */
48      protected AbstractViolationReporter() {
49          // no code by default
50      }
51  
52      /**
53       * Returns the severity level of the violations generated by this module.
54       *
55       * @return the severity level
56       * @see SeverityLevel
57       * @see Violation#getSeverityLevel
58       * @noinspection WeakerAccess
59       * @noinspectionreason we avoid 'protected' when possible
60       */
61      public final SeverityLevel getSeverityLevel() {
62          return severityLevel;
63      }
64  
65      /**
66       * Sets the severity level.  The string should be one of the names
67       * defined in the {@code SeverityLevel} class.
68       *
69       * @param severity  The new severity level
70       * @see SeverityLevel
71       */
72      public final void setSeverity(String severity) {
73          severityLevel = SeverityLevel.getInstance(severity);
74      }
75  
76      /**
77       *  Get the severity level's name.
78       *
79       *  @return  the check's severity level name.
80       *  @noinspection WeakerAccess
81       *  @noinspectionreason WeakerAccess - we avoid 'protected' when possible
82       */
83      public final String getSeverity() {
84          return severityLevel.getName();
85      }
86  
87      /**
88       * Returns the identifier of the reporter. Can be null.
89       *
90       * @return the id
91       */
92      public final String getId() {
93          return id;
94      }
95  
96      /**
97       * Sets the identifier of the reporter. Can be null.
98       *
99       * @param id the id
100      */
101     public final void setId(final String id) {
102         this.id = id;
103     }
104 
105     /**
106      * Returns an unmodifiable map instance containing the custom messages
107      * for this configuration.
108      *
109      * @return unmodifiable map containing custom messages
110      */
111     protected Map<String, String> getCustomMessages() {
112         return getConfiguration().getMessages();
113     }
114 
115     /**
116      * Returns the message bundle name resource bundle that contains the messages
117      * used by this module.
118      *
119      * <p>
120      * The default implementation expects the resource files to be named
121      * messages.properties, messages_de.properties, etc. The file must
122      * be placed in the same package as the module implementation.
123      * </p>
124      *
125      * <p>
126      * Example: If you write com/foo/MyCoolCheck, create resource files
127      * com/foo/messages.properties, com/foo/messages_de.properties, etc.
128      * </p>
129      *
130      * @return name of a resource bundle that contains the messages
131      *     used by this module.
132      */
133     protected String getMessageBundle() {
134         final String className = getClass().getName();
135         return getMessageBundle(className);
136     }
137 
138     /**
139      * For unit tests, especially with a class with no package name.
140      *
141      * @param className class name of the module.
142      * @return name of a resource bundle that contains the messages
143      *     used by the module.
144      */
145     private static String getMessageBundle(final String className) {
146         final String messageBundle;
147         final int endIndex = className.lastIndexOf('.');
148         final String messages = "messages";
149         if (endIndex == -1) {
150             messageBundle = messages;
151         }
152         else {
153             final String packageName = className.substring(0, endIndex);
154             messageBundle = packageName + "." + messages;
155         }
156         return messageBundle;
157     }
158 
159     @Override
160     protected void finishLocalSetup() throws CheckstyleException {
161         // No code by default
162     }
163 
164     /**
165      * Log a message that has no column information.
166      *
167      * @param line the line number where the audit event was found
168      * @param key the message that describes the audit event
169      * @param args the details of the message
170      *
171      * @see java.text.MessageFormat
172      */
173     // -@cs[CustomDeclarationOrder] CustomDeclarationOrder does not treat groups of
174     // overloaded methods. See https://github.com/sevntu-checkstyle/sevntu.checkstyle/issues/414
175     public abstract void log(int line, String key, Object... args);
176 
177     /**
178      * Log a message that has column information.
179      *
180      * @param line the line number where the audit event was found
181      * @param col the column number where the audit event was found
182      * @param key the message that describes the audit event
183      * @param args the details of the message
184      *
185      * @see java.text.MessageFormat
186      */
187     // -@cs[CustomDeclarationOrder] CustomDeclarationOrder does not treat groups of
188     // overloaded methods. See https://github.com/sevntu-checkstyle/sevntu.checkstyle/issues/414
189     public abstract void log(int line, int col, String key,
190             Object... args);
191 
192 }