1 /* 2 * Copyright (c) 2011-2025 Yegor Bugayenko 3 * 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 1) Redistributions of source code must retain the above 9 * copyright notice, this list of conditions and the following 10 * disclaimer. 2) Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following 12 * disclaimer in the documentation and/or other materials provided 13 * with the distribution. 3) Neither the name of the Qulice.com nor 14 * the names of its contributors may be used to endorse or promote 15 * products derived from this software without specific prior written 16 * permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 20 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 21 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 22 * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 29 * OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 package com.qulice.checkstyle; 32 33 import com.jcabi.log.Logger; 34 import com.puppycrawl.tools.checkstyle.api.AuditEvent; 35 import com.puppycrawl.tools.checkstyle.api.AuditListener; 36 import com.qulice.spi.Environment; 37 import java.util.Collections; 38 import java.util.LinkedList; 39 import java.util.List; 40 41 /** 42 * Listener of Checkstyle events. 43 * 44 * @since 0.3 45 * @checkstyle ClassDataAbstractionCoupling (260 lines) 46 */ 47 final class CheckstyleListener implements AuditListener { 48 49 /** 50 * Environment. 51 */ 52 private final Environment env; 53 54 /** 55 * Collection of events collected. 56 */ 57 private final List<AuditEvent> all; 58 59 /** 60 * Public ctor. 61 * @param environ The environment 62 */ 63 CheckstyleListener(final Environment environ) { 64 this.all = new LinkedList<>(); 65 this.env = environ; 66 } 67 68 /** 69 * Get all events. 70 * @return List of events 71 */ 72 public List<AuditEvent> events() { 73 return Collections.unmodifiableList(this.all); 74 } 75 76 @Override 77 public void auditStarted(final AuditEvent event) { 78 // intentionally empty 79 } 80 81 @Override 82 public void auditFinished(final AuditEvent event) { 83 // intentionally empty 84 } 85 86 @Override 87 public void fileStarted(final AuditEvent event) { 88 // intentionally empty 89 } 90 91 @Override 92 public void fileFinished(final AuditEvent event) { 93 // intentionally empty 94 } 95 96 @Override 97 public void addError(final AuditEvent event) { 98 final String name = event.getFileName().substring( 99 this.env.basedir().toString().length() 100 ); 101 if (!this.env.exclude("checkstyle", name)) { 102 this.all.add(event); 103 } 104 } 105 106 @Override 107 public void addException(final AuditEvent event, 108 final Throwable throwable) { 109 final String check = event.getSourceName(); 110 Logger.error( 111 this, 112 "%s[%d]: %s (%s), %[exception]s", 113 event.getFileName().substring( 114 this.env.basedir().toString().length() 115 ), 116 event.getLine(), 117 event.getMessage(), 118 check.substring(check.lastIndexOf('.') + 1), 119 throwable 120 ); 121 } 122 123 }