1 package com.puppycrawl.tools.checkstyle.checks.sizes; 20 21 import com.puppycrawl.tools.checkstyle.api.Check; 22 import com.puppycrawl.tools.checkstyle.api.DetailAST; 23 import com.puppycrawl.tools.checkstyle.api.FileContents; 24 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 25 26 56 public class MethodLengthCheck extends Check 57 { 58 59 private boolean mCountEmpty = true; 60 61 62 private static final int DEFAULT_MAX_LINES = 150; 63 64 65 private int mMax = DEFAULT_MAX_LINES; 66 67 68 public int[] getDefaultTokens() 69 { 70 return new int[] {TokenTypes.METHOD_DEF, TokenTypes.CTOR_DEF}; 71 } 72 73 74 public void visitToken(DetailAST aAST) 75 { 76 final DetailAST openingBrace = aAST.findFirstToken(TokenTypes.SLIST); 77 if (openingBrace != null) { 78 final DetailAST closingBrace = 79 openingBrace.findFirstToken(TokenTypes.RCURLY); 80 int length = 81 closingBrace.getLineNo() - openingBrace.getLineNo() + 1; 82 83 if (!mCountEmpty) { 84 final FileContents contents = getFileContents(); 85 final int lastLine = closingBrace.getLineNo(); 86 for (int i = openingBrace.getLineNo() - 1; i < lastLine; i++) { 87 if (contents.lineIsBlank(i) || contents.lineIsComment(i)) { 88 length--; 89 } 90 } 91 } 92 if (length > mMax) { 93 log(aAST.getLineNo(), 94 aAST.getColumnNo(), 95 "maxLen.method", 96 new Integer (length), 97 new Integer (mMax)); 98 } 99 } 100 } 101 102 105 public void setMax(int aLength) 106 { 107 mMax = aLength; 108 } 109 110 114 public void setCountEmpty(boolean aCountEmpty) 115 { 116 mCountEmpty = aCountEmpty; 117 } 118 } 119 | Popular Tags |