View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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  /**
23   * Raw event for audit.
24   *
25   * <p>
26   * <i>
27   * I'm not very satisfied about the design of this event since there are
28   * optional methods that will return null in most of the case. This will
29   * need some work to clean it up especially if we want to introduce
30   * a more sequential reporting action rather than a packet
31   * reporting. This will allow for example to follow the process quickly
32   * in an interface or a servlet (yep, that's cool to run a check via
33   * a web interface in a source repository ;-)
34   * </i>
35   * </p>
36   *
37   * @see AuditListener
38   * @noinspection ClassCanBeRecord
39   * @noinspectionreason We can not break compatibility with previous versions.
40   */
41  public final class AuditEvent {
42  
43      /** The object on which the Event initially occurred. */
44      private final Object source;
45      /** Filename event associated with. **/
46      private final String fileName;
47      /** Violation associated with the event. **/
48      private final Violation violation;
49  
50      /**
51       * Creates a new instance.
52       *
53       * @param source the object that created the event
54       */
55      public AuditEvent(Object source) {
56          this(source, null);
57      }
58  
59      /**
60       * Creates a new {@code AuditEvent} instance.
61       *
62       * @param src source of the event
63       * @param fileName file associated with the event
64       */
65      public AuditEvent(Object src, String fileName) {
66          this(src, fileName, null);
67      }
68  
69      /**
70       * Creates a new {@code AuditEvent} instance.
71       *
72       * @param src source of the event
73       * @param fileName file associated with the event
74       * @param violation the actual violation
75       * @throws IllegalArgumentException if {@code src} is {@code null}.
76       */
77      public AuditEvent(Object src, String fileName, Violation violation) {
78          if (src == null) {
79              throw new IllegalArgumentException("null source");
80          }
81  
82          source = src;
83          this.fileName = fileName;
84          this.violation = violation;
85      }
86  
87      /**
88       * The object on which the Event initially occurred.
89       *
90       * @return the object on which the Event initially occurred
91       */
92      public Object getSource() {
93          return source;
94      }
95  
96      /**
97       * Returns name of file being audited.
98       *
99       * @return the file name currently being audited or null if there is
100      *     no relation to a file.
101      */
102     public String getFileName() {
103         return fileName;
104     }
105 
106     /**
107      * Return the line number on the source file where the event occurred.
108      * This may be 0 if there is no relation to a file content.
109      *
110      * @return an integer representing the line number in the file source code.
111      */
112     public int getLine() {
113         return violation.getLineNo();
114     }
115 
116     /**
117      * Return the violation associated to the event.
118      *
119      * @return the event violation
120      */
121     public String getMessage() {
122         return violation.getViolation();
123     }
124 
125     /**
126      * Gets the column associated with the violation.
127      *
128      * @return the column associated with the violation
129      */
130     public int getColumn() {
131         return violation.getColumnNo();
132     }
133 
134     /**
135      * Gets the audit event severity level.
136      *
137      * @return the audit event severity level
138      */
139     public SeverityLevel getSeverityLevel() {
140         SeverityLevel severityLevel = SeverityLevel.INFO;
141         if (violation != null) {
142             severityLevel = violation.getSeverityLevel();
143         }
144         return severityLevel;
145     }
146 
147     /**
148      * Returns id of module.
149      *
150      * @return the identifier of the module that generated the event. Can return
151      *         null.
152      */
153     public String getModuleId() {
154         return violation.getModuleId();
155     }
156 
157     /**
158      * Gets the name of the source for the violation.
159      *
160      * @return the name of the source for the violation
161      */
162     public String getSourceName() {
163         return violation.getSourceName();
164     }
165 
166     /**
167      * Gets the violation.
168      *
169      * @return the violation
170      */
171     public Violation getViolation() {
172         return violation;
173     }
174 
175 }