CauseParameterInExceptionCheck extends AbstractCheck { @Override public void visitToken(DetailAST ast) { switch (ast.getType()) { case TokenTypes.CLASS_DEF: final String exceptionClassName = getName(ast); if (classNamesRegexp.matcher(exceptionClassName).matches() && !ignoredClassNamesRegexp.matcher(exceptionClassName) .matches()) { exceptionClassesToWarn.add(ast); } break; case TokenTypes.CTOR_DEF: final DetailAST exceptionClass = getClassDef(ast); if (exceptionClassesToWarn.contains(exceptionClass) && hasCauseAsParameter(ast)) { exceptionClassesToWarn.remove(exceptionClass); } break; default: SevntuUtil.reportInvalidToken(ast.getType()); break; } } CauseParameterInExceptionCheck(); void setClassNamesRegexp(String classNamesRegexp); void setIgnoredClassNamesRegexp(String ignoredClassNamesRegexp); void setAllowedCauseTypes(final String... allowedCauseTypes); @Override int[] getDefaultTokens(); @Override int[] getAcceptableTokens(); @Override int[] getRequiredTokens(); @Override void beginTree(DetailAST rootAST); @Override void visitToken(DetailAST ast); @Override void finishTree(DetailAST treeRootAST); static final String MSG_KEY; }
@Test public void testUnsupportedNode() { final DetailAstImpl sync = new DetailAstImpl(); sync.setType(TokenTypes.LITERAL_SYNCHRONIZED); try { final CauseParameterInExceptionCheck check = new CauseParameterInExceptionCheck(); check.visitToken(sync); fail("exception expected"); } catch (IllegalArgumentException ex) { Assert.assertEquals("Found unsupported token: LITERAL_SYNCHRONIZED", ex.getMessage()); } }