From c8c8a04dda2be8104c54ed730378ced7af8007b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Mon, 2 Mar 2020 20:13:56 +0100 Subject: [PATCH] Fix new java nodes --- pmd-java/etc/grammar/Java.jjt | 5 ++--- .../pmd/lang/java/ast/ASTRecordBody.java | 9 +++++---- .../pmd/lang/java/ast/ASTRecordComponent.java | 9 +++++---- .../pmd/lang/java/ast/ASTRecordComponentList.java | 9 +++++---- .../java/ast/ASTRecordConstructorDeclaration.java | 13 +++++++------ .../pmd/lang/java/ast/ASTRecordDeclaration.java | 9 +++++---- .../pmd/lang/java/ast/ASTTypeTestPattern.java | 8 ++++---- .../java/ast/internal/LanguageLevelChecker.java | 12 +++++------- .../rule/design/UselessOverridingMethodRule.java | 7 ------- .../sourceforge/pmd/lang/java/ast/ASTPatternTest.kt | 7 +++++-- .../pmd/lang/java/ast/KotlinTestingDsl.kt | 4 +--- 11 files changed, 44 insertions(+), 48 deletions(-) diff --git a/pmd-java/etc/grammar/Java.jjt b/pmd-java/etc/grammar/Java.jjt index e353957ef3..34a5347f04 100644 --- a/pmd-java/etc/grammar/Java.jjt +++ b/pmd-java/etc/grammar/Java.jjt @@ -876,7 +876,6 @@ void RecordDeclaration(int modifiers): { Token t; jjtThis.setModifiers(modifiers); - checkForRecordType(); } { t = { @@ -884,7 +883,7 @@ void RecordDeclaration(int modifiers): throw new ParseException("ERROR: expecting record"); } } - t= {checkForBadTypeIdentifierUsage(t.image); jjtThis.setImage(t.image);} + t= {jjtThis.setImage(t.image);} [ TypeParameters() ] RecordComponentList() [ ImplementsList() ] @@ -1286,7 +1285,7 @@ void EqualityExpression() #EqualityExpression(>1): void InstanceOfExpression() #InstanceOfExpression(>1): {} { - RelationalExpression() [ LOOKAHEAD(2) "instanceof" Type() [ {checkforBadInstanceOfPattern();} VariableDeclaratorId() #TypeTestPattern(2) ] ] + RelationalExpression() [ LOOKAHEAD(2) "instanceof" Type() [ VariableDeclaratorId() #TypeTestPattern(2) ] ] } void RelationalExpression() #RelationalExpression(>1): diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTRecordBody.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTRecordBody.java index 904b62e56b..2f80ab773b 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTRecordBody.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTRecordBody.java @@ -25,12 +25,13 @@ public final class ASTRecordBody extends AbstractJavaNode { super(id); } - ASTRecordBody(JavaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); } + + @Override + public void jjtAccept(SideEffectingVisitor visitor, T data) { + visitor.visit(this, data); + } } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTRecordComponent.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTRecordComponent.java index 4c9943e258..e58d185704 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTRecordComponent.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTRecordComponent.java @@ -27,15 +27,16 @@ public final class ASTRecordComponent extends AbstractJavaNode { super(id); } - ASTRecordComponent(JavaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); } + @Override + public void jjtAccept(SideEffectingVisitor visitor, T data) { + visitor.visit(this, data); + } + public boolean isVarargs() { return varargs; } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTRecordComponentList.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTRecordComponentList.java index 8139e687cb..f3344a6e8b 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTRecordComponentList.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTRecordComponentList.java @@ -22,12 +22,13 @@ public final class ASTRecordComponentList extends AbstractJavaNode { super(id); } - ASTRecordComponentList(JavaParser p, int id) { - super(p, id); - } - @Override public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); } + + @Override + public void jjtAccept(SideEffectingVisitor visitor, T data) { + visitor.visit(this, data); + } } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTRecordConstructorDeclaration.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTRecordConstructorDeclaration.java index 9d5fb279d5..f05bbb978f 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTRecordConstructorDeclaration.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTRecordConstructorDeclaration.java @@ -12,8 +12,8 @@ import net.sourceforge.pmd.annotation.Experimental; * *
  *
- * RecordConstructorDeclaration ::=  ({@linkplain ASTTypeAnnotation TypeAnnotation})*
- *                                   {@linkplain ASTModifiers Modifiers}
+ * RecordConstructorDeclaration ::=  ({@linkplain ASTAnnotation TypeAnnotation})*
+ *                                   {@linkplain ASTModifierList Modifiers}
  *                                   {@linkplain ASTTypeParameters TypeParameters}?
  *                                   <IDENTIFIER>
  *                                   ( "throws" {@linkplain ASTNameList NameList} )?
@@ -28,15 +28,16 @@ public final class ASTRecordConstructorDeclaration extends AbstractJavaAccessNod
         super(id);
     }
 
-    ASTRecordConstructorDeclaration(JavaParser p, int id) {
-        super(p, id);
-    }
-
     @Override
     public Object jjtAccept(JavaParserVisitor visitor, Object data) {
         return visitor.visit(this, data);
     }
 
+    @Override
+    public  void jjtAccept(SideEffectingVisitor visitor, T data) {
+        visitor.visit(this, data);
+    }
+
     @Override
     public ASTRecordConstructorDeclaration getDeclarationNode() {
         return this;
diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTRecordDeclaration.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTRecordDeclaration.java
index 1d84939d8c..660f6b52b4 100644
--- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTRecordDeclaration.java
+++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTRecordDeclaration.java
@@ -33,15 +33,16 @@ public final class ASTRecordDeclaration extends AbstractAnyTypeDeclaration {
         super(id);
     }
 
-    ASTRecordDeclaration(JavaParser p, int id) {
-        super(p, id);
-    }
-
     @Override
     public Object jjtAccept(JavaParserVisitor visitor, Object data) {
         return visitor.visit(this, data);
     }
 
+    @Override
+    public  void jjtAccept(SideEffectingVisitor visitor, T data) {
+        visitor.visit(this, data);
+    }
+
     @Override
     public TypeKind getTypeKind() {
         return TypeKind.RECORD;
diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTTypeTestPattern.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTTypeTestPattern.java
index c126b89198..f04ecab633 100644
--- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTTypeTestPattern.java
+++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTTypeTestPattern.java
@@ -24,16 +24,16 @@ public final class ASTTypeTestPattern extends AbstractJavaNode implements ASTPat
         super(id);
     }
 
-    ASTTypeTestPattern(JavaParser p, int id) {
-        super(p, id);
-    }
-
 
     @Override
     public Object jjtAccept(JavaParserVisitor visitor, Object data) {
         return visitor.visit(this, data);
     }
 
+    @Override
+    public  void jjtAccept(SideEffectingVisitor visitor, T data) {
+        visitor.visit(this, data);
+    }
 
     /**
      * Gets the type against which the expression is tested.
diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/internal/LanguageLevelChecker.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/internal/LanguageLevelChecker.java
index 667a148786..7b5758e5d0 100644
--- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/internal/LanguageLevelChecker.java
+++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/internal/LanguageLevelChecker.java
@@ -55,7 +55,7 @@ import net.sourceforge.pmd.lang.java.ast.SideEffectingVisitorAdapter;
  */
 public class LanguageLevelChecker {
 
-    private static final Pattern SPACE_ESCAPE_PATTERN = Pattern.compile("(?!\\\\)\\\\s");
+    private static final Pattern SPACE_ESCAPE_PATTERN = Pattern.compile("(? {
         /** \s */
         SPACE_STRING_ESCAPES(14, 14, false),
         RECORD_DECLARATIONS(14, 14, false),
-        TYPE_TEST_PATTERNS_IN_INSTANCEOF(14, 14, false),
-        ;
+        TYPE_TEST_PATTERNS_IN_INSTANCEOF(14, 14, false);
 
 
         private final int minPreviewVersion;
@@ -148,7 +147,7 @@ public class LanguageLevelChecker {
 
             String message = StringUtils.capitalize(displayNameLower(name()));
             if (canBePreview) {
-                message += " is a only a preview feature on JDK " + jdk;
+                message += " is a preview feature of JDK " + jdk;
             } else if (wasStandardized) {
                 message = message + " was only standardized in Java " + (maxPreviewVersion + 1);
             } else if (minPreviewVersion == maxPreviewVersion) {
@@ -166,8 +165,7 @@ public class LanguageLevelChecker {
         ENUM_AS_AN_IDENTIFIER(5, "enum"),
         UNDERSCORE_AS_AN_IDENTIFIER(9, "_"),
         VAR_AS_A_TYPE_NAME(10, "var"),
-        RECORD_AS_A_TYPE_NAME(14, "record") // TODO
-        ;
+        RECORD_AS_A_TYPE_NAME(14, "record");
 
         private final int maxJdkVersion;
         private final String reserved;
@@ -280,7 +278,7 @@ public class LanguageLevelChecker {
 
         @Override
         public void visit(ASTYieldStatement node, T data) {
-            check(node, PreviewFeature.RECORD_DECLARATIONS, data);
+            check(node, PreviewFeature.YIELD_STATEMENTS, data);
             visitChildren(node, data);
         }
 
diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/UselessOverridingMethodRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/UselessOverridingMethodRule.java
index f73a34ebad..a32e7f8417 100644
--- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/UselessOverridingMethodRule.java
+++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/UselessOverridingMethodRule.java
@@ -292,11 +292,4 @@ public class UselessOverridingMethodRule extends AbstractJavaRule {
                 || elevatingIntoDifferentPackage;
     }
 
-    /**
-     * @deprecated this method will be removed. Just use {@link Node#findChildrenOfType(Class)} directly.
-     */
-    @Deprecated
-    public  List findFirstDegreeChaildrenOfType(Node n, Class targetType) {
-        return n.findChildrenOfType(targetType);
-    }
 }
diff --git a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ASTPatternTest.kt b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ASTPatternTest.kt
index 64dc9dd6c1..e77efc7ec6 100644
--- a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ASTPatternTest.kt
+++ b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ASTPatternTest.kt
@@ -1,5 +1,6 @@
 package net.sourceforge.pmd.lang.java.ast
 
+import io.kotlintest.matchers.string.shouldContain
 import net.sourceforge.pmd.lang.ast.test.shouldBe
 import net.sourceforge.pmd.lang.java.ast.JavaVersion.J14__PREVIEW
 import java.io.IOException
@@ -8,8 +9,10 @@ class ASTPatternTest : ParserTestSpec({
 
     parserTest("Test patterns only available on JDK 14 (preview)", javaVersions = !J14__PREVIEW) {
 
-        expectParseException("Cannot use type test patterns in instanceof when running in JDK other than 14-preview") {
-            parseAstExpression("obj instanceof Class c")
+        inContext(ExpressionParsingCtx) {
+            "obj instanceof Class c" should throwParseException {
+                it.message.shouldContain("Type test patterns in instanceof is a preview feature of JDK 14, you should select your language version accordingly")
+            }
         }
 
     }
diff --git a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/KotlinTestingDsl.kt b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/KotlinTestingDsl.kt
index 3e05589036..1a1cb69e96 100644
--- a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/KotlinTestingDsl.kt
+++ b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/KotlinTestingDsl.kt
@@ -8,9 +8,7 @@ import io.kotlintest.matchers.string.shouldContain
 import io.kotlintest.shouldThrow
 import net.sourceforge.pmd.lang.ast.Node
 import net.sourceforge.pmd.lang.ast.ParseException
-import net.sourceforge.pmd.lang.ast.test.Assertions
-import net.sourceforge.pmd.lang.ast.test.NodeSpec
-import net.sourceforge.pmd.lang.ast.test.matchNode
+import net.sourceforge.pmd.lang.ast.test.*
 import net.sourceforge.pmd.lang.ast.test.shouldMatchNode
 import net.sourceforge.pmd.lang.java.JavaParsingHelper
 import java.beans.PropertyDescriptor