From 5d11e77208172db4469f862b04b362f024960fbc Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Sat, 29 Oct 2016 17:12:20 +0200 Subject: [PATCH 1/3] Add test for #1533 [java] BooleanInstantiation: ClassCastException with Annotation --- .../java/rule/basic/xml/BooleanInstantiation.xml | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/basic/xml/BooleanInstantiation.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/basic/xml/BooleanInstantiation.xml index bc272936f1..f6fe35d2cc 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/basic/xml/BooleanInstantiation.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/basic/xml/BooleanInstantiation.xml @@ -158,4 +158,16 @@ public class Foo { } ]]> + + + #1533 [java] BooleanInstantiation: ClassCastException with Annotation + 0 + + From 1e5c1c05cab08dbafaaeceddedc75a7323cdd50a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Mart=C3=ADn=20Sotuyo=20Dodero?= Date: Wed, 12 Oct 2016 17:31:41 -0300 Subject: [PATCH 2/3] In Java 8, annotations may appear as first child - For instance `Object o = new @Interned MyObject();` would fail with a ClassCastException - The current code deals with it gracefully --- .../pmd/lang/java/rule/basic/BooleanInstantiationRule.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/basic/BooleanInstantiationRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/basic/BooleanInstantiationRule.java index c5091dd89f..032db19a4d 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/basic/BooleanInstantiationRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/basic/BooleanInstantiationRule.java @@ -3,6 +3,7 @@ */ package net.sourceforge.pmd.lang.java.rule.basic; +import net.sourceforge.pmd.lang.ast.Node; import net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression; import net.sourceforge.pmd.lang.java.ast.ASTArrayDimsAndInits; import net.sourceforge.pmd.lang.java.ast.ASTBooleanLiteral; @@ -63,7 +64,9 @@ public class BooleanInstantiationRule extends AbstractJavaRule { if (node.hasDescendantOfType(ASTArrayDimsAndInits.class)) { return super.visit(node, data); } - if (TypeHelper.isA((ASTClassOrInterfaceType) node.jjtGetChild(0), Boolean.class)) { + + Node n1 = node.getFirstChildOfType(ASTClassOrInterfaceType.class); + if (TypeHelper.isA((ASTClassOrInterfaceType) n1, Boolean.class)) { super.addViolation(data, node); return data; } From 7d5be34cd268ed065438ac0e4adb71f127338392 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Sat, 29 Oct 2016 17:16:01 +0200 Subject: [PATCH 3/3] Update changelog --- src/site/markdown/overview/changelog.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/site/markdown/overview/changelog.md b/src/site/markdown/overview/changelog.md index 6ac407ab54..db7bf894b1 100644 --- a/src/site/markdown/overview/changelog.md +++ b/src/site/markdown/overview/changelog.md @@ -14,11 +14,14 @@ * [#35](https://github.com/adangel/pmd/pull/35): Javascript tokenizer now ignores comment tokens. * [#103](https://github.com/pmd/pmd/pull/103): \[java] \[apex] Fix for 1501: CyclomaticComplexity rule causes OOM when class reporting is disabled +* [#111](https://github.com/pmd/pmd/pull/111): \[java] Fix BooleanInstantiationRule for Java 8 **Bugfixes:** * Java * [#1501](https://sourceforge.net/p/pmd/bugs/1501/): \[java] \[apex] CyclomaticComplexity rule causes OOM when class reporting is disabled +* java-basic/BooleanInstantiation + * [#1533](https://sourceforge.net/p/pmd/bugs/1533/): \[java] BooleanInstantiation: ClassCastException with Annotation * java-comments * [#1522](https://sourceforge.net/p/pmd/bugs/1522/): \[java] CommentRequired: false positive * java-design/CloseResource