aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/puppycrawl/tools/checkstyle/api/AbstractViolationReporter.java
blob: 1739b6bd5b7aa84c386cdf42aaa6b5944178a087 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2017 the original author or authors.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
////////////////////////////////////////////////////////////////////////////////

package com.puppycrawl.tools.checkstyle.api;

import java.util.Map;

/**
 * Serves as an abstract base class for all modules that report inspection
 * findings. Such modules have a Severity level which is used for the
 * {@link LocalizedMessage localized messages} that are created by the module.
 *
 * @author lkuehne
 */
public abstract class AbstractViolationReporter
    extends AutomaticBean {
    /** The severity level of any violations found. */
    private SeverityLevel severityLevel = SeverityLevel.ERROR;

    /** The identifier of the reporter. */
    private String id;

    /**
     * Returns the severity level of the messages generated by this module.
     * @return the severity level
     * @see SeverityLevel
     * @see LocalizedMessage#getSeverityLevel
     * @noinspection WeakerAccess
     */
    public final SeverityLevel getSeverityLevel() {
        return severityLevel;
    }

    /**
     * Sets the severity level.  The string should be one of the names
     * defined in the {@code SeverityLevel} class.
     *
     * @param severity  The new severity level
     * @see SeverityLevel
     */
    public final void setSeverity(String severity) {
        severityLevel = SeverityLevel.getInstance(severity);
    }

    /**
     *  Get the severity level's name.
     *
     *  @return  the check's severity level name.
     *  @noinspection WeakerAccess
     */
    public final String getSeverity() {
        return severityLevel.getName();
    }

    /**
     * Returns the identifier of the reporter. Can be null.
     * @return the id
     */
    public final String getId() {
        return id;
    }

    /**
     * Sets the identifier of the reporter. Can be null.
     * @param id the id
     */
    public final void setId(final String id) {
        this.id = id;
    }

    /**
     * Returns an unmodifiable map instance containing the custom messages
     * for this configuration.
     * @return unmodifiable map containing custom messages
     */
    protected Map<String, String> getCustomMessages() {
        return getConfiguration().getMessages();
    }

    /**
     * Returns the message bundle name resource bundle that contains the messages
     * used by this module.
     * <p>
     * The default implementation expects the resource files to be named
     * messages.properties, messages_de.properties, etc. The file must
     * be placed in the same package as the module implementation.
     * </p>
     * <p>
     * Example: If you write com/foo/MyCoolCheck, create resource files
     * com/foo/messages.properties, com/foo/messages_de.properties, etc.
     * </p>
     *
     * @return name of a resource bundle that contains the messages
     *     used by this module.
     */
    protected String getMessageBundle() {
        final String className = getClass().getName();
        return getMessageBundle(className);
    }

    /**
     * For unit tests, especially with a class with no package name.
     * @param className class name of the module.
     * @return name of a resource bundle that contains the messages
     *     used by the module.
     */
    private static String getMessageBundle(final String className) {
        final String messageBundle;
        final int endIndex = className.lastIndexOf('.');
        final String messages = "messages";
        if (endIndex == -1) {
            messageBundle = messages;
        }
        else {
            final String packageName = className.substring(0, endIndex);
            messageBundle = packageName + "." + messages;
        }
        return messageBundle;
    }

    /**
     * Log a message that has no column information.
     *
     * @param line the line number where the error was found
     * @param key the message that describes the error
     * @param args the details of the message
     *
     * @see java.text.MessageFormat
     */
    // -@cs[CustomDeclarationOrder] CustomDeclarationOrder does not treat groups of
    // overloaded methods. See https://github.com/sevntu-checkstyle/sevntu.checkstyle/issues/414
    public abstract void log(int line, String key, Object... args);

    /**
     * Log a message that has column information.
     *
     * @param line the line number where the error was found
     * @param col the column number where the error was found
     * @param key the message that describes the error
     * @param args the details of the message
     *
     * @see java.text.MessageFormat
     */
    // -@cs[CustomDeclarationOrder] CustomDeclarationOrder does not treat groups of
    // overloaded methods. See https://github.com/sevntu-checkstyle/sevntu.checkstyle/issues/414
    public abstract void log(int line, int col, String key,
            Object... args);
}