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 import com.puppycrawl.tools.checkstyle.checks.CheckUtils; 26 27 62 public class RightCurlyCheck extends AbstractOptionCheck 63 { 64 65 private boolean mShouldStartLine = true; 66 67 70 public RightCurlyCheck() 71 { 72 super(RightCurlyOption.SAME); 73 } 74 75 79 public void setShouldStartLine(boolean aFlag) 80 { 81 mShouldStartLine = aFlag; 82 } 83 84 85 public int[] getDefaultTokens() 86 { 87 return new int[] { 88 TokenTypes.LITERAL_TRY, 89 TokenTypes.LITERAL_CATCH, 90 TokenTypes.LITERAL_FINALLY, 91 TokenTypes.LITERAL_IF, 92 TokenTypes.LITERAL_ELSE, 93 }; 94 } 95 96 97 public void visitToken(DetailAST aAST) 98 { 99 DetailAST rcurly; 101 DetailAST lcurly; 102 DetailAST nextToken; 103 boolean shouldCheckLastRcurly = false; 104 105 switch (aAST.getType()) { 106 case TokenTypes.LITERAL_TRY: 107 lcurly = (DetailAST) aAST.getFirstChild(); 108 nextToken = (DetailAST) lcurly.getNextSibling(); 109 rcurly = lcurly.getLastChild(); 110 break; 111 case TokenTypes.LITERAL_CATCH: 112 nextToken = (DetailAST) aAST.getNextSibling(); 113 lcurly = aAST.getLastChild(); 114 rcurly = lcurly.getLastChild(); 115 if (nextToken == null) { 116 shouldCheckLastRcurly = true; 117 nextToken = getNextToken(aAST); 118 } 119 break; 120 case TokenTypes.LITERAL_IF: 121 nextToken = aAST.findFirstToken(TokenTypes.LITERAL_ELSE); 122 if (nextToken != null) { 123 lcurly = nextToken.getPreviousSibling(); 124 rcurly = lcurly.getLastChild(); 125 } 126 else { 127 shouldCheckLastRcurly = true; 128 nextToken = getNextToken(aAST); 129 lcurly = aAST.getLastChild(); 130 rcurly = lcurly.getLastChild(); 131 } 132 break; 133 case TokenTypes.LITERAL_ELSE: 134 shouldCheckLastRcurly = true; 135 nextToken = getNextToken(aAST); 136 lcurly = (DetailAST) aAST.getFirstChild(); 137 rcurly = lcurly.getLastChild(); 138 break; 139 case TokenTypes.LITERAL_FINALLY: 140 shouldCheckLastRcurly = true; 141 nextToken = getNextToken(aAST); 142 lcurly = (DetailAST) aAST.getFirstChild(); 143 rcurly = lcurly.getLastChild(); 144 break; 145 default: 146 throw new RuntimeException ("Unexpected token type (" 147 + TokenTypes.getTokenName(aAST.getType()) + ")"); 148 } 149 150 if ((rcurly == null) || (rcurly.getType() != TokenTypes.RCURLY)) { 151 return; 153 } 154 155 if (shouldCheckLastRcurly) { 156 if (rcurly.getLineNo() == nextToken.getLineNo()) { 157 log(rcurly, "line.alone", "}"); 158 } 159 } 160 else if ((getAbstractOption() == RightCurlyOption.SAME) 161 && (rcurly.getLineNo() != nextToken.getLineNo())) 162 { 163 log(rcurly, "line.same", "}"); 164 } 165 else if ((getAbstractOption() == RightCurlyOption.ALONE) 166 && (rcurly.getLineNo() == nextToken.getLineNo())) 167 { 168 log(rcurly, "line.alone", "}"); 169 } 170 171 if (!mShouldStartLine) { 172 return; 173 } 174 final boolean startsLine = 175 Utils.whitespaceBefore(rcurly.getColumnNo(), 176 getLines()[rcurly.getLineNo() - 1]); 177 178 if (!startsLine && (lcurly.getLineNo() != rcurly.getLineNo())) { 179 log(rcurly, "line.new", "}"); 180 } 181 } 182 183 188 private DetailAST getNextToken(DetailAST aAST) 189 { 190 DetailAST next = null; 191 DetailAST parent = aAST; 192 while ((parent != null) && (next == null)) { 193 next = (DetailAST) parent.getNextSibling(); 194 parent = parent.getParent(); 195 } 196 return CheckUtils.getFirstNode(next); 197 } 198 } 199 | Popular Tags |