001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2019 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.checks.regexp; 021 022import com.puppycrawl.tools.checkstyle.StatelessCheck; 023import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 024import com.puppycrawl.tools.checkstyle.api.DetailAST; 025import com.puppycrawl.tools.checkstyle.utils.CommonUtil; 026 027/** 028 * Implementation of a check that looks for a single line in Java files. 029 * Supports ignoring comments for matches. 030 */ 031@StatelessCheck 032public class RegexpSinglelineJavaCheck extends AbstractCheck { 033 034 /** The format of the regular expression to match. */ 035 private String format = "$."; 036 /** The message to report for a match. */ 037 private String message; 038 /** The minimum number of matches required per file. */ 039 private int minimum; 040 /** The maximum number of matches required per file. */ 041 private int maximum; 042 /** Whether to ignore case when matching. */ 043 private boolean ignoreCase; 044 /** Suppress comments. **/ 045 private boolean ignoreComments; 046 047 @Override 048 public int[] getDefaultTokens() { 049 return getRequiredTokens(); 050 } 051 052 @Override 053 public int[] getAcceptableTokens() { 054 return getRequiredTokens(); 055 } 056 057 @Override 058 public int[] getRequiredTokens() { 059 return CommonUtil.EMPTY_INT_ARRAY; 060 } 061 062 @Override 063 public void beginTree(DetailAST rootAST) { 064 MatchSuppressor suppressor = null; 065 if (ignoreComments) { 066 suppressor = new CommentSuppressor(getFileContents()); 067 } 068 069 final DetectorOptions options = DetectorOptions.newBuilder() 070 .reporter(this) 071 .compileFlags(0) 072 .suppressor(suppressor) 073 .format(format) 074 .message(message) 075 .minimum(minimum) 076 .maximum(maximum) 077 .ignoreCase(ignoreCase) 078 .build(); 079 final SinglelineDetector detector = new SinglelineDetector(options); 080 detector.processLines(getFileContents().getText()); 081 } 082 083 /** 084 * Set the format of the regular expression to match. 085 * @param format the format of the regular expression to match. 086 */ 087 public void setFormat(String format) { 088 this.format = format; 089 } 090 091 /** 092 * Set the message to report for a match. 093 * @param message the message to report for a match. 094 */ 095 public void setMessage(String message) { 096 this.message = message; 097 } 098 099 /** 100 * Set the minimum number of matches required per file. 101 * @param minimum the minimum number of matches required per file. 102 */ 103 public void setMinimum(int minimum) { 104 this.minimum = minimum; 105 } 106 107 /** 108 * Set the maximum number of matches required per file. 109 * @param maximum the maximum number of matches required per file. 110 */ 111 public void setMaximum(int maximum) { 112 this.maximum = maximum; 113 } 114 115 /** 116 * Set whether to ignore case when matching. 117 * @param ignoreCase whether to ignore case when matching. 118 */ 119 public void setIgnoreCase(boolean ignoreCase) { 120 this.ignoreCase = ignoreCase; 121 } 122 123 /** 124 * Set whether to ignore comments when matching. 125 * @param ignore whether to ignore comments when matching. 126 */ 127 public void setIgnoreComments(boolean ignore) { 128 ignoreComments = ignore; 129 } 130 131}