1 package com.puppycrawl.tools.checkstyle.checks.usage; 20 21 import com.puppycrawl.tools.checkstyle.api.DetailAST; 22 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 23 24 25 37 public class UnusedParameterCheck extends AbstractUsageCheck 38 { 39 40 private boolean mIgnoreCatch = true; 41 42 private boolean mIgnoreNonLocal; 43 44 45 public int[] getDefaultTokens() 46 { 47 return new int[] { 48 TokenTypes.PARAMETER_DEF, 49 }; 50 } 51 52 53 public String getErrorKey() 54 { 55 return "unused.parameter"; 56 } 57 58 59 public boolean mustCheckReferenceCount(DetailAST aAST) 60 { 61 boolean result = false; 62 final DetailAST parent = aAST.getParent(); 63 if (parent != null) { 64 if (parent.getType() == TokenTypes.PARAMETERS) { 65 final DetailAST grandparent = parent.getParent(); 66 if (grandparent != null) { 67 result = hasBody(grandparent) 68 && (!mIgnoreNonLocal || isLocal(grandparent)); 69 } 70 } 71 else if (parent.getType() == TokenTypes.LITERAL_CATCH) { 72 result = !mIgnoreCatch; 73 } 74 } 75 return result; 76 } 77 78 84 private boolean hasBody(DetailAST aAST) 85 { 86 if (aAST.getType() == TokenTypes.METHOD_DEF) { 87 return aAST.branchContains(TokenTypes.SLIST); 88 } 89 else if (aAST.getType() == TokenTypes.CTOR_DEF) { 90 return true; 91 } 92 return false; 93 } 94 95 100 private boolean isLocal(DetailAST aAST) 101 { 102 if (aAST.getType() == TokenTypes.METHOD_DEF) { 103 final DetailAST modifiers = 104 aAST.findFirstToken(TokenTypes.MODIFIERS); 105 return (modifiers == null) 106 || modifiers.branchContains(TokenTypes.LITERAL_STATIC) 107 || modifiers.branchContains(TokenTypes.LITERAL_PRIVATE); 108 } 109 return true; 110 } 111 112 117 public void setIgnoreCatch(boolean aIgnoreCatch) 118 { 119 mIgnoreCatch = aIgnoreCatch; 120 } 121 122 127 public void setIgnoreNonLocal(boolean aIgnoreNonLocal) 128 { 129 mIgnoreNonLocal = aIgnoreNonLocal; 130 } 131 132 } 133 | Popular Tags |