From 7dd31794bcc6258ffaa1ed5533a3b5a7c2f2af1a Mon Sep 17 00:00:00 2001 From: Oleg Sukhodolsky Date: Mon, 21 Feb 2005 14:37:43 +0000 Subject: [PATCH] Two more annotation-related problems (module Indentation, bug 1109214 and module UnnecessaryParentheses, bug 1109238) --- .../coding/UnnecessaryParenthesesCheck.java | 16 ++++++++++++ .../checks/indentation/AssignHandler.java | 11 +++++--- .../tools/checkstyle/grammars/java.g | 11 +++++--- .../tools/checkstyle/Input15Extensions.java | 3 ++- .../UnnecessaryParenthesesCheckTest.java | 13 +++++++--- .../indentation/IndentationCheckTest.java | 25 ++++++------------- src/xdocs/releasenotes.xml | 3 +++ 7 files changed, 54 insertions(+), 28 deletions(-) diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/UnnecessaryParenthesesCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/UnnecessaryParenthesesCheck.java index 280f746d8..197eb0655 100755 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/UnnecessaryParenthesesCheck.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/UnnecessaryParenthesesCheck.java @@ -128,6 +128,14 @@ public class UnnecessaryParenthesesCheck extends Check { final int type = aAST.getType(); final boolean surrounded = isSurrounded(aAST); + final DetailAST parent = aAST.getParent(); + + if (type == TokenTypes.ASSIGN + && parent.getType() == TokenTypes.ANNOTATION_MEMBER_VALUE_PAIR) + { + // shouldn't process assign in annotation pairs + return; + } // An identifier surrounded by parentheses. if (surrounded && type == TokenTypes.IDENT) { @@ -163,6 +171,14 @@ public class UnnecessaryParenthesesCheck extends Check public void leaveToken(DetailAST aAST) { final int type = aAST.getType(); + final DetailAST parent = aAST.getParent(); + + if (type == TokenTypes.ASSIGN + && parent.getType() == TokenTypes.ANNOTATION_MEMBER_VALUE_PAIR) + { + // shouldn't process assign in annotation pairs + return; + } // An expression is surrounded by parentheses. if (type == TokenTypes.EXPR) { diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/indentation/AssignHandler.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/indentation/AssignHandler.java index 892f33acc..529759c33 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/indentation/AssignHandler.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/indentation/AssignHandler.java @@ -62,11 +62,16 @@ public class AssignHandler extends BlockParentHandler // if this is assign in expression then skip first child, // because it's lvalue. - if (assign.getParent() != null - && assign.getParent().getType() == TokenTypes.EXPR) - { + final DetailAST parent = assign.getParent(); + if (parent != null && parent.getType() == TokenTypes.EXPR) { child = (DetailAST) child.getNextSibling(); } + if (parent != null + && parent.getType() == TokenTypes.ANNOTATION_MEMBER_VALUE_PAIR) + { + child = (DetailAST) assign.getNextSibling(); + } + checkExpressionSubtree(child, expectedLevel, false, true); } diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/grammars/java.g b/src/checkstyle/com/puppycrawl/tools/checkstyle/grammars/java.g index f159bef82..d0c52befe 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/grammars/java.g +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/grammars/java.g @@ -453,7 +453,7 @@ annotationMemberValuePair! annotationMemberValueInitializer : - conditionalExpression | annotation | annotationMemberArrayInitializer + annotationExpression | annotation | annotationMemberArrayInitializer ; // This is an initializer used to set up an annotation member array. @@ -479,10 +479,15 @@ annotationMemberArrayInitializer // The two things that can initialize an annotation array element are a conditional expression // and an annotation (nested annotation array initialisers are not valid) annotationMemberArrayValueInitializer - : conditionalExpression + : annotationExpression | annotation ; +annotationExpression + : conditionalExpression + {#annotationExpression = #(#[EXPR,"EXPR"],#annotationExpression);} + ; + // Definition of a Java class classDefinition![AST modifiers] : c:"class" IDENT @@ -1786,4 +1791,4 @@ EXPONENT protected FLOAT_SUFFIX : 'f'|'F'|'d'|'D' - ; \ No newline at end of file + ; diff --git a/src/testinputs/com/puppycrawl/tools/checkstyle/Input15Extensions.java b/src/testinputs/com/puppycrawl/tools/checkstyle/Input15Extensions.java index 5390b146e..d4b6ca86b 100644 --- a/src/testinputs/com/puppycrawl/tools/checkstyle/Input15Extensions.java +++ b/src/testinputs/com/puppycrawl/tools/checkstyle/Input15Extensions.java @@ -2,9 +2,10 @@ @interface MyAnnotation { String name(); + int version(); } -@MyAnnotation(name = "ABC") +@MyAnnotation(name = "ABC", version = 1) public class Input15Extensions { diff --git a/src/tests/com/puppycrawl/tools/checkstyle/checks/coding/UnnecessaryParenthesesCheckTest.java b/src/tests/com/puppycrawl/tools/checkstyle/checks/coding/UnnecessaryParenthesesCheckTest.java index a86c36097..475e55bf1 100755 --- a/src/tests/com/puppycrawl/tools/checkstyle/checks/coding/UnnecessaryParenthesesCheckTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/checks/coding/UnnecessaryParenthesesCheckTest.java @@ -6,18 +6,18 @@ import com.puppycrawl.tools.checkstyle.DefaultConfiguration; /** * Test fixture for the UnnecessaryParenthesesCheck. - * + * * @author Eric K. Roe */ public class UnnecessaryParenthesesCheckTest extends BaseCheckTestCase { private static final String TEST_FILE = "coding" + File.separator + "InputUnnecessaryParentheses.java"; - + public void testDefault() throws Exception { final DefaultConfiguration checkConfig = createCheckConfig(UnnecessaryParenthesesCheck.class); - + final String[] expected = { "4:22: Unnecessary parentheses around assignment right-hand side.", "4:29: Unnecessary parentheses around expression.", @@ -66,4 +66,11 @@ public class UnnecessaryParenthesesCheckTest extends BaseCheckTestCase { verify(checkConfig, getPath(TEST_FILE), expected); } + + public void test15Extensions() throws Exception + { + final DefaultConfiguration checkConfig = createCheckConfig(UnnecessaryParenthesesCheck.class); + final String[] expected = {}; + verify(checkConfig, getPath("Input15Extensions.java"), expected); + } } diff --git a/src/tests/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheckTest.java b/src/tests/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheckTest.java index 39a2259b6..5c02e3f46 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheckTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheckTest.java @@ -9,8 +9,6 @@ import com.puppycrawl.tools.checkstyle.Checker; * @author jrichard */ public class IndentationCheckTest extends BaseCheckTestCase { - - public void testInvalidLabel() throws Exception { @@ -29,8 +27,6 @@ public class IndentationCheckTest extends BaseCheckTestCase { verify(checkConfig, getPath("indentation/InputInvalidLabelIndent.java"), expected); } - - public void testValidLabel() throws Exception { @@ -40,7 +36,6 @@ public class IndentationCheckTest extends BaseCheckTestCase { verify(checkConfig, getPath("indentation/InputValidLabelIndent.java"), expected); } - public void testValidIfWithChecker() throws Exception { @@ -52,7 +47,6 @@ public class IndentationCheckTest extends BaseCheckTestCase { verify(c, fname, expected); } - public void testValidDotWithChecker() throws Exception { @@ -64,7 +58,6 @@ public class IndentationCheckTest extends BaseCheckTestCase { verify(c, fname, expected); } - public void testValidMethodWithChecker() throws Exception { @@ -76,7 +69,6 @@ public class IndentationCheckTest extends BaseCheckTestCase { verify(c, fname, expected); } - public void testInvalidMethodWithChecker() throws Exception { @@ -122,9 +114,6 @@ public class IndentationCheckTest extends BaseCheckTestCase { verify(c, fname, expected); } - - - public void testInvalidSwitchWithChecker() throws Exception { @@ -173,8 +162,6 @@ public class IndentationCheckTest extends BaseCheckTestCase { verify(c, fname, expected); } - - public void testValidArrayInitWithChecker() throws Exception { @@ -228,9 +215,6 @@ public class IndentationCheckTest extends BaseCheckTestCase { verify(c, fname, expected); } - - - public void testValidTryWithChecker() throws Exception { @@ -279,7 +263,6 @@ public class IndentationCheckTest extends BaseCheckTestCase { verify(c, fname, expected); } - // TODO: needs to be finished public void testInvalidClassDefWithChecker() throws Exception @@ -333,7 +316,6 @@ public class IndentationCheckTest extends BaseCheckTestCase { verify(c, fname, expected); } - public void testInvalidBlockWithChecker() throws Exception { @@ -694,4 +676,11 @@ public class IndentationCheckTest extends BaseCheckTestCase { final String[] expected = {}; verify(checkConfig, getPath("indentation/InputValidAssignIndent.java"), expected); } + + public void test15Extensions() throws Exception + { + final DefaultConfiguration checkConfig = createCheckConfig(IndentationCheck.class); + final String[] expected = {}; + verify(checkConfig, getPath("Input15Extensions.java"), expected); + } } diff --git a/src/xdocs/releasenotes.xml b/src/xdocs/releasenotes.xml index 3d5e19cf0..44c6d6bbc 100755 --- a/src/xdocs/releasenotes.xml +++ b/src/xdocs/releasenotes.xml @@ -72,6 +72,9 @@
  • Annotations and enums are classes too and should be treated so by ClassFanOutComplexity checks (bug 1109205)
  • +
  • Two more annotation-related problems (module Indentation, bug + 1109214 and module UnnecessaryParentheses, bug 1109238)
  • +