Coverage Report - com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractViolationReporter
90%
18/20
100%
2/2
1.182
 
 1  
 ////////////////////////////////////////////////////////////////////////////////
 2  
 // checkstyle: Checks Java source code for adherence to a set of rules.
 3  
 // Copyright (C) 2001-2014  Oliver Burn
 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  
 package com.puppycrawl.tools.checkstyle.api;
 20  
 
 21  
 import java.util.Map;
 22  
 
 23  
 /**
 24  
  * Serves as an abstract base class for all modules that report inspection
 25  
  * findings. Such modules have a Severity level which is used for the
 26  
  * {@link LocalizedMessage localized messages} that are created by the module.
 27  
  *
 28  
  * @author lkuehne
 29  
  */
 30  1206
 public abstract class AbstractViolationReporter
 31  
     extends AutomaticBean
 32  
 {
 33  
     /** the severity level of any violations found */
 34  1206
     private SeverityLevel mSeverityLevel = SeverityLevel.ERROR;
 35  
 
 36  
     /** the identifier of the reporter */
 37  
     private String mId;
 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  
      */
 45  
     public final SeverityLevel getSeverityLevel()
 46  
     {
 47  3428
         return mSeverityLevel;
 48  
     }
 49  
 
 50  
     /**
 51  
      * Sets the severity level.  The string should be one of the names
 52  
      * defined in the <code>SeverityLevel</code> class.
 53  
      *
 54  
      * @param aSeverity  The new severity level
 55  
      * @see SeverityLevel
 56  
      */
 57  
     public final void setSeverity(String aSeverity)
 58  
     {
 59  1230
         mSeverityLevel = SeverityLevel.getInstance(aSeverity);
 60  1230
     }
 61  
 
 62  
     /**
 63  
      *  Get the severity level's name.
 64  
      *
 65  
      *  @return  the check's severity level name.
 66  
      */
 67  
     public final String getSeverity()
 68  
     {
 69  560
         return mSeverityLevel.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  
     {
 78  3429
         return mId;
 79  
     }
 80  
 
 81  
     /**
 82  
      * Sets the identifier of the reporter. Can be null.
 83  
      * @param aId the id
 84  
      */
 85  
     public final void setId(final String aId)
 86  
     {
 87  0
         mId = aId;
 88  0
     }
 89  
 
 90  
     /**
 91  
      * Helper method to log a LocalizedMessage.
 92  
      *
 93  
      * @param aAST a node to get line and column numbers associated
 94  
      *             with the message
 95  
      * @param aKey key to locale message format
 96  
      * @param aArgs arguments to format
 97  
      */
 98  
     protected final void log(DetailAST aAST, String aKey, Object... aArgs)
 99  
     {
 100  799
         log(aAST.getLineNo(), aAST.getColumnNo(), aKey, aArgs);
 101  799
     }
 102  
 
 103  
     /**
 104  
      * Returns the message bundle name resourcebundle that contains the messages
 105  
      * used by this module.
 106  
      * <p>
 107  
      * The default implementation expects the resource files to be named
 108  
      * messages.properties, messages_de.properties, etc. The file must
 109  
      * be placed in the same package as the module implementation.
 110  
      * </p>
 111  
      * <p>
 112  
      * Example: If you write com/foo/MyCoolCheck, create resource files
 113  
      * com/foo/messages.properties, com/foo/messages_de.properties, etc.
 114  
      * </p>
 115  
      *
 116  
      * @return name of a resource bundle that contains the messages
 117  
      * used by this module.
 118  
      */
 119  
     protected String getMessageBundle()
 120  
     {
 121  3428
         final String className = this.getClass().getName();
 122  3428
         return getMessageBundle(className);
 123  
     }
 124  
 
 125  
     /**
 126  
      * Returns an unmodifiable map instance containing the custom messages
 127  
      * for this configuration.
 128  
      * @return unmodifiable map containing custom messages
 129  
      */
 130  
     protected Map<String, String> getCustomMessages()
 131  
     {
 132  3428
         return getConfiguration().getMessages();
 133  
     }
 134  
 
 135  
     /**
 136  
      * for unit tests, especially with a class with no package name.
 137  
      * @param aClassName class name of the module.
 138  
      * @return name of a resource bundle that contains the messages
 139  
      * used by the module.
 140  
      */
 141  
     String getMessageBundle(final String aClassName)
 142  
     {
 143  3430
         final int endIndex = aClassName.lastIndexOf('.');
 144  3430
         final String messages = "messages";
 145  3430
         if (endIndex < 0) {
 146  1
             return messages;
 147  
         }
 148  3429
         final String packageName = aClassName.substring(0, endIndex);
 149  3429
         return packageName + "." + messages;
 150  
     }
 151  
 
 152  
     /**
 153  
      * Log a message that has no column information.
 154  
      *
 155  
      * @param aLine the line number where the error was found
 156  
      * @param aKey the message that describes the error
 157  
      * @param aArgs the details of the message
 158  
      *
 159  
      * @see java.text.MessageFormat
 160  
      */
 161  
     public abstract void log(int aLine, String aKey, Object... aArgs);
 162  
 
 163  
     /**
 164  
      * Log a message that has column information.
 165  
      *
 166  
      * @param aLine the line number where the error was found
 167  
      * @param aCol the column number where the error was found
 168  
      * @param aKey the message that describes the error
 169  
      * @param aArgs the details of the message
 170  
      *
 171  
      * @see java.text.MessageFormat
 172  
      */
 173  
     public abstract void log(int aLine, int aCol, String aKey,
 174  
             Object... aArgs);
 175  
 }