diff options
author | Andrei Selkin <andreyselkin@gmail.com> | 2015-11-05 23:21:56 +0300 |
---|---|---|
committer | Roman Ivanov <ivanov-jr@mail.ru> | 2015-11-05 20:59:34 -0800 |
commit | 8381754587bee0de49489e9bfb11e5912f664e87 (patch) | |
tree | ad3a1bf561ac8e5c25ebb79775e5f0af8e8f76c0 | |
parent | be8a60a4d95978e1b6cdb32fefca58f424e61178 (diff) | |
download | checkstyle-8381754587bee0de49489e9bfb11e5912f664e87.tar.gz |
Issue #2290: Fix NPE in isOverriddenMethod during validation of methods with implicit modifiers
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<String> packageNames; + + InputOverrideAnnotation() {} // No NPE here! + + InputOverrideAnnotation(int field, java.util.Set<String> packageNames) {} // No NPE here! + + } |