From f9df6ed01e50e2d99332bf626ea701275d426fc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Mart=C3=ADn=20Sotuyo=20Dodero?= Date: Mon, 29 Jan 2018 14:10:40 -0300 Subject: [PATCH] [java] Fix grammar for <> in Java 1.8 - Fixes #888 - Allow the contents of an anonymous class to use diamong notation, but the anonymous class itself can't use it --- pmd-java/etc/grammar/Java.jjt | 9 ++++--- .../pmd/lang/java/ast/ParserCornersTest.java | 27 +++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/pmd-java/etc/grammar/Java.jjt b/pmd-java/etc/grammar/Java.jjt index 7b47f33085..e4ca0373da 100644 --- a/pmd-java/etc/grammar/Java.jjt +++ b/pmd-java/etc/grammar/Java.jjt @@ -1,4 +1,7 @@ /** + * Fixes #888 [java] ParseException occurs with valid '<>' in Java 1.8 mode + * Juan Martin Sotuyo Dodero 01/2018 + *==================================================================== * Fixes #793 [java] Parser error with private method in nested classes in interfaces * Andreas Dangel 12/2017 *==================================================================== @@ -333,9 +336,9 @@ public class JavaParser { private void checkForBadAnonymousDiamondUsage() { if (jdkVersion < 9) { ASTAllocationExpression node = (ASTAllocationExpression)jjtree.peekNode(); - ASTTypeArguments types = node.getFirstDescendantOfType(ASTTypeArguments.class); + ASTTypeArguments types = node.getFirstChildOfType(ASTClassOrInterfaceType.class).getFirstChildOfType(ASTTypeArguments.class); if (node.isAnonymousClass() && types != null && types.isDiamond()) { - throwParseException("Cannot use '<>' with anonymous inner classes when running in JDK inferior to 9 mode!"); + throwParseException("Cannot use '<>' with anonymous inner classes when running in JDK inferior to 9 mode!"); } } } @@ -2026,8 +2029,8 @@ void AllocationExpression(): { inInterface = inInterfaceOld; } // always restore the flag after leaving the node ] ) + { checkForBadAnonymousDiamondUsage(); } ) - { checkForBadAnonymousDiamondUsage(); } } /* diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/ParserCornersTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/ParserCornersTest.java index 78fb900f1c..63169c238e 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/ParserCornersTest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/ParserCornersTest.java @@ -35,6 +35,33 @@ public class ParserCornersTest { + " TestInnerClassCallsOuterParent.super.toString();\n" + " }\n" + " };\n" + " }\n" + "}\n"); } + + /** + * #888 PMD 6.0.0 can't parse valid <> under 1.8. + */ + @Test + public void testDiamondUsageJava8() { + parseJava18("public class PMDExceptionTest {\n" + + " private Component makeUI() {\n" + + " String[] model = {\"123456\", \"7890\"};\n" + + " JComboBox comboBox = new JComboBox<>(model);\n" + + " comboBox.setEditable(true);\n" + + " comboBox.setEditor(new BasicComboBoxEditor() {\n" + + " private Component editorComponent;\n" + + " @Override public Component getEditorComponent() {\n" + + " if (editorComponent == null) {\n" + + " JTextField tc = (JTextField) super.getEditorComponent();\n" + + " editorComponent = new JLayer<>(tc, new ValidationLayerUI<>());\n" + + " }\n" + + " return editorComponent;\n" + + " }\n" + + " });\n" + + " JPanel p = new JPanel();\n" + + " p.add(comboBox);\n" + + " return p;\n" + + " }\n" + + "}"); + } @Test public final void testGetFirstASTNameImageNull() {