1 package com.puppycrawl.tools.checkstyle.checks.coding; 20 21 import com.puppycrawl.tools.checkstyle.api.DetailAST; 22 import com.puppycrawl.tools.checkstyle.api.FullIdent; 23 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 24 import com.puppycrawl.tools.checkstyle.checks.AbstractTypeAwareCheck; 25 import java.util.Iterator ; 26 import java.util.LinkedList ; 27 import java.util.List ; 28 29 45 public class RedundantThrowsCheck extends AbstractTypeAwareCheck 46 { 47 51 private boolean mAllowUnchecked; 52 53 57 private boolean mAllowSubclasses; 58 59 64 public void setAllowUnchecked(boolean aAllowUnchecked) 65 { 66 mAllowUnchecked = aAllowUnchecked; 67 } 68 69 74 public void setAllowSubclasses(boolean aAllowSubclasses) 75 { 76 mAllowSubclasses = aAllowSubclasses; 77 } 78 79 80 public int[] getDefaultTokens() 81 { 82 return new int[] { 83 TokenTypes.PACKAGE_DEF, 84 TokenTypes.IMPORT, 85 TokenTypes.CLASS_DEF, 86 TokenTypes.ENUM_DEF, 87 TokenTypes.METHOD_DEF, 88 TokenTypes.CTOR_DEF, 89 }; 90 } 91 92 96 protected final void processAST(DetailAST aAST) 97 { 98 final List knownExcs = new LinkedList (); 99 final DetailAST throwsAST = 100 aAST.findFirstToken(TokenTypes.LITERAL_THROWS); 101 if (throwsAST != null) { 102 DetailAST child = (DetailAST) throwsAST.getFirstChild(); 103 while (child != null) { 104 if ((child.getType() == TokenTypes.IDENT) 105 || (child.getType() == TokenTypes.DOT)) 106 { 107 final FullIdent fi = FullIdent.createFullIdent(child); 108 checkException(fi, knownExcs); 109 } 110 child = (DetailAST) child.getNextSibling(); 111 } 112 } 113 } 114 115 119 protected final void logLoadError(Token aIdent) 120 { 121 logLoadErrorImpl(aIdent.getLineNo(), aIdent.getColumnNo(), 122 "redundant.throws.classInfo", 123 new Object [] {aIdent.getText()}); 124 } 125 126 138 private void checkException(FullIdent aExc, List aKnownExcs) 139 { 140 final ClassInfo newClassInfo = 142 createClassInfo(new Token(aExc), getCurrentClassName()); 143 144 if (!mAllowUnchecked) { 145 if (isUnchecked(newClassInfo.getClazz())) { 146 log(aExc.getLineNo(), aExc.getColumnNo(), 147 "redundant.throws.unchecked", aExc.getText()); 148 } 149 } 150 151 boolean shouldAdd = true; 152 for (final Iterator known = aKnownExcs.iterator(); known.hasNext();) { 153 final ClassInfo ci = (ClassInfo) known.next(); 154 final Token fi = ci.getName(); 155 156 if (ci.getClazz() == newClassInfo.getClazz()) { 157 shouldAdd = false; 158 log(aExc.getLineNo(), aExc.getColumnNo(), 159 "redundant.throws.duplicate", aExc.getText()); 160 } 161 else if (!mAllowSubclasses) { 162 if (isSubclass(ci.getClazz(), newClassInfo.getClazz())) { 163 known.remove(); 164 log(fi.getLineNo(), fi.getColumnNo(), 165 "redundant.throws.subclass", 166 fi.getText(), aExc.getText()); 167 } 168 else if (isSubclass(newClassInfo.getClazz(), ci.getClazz())) { 169 shouldAdd = false; 170 log(aExc.getLineNo(), aExc.getColumnNo(), 171 "redundant.throws.subclass", 172 aExc.getText(), fi.getText()); 173 } 174 } 175 } 176 177 if (shouldAdd) { 178 aKnownExcs.add(newClassInfo); 179 } 180 } 181 } 182 | Popular Tags |