001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2014 Oliver Burn 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//////////////////////////////////////////////////////////////////////////////// 019package com.puppycrawl.tools.checkstyle.api; 020 021import java.util.Map; 022 023/** 024 * Serves as an abstract base class for all modules that report inspection 025 * findings. Such modules have a Severity level which is used for the 026 * {@link LocalizedMessage localized messages} that are created by the module. 027 * 028 * @author lkuehne 029 */ 030public abstract class AbstractViolationReporter 031 extends AutomaticBean 032{ 033 /** the severity level of any violations found */ 034 private SeverityLevel severityLevel = SeverityLevel.ERROR; 035 036 /** the identifier of the reporter */ 037 private String id; 038 039 /** 040 * Returns the severity level of the messages generated by this module. 041 * @return the severity level 042 * @see SeverityLevel 043 * @see LocalizedMessage#getSeverityLevel 044 */ 045 public final SeverityLevel getSeverityLevel() 046 { 047 return severityLevel; 048 } 049 050 /** 051 * Sets the severity level. The string should be one of the names 052 * defined in the <code>SeverityLevel</code> class. 053 * 054 * @param severity The new severity level 055 * @see SeverityLevel 056 */ 057 public final void setSeverity(String severity) 058 { 059 severityLevel = SeverityLevel.getInstance(severity); 060 } 061 062 /** 063 * Get the severity level's name. 064 * 065 * @return the check's severity level name. 066 */ 067 public final String getSeverity() 068 { 069 return severityLevel.getName(); 070 } 071 072 /** 073 * Returns the identifier of the reporter. Can be null. 074 * @return the id 075 */ 076 public final String getId() 077 { 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 { 087 this.id = id; 088 } 089 090 /** 091 * Helper method to log a LocalizedMessage. 092 * 093 * @param ast a node to get line id column numbers associated 094 * with the message 095 * @param key key to locale message format 096 * @param args arguments to format 097 */ 098 protected final void log(DetailAST ast, String key, Object... args) 099 { 100 log(ast.getLineNo(), ast.getColumnNo(), key, args); 101 } 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 final String className = this.getClass().getName(); 122 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 return getConfiguration().getMessages(); 133 } 134 135 /** 136 * for unit tests, especially with a class with no package name. 137 * @param className 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 className) 142 { 143 final int endIndex = className.lastIndexOf('.'); 144 final String messages = "messages"; 145 if (endIndex < 0) { 146 return messages; 147 } 148 final String packageName = className.substring(0, endIndex); 149 return packageName + "." + messages; 150 } 151 152 /** 153 * Log a message that has no column information. 154 * 155 * @param line the line number where the error was found 156 * @param key the message that describes the error 157 * @param args the details of the message 158 * 159 * @see java.text.MessageFormat 160 */ 161 public abstract void log(int line, String key, Object... args); 162 163 /** 164 * Log a message that has column information. 165 * 166 * @param line the line number where the error was found 167 * @param col the column number where the error was found 168 * @param key the message that describes the error 169 * @param args the details of the message 170 * 171 * @see java.text.MessageFormat 172 */ 173 public abstract void log(int line, int col, String key, 174 Object... args); 175}