1 package com.puppycrawl.tools.checkstyle.checks.blocks; 20 21 import com.puppycrawl.tools.checkstyle.api.DetailAST; 22 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 23 import com.puppycrawl.tools.checkstyle.api.Utils; 24 import com.puppycrawl.tools.checkstyle.checks.AbstractOptionCheck; 25 26 67 public class LeftCurlyCheck 68 extends AbstractOptionCheck 69 { 70 71 private static final int DEFAULT_MAX_LINE_LENGTH = 80; 72 73 74 private int mMaxLineLength = DEFAULT_MAX_LINE_LENGTH; 75 76 79 public LeftCurlyCheck() 80 { 81 super(LeftCurlyOption.EOL); 82 } 83 84 89 public void setMaxLineLength(int aMaxLineLength) 90 { 91 mMaxLineLength = aMaxLineLength; 92 } 93 94 95 public int[] getDefaultTokens() 96 { 97 return new int[] { 98 TokenTypes.INTERFACE_DEF, 99 TokenTypes.CLASS_DEF, 100 TokenTypes.ANNOTATION_DEF, 101 TokenTypes.ENUM_DEF, 102 TokenTypes.CTOR_DEF, 103 TokenTypes.METHOD_DEF, 104 TokenTypes.ENUM_CONSTANT_DEF, 105 TokenTypes.LITERAL_WHILE, 106 TokenTypes.LITERAL_TRY, 107 TokenTypes.LITERAL_CATCH, 108 TokenTypes.LITERAL_FINALLY, 109 TokenTypes.LITERAL_SYNCHRONIZED, 110 TokenTypes.LITERAL_SWITCH, 111 TokenTypes.LITERAL_DO, 112 TokenTypes.LITERAL_IF, 113 TokenTypes.LITERAL_ELSE, 114 TokenTypes.LITERAL_FOR, 115 }; 118 } 119 120 121 public void visitToken(DetailAST aAST) 122 { 123 final DetailAST startToken; 124 final DetailAST brace; 125 126 switch (aAST.getType()) { 127 case TokenTypes.CTOR_DEF : 128 case TokenTypes.METHOD_DEF : 129 startToken = aAST; 130 brace = aAST.findFirstToken(TokenTypes.SLIST); 131 break; 132 133 case TokenTypes.INTERFACE_DEF : 134 case TokenTypes.CLASS_DEF : 135 case TokenTypes.ANNOTATION_DEF : 136 case TokenTypes.ENUM_DEF : 137 case TokenTypes.ENUM_CONSTANT_DEF : 138 startToken = (DetailAST) aAST.getFirstChild(); 139 final DetailAST objBlock = aAST.findFirstToken(TokenTypes.OBJBLOCK); 140 brace = (objBlock == null) 141 ? null 142 : (DetailAST) objBlock.getFirstChild(); 143 break; 144 145 case TokenTypes.LITERAL_WHILE: 146 case TokenTypes.LITERAL_CATCH: 147 case TokenTypes.LITERAL_SYNCHRONIZED: 148 case TokenTypes.LITERAL_FOR: 149 case TokenTypes.LITERAL_TRY: 150 case TokenTypes.LITERAL_FINALLY: 151 case TokenTypes.LITERAL_DO: 152 case TokenTypes.LITERAL_IF : 153 startToken = aAST; 154 brace = aAST.findFirstToken(TokenTypes.SLIST); 155 break; 156 157 case TokenTypes.LITERAL_ELSE : 158 startToken = aAST; 159 final DetailAST candidate = (DetailAST) aAST.getFirstChild(); 160 brace = 161 (candidate.getType() == TokenTypes.SLIST) 162 ? candidate 163 : null; break; 165 166 case TokenTypes.LITERAL_SWITCH : 167 startToken = aAST; 168 brace = aAST.findFirstToken(TokenTypes.LCURLY); 169 break; 170 171 default : 172 startToken = null; 173 brace = null; 174 } 175 176 if ((brace != null) && (startToken != null)) { 177 verifyBrace(brace, startToken); 178 } 179 } 180 181 187 private void verifyBrace(final DetailAST aBrace, 188 final DetailAST aStartToken) 189 { 190 final String braceLine = getLines()[aBrace.getLineNo() - 1]; 191 192 final int prevLineLen = (aBrace.getLineNo() == 1) 196 ? mMaxLineLength 197 : Utils.lengthMinusTrailingWhitespace( 198 getLines()[aBrace.getLineNo() - 2]); 199 200 if ((braceLine.length() > (aBrace.getColumnNo() + 1)) 202 && (braceLine.charAt(aBrace.getColumnNo() + 1) == '}')) 203 { 204 ; } 206 else if (getAbstractOption() == LeftCurlyOption.NL) { 207 if (!Utils.whitespaceBefore(aBrace.getColumnNo(), braceLine)) { 208 log(aBrace.getLineNo(), aBrace.getColumnNo(), 209 "line.new", "{"); 210 } 211 } 212 else if (getAbstractOption() == LeftCurlyOption.EOL) { 213 if (Utils.whitespaceBefore(aBrace.getColumnNo(), braceLine) 214 && ((prevLineLen + 2) <= mMaxLineLength)) 215 { 216 log(aBrace.getLineNo(), aBrace.getColumnNo(), 217 "line.previous", "{"); 218 } 219 } 220 else if (getAbstractOption() == LeftCurlyOption.NLOW) { 221 if (aStartToken.getLineNo() == aBrace.getLineNo()) { 222 ; } 224 else if ((aStartToken.getLineNo() + 1) == aBrace.getLineNo()) { 225 if (!Utils.whitespaceBefore(aBrace.getColumnNo(), braceLine)) { 226 log(aBrace.getLineNo(), aBrace.getColumnNo(), 227 "line.new", "{"); 228 } 229 else if ((prevLineLen + 2) <= mMaxLineLength) { 230 log(aBrace.getLineNo(), aBrace.getColumnNo(), 231 "line.previous", "{"); 232 } 233 } 234 else if (!Utils.whitespaceBefore(aBrace.getColumnNo(), braceLine)) { 235 log(aBrace.getLineNo(), aBrace.getColumnNo(), 236 "line.new", "{"); 237 } 238 } 239 } 240 } 241 | Popular Tags |