001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2020 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 * @noinspection NoopMethodInAbstractClass 030 */ 031public abstract class AbstractViolationReporter 032 extends AutomaticBean { 033 034 /** The severity level of any violations found. */ 035 private SeverityLevel severityLevel = SeverityLevel.ERROR; 036 037 /** The identifier of the reporter. */ 038 private String id; 039 040 /** 041 * Returns the severity level of the messages generated by this module. 042 * 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 * 076 * @return the id 077 */ 078 public final String getId() { 079 return id; 080 } 081 082 /** 083 * Sets the identifier of the reporter. Can be null. 084 * 085 * @param id the id 086 */ 087 public final void setId(final String id) { 088 this.id = id; 089 } 090 091 /** 092 * Returns an unmodifiable map instance containing the custom messages 093 * for this configuration. 094 * 095 * @return unmodifiable map containing custom messages 096 */ 097 protected Map<String, String> getCustomMessages() { 098 return getConfiguration().getMessages(); 099 } 100 101 /** 102 * Returns the message bundle name resource bundle that contains the messages 103 * used by this module. 104 * <p> 105 * The default implementation expects the resource files to be named 106 * messages.properties, messages_de.properties, etc. The file must 107 * be placed in the same package as the module implementation. 108 * </p> 109 * <p> 110 * Example: If you write com/foo/MyCoolCheck, create resource files 111 * com/foo/messages.properties, com/foo/messages_de.properties, etc. 112 * </p> 113 * 114 * @return name of a resource bundle that contains the messages 115 * used by this module. 116 */ 117 protected String getMessageBundle() { 118 final String className = getClass().getName(); 119 return getMessageBundle(className); 120 } 121 122 /** 123 * For unit tests, especially with a class with no package name. 124 * 125 * @param className class name of the module. 126 * @return name of a resource bundle that contains the messages 127 * used by the module. 128 */ 129 private static String getMessageBundle(final String className) { 130 final String messageBundle; 131 final int endIndex = className.lastIndexOf('.'); 132 final String messages = "messages"; 133 if (endIndex == -1) { 134 messageBundle = messages; 135 } 136 else { 137 final String packageName = className.substring(0, endIndex); 138 messageBundle = packageName + "." + messages; 139 } 140 return messageBundle; 141 } 142 143 @Override 144 protected void finishLocalSetup() throws CheckstyleException { 145 // No code by default 146 } 147 148 /** 149 * Log a message that has no column information. 150 * 151 * @param line the line number where the audit event was found 152 * @param key the message that describes the audit event 153 * @param args the details of the message 154 * 155 * @see java.text.MessageFormat 156 */ 157 // -@cs[CustomDeclarationOrder] CustomDeclarationOrder does not treat groups of 158 // overloaded methods. See https://github.com/sevntu-checkstyle/sevntu.checkstyle/issues/414 159 public abstract void log(int line, String key, Object... args); 160 161 /** 162 * Log a message that has column information. 163 * 164 * @param line the line number where the audit event was found 165 * @param col the column number where the audit event was found 166 * @param key the message that describes the audit event 167 * @param args the details of the message 168 * 169 * @see java.text.MessageFormat 170 */ 171 // -@cs[CustomDeclarationOrder] CustomDeclarationOrder does not treat groups of 172 // overloaded methods. See https://github.com/sevntu-checkstyle/sevntu.checkstyle/issues/414 173 public abstract void log(int line, int col, String key, 174 Object... args); 175 176}