1 //////////////////////////////////////////////////////////////////////////////// 2 // checkstyle: Checks Java source code for adherence to a set of rules. 3 // Copyright (C) 2001-2020 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 * @noinspection NoopMethodInAbstractClass 30 */ 31 public abstract class AbstractViolationReporter 32 extends AutomaticBean { 33 34 /** The severity level of any violations found. */ 35 private SeverityLevel severityLevel = SeverityLevel.ERROR; 36 37 /** The identifier of the reporter. */ 38 private String id; 39 40 /** 41 * Returns the severity level of the messages generated by this module. 42 * 43 * @return the severity level 44 * @see SeverityLevel 45 * @see LocalizedMessage#getSeverityLevel 46 * @noinspection WeakerAccess 47 */ 48 public final SeverityLevel getSeverityLevel() { 49 return severityLevel; 50 } 51 52 /** 53 * Sets the severity level. The string should be one of the names 54 * defined in the {@code SeverityLevel} class. 55 * 56 * @param severity The new severity level 57 * @see SeverityLevel 58 */ 59 public final void setSeverity(String severity) { 60 severityLevel = SeverityLevel.getInstance(severity); 61 } 62 63 /** 64 * Get the severity level's name. 65 * 66 * @return the check's severity level name. 67 * @noinspection WeakerAccess 68 */ 69 public final String getSeverity() { 70 return severityLevel.getName(); 71 } 72 73 /** 74 * Returns the identifier of the reporter. Can be null. 75 * 76 * @return the id 77 */ 78 public final String getId() { 79 return id; 80 } 81 82 /** 83 * Sets the identifier of the reporter. Can be null. 84 * 85 * @param id the id 86 */ 87 public final void setId(final String id) { 88 this.id = id; 89 } 90 91 /** 92 * Returns an unmodifiable map instance containing the custom messages 93 * for this configuration. 94 * 95 * @return unmodifiable map containing custom messages 96 */ 97 protected Map<String, String> getCustomMessages() { 98 return getConfiguration().getMessages(); 99 } 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 }