1 package com.puppycrawl.tools.checkstyle.checks.modifier; 20 21 import com.puppycrawl.tools.checkstyle.api.Check; 22 import com.puppycrawl.tools.checkstyle.api.DetailAST; 23 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 24 import com.puppycrawl.tools.checkstyle.api.ScopeUtils; 25 26 32 public class RedundantModifierCheck 33 extends Check 34 { 35 36 public int[] getDefaultTokens() 37 { 38 return new int[] { 39 TokenTypes.METHOD_DEF, 40 TokenTypes.VARIABLE_DEF, 41 TokenTypes.ANNOTATION_FIELD_DEF, 42 }; 43 } 44 45 46 public int[] getRequiredTokens() 47 { 48 return new int[] {}; 49 } 50 51 52 public void visitToken(DetailAST aAST) 53 { 54 if (ScopeUtils.inInterfaceOrAnnotationBlock(aAST)) { 55 final DetailAST modifiers = 56 aAST.findFirstToken(TokenTypes.MODIFIERS); 57 58 DetailAST modifier = (DetailAST) modifiers.getFirstChild(); 59 while (modifier != null) { 60 61 65 final int type = modifier.getType(); 66 if ((type == TokenTypes.LITERAL_PUBLIC) 67 || (type == TokenTypes.ABSTRACT) 68 || (type == TokenTypes.LITERAL_STATIC) 69 || (type == TokenTypes.FINAL)) 70 { 71 log(modifier.getLineNo(), 72 modifier.getColumnNo(), 73 "redundantModifier", 74 new String [] {modifier.getText()}); 75 break; 76 } 77 78 modifier = (DetailAST) modifier.getNextSibling(); 79 } 80 } 81 else if (aAST.getType() == TokenTypes.METHOD_DEF) { 82 final DetailAST modifiers = 83 aAST.findFirstToken(TokenTypes.MODIFIERS); 84 boolean checkFinal = 86 modifiers.branchContains(TokenTypes.LITERAL_PRIVATE); 87 DetailAST parent = aAST.getParent(); 89 while (parent != null) { 90 if (parent.getType() == TokenTypes.CLASS_DEF) { 91 final DetailAST classModifiers = 92 parent.findFirstToken(TokenTypes.MODIFIERS); 93 checkFinal |= 94 classModifiers.branchContains(TokenTypes.FINAL); 95 break; 96 } 97 parent = parent.getParent(); 98 } 99 if (checkFinal) { 100 DetailAST modifier = (DetailAST) modifiers.getFirstChild(); 101 while (modifier != null) { 102 final int type = modifier.getType(); 103 if (type == TokenTypes.FINAL) { 104 log(modifier.getLineNo(), 105 modifier.getColumnNo(), 106 "redundantModifier", 107 new String [] {modifier.getText()}); 108 break; 109 } 110 modifier = (DetailAST) modifier.getNextSibling(); 111 } 112 } 113 } 114 } 115 } 116 | Popular Tags |