1 20 package com.puppycrawl.tools.checkstyle.checks.whitespace; 21 22 import com.puppycrawl.tools.checkstyle.api.Check; 23 import com.puppycrawl.tools.checkstyle.api.DetailAST; 24 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 25 26 61 public class NoWhitespaceBeforeCheck 62 extends Check 63 { 64 65 private boolean mAllowLineBreaks; 66 67 68 public int[] getDefaultTokens() 69 { 70 return new int[] { 71 TokenTypes.SEMI, 72 TokenTypes.POST_INC, 73 TokenTypes.POST_DEC, 74 }; 75 } 76 77 78 public int[] getAcceptableTokens() 79 { 80 return new int[] { 81 TokenTypes.SEMI, 82 TokenTypes.POST_INC, 83 TokenTypes.POST_DEC, 84 TokenTypes.DOT, 85 }; 86 } 87 88 89 public void visitToken(DetailAST aAST) 90 { 91 final String [] lines = getLines(); 92 final String line = lines[aAST.getLineNo() - 1]; 93 final int before = aAST.getColumnNo() - 1; 94 95 if ((before < 0) || Character.isWhitespace(line.charAt(before))) { 96 97 if (aAST.getType() == TokenTypes.SEMI) { 99 final DetailAST sibling = aAST.getPreviousSibling(); 100 if ((sibling != null) 101 && (sibling.getType() == TokenTypes.FOR_INIT) 102 && (sibling.getChildCount() == 0)) 103 { 104 return; 105 } 106 } 107 108 boolean flag = !mAllowLineBreaks; 109 for (int i = 0; !flag && (i < before); i++) { 111 if (!Character.isWhitespace(line.charAt(i))) { 112 flag = true; 113 } 114 } 115 if (flag) { 116 log(aAST.getLineNo(), before, "ws.preceded", aAST.getText()); 117 } 118 } 119 } 120 121 126 public void setAllowLineBreaks(boolean aAllowLineBreaks) 127 { 128 mAllowLineBreaks = aAllowLineBreaks; 129 } 130 } 131 | Popular Tags |