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