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