From 8381754587bee0de49489e9bfb11e5912f664e87 Mon Sep 17 00:00:00 2001 From: Andrei Selkin Date: Thu, 5 Nov 2015 23:21:56 +0300 Subject: Issue #2290: Fix NPE in isOverriddenMethod during validation of methods with implicit modifiers --- .../checkstyle/checks/naming/ParameterNameCheck.java | 12 +++++++----- .../checkstyle/checks/naming/ParameterNameCheckTest.java | 10 ++++++++++ .../checkstyle/checks/naming/InputOverrideAnnotation.java | 15 +++++++++++++++ 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/naming/ParameterNameCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/naming/ParameterNameCheck.java index 9152f7063..3658dc4b4 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/naming/ParameterNameCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/naming/ParameterNameCheck.java @@ -118,11 +118,13 @@ public class ParameterNameCheck private static boolean isOverriddenMethod(DetailAST ast) { boolean overridden = false; final DetailAST parent = ast.getParent().getParent(); - final DetailAST annotation = parent.getFirstChild().getFirstChild(); - if (annotation.getType() == TokenTypes.ANNOTATION) { - final DetailAST overrideToken = annotation.findFirstToken(TokenTypes.IDENT); - if ("Override".equals(overrideToken.getText())) { - overridden = true; + if (parent.getFirstChild().getFirstChild() != null) { + final DetailAST annotation = parent.getFirstChild().getFirstChild(); + if (annotation.getType() == TokenTypes.ANNOTATION) { + final DetailAST overrideToken = annotation.findFirstToken(TokenTypes.IDENT); + if ("Override".equals(overrideToken.getText())) { + overridden = true; + } } } return overridden; diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/naming/ParameterNameCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/naming/ParameterNameCheckTest.java index e595e4999..1c35fd32e 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/naming/ParameterNameCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/naming/ParameterNameCheckTest.java @@ -106,6 +106,11 @@ public class ParameterNameCheckTest final String[] expected = { "11:28: " + getCheckMessage(MSG_INVALID_PATTERN, "object", pattern), "15:30: " + getCheckMessage(MSG_INVALID_PATTERN, "aaaa", pattern), + "19:19: " + getCheckMessage(MSG_INVALID_PATTERN, "abc", pattern), + "19:28: " + getCheckMessage(MSG_INVALID_PATTERN, "bd", pattern), + "21:18: " + getCheckMessage(MSG_INVALID_PATTERN, "abc", pattern), + "28:33: " + getCheckMessage(MSG_INVALID_PATTERN, "field", pattern), + "28:62: " + getCheckMessage(MSG_INVALID_PATTERN, "packageNames", pattern), }; verify(checkConfig, getPath("InputOverrideAnnotation.java"), expected); } @@ -124,6 +129,11 @@ public class ParameterNameCheckTest "6:34: " + getCheckMessage(MSG_INVALID_PATTERN, "o", pattern), "11:28: " + getCheckMessage(MSG_INVALID_PATTERN, "object", pattern), "15:30: " + getCheckMessage(MSG_INVALID_PATTERN, "aaaa", pattern), + "19:19: " + getCheckMessage(MSG_INVALID_PATTERN, "abc", pattern), + "19:28: " + getCheckMessage(MSG_INVALID_PATTERN, "bd", pattern), + "21:18: " + getCheckMessage(MSG_INVALID_PATTERN, "abc", pattern), + "28:33: " + getCheckMessage(MSG_INVALID_PATTERN, "field", pattern), + "28:62: " + getCheckMessage(MSG_INVALID_PATTERN, "packageNames", pattern), }; verify(checkConfig, getPath("InputOverrideAnnotation.java"), expected); } diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/naming/InputOverrideAnnotation.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/naming/InputOverrideAnnotation.java index 82d99508a..e4e40d126 100644 --- a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/naming/InputOverrideAnnotation.java +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/naming/InputOverrideAnnotation.java @@ -13,4 +13,19 @@ public class InputOverrideAnnotation { } public void foo2(Integer aaaa) {} + + void foo3() {} // No NPE here! + + void foo4(int abc, int bd) {} // No NPE here! + + int foo5(int abc) {return 1;} // No NPE here! + + private int field; + private java.util.Set packageNames; + + InputOverrideAnnotation() {} // No NPE here! + + InputOverrideAnnotation(int field, java.util.Set packageNames) {} // No NPE here! + + } -- cgit v1.2.3