View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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;
21  
22  import com.puppycrawl.tools.checkstyle.api.DetailAST;
23  import com.puppycrawl.tools.checkstyle.api.FileContents;
24  import com.puppycrawl.tools.checkstyle.api.Violation;
25  
26  /**
27   * Raw {@code TreeWalker} event for audit.
28   *
29   */
30  public class TreeWalkerAuditEvent {
31  
32      /** Filename event associated with. **/
33      private final String fileName;
34      /** The file contents. */
35      private final FileContents fileContents;
36      /** Violation associated with the event. **/
37      private final Violation violation;
38      /** Root ast element. **/
39      private final DetailAST rootAst;
40  
41      /**
42       * Creates a new {@code TreeWalkerAuditEvent} instance.
43       *
44       * @param fileContents contents of the file associated with the event
45       * @param fileName file associated with the event
46       * @param violation the actual violation
47       * @param rootAst root AST element {@link DetailAST} of the file
48       */
49      public TreeWalkerAuditEvent(FileContents fileContents, String fileName,
50                                  Violation violation, DetailAST rootAst) {
51          this.fileContents = fileContents;
52          this.fileName = fileName;
53          this.violation = violation;
54          this.rootAst = rootAst;
55      }
56  
57      /**
58       * Returns name of file being audited.
59       *
60       * @return the file name currently being audited or null if there is
61       *     no relation to a file.
62       */
63      public String getFileName() {
64          return fileName;
65      }
66  
67      /**
68       * Returns contents of the file.
69       *
70       * @return contents of the file.
71       */
72      public FileContents getFileContents() {
73          return fileContents;
74      }
75  
76      /**
77       * Gets the violation.
78       *
79       * @return the violation
80       */
81      public Violation getViolation() {
82          return violation;
83      }
84  
85      /**
86       * Return the line number on the source file where the event occurred.
87       * This may be 0 if there is no relation to a file content.
88       *
89       * @return an integer representing the line number in the file source code.
90       */
91      public int getLine() {
92          return violation.getLineNo();
93      }
94  
95      /**
96       * Return the violation associated to the event.
97       *
98       * @return the violation message
99       */
100     public String getMessage() {
101         return violation.getViolation();
102     }
103 
104     /**
105      * Gets the column associated with the violation.
106      *
107      * @return the column associated with the violation
108      */
109     public int getColumn() {
110         return violation.getColumnNo();
111     }
112 
113     /**
114      * Gets the column char index associated with the violation.
115      *
116      * @return the column char index associated with the violation
117      */
118     public int getColumnCharIndex() {
119         return violation.getColumnCharIndex();
120     }
121 
122     /**
123      * Returns id of module.
124      *
125      * @return the identifier of the module that generated the event. Can return
126      *         null.
127      */
128     public String getModuleId() {
129         return violation.getModuleId();
130     }
131 
132     /**
133      * Gets the name of the source for the violation.
134      *
135      * @return the name of the source for the violation
136      */
137     public String getSourceName() {
138         return violation.getSourceName();
139     }
140 
141     /**
142      * Gets the token type of the violation.
143      *
144      * @return the token type of the violation
145      */
146     public int getTokenType() {
147         return violation.getTokenType();
148     }
149 
150     /**
151      * Gets the root element of the AST tree.
152      *
153      * @return the root element of the AST tree
154      */
155     public DetailAST getRootAst() {
156         return rootAst;
157     }
158 
159 }