1 package com.puppycrawl.tools.checkstyle.checks.naming; 20 21 import com.puppycrawl.tools.checkstyle.api.DetailAST; 22 import com.puppycrawl.tools.checkstyle.api.ScopeUtils; 23 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 24 25 52 public class MemberNameCheck 53 extends AbstractNameCheck 54 { 55 56 private boolean mApplyToPublic = true; 57 58 private boolean mApplyToProtected = true; 59 60 private boolean mApplyToPackage = true; 61 62 private boolean mApplyToPrivate = true; 63 64 65 public MemberNameCheck() 66 { 67 super("^[a-z][a-zA-Z0-9]*$"); 68 } 69 70 71 public int[] getDefaultTokens() 72 { 73 return new int[] {TokenTypes.VARIABLE_DEF}; 74 } 75 76 77 protected final boolean mustCheckName(DetailAST aAST) 78 { 79 final DetailAST modifiersAST = 80 aAST.findFirstToken(TokenTypes.MODIFIERS); 81 final boolean isStatic = (modifiersAST != null) 82 && modifiersAST.branchContains(TokenTypes.LITERAL_STATIC); 83 84 return (!isStatic && !ScopeUtils.inInterfaceOrAnnotationBlock(aAST) 85 && !ScopeUtils.isLocalVariableDef(aAST)) 86 && (modifiersAST != null) 87 && shouldCheckInScope(modifiersAST); 88 } 89 90 95 private boolean shouldCheckInScope(DetailAST aModifiers) 96 { 97 if (aModifiers == null) { 98 return mApplyToPackage; 100 } 101 102 final boolean isPublic = 103 aModifiers.branchContains(TokenTypes.LITERAL_PUBLIC); 104 final boolean isProtected = 105 aModifiers.branchContains(TokenTypes.LITERAL_PROTECTED); 106 final boolean isPrivate = 107 aModifiers.branchContains(TokenTypes.LITERAL_PRIVATE); 108 final boolean isPackage = !(isPublic || isProtected || isPrivate); 109 110 return (mApplyToPublic && isPublic) 111 || (mApplyToProtected && isProtected) 112 || (mApplyToPackage && isPackage) || (mApplyToPrivate && isPrivate); 113 } 114 115 119 public void setApplyToPublic(boolean aApplyTo) 120 { 121 mApplyToPublic = aApplyTo; 122 } 123 124 125 public boolean getApplyToPublic() 126 { 127 return mApplyToPublic; 128 } 129 130 134 public void setApplyToProtected(boolean aApplyTo) 135 { 136 mApplyToProtected = aApplyTo; 137 } 138 139 140 public boolean getApplyToProtected() 141 { 142 return mApplyToProtected; 143 } 144 145 149 public void setApplyToPackage(boolean aApplyTo) 150 { 151 mApplyToPackage = aApplyTo; 152 } 153 154 157 public boolean getApplyToPackage() 158 { 159 return mApplyToPackage; 160 } 161 162 166 public void setApplyToPrivate(boolean aApplyTo) 167 { 168 mApplyToPrivate = aApplyTo; 169 } 170 171 172 public boolean getApplyToPrivate() 173 { 174 return mApplyToPrivate; 175 } 176 } 177 | Popular Tags |