001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2018 the original author or authors. 004// 005// This library is free software; you can redistribute it and/or 006// modify it under the terms of the GNU Lesser General Public 007// License as published by the Free Software Foundation; either 008// version 2.1 of the License, or (at your option) any later version. 009// 010// This library is distributed in the hope that it will be useful, 011// but WITHOUT ANY WARRANTY; without even the implied warranty of 012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013// Lesser General Public License for more details. 014// 015// You should have received a copy of the GNU Lesser General Public 016// License along with this library; if not, write to the Free Software 017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018//////////////////////////////////////////////////////////////////////////////// 019 020package com.puppycrawl.tools.checkstyle.api; 021 022import java.util.Map; 023 024/** 025 * Serves as an abstract base class for all modules that report inspection 026 * findings. Such modules have a Severity level which is used for the 027 * {@link LocalizedMessage localized messages} that are created by the module. 028 * 029 * @author lkuehne 030 * @noinspection NoopMethodInAbstractClass 031 */ 032public abstract class AbstractViolationReporter 033 extends AutomaticBean { 034 035 /** The severity level of any violations found. */ 036 private SeverityLevel severityLevel = SeverityLevel.ERROR; 037 038 /** The identifier of the reporter. */ 039 private String id; 040 041 /** 042 * Returns the severity level of the messages generated by this module. 043 * @return the severity level 044 * @see SeverityLevel 045 * @see LocalizedMessage#getSeverityLevel 046 * @noinspection WeakerAccess 047 */ 048 public final SeverityLevel getSeverityLevel() { 049 return severityLevel; 050 } 051 052 /** 053 * Sets the severity level. The string should be one of the names 054 * defined in the {@code SeverityLevel} class. 055 * 056 * @param severity The new severity level 057 * @see SeverityLevel 058 */ 059 public final void setSeverity(String severity) { 060 severityLevel = SeverityLevel.getInstance(severity); 061 } 062 063 /** 064 * Get the severity level's name. 065 * 066 * @return the check's severity level name. 067 * @noinspection WeakerAccess 068 */ 069 public final String getSeverity() { 070 return severityLevel.getName(); 071 } 072 073 /** 074 * Returns the identifier of the reporter. Can be null. 075 * @return the id 076 */ 077 public final String getId() { 078 return id; 079 } 080 081 /** 082 * Sets the identifier of the reporter. Can be null. 083 * @param id the id 084 */ 085 public final void setId(final String id) { 086 this.id = id; 087 } 088 089 /** 090 * Returns an unmodifiable map instance containing the custom messages 091 * for this configuration. 092 * @return unmodifiable map containing custom messages 093 */ 094 protected Map<String, String> getCustomMessages() { 095 return getConfiguration().getMessages(); 096 } 097 098 /** 099 * 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}