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