1 package com.puppycrawl.tools.checkstyle.checks.usage; 20 21 import com.puppycrawl.tools.checkstyle.api.DetailAST; 22 import com.puppycrawl.tools.checkstyle.api.FullIdent; 23 import com.puppycrawl.tools.checkstyle.api.Scope; 24 import com.puppycrawl.tools.checkstyle.api.ScopeUtils; 25 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 26 27 39 public class UnusedPrivateMethodCheck 40 extends AbstractUsageCheck 41 42 { 43 44 private boolean mAllowSerializationMethods; 45 46 public int[] getDefaultTokens() 47 { 48 return new int[] { 49 TokenTypes.METHOD_DEF, 50 }; 51 } 52 53 54 public String getErrorKey() 55 { 56 return "unused.method"; 57 } 58 59 63 public void setAllowSerializationMethods(boolean aFlag) 64 { 65 mAllowSerializationMethods = aFlag; 66 } 67 68 69 public boolean mustCheckReferenceCount(DetailAST aAST) 70 { 71 final DetailAST mods = aAST.findFirstToken(TokenTypes.MODIFIERS); 72 if ((mods == null) 73 || (ScopeUtils.getScopeFromMods(mods) != Scope.PRIVATE)) 74 { 75 return false; 76 } 77 78 return !mAllowSerializationMethods 79 || !(isWriteObject(aAST) || isReadObject(aAST) 80 || isWriteReplaceOrReadResolve(aAST)); 81 } 82 83 88 private boolean isWriteObject(DetailAST aAST) 89 { 90 final DetailAST ident = aAST.findFirstToken(TokenTypes.IDENT); 92 if (!"writeObject".equals(ident.getText())) { 93 return false; 94 } 95 96 final DetailAST typeAST = 98 (DetailAST) aAST.findFirstToken(TokenTypes.TYPE).getFirstChild(); 99 if (typeAST.getType() != TokenTypes.LITERAL_VOID) { 100 return false; 101 } 102 103 final DetailAST params = aAST.findFirstToken(TokenTypes.PARAMETERS); 105 if (params == null || params.getChildCount() != 1) { 106 return false; 107 } 108 final DetailAST type = 110 (DetailAST) ((DetailAST) params.getFirstChild()) 111 .findFirstToken(TokenTypes.TYPE).getFirstChild(); 112 final String typeName = FullIdent.createFullIdent(type).getText(); 113 if (!"java.io.ObjectOutputStream".equals(typeName) 114 && !"ObjectOutputStream".equals(typeName)) 115 { 116 return false; 117 } 118 119 final DetailAST throwsAST = 121 aAST.findFirstToken(TokenTypes.LITERAL_THROWS); 122 if (throwsAST == null || throwsAST.getChildCount() != 1) { 123 return false; 124 } 125 final DetailAST expt = (DetailAST) throwsAST.getFirstChild(); 126 final String exceptionName = FullIdent.createFullIdent(expt).getText(); 127 if (!"java.io.IOException".equals(exceptionName) 128 && !"IOException".equals(exceptionName)) 129 { 130 return false; 131 } 132 133 return true; 134 } 135 136 141 private boolean isReadObject(DetailAST aAST) 142 { 143 final DetailAST ident = aAST.findFirstToken(TokenTypes.IDENT); 145 if (!"readObject".equals(ident.getText())) { 146 return false; 147 } 148 149 final DetailAST typeAST = 151 (DetailAST) aAST.findFirstToken(TokenTypes.TYPE).getFirstChild(); 152 if (typeAST.getType() != TokenTypes.LITERAL_VOID) { 153 return false; 154 } 155 156 final DetailAST params = aAST.findFirstToken(TokenTypes.PARAMETERS); 158 if (params == null || params.getChildCount() != 1) { 159 return false; 160 } 161 final DetailAST type = 163 (DetailAST) ((DetailAST) params.getFirstChild()) 164 .findFirstToken(TokenTypes.TYPE).getFirstChild(); 165 final String typeName = FullIdent.createFullIdent(type).getText(); 166 if (!"java.io.ObjectInputStream".equals(typeName) 167 && !"ObjectInputStream".equals(typeName)) 168 { 169 return false; 170 } 171 172 final DetailAST throwsAST = 175 aAST.findFirstToken(TokenTypes.LITERAL_THROWS); 176 if (throwsAST == null || throwsAST.getChildCount() != 3) { 177 return false; 178 } 179 final DetailAST excpt1 = (DetailAST) throwsAST.getFirstChild(); 180 final String exception1 = FullIdent.createFullIdent(excpt1).getText(); 181 final String exception2 = 182 FullIdent.createFullIdent(throwsAST.getLastChild()).getText(); 183 if (!"java.io.IOException".equals(exception1) 184 && !"IOException".equals(exception1) 185 && !"java.io.IOException".equals(exception2) 186 && !"IOException".equals(exception2) 187 || !"java.lang.ClassNotFoundException".equals(exception1) 188 && !"ClassNotFoundException".equals(exception1) 189 && !"java.lang.ClassNotFoundException".equals(exception2) 190 && !"ClassNotFoundException".equals(exception2)) 191 { 192 return false; 193 } 194 195 return true; 196 } 197 198 203 private boolean isWriteReplaceOrReadResolve(DetailAST aAST) 204 { 205 final DetailAST ident = aAST.findFirstToken(TokenTypes.IDENT); 207 if (!"writeReplace".equals(ident.getText()) 208 && !"readResolve".equals(ident.getText())) 209 { 210 return false; 211 } 212 213 final DetailAST typeAST = 215 (DetailAST) aAST.findFirstToken(TokenTypes.TYPE).getFirstChild(); 216 if (typeAST.getType() != TokenTypes.DOT 217 && typeAST.getType() != TokenTypes.IDENT) 218 { 219 return false; 220 } 221 222 final DetailAST params = aAST.findFirstToken(TokenTypes.PARAMETERS); 224 if (params != null && params.getChildCount() != 0) { 225 return false; 226 } 227 228 final DetailAST throwsAST = 230 aAST.findFirstToken(TokenTypes.LITERAL_THROWS); 231 if (throwsAST == null || throwsAST.getChildCount() != 1) { 232 return false; 233 } 234 final DetailAST excpt = (DetailAST) throwsAST.getFirstChild(); 235 final String exception = FullIdent.createFullIdent(excpt).getText(); 236 if (!"java.io.ObjectStreamException".equals(exception) 237 && !"ObjectStreamException".equals(exception)) 238 { 239 return false; 240 } 241 242 return true; 243 } 244 } 245 | Popular Tags |