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     *
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}