diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/MagicNumberCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/MagicNumberCheck.java index b3f2b26261b..60cb98bb019 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/MagicNumberCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/MagicNumberCheck.java @@ -387,7 +387,7 @@ private boolean isInIgnoreList(DetailAST ast) { private static boolean isFieldDeclaration(DetailAST ast) { DetailAST varDefAST = null; DetailAST node = ast; - while (node.getType() != TokenTypes.OBJBLOCK) { + while (node != null && node.getType() != TokenTypes.OBJBLOCK) { if (node.getType() == TokenTypes.VARIABLE_DEF) { varDefAST = node; break; diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/MagicNumberCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/MagicNumberCheckTest.java index 59cf477d01a..6c189aa7f51 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/MagicNumberCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/MagicNumberCheckTest.java @@ -632,6 +632,19 @@ public void testMagicNumberIgnoreFieldDeclarationRecords() expected); } + @Test + public void testMagicNumberIgnoreFieldDeclarationWithAnnotation() + throws Exception { + final String[] expected = { + "16:38: " + getCheckMessage(MSG_KEY, "3"), + "20:40: " + getCheckMessage(MSG_KEY, "60"), + "21:34: " + getCheckMessage(MSG_KEY, "20"), + }; + verifyWithInlineConfigParser( + getPath("InputMagicNumberIgnoreFieldDeclarationWithAnnotation.java"), + expected); + } + @Test public void testIgnoreInAnnotationElementDefault() throws Exception { final String[] expected = { diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/magicnumber/InputMagicNumberIgnoreFieldDeclarationWithAnnotation.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/magicnumber/InputMagicNumberIgnoreFieldDeclarationWithAnnotation.java new file mode 100644 index 00000000000..4b86307ddeb --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/magicnumber/InputMagicNumberIgnoreFieldDeclarationWithAnnotation.java @@ -0,0 +1,26 @@ +/* +MagicNumber +ignoreNumbers = 0.99 +ignoreHashCodeMethod = (default)false +ignoreAnnotation = (default)false +ignoreFieldDeclaration = true +ignoreAnnotationElementDefaults = false +constantWaiverParentToken = (default)TYPECAST, METHOD_CALL, EXPR, ARRAY_INIT, UNARY_MINUS, \ + UNARY_PLUS, ELIST, STAR, ASSIGN, PLUS, MINUS, DIV, LITERAL_NEW +tokens = (default)NUM_DOUBLE, NUM_FLOAT, NUM_INT, NUM_LONG + + +*/ +package com.puppycrawl.tools.checkstyle.checks.coding.magicnumber; + +@InputMagicNumberIntMethodAnnotation(3) // violation +public class InputMagicNumberIgnoreFieldDeclarationWithAnnotation { + public void createEvents(Double d, String s) { + if ((d != null) && (s != null && s.equalsIgnoreCase("Fiit"))) { + double anotherDouble = d / 60; // violation + if (anotherDouble >= 20) { // violation + // do something + } + } + } +}