001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2024 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
024import com.puppycrawl.tools.checkstyle.AbstractAutomaticBean;
025
026/**
027 * Serves as an abstract base class for all modules that report inspection
028 * findings. Such modules have a Severity level which is used for the
029 * {@link Violation violations} that are created by the module.
030 *
031 * @noinspection NoopMethodInAbstractClass
032 * @noinspectionreason NoopMethodInAbstractClass - we allow each check to
033 *      define these methods, as needed. They should be overridden only
034 *      by demand in subclasses
035 */
036public abstract class AbstractViolationReporter
037    extends AbstractAutomaticBean {
038
039    /** The severity level of any violations found. */
040    private SeverityLevel severityLevel = SeverityLevel.ERROR;
041
042    /** The identifier of the reporter. */
043    private String id;
044
045    /**
046     * Returns the severity level of the violations generated by this module.
047     *
048     * @return the severity level
049     * @see SeverityLevel
050     * @see Violation#getSeverityLevel
051     * @noinspection WeakerAccess
052     * @noinspectionreason we avoid 'protected' when possible
053     */
054    public final SeverityLevel getSeverityLevel() {
055        return severityLevel;
056    }
057
058    /**
059     * Sets the severity level.  The string should be one of the names
060     * defined in the {@code SeverityLevel} class.
061     *
062     * @param severity  The new severity level
063     * @see SeverityLevel
064     */
065    public final void setSeverity(String severity) {
066        severityLevel = SeverityLevel.getInstance(severity);
067    }
068
069    /**
070     *  Get the severity level's name.
071     *
072     *  @return  the check's severity level name.
073     *  @noinspection WeakerAccess
074     *  @noinspectionreason WeakerAccess - we avoid 'protected' when possible
075     */
076    public final String getSeverity() {
077        return severityLevel.getName();
078    }
079
080    /**
081     * Returns the identifier of the reporter. Can be null.
082     *
083     * @return the id
084     */
085    public final String getId() {
086        return id;
087    }
088
089    /**
090     * Sets the identifier of the reporter. Can be null.
091     *
092     * @param id the id
093     */
094    public final void setId(final String id) {
095        this.id = id;
096    }
097
098    /**
099     * 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     * <p>
112     * The default implementation expects the resource files to be named
113     * messages.properties, messages_de.properties, etc. The file must
114     * be placed in the same package as the module implementation.
115     * </p>
116     * <p>
117     * Example: If you write com/foo/MyCoolCheck, create resource files
118     * com/foo/messages.properties, com/foo/messages_de.properties, etc.
119     * </p>
120     *
121     * @return name of a resource bundle that contains the messages
122     *     used by this module.
123     */
124    protected String getMessageBundle() {
125        final String className = getClass().getName();
126        return getMessageBundle(className);
127    }
128
129    /**
130     * For unit tests, especially with a class with no package name.
131     *
132     * @param className class name of the module.
133     * @return name of a resource bundle that contains the messages
134     *     used by the module.
135     */
136    private static String getMessageBundle(final String className) {
137        final String messageBundle;
138        final int endIndex = className.lastIndexOf('.');
139        final String messages = "messages";
140        if (endIndex == -1) {
141            messageBundle = messages;
142        }
143        else {
144            final String packageName = className.substring(0, endIndex);
145            messageBundle = packageName + "." + messages;
146        }
147        return messageBundle;
148    }
149
150    @Override
151    protected void finishLocalSetup() throws CheckstyleException {
152        // No code by default
153    }
154
155    /**
156     * Log a message that has no column information.
157     *
158     * @param line the line number where the audit event was found
159     * @param key the message that describes the audit event
160     * @param args the details of the message
161     *
162     * @see java.text.MessageFormat
163     */
164    // -@cs[CustomDeclarationOrder] CustomDeclarationOrder does not treat groups of
165    // overloaded methods. See https://github.com/sevntu-checkstyle/sevntu.checkstyle/issues/414
166    public abstract void log(int line, String key, Object... args);
167
168    /**
169     * Log a message that has column information.
170     *
171     * @param line the line number where the audit event was found
172     * @param col the column number where the audit event was found
173     * @param key the message that describes the audit event
174     * @param args the details of the message
175     *
176     * @see java.text.MessageFormat
177     */
178    // -@cs[CustomDeclarationOrder] CustomDeclarationOrder does not treat groups of
179    // overloaded methods. See https://github.com/sevntu-checkstyle/sevntu.checkstyle/issues/414
180    public abstract void log(int line, int col, String key,
181            Object... args);
182
183}