1 package com.puppycrawl.tools.checkstyle.checks.j2ee; 20 21 import com.puppycrawl.tools.checkstyle.api.DetailAST; 22 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 23 24 28 public abstract class MethodChecker 29 { 30 31 private final AbstractJ2eeCheck mCheck; 32 33 37 public MethodChecker(AbstractJ2eeCheck aCheck) 38 { 39 mCheck = aCheck; 40 } 41 42 46 protected AbstractJ2eeCheck getCheck() 47 { 48 return mCheck; 49 } 50 51 55 public void checkMethods(DetailAST aAST) 56 { 57 final DetailAST objBlock = aAST.findFirstToken(TokenTypes.OBJBLOCK); 58 if (objBlock != null) { 59 DetailAST child = (DetailAST) objBlock.getFirstChild(); 60 while (child != null) { 61 if (child.getType() == TokenTypes.METHOD_DEF) { 62 checkMethod(child); 63 } 64 child = (DetailAST) child.getNextSibling(); 65 } 66 } 67 } 68 69 73 public abstract void checkMethod(DetailAST aMethodAST); 74 75 81 public void checkMethod( 82 DetailAST aMethodAST, 83 boolean aAllowFinal) 84 { 85 if (!Utils.isPublic(aMethodAST)) { 87 logName(aMethodAST, "nonpublicmethod.bean", new Object [] {}); 88 } 89 90 if (!aAllowFinal && Utils.isFinal(aMethodAST)) { 92 logName(aMethodAST, "illegalmodifiermethod.bean", 93 new Object [] {"final"}); 94 } 95 96 if (Utils.isStatic(aMethodAST)) { 98 logName(aMethodAST, "illegalmodifiermethod.bean", 99 new Object [] {"static"}); 100 } 101 } 102 103 109 protected void checkThrows(DetailAST aMethodAST, String aException) 110 { 111 if (!Utils.hasThrows(aMethodAST, aException)) { 112 logName(aMethodAST, "missingthrows.bean", 113 new Object [] {aException}); 114 } 115 } 116 117 123 protected void checkNotThrows(DetailAST aMethodAST, String aException) 124 { 125 if (Utils.hasThrows(aMethodAST, aException)) { 126 logName(aMethodAST, "hasthrows.bean", new Object [] {aException}); 127 } 128 } 129 130 136 protected void log(DetailAST aMethodAST, String aKey, Object [] aArgs) 137 { 138 mCheck.log(aMethodAST, aKey, aArgs); 139 } 140 141 147 protected void logName(DetailAST aMethodAST, String aKey, Object [] aArgs) 148 { 149 mCheck.logName(aMethodAST, aKey, aArgs); 150 } 151 } 152 | Popular Tags |