diff --git a/pmd-java/etc/grammar/Java.jjt b/pmd-java/etc/grammar/Java.jjt index 132280c698..3eee2c2100 100644 --- a/pmd-java/etc/grammar/Java.jjt +++ b/pmd-java/etc/grammar/Java.jjt @@ -381,8 +381,8 @@ public class JavaParser { } private void checkforBadInstanceOfPattern() { - if (!(jdkVersion == 14 && preview || jdkVersion == 15 && preview || jdkVersion == 16)) { - throwParseException("Pattern Matching for instanceof is only supported with Java 14 Preview and Java 15 Preview and Java 16"); + if (!(jdkVersion == 14 && preview || jdkVersion == 15 && preview || jdkVersion >= 16)) { + throwParseException("Pattern Matching for instanceof is only supported with Java 14 Preview and Java 15 Preview and Java >= 16"); } } @@ -1685,7 +1685,7 @@ void EqualityExpression() #EqualityExpression(>1): InstanceOfExpression() ( LOOKAHEAD(2) ( "==" {jjtThis.setImage("==");} | "!=" {jjtThis.setImage("!=");} ) InstanceOfExpression() )* } -void TypePattern() #TypeTestPattern: +void TypePattern(): {} { ( "final" {jjtThis.setFinal(true);} | Annotation() )* @@ -1702,7 +1702,7 @@ void InstanceOfExpression() #InstanceOfExpression(>1): LOOKAHEAD("final" | "@") {checkforBadInstanceOfPattern();} TypePattern() | Type() - [ {checkforBadInstanceOfPattern();} VariableDeclaratorId() #TypeTestPattern(2) ] + [ {checkforBadInstanceOfPattern();} VariableDeclaratorId() #TypePattern(2) ] ) ] } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTInstanceOfExpression.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTInstanceOfExpression.java index 091810cbd2..e20c5917bc 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTInstanceOfExpression.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTInstanceOfExpression.java @@ -48,7 +48,7 @@ public class ASTInstanceOfExpression extends AbstractJavaTypeNode { public ASTType getTypeNode() { JavaNode child = getChild(1); return child instanceof ASTType ? (ASTType) child - : ((ASTTypeTestPattern) child).getTypeNode(); + : ((ASTTypePattern) child).getTypeNode(); } } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTPattern.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTPattern.java index ef957a26c5..5a2b275d2c 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTPattern.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTPattern.java @@ -4,8 +4,6 @@ package net.sourceforge.pmd.lang.java.ast; -import net.sourceforge.pmd.annotation.Experimental; - /** * A pattern (for pattern matching constructs like {@link ASTInstanceOfExpression InstanceOfExpression}). * This is a JDK 16 feature. @@ -16,13 +14,12 @@ import net.sourceforge.pmd.annotation.Experimental; * *
  *
- * Pattern ::= {@link ASTTypeTestPattern TypeTestPattern}
+ * Pattern ::= {@link ASTTypePattern TypePattern}
  *
  * 
* * @see JEP 394: Pattern Matching for instanceof */ -@Experimental public interface ASTPattern extends JavaNode { } 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/ASTTypePattern.java similarity index 75% rename from pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTTypeTestPattern.java rename to pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTTypePattern.java index 40c59ff9b4..2c7c74d576 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/ASTTypePattern.java @@ -6,30 +6,27 @@ package net.sourceforge.pmd.lang.java.ast; import java.util.List; -import net.sourceforge.pmd.annotation.Experimental; - /** * A type pattern (JDK16). This can be found on * the right-hand side of an {@link ASTInstanceOfExpression InstanceOfExpression}. * *
  *
- * TypeTestPattern ::= ( "final" | {@linkplain ASTAnnotation Annotation} )* {@linkplain ASTType Type} {@link ASTVariableDeclaratorId VariableDeclaratorId}
+ * TypePattern ::= ( "final" | {@linkplain ASTAnnotation Annotation} )* {@linkplain ASTType Type} {@link ASTVariableDeclaratorId VariableDeclaratorId}
  *
  * 
* * @see JEP 394: Pattern Matching for instanceof */ -@Experimental -public final class ASTTypeTestPattern extends AbstractJavaAnnotatableNode implements ASTPattern { +public final class ASTTypePattern extends AbstractJavaAnnotatableNode implements ASTPattern { private boolean isFinal; - ASTTypeTestPattern(int id) { + ASTTypePattern(int id) { super(id); } - ASTTypeTestPattern(JavaParser p, int id) { + ASTTypePattern(JavaParser p, int id) { super(p, id); } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTVariableDeclaratorId.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTVariableDeclaratorId.java index c1c213f000..bd063be244 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTVariableDeclaratorId.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTVariableDeclaratorId.java @@ -6,7 +6,6 @@ package net.sourceforge.pmd.lang.java.ast; import java.util.List; -import net.sourceforge.pmd.annotation.Experimental; import net.sourceforge.pmd.annotation.InternalApi; import net.sourceforge.pmd.lang.ast.Node; import net.sourceforge.pmd.lang.ast.xpath.internal.DeprecatedAttribute; @@ -201,8 +200,8 @@ public class ASTVariableDeclaratorId extends AbstractJavaTypeNode implements Dim return false; } - if (getParent() instanceof ASTTypeTestPattern) { - return ((ASTTypeTestPattern) getParent()).isFinal(); + if (getParent() instanceof ASTTypePattern) { + return ((ASTTypePattern) getParent()).isFinal(); } if (getParent() instanceof ASTRecordComponent) { @@ -277,7 +276,6 @@ public class ASTVariableDeclaratorId extends AbstractJavaTypeNode implements Dim * Returns true if this is a binding variable in a * {@linkplain ASTPattern pattern}. */ - @Experimental public boolean isPatternBinding() { return getParent() instanceof ASTPattern; } @@ -331,8 +329,8 @@ public class ASTVariableDeclaratorId extends AbstractJavaTypeNode implements Dim } else if (isTypeInferred()) { // lambda expression with lax types. The type is inferred... return null; - } else if (getParent() instanceof ASTTypeTestPattern) { - return ((ASTTypeTestPattern) getParent()).getTypeNode(); + } else if (getParent() instanceof ASTTypePattern) { + return ((ASTTypePattern) getParent()).getTypeNode(); } else if (getParent() instanceof ASTRecordComponent) { return ((ASTRecordComponent) getParent()).getTypeNode(); } else { diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/JavaParserDecoratedVisitor.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/JavaParserDecoratedVisitor.java index ecc15eacd9..436c18e569 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/JavaParserDecoratedVisitor.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/JavaParserDecoratedVisitor.java @@ -903,7 +903,7 @@ public class JavaParserDecoratedVisitor implements JavaParserVisitor { @Override @Experimental - public Object visit(ASTTypeTestPattern node, Object data) { + public Object visit(ASTTypePattern node, Object data) { visitor.visit(node, data); return visit((JavaNode) node, data); } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/JavaParserVisitorAdapter.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/JavaParserVisitorAdapter.java index 359cd8573e..656b66a613 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/JavaParserVisitorAdapter.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/JavaParserVisitorAdapter.java @@ -630,7 +630,7 @@ public class JavaParserVisitorAdapter implements JavaParserVisitor { @Override @Experimental - public Object visit(ASTTypeTestPattern node, Object data) { + public Object visit(ASTTypePattern node, Object data) { return visit((JavaNode) node, data); } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/JavaParserVisitorDecorator.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/JavaParserVisitorDecorator.java index fef4a1bb7f..827f4b4f4f 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/JavaParserVisitorDecorator.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/JavaParserVisitorDecorator.java @@ -762,7 +762,7 @@ public class JavaParserVisitorDecorator implements JavaParserControllessVisitor @Override @Experimental - public Object visit(ASTTypeTestPattern node, Object data) { + public Object visit(ASTTypePattern node, Object data) { return visitor.visit(node, data); } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/AbstractJavaRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/AbstractJavaRule.java index 294eb61ee8..e77ff13efe 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/AbstractJavaRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/AbstractJavaRule.java @@ -130,7 +130,7 @@ import net.sourceforge.pmd.lang.java.ast.ASTTypeBound; import net.sourceforge.pmd.lang.java.ast.ASTTypeDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTTypeParameter; import net.sourceforge.pmd.lang.java.ast.ASTTypeParameters; -import net.sourceforge.pmd.lang.java.ast.ASTTypeTestPattern; +import net.sourceforge.pmd.lang.java.ast.ASTTypePattern; import net.sourceforge.pmd.lang.java.ast.ASTUnaryExpression; import net.sourceforge.pmd.lang.java.ast.ASTUnaryExpressionNotPlusMinus; import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclarator; @@ -839,7 +839,7 @@ public abstract class AbstractJavaRule extends AbstractRule implements JavaParse @Override @Experimental - public Object visit(ASTTypeTestPattern node, Object data) { + public Object visit(ASTTypePattern node, Object data) { return visit((JavaNode) node, data); } diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/Java14PreviewTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/Java14PreviewTest.java index ac22623748..e2365d630c 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/Java14PreviewTest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/Java14PreviewTest.java @@ -78,7 +78,7 @@ public class Java14PreviewTest { List instanceOfExpressions = compilationUnit.findDescendantsOfType(ASTInstanceOfExpression.class); Assert.assertEquals(4, instanceOfExpressions.size()); for (ASTInstanceOfExpression expr : instanceOfExpressions) { - Assert.assertTrue(expr.getChild(1) instanceof ASTTypeTestPattern); + Assert.assertTrue(expr.getChild(1) instanceof ASTTypePattern); ASTVariableDeclaratorId variable = expr.getChild(1).getFirstChildOfType(ASTVariableDeclaratorId.class); Assert.assertEquals(String.class, variable.getType()); Assert.assertEquals("s", variable.getVariableName()); 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 806588b2eb..6d3187e205 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 @@ -15,7 +15,7 @@ class ASTPatternTest : ParserTestSpec({ parserTest("Test patterns only available on JDK 14+15 (preview) and JDK16 and JDK16 (preview)", javaVersions = JavaVersion.values().asList().minus(J14__PREVIEW).minus(J15__PREVIEW).minus(J16).minus(J16__PREVIEW)) { - expectParseException("Pattern Matching for instanceof is only supported with Java 14 Preview and Java 15 Preview and Java 16") { + expectParseException("Pattern Matching for instanceof is only supported with Java 14 Preview and Java 15 Preview and Java >= 16") { parseAstExpression("obj instanceof Class c") } @@ -27,7 +27,7 @@ class ASTPatternTest : ParserTestSpec({ "obj instanceof Class c" should matchExpr { unspecifiedChild() - child { + child { it.isAnnotationPresent("java.lang.Deprecated") shouldBe false it::getTypeNode typeShouldBe child(ignoreChildren = true) {} @@ -40,7 +40,7 @@ class ASTPatternTest : ParserTestSpec({ "obj instanceof final Class c" should matchExpr { unspecifiedChild() - child { + child { it.isAnnotationPresent("java.lang.Deprecated") shouldBe false it::getTypeNode typeShouldBe child(ignoreChildren = true) {} @@ -53,7 +53,7 @@ class ASTPatternTest : ParserTestSpec({ "obj instanceof @Deprecated Class c" should matchExpr { unspecifiedChild() - child { + child { child(ignoreChildren = true) { it.annotationName shouldBe "Deprecated" } diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java15p/PatternMatchingInstanceof.txt b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java15p/PatternMatchingInstanceof.txt index 00481b37e4..7802f51b10 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java15p/PatternMatchingInstanceof.txt +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java15p/PatternMatchingInstanceof.txt @@ -40,7 +40,7 @@ | | | +- PrimaryExpression[] | | | | +- PrimaryPrefix[@SuperModifier = false, @ThisModifier = false] | | | | +- Name[@Image = "obj"] - | | | +- TypeTestPattern[] + | | | +- TypePattern[] | | | +- Type[@Array = false, @ArrayDepth = 0, @ArrayType = false, @TypeImage = "String"] | | | | +- ReferenceType[@Array = false, @ArrayDepth = 0] | | | | +- ClassOrInterfaceType[@AnonymousClass = false, @Array = false, @ArrayDepth = 0, @Image = "String", @ReferenceToClassSameCompilationUnit = false] @@ -109,7 +109,7 @@ | | | +- PrimaryExpression[] | | | | +- PrimaryPrefix[@SuperModifier = false, @ThisModifier = false] | | | | +- Name[@Image = "obj"] - | | | +- TypeTestPattern[] + | | | +- TypePattern[] | | | +- Type[@Array = false, @ArrayDepth = 0, @ArrayType = false, @TypeImage = "String"] | | | | +- ReferenceType[@Array = false, @ArrayDepth = 0] | | | | +- ClassOrInterfaceType[@AnonymousClass = false, @Array = false, @ArrayDepth = 0, @Image = "String", @ReferenceToClassSameCompilationUnit = false] @@ -175,7 +175,7 @@ | | | | +- PrimaryExpression[] | | | | | +- PrimaryPrefix[@SuperModifier = false, @ThisModifier = false] | | | | | +- Name[@Image = "obj"] - | | | | +- TypeTestPattern[] + | | | | +- TypePattern[] | | | | +- Type[@Array = false, @ArrayDepth = 0, @ArrayType = false, @TypeImage = "String"] | | | | | +- ReferenceType[@Array = false, @ArrayDepth = 0] | | | | | +- ClassOrInterfaceType[@AnonymousClass = false, @Array = false, @ArrayDepth = 0, @Image = "String", @ReferenceToClassSameCompilationUnit = false] @@ -224,7 +224,7 @@ | | | +- PrimaryExpression[] | | | | +- PrimaryPrefix[@SuperModifier = false, @ThisModifier = false] | | | | +- Name[@Image = "obj"] - | | | +- TypeTestPattern[] + | | | +- TypePattern[] | | | +- Type[@Array = false, @ArrayDepth = 0, @ArrayType = false, @TypeImage = "String"] | | | | +- ReferenceType[@Array = false, @ArrayDepth = 0] | | | | +- ClassOrInterfaceType[@AnonymousClass = false, @Array = false, @ArrayDepth = 0, @Image = "String", @ReferenceToClassSameCompilationUnit = false] diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java16/PatternMatchingInstanceof.txt b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java16/PatternMatchingInstanceof.txt index dca5fe68c6..37b741a663 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java16/PatternMatchingInstanceof.txt +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java16/PatternMatchingInstanceof.txt @@ -40,7 +40,7 @@ | | | +- PrimaryExpression[] | | | | +- PrimaryPrefix[@SuperModifier = false, @ThisModifier = false] | | | | +- Name[@Image = "obj"] - | | | +- TypeTestPattern[] + | | | +- TypePattern[] | | | +- Type[@Array = false, @ArrayDepth = 0, @ArrayType = false, @TypeImage = "String"] | | | | +- ReferenceType[@Array = false, @ArrayDepth = 0] | | | | +- ClassOrInterfaceType[@AnonymousClass = false, @Array = false, @ArrayDepth = 0, @Image = "String", @ReferenceToClassSameCompilationUnit = false] @@ -150,7 +150,7 @@ | | | +- PrimaryExpression[] | | | | +- PrimaryPrefix[@SuperModifier = false, @ThisModifier = false] | | | | +- Name[@Image = "obj"] - | | | +- TypeTestPattern[] + | | | +- TypePattern[] | | | +- Type[@Array = false, @ArrayDepth = 0, @ArrayType = false, @TypeImage = "String"] | | | | +- ReferenceType[@Array = false, @ArrayDepth = 0] | | | | +- ClassOrInterfaceType[@AnonymousClass = false, @Array = false, @ArrayDepth = 0, @Image = "String", @ReferenceToClassSameCompilationUnit = false] @@ -216,7 +216,7 @@ | | | | +- PrimaryExpression[] | | | | | +- PrimaryPrefix[@SuperModifier = false, @ThisModifier = false] | | | | | +- Name[@Image = "obj"] - | | | | +- TypeTestPattern[] + | | | | +- TypePattern[] | | | | +- Type[@Array = false, @ArrayDepth = 0, @ArrayType = false, @TypeImage = "String"] | | | | | +- ReferenceType[@Array = false, @ArrayDepth = 0] | | | | | +- ClassOrInterfaceType[@AnonymousClass = false, @Array = false, @ArrayDepth = 0, @Image = "String", @ReferenceToClassSameCompilationUnit = false] @@ -265,7 +265,7 @@ | | | | +- PrimaryExpression[] | | | | | +- PrimaryPrefix[@SuperModifier = false, @ThisModifier = false] | | | | | +- Name[@Image = "obj"] - | | | | +- TypeTestPattern[] + | | | | +- TypePattern[] | | | | +- Type[@Array = false, @ArrayDepth = 0, @ArrayType = false, @TypeImage = "String"] | | | | | +- ReferenceType[@Array = false, @ArrayDepth = 0] | | | | | +- ClassOrInterfaceType[@AnonymousClass = false, @Array = false, @ArrayDepth = 0, @Image = "String", @ReferenceToClassSameCompilationUnit = false] @@ -313,7 +313,7 @@ | | | +- PrimaryExpression[] | | | | +- PrimaryPrefix[@SuperModifier = false, @ThisModifier = false] | | | | +- Name[@Image = "obj"] - | | | +- TypeTestPattern[] + | | | +- TypePattern[] | | | +- Type[@Array = false, @ArrayDepth = 0, @ArrayType = false, @TypeImage = "String"] | | | | +- ReferenceType[@Array = false, @ArrayDepth = 0] | | | | +- ClassOrInterfaceType[@AnonymousClass = false, @Array = false, @ArrayDepth = 0, @Image = "String", @ReferenceToClassSameCompilationUnit = false] @@ -378,7 +378,7 @@ | | | +- PrimaryExpression[] | | | | +- PrimaryPrefix[@SuperModifier = false, @ThisModifier = false] | | | | +- Name[@Image = "obj"] - | | | +- TypeTestPattern[] + | | | +- TypePattern[] | | | +- Annotation[@AnnotationName = "Deprecated"] | | | | +- MarkerAnnotation[@AnnotationName = "Deprecated"] | | | | +- Name[@Image = "Deprecated"] @@ -446,7 +446,7 @@ | | +- PrimaryExpression[] | | | +- PrimaryPrefix[@SuperModifier = false, @ThisModifier = false] | | | +- Name[@Image = "obj"] - | | +- TypeTestPattern[] + | | +- TypePattern[] | | +- Annotation[@AnnotationName = "Deprecated"] | | | +- MarkerAnnotation[@AnnotationName = "Deprecated"] | | | +- Name[@Image = "Deprecated"]