1 package com.puppycrawl.tools.checkstyle.checks.javadoc; 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.Scope; 25 import com.puppycrawl.tools.checkstyle.api.ScopeUtils; 26 import com.puppycrawl.tools.checkstyle.api.TextBlock; 27 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 28 29 30 36 public class JavadocVariableCheck 37 extends Check 38 { 39 40 private Scope mScope = Scope.PRIVATE; 41 42 43 private Scope mExcludeScope; 44 45 49 public void setScope(String aFrom) 50 { 51 mScope = Scope.getInstance(aFrom); 52 } 53 54 58 public void setExcludeScope(String aScope) 59 { 60 mExcludeScope = Scope.getInstance(aScope); 61 } 62 63 64 public int[] getDefaultTokens() 65 { 66 return new int[] { 67 TokenTypes.VARIABLE_DEF, 68 TokenTypes.ENUM_CONSTANT_DEF, 69 }; 70 } 71 72 73 public void visitToken(DetailAST aAST) 74 { 75 if (shouldCheck(aAST)) { 76 final FileContents contents = getFileContents(); 77 final TextBlock cmt = 78 contents.getJavadocBefore(aAST.getLineNo()); 79 80 if (cmt == null) { 81 log(aAST, "javadoc.missing"); 82 } 83 } 84 } 85 86 91 private boolean shouldCheck(final DetailAST aAST) 92 { 93 if (ScopeUtils.inCodeBlock(aAST)) { 94 return false; 95 } 96 97 final Scope scope; 98 if (aAST.getType() == TokenTypes.ENUM_CONSTANT_DEF) { 99 scope = Scope.PUBLIC; 100 } 101 else { 102 final DetailAST mods = aAST.findFirstToken(TokenTypes.MODIFIERS); 103 final Scope declaredScope = ScopeUtils.getScopeFromMods(mods); 104 scope = 105 ScopeUtils.inInterfaceOrAnnotationBlock(aAST) 106 ? Scope.PUBLIC : declaredScope; 107 } 108 109 final Scope surroundingScope = ScopeUtils.getSurroundingScope(aAST); 110 111 return scope.isIn(mScope) && surroundingScope.isIn(mScope) 112 && ((mExcludeScope == null) 113 || !scope.isIn(mExcludeScope) 114 || !surroundingScope.isIn(mExcludeScope)); 115 } 116 117 } 118 | Popular Tags |