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; 021 022import com.puppycrawl.tools.checkstyle.api.DetailAST; 023import com.puppycrawl.tools.checkstyle.api.FileContents; 024import com.puppycrawl.tools.checkstyle.api.Violation; 025 026/** 027 * Raw {@code TreeWalker} event for audit. 028 * 029 */ 030public class TreeWalkerAuditEvent { 031 032 /** Filename event associated with. **/ 033 private final String fileName; 034 /** The file contents. */ 035 private final FileContents fileContents; 036 /** Violation associated with the event. **/ 037 private final Violation violation; 038 /** Root ast element. **/ 039 private final DetailAST rootAst; 040 041 /** 042 * Creates a new {@code TreeWalkerAuditEvent} instance. 043 * 044 * @param fileContents contents of the file associated with the event 045 * @param fileName file associated with the event 046 * @param violation the actual violation 047 * @param rootAst root AST element {@link DetailAST} of the file 048 */ 049 public TreeWalkerAuditEvent(FileContents fileContents, String fileName, 050 Violation violation, DetailAST rootAst) { 051 this.fileContents = fileContents; 052 this.fileName = fileName; 053 this.violation = violation; 054 this.rootAst = rootAst; 055 } 056 057 /** 058 * Returns name of file being audited. 059 * 060 * @return the file name currently being audited or null if there is 061 * no relation to a file. 062 */ 063 public String getFileName() { 064 return fileName; 065 } 066 067 /** 068 * Returns contents of the file. 069 * 070 * @return contents of the file. 071 */ 072 public FileContents getFileContents() { 073 return fileContents; 074 } 075 076 /** 077 * Gets the violation. 078 * 079 * @return the violation 080 */ 081 public Violation getViolation() { 082 return violation; 083 } 084 085 /** 086 * Return the line number on the source file where the event occurred. 087 * This may be 0 if there is no relation to a file content. 088 * 089 * @return an integer representing the line number in the file source code. 090 */ 091 public int getLine() { 092 return violation.getLineNo(); 093 } 094 095 /** 096 * Return the violation associated to the event. 097 * 098 * @return the violation message 099 */ 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}