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