diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/SuppressWarningsHolder.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/SuppressWarningsHolder.java index d72133d46..980131756 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/SuppressWarningsHolder.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/SuppressWarningsHolder.java @@ -277,39 +277,9 @@ public class SuppressWarningsHolder return; } - // get target of annotation - DetailAST targetAST = null; - DetailAST parentAST = ast.getParent(); - if (parentAST != null) { - switch (parentAST.getType()) { - case TokenTypes.MODIFIERS: - case TokenTypes.ANNOTATIONS: - parentAST = parentAST.getParent(); - if (parentAST != null) { - switch (parentAST.getType()) { - case TokenTypes.ANNOTATION_DEF: - case TokenTypes.PACKAGE_DEF: - case TokenTypes.CLASS_DEF: - case TokenTypes.INTERFACE_DEF: - case TokenTypes.ENUM_DEF: - case TokenTypes.ENUM_CONSTANT_DEF: - case TokenTypes.CTOR_DEF: - case TokenTypes.METHOD_DEF: - case TokenTypes.PARAMETER_DEF: - case TokenTypes.VARIABLE_DEF: - targetAST = parentAST; - break; + final DetailAST targetAST = getAnnotationTarget(ast); - default: - // unexpected target type - } - } - break; - default: - // unexpected container type - } - } if (targetAST == null) { log(ast, "suppress.warnings.invalid.target"); return; @@ -344,6 +314,48 @@ public class SuppressWarningsHolder } } + /** + * get target of annotation + * @param ast the AST node to get the child of + * @return get target of annotation + */ + private DetailAST getAnnotationTarget(DetailAST ast) + { + DetailAST targetAST = null; + DetailAST parentAST = ast.getParent(); + if (parentAST != null) { + switch (parentAST.getType()) { + case TokenTypes.MODIFIERS: + case TokenTypes.ANNOTATIONS: + parentAST = parentAST.getParent(); + if (parentAST != null) { + switch (parentAST.getType()) { + case TokenTypes.ANNOTATION_DEF: + case TokenTypes.PACKAGE_DEF: + case TokenTypes.CLASS_DEF: + case TokenTypes.INTERFACE_DEF: + case TokenTypes.ENUM_DEF: + case TokenTypes.ENUM_CONSTANT_DEF: + case TokenTypes.CTOR_DEF: + case TokenTypes.METHOD_DEF: + case TokenTypes.PARAMETER_DEF: + case TokenTypes.VARIABLE_DEF: + targetAST = parentAST; + break; + + default: + // unexpected target type + } + } + break; + + default: + // unexpected container type + } + } + return targetAST; + } + /** * Returns the n'th child of an AST node. * @param ast the AST node to get the child of