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