001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2020 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.naming; 021 022import com.puppycrawl.tools.checkstyle.api.DetailAST; 023import com.puppycrawl.tools.checkstyle.api.TokenTypes; 024 025/** 026 * <p> 027 * Checks that {@code catch} parameter names conform to a specified pattern. 028 * </p> 029 * <p> 030 * Default pattern has the following characteristic: 031 * </p> 032 * <ul> 033 * <li>allows names beginning with two lowercase letters followed by at least one uppercase or 034 * lowercase letter</li> 035 * <li>allows {@code e} abbreviation (suitable for exceptions end errors)</li> 036 * <li>allows {@code ex} abbreviation (suitable for exceptions)</li> 037 * <li>allows {@code t} abbreviation (suitable for throwables)</li> 038 * <li>prohibits numbered abbreviations like {@code e1} or {@code t2}</li> 039 * <li>prohibits one letter prefixes like {@code pException}</li> 040 * <li>prohibits two letter abbreviations like {@code ie} or {@code ee}</li> 041 * <li>prohibits any other characters than letters</li> 042 * </ul> 043 * <ul> 044 * <li> 045 * Property {@code format} - Specifies valid identifiers. 046 * Type is {@code java.util.regex.Pattern}. 047 * Default value is {@code "^(e|t|ex|[a-z][a-z][a-zA-Z]+)$"}. 048 * </li> 049 * </ul> 050 * <p> 051 * An example of how to configure the check is: 052 * </p> 053 * <pre> 054 * <module name="CatchParameterName"/> 055 * </pre> 056 * <p>Example:</p> 057 * <pre> 058 * public class MyTest { 059 * public void myTest() { 060 * try { 061 * // ... 062 * } catch (ArithmeticException e) { // OK 063 * // ... 064 * } catch (ArrayIndexOutOfBoundsException ex) { // OK 065 * // ... 066 * } catch (Throwable t) { // OK 067 * // ... 068 * } catch (IndexOutOfBoundsException e123) { // violation, digits 069 * // not allowed 070 * // ... 071 * } catch (NullPointerException ab) { // violation, should have at least 072 * // three characters if not e|t|ex 073 * // ... 074 * } catch (ArrayStoreException abc) { // OK 075 * // ... 076 * } catch (InterruptedException aBC) { // violation, first two characters 077 * // should be in lowercase 078 * // ... 079 * } catch (RuntimeException abC) { // OK 080 * // ... 081 * } catch (Exception abCD) { // OK 082 * // ... 083 * } 084 * } 085 * } 086 * </pre> 087 * <p> 088 * An example of how to configure the check for names that begin with a lower case letter, 089 * followed by any letters or digits is: 090 * </p> 091 * <p>Configuration:</p> 092 * <pre> 093 * <module name="CatchParameterName"> 094 * <property name="format" value="^[a-z][a-zA-Z0-9]+$"/> 095 * </module> 096 * </pre> 097 * <p>Example:</p> 098 * <pre> 099 * public class MyTest { 100 * public void myTest() { 101 * try { 102 * // ... 103 * } catch (ArithmeticException ex) { // OK 104 * // ... 105 * } catch (ArrayIndexOutOfBoundsException ex2) { // OK 106 * // ... 107 * } catch (IOException thirdException) { // OK 108 * // ... 109 * } catch (Exception FourthException) { // violation, the initial letter 110 * // should be lowercase 111 * // ... 112 * } 113 * } 114 * } 115 * </pre> 116 * <p> 117 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 118 * </p> 119 * <p> 120 * Violation Message Keys: 121 * </p> 122 * <ul> 123 * <li> 124 * {@code name.invalidPattern} 125 * </li> 126 * </ul> 127 * 128 * @since 6.14 129 */ 130public class CatchParameterNameCheck extends AbstractNameCheck { 131 132 /** 133 * Creates a new {@code CatchParameterNameCheck} instance. 134 */ 135 public CatchParameterNameCheck() { 136 super("^(e|t|ex|[a-z][a-z][a-zA-Z]+)$"); 137 } 138 139 @Override 140 public int[] getDefaultTokens() { 141 return getRequiredTokens(); 142 } 143 144 @Override 145 public int[] getAcceptableTokens() { 146 return getRequiredTokens(); 147 } 148 149 @Override 150 public int[] getRequiredTokens() { 151 return new int[] {TokenTypes.PARAMETER_DEF}; 152 } 153 154 @Override 155 protected boolean mustCheckName(DetailAST ast) { 156 return ast.getParent().getType() == TokenTypes.LITERAL_CATCH; 157 } 158 159}