Apply fixes from review (#4038)

This commit is contained in:
Andreas Dangel
2022-07-10 14:29:03 +02:00
parent 78e4ca6c04
commit b197daf84e
14 changed files with 50 additions and 46 deletions

View File

@ -60,9 +60,9 @@ Being based on a proper Antlr grammar, CPD can:
* To support the Java preview language features "Pattern Matching for Switch" and "Record Patterns", the following
AST nodes have been introduced as experimental:
* {% jdoc java::lang.java.ast.ASTGuard %}
* {% jdoc java::lang.java.ast.ASTSwitchGuard %}
* {% jdoc java::lang.java.ast.ASTRecordPattern %}
* {% jdoc java::lang.java.ast.ASTRecordStructurePattern %}
* {% jdoc java::lang.java.ast.ASTComponentPatternList %}
### External Contributions
* [#3984](https://github.com/pmd/pmd/pull/3984): \[java] Fix AddEmptyString false-negative issue - [@LiGaOg](https://github.com/LiGaOg)

View File

@ -1,8 +1,9 @@
/**
* Support "JEP 427: Pattern Matching for switch (Third Preview)" for Java 19 Preview
* Note: GuardedPattern is deprecated and only valid for 17-preview and 18-preview
* New AST node: Guard - used within switch case labels for refining a pattern
* New AST node: ASTSwitchGuard (production "Guard") - used within switch case labels for refining a pattern
* Support "JEP 405: Record Patterns (Preview)" for Java 19 Preview
* New AST node: ASTRecordPattern and ASTComponentPatternList (production "RecordStructurePattern")
* Remove support for Java 17 preview language features
* Andreas Dangel 07/2022
*====================================================================
@ -1817,7 +1818,7 @@ void RecordPattern():
ReferenceType() RecordStructurePattern() [ VariableDeclaratorId() ]
}
void RecordStructurePattern():
void RecordStructurePattern() #ComponentPatternList:
{}
{
"(" [ RecordComponentPatternList() ] ")"
@ -2331,7 +2332,7 @@ void CaseLabelElement(ASTSwitchLabel label) #void:
}
}
void Guard() :
void Guard() #SwitchGuard:
{
Token t;
checkForGuard();

View File

@ -12,19 +12,19 @@ import net.sourceforge.pmd.annotation.Experimental;
*
* <pre class="grammar">
*
* RecordStructurePattern ::= "(" {@linkplain ASTPattern Pattern} ( "," {@linkplain ASTPattern pattern} ) ")"
* ComponentPatternList ::= "(" {@linkplain ASTPattern Pattern} ( "," {@linkplain ASTPattern pattern} ) ")"
*
* </pre>
*
* @see <a href="https://openjdk.org/jeps/405">JEP 405: Record Patterns (Preview)</a>
*/
@Experimental
public final class ASTRecordStructurePattern extends AbstractJavaNode {
ASTRecordStructurePattern(int id) {
public final class ASTComponentPatternList extends AbstractJavaNode {
ASTComponentPatternList(int id) {
super(id);
}
ASTRecordStructurePattern(JavaParser p, int id) {
ASTComponentPatternList(JavaParser p, int id) {
super(p, id);
}

View File

@ -10,7 +10,7 @@ import net.sourceforge.pmd.annotation.Experimental;
* A pattern (for pattern matching constructs like {@link ASTInstanceOfExpression InstanceOfExpression}
* or within a {@link ASTSwitchLabel}). This is a JDK 16 feature.
*
* <p>This interface will be implemented by all forms of patterns.
* <p>This interface is implemented by all forms of patterns.
*
* <pre class="grammar">
*

View File

@ -7,12 +7,11 @@ package net.sourceforge.pmd.lang.java.ast;
import net.sourceforge.pmd.annotation.Experimental;
/**
* A record pattern (JDK19). This can be found on
* the right-hand side of an {@link ASTInstanceOfExpression InstanceOfExpression}.
* A record pattern (JDK19).
*
* <pre class="grammar">
*
* RecordPattern ::= {@linkplain ASTReferenceType ReferenceType} {@linkplain ASTRecordStructurePattern RecordStructurePattern} [ {@linkplain ASTVariableDeclaratorId} VariableDeclaratorId ]
* RecordPattern ::= {@linkplain ASTReferenceType ReferenceType} {@linkplain ASTComponentPatternList ComponentPatternList} [ {@linkplain ASTVariableDeclaratorId VariableDeclaratorId} ]
*
* </pre>
*

View File

@ -11,21 +11,21 @@ import net.sourceforge.pmd.annotation.Experimental;
*
* <pre class="grammar">
*
* SwitchLabel := "case" {@linkplain ASTPattern Pattern} "when" {@linkplain ASTGuard Guard}
* Guard ::= {@linkplain ASTExpression Expression}
* SwitchLabel := "case" {@linkplain ASTPattern Pattern} SwitchGuard?
* SwitchGuard ::= "when" {@linkplain ASTExpression Expression}
*
* </pre>
*
* @see <a href="https://openjdk.org/jeps/427">JEP 427: Pattern Matching for switch (Third Preview)</a>
*/
@Experimental
public final class ASTGuard extends AbstractJavaNode {
public final class ASTSwitchGuard extends AbstractJavaNode {
ASTGuard(int id) {
ASTSwitchGuard(int id) {
super(id);
}
ASTGuard(JavaParser p, int id) {
ASTSwitchGuard(JavaParser p, int id) {
super(p, id);
}

View File

@ -13,11 +13,15 @@ final class AstImplUtil {
}
static void bumpParenDepth(ASTPattern pattern) {
assert pattern instanceof ASTTypePattern || pattern instanceof ASTGuardedPattern
assert pattern instanceof ASTTypePattern
|| pattern instanceof ASTRecordPattern
|| pattern instanceof ASTGuardedPattern
: pattern.getClass() + " doesn't have parenDepth attribute!";
if (pattern instanceof ASTTypePattern) {
((ASTTypePattern) pattern).bumpParenDepth();
} else if (pattern instanceof ASTRecordPattern) {
((ASTRecordPattern) pattern).bumpParenDepth();
} else if (pattern instanceof ASTGuardedPattern) {
((ASTGuardedPattern) pattern).bumpParenDepth();
}

View File

@ -953,7 +953,7 @@ public class JavaParserDecoratedVisitor implements JavaParserVisitor {
@Experimental
@Override
public Object visit(ASTGuard node, Object data) {
public Object visit(ASTSwitchGuard node, Object data) {
visitor.visit(node, data);
return visit((JavaNode) node, data);
}
@ -967,7 +967,7 @@ public class JavaParserDecoratedVisitor implements JavaParserVisitor {
@Experimental
@Override
public Object visit(ASTRecordStructurePattern node, Object data) {
public Object visit(ASTComponentPatternList node, Object data) {
visitor.visit(node, data);
return visit((JavaNode) node, data);
}

View File

@ -672,7 +672,7 @@ public class JavaParserVisitorAdapter implements JavaParserVisitor {
@Experimental
@Override
public Object visit(ASTGuard node, Object data) {
public Object visit(ASTSwitchGuard node, Object data) {
return visit((JavaNode) node, data);
}
@ -684,7 +684,7 @@ public class JavaParserVisitorAdapter implements JavaParserVisitor {
@Experimental
@Override
public Object visit(ASTRecordStructurePattern node, Object data) {
public Object visit(ASTComponentPatternList node, Object data) {
return visit((JavaNode) node, data);
}
}

View File

@ -804,7 +804,7 @@ public class JavaParserVisitorDecorator implements JavaParserControllessVisitor
@Experimental
@Override
public Object visit(ASTGuard node, Object data) {
public Object visit(ASTSwitchGuard node, Object data) {
return visitor.visit(node, data);
}
@ -816,7 +816,7 @@ public class JavaParserVisitorDecorator implements JavaParserControllessVisitor
@Experimental
@Override
public Object visit(ASTRecordStructurePattern node, Object data) {
public Object visit(ASTComponentPatternList node, Object data) {
return visitor.visit(node, data);
}
}

View File

@ -37,6 +37,7 @@ import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType;
import net.sourceforge.pmd.lang.java.ast.ASTCompactConstructorDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
import net.sourceforge.pmd.lang.java.ast.ASTComponentPatternList;
import net.sourceforge.pmd.lang.java.ast.ASTConditionalAndExpression;
import net.sourceforge.pmd.lang.java.ast.ASTConditionalExpression;
import net.sourceforge.pmd.lang.java.ast.ASTConditionalOrExpression;
@ -60,7 +61,6 @@ import net.sourceforge.pmd.lang.java.ast.ASTForStatement;
import net.sourceforge.pmd.lang.java.ast.ASTForUpdate;
import net.sourceforge.pmd.lang.java.ast.ASTFormalParameter;
import net.sourceforge.pmd.lang.java.ast.ASTFormalParameters;
import net.sourceforge.pmd.lang.java.ast.ASTGuard;
import net.sourceforge.pmd.lang.java.ast.ASTGuardedPattern;
import net.sourceforge.pmd.lang.java.ast.ASTIfStatement;
import net.sourceforge.pmd.lang.java.ast.ASTImplementsList;
@ -105,7 +105,6 @@ import net.sourceforge.pmd.lang.java.ast.ASTRecordComponent;
import net.sourceforge.pmd.lang.java.ast.ASTRecordComponentList;
import net.sourceforge.pmd.lang.java.ast.ASTRecordDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTRecordPattern;
import net.sourceforge.pmd.lang.java.ast.ASTRecordStructurePattern;
import net.sourceforge.pmd.lang.java.ast.ASTReferenceType;
import net.sourceforge.pmd.lang.java.ast.ASTRelationalExpression;
import net.sourceforge.pmd.lang.java.ast.ASTResource;
@ -119,6 +118,7 @@ import net.sourceforge.pmd.lang.java.ast.ASTStatement;
import net.sourceforge.pmd.lang.java.ast.ASTStatementExpression;
import net.sourceforge.pmd.lang.java.ast.ASTStatementExpressionList;
import net.sourceforge.pmd.lang.java.ast.ASTSwitchExpression;
import net.sourceforge.pmd.lang.java.ast.ASTSwitchGuard;
import net.sourceforge.pmd.lang.java.ast.ASTSwitchLabel;
import net.sourceforge.pmd.lang.java.ast.ASTSwitchLabeledBlock;
import net.sourceforge.pmd.lang.java.ast.ASTSwitchLabeledExpression;
@ -887,7 +887,7 @@ public abstract class AbstractJavaRule extends AbstractRule implements JavaParse
@Experimental
@Override
public Object visit(ASTGuard node, Object data) {
public Object visit(ASTSwitchGuard node, Object data) {
return visit((JavaNode) node, data);
}
@ -899,7 +899,7 @@ public abstract class AbstractJavaRule extends AbstractRule implements JavaParse
@Experimental
@Override
public Object visit(ASTRecordStructurePattern node, Object data) {
public Object visit(ASTComponentPatternList node, Object data) {
return visit((JavaNode) node, data);
}

View File

@ -27,7 +27,7 @@
| | | | | +- ReferenceType[@Array = false, @ArrayDepth = 0]
| | | | | +- ClassOrInterfaceType[@AnonymousClass = false, @Array = false, @ArrayDepth = 0, @Image = "String", @ReferenceToClassSameCompilationUnit = false]
| | | | +- VariableDeclaratorId[@Array = false, @ArrayDepth = 0, @ArrayType = false, @ExceptionBlockParameter = false, @ExplicitReceiverParameter = false, @Field = false, @Final = false, @ForeachVariable = false, @FormalParameter = false, @Image = "s", @LambdaParameter = false, @LocalVariable = false, @Name = "s", @PatternBinding = true, @ResourceDeclaration = false, @TypeInferred = false, @VariableName = "s"]
| | | +- Guard[]
| | | +- SwitchGuard[]
| | | +- Expression[@StandAlonePrimitive = false]
| | | +- EqualityExpression[@Image = "==", @Operator = "=="]
| | | +- PrimaryExpression[]
@ -74,7 +74,7 @@
| | | | | +- ReferenceType[@Array = false, @ArrayDepth = 0]
| | | | | +- ClassOrInterfaceType[@AnonymousClass = false, @Array = false, @ArrayDepth = 0, @Image = "Integer", @ReferenceToClassSameCompilationUnit = false]
| | | | +- VariableDeclaratorId[@Array = false, @ArrayDepth = 0, @ArrayType = false, @ExceptionBlockParameter = false, @ExplicitReceiverParameter = false, @Field = false, @Final = false, @ForeachVariable = false, @FormalParameter = false, @Image = "i", @LambdaParameter = false, @LocalVariable = false, @Name = "i", @PatternBinding = true, @ResourceDeclaration = false, @TypeInferred = false, @VariableName = "i"]
| | | +- Guard[]
| | | +- SwitchGuard[]
| | | +- Expression[@StandAlonePrimitive = false]
| | | +- EqualityExpression[@Image = "==", @Operator = "=="]
| | | +- PrimaryExpression[]
@ -103,7 +103,7 @@
| | | | | +- ReferenceType[@Array = false, @ArrayDepth = 0]
| | | | | +- ClassOrInterfaceType[@AnonymousClass = false, @Array = false, @ArrayDepth = 0, @Image = "Long", @ReferenceToClassSameCompilationUnit = false]
| | | | +- VariableDeclaratorId[@Array = false, @ArrayDepth = 0, @ArrayType = false, @ExceptionBlockParameter = false, @ExplicitReceiverParameter = false, @Field = false, @Final = false, @ForeachVariable = false, @FormalParameter = false, @Image = "l", @LambdaParameter = false, @LocalVariable = false, @Name = "l", @PatternBinding = true, @ResourceDeclaration = false, @TypeInferred = false, @VariableName = "l"]
| | | +- Guard[]
| | | +- SwitchGuard[]
| | | +- Expression[@StandAlonePrimitive = false]
| | | +- EqualityExpression[@Image = "==", @Operator = "=="]
| | | +- PrimaryExpression[]
@ -235,7 +235,7 @@
| | | | | +- ReferenceType[@Array = false, @ArrayDepth = 0]
| | | | | +- ClassOrInterfaceType[@AnonymousClass = false, @Array = false, @ArrayDepth = 0, @Image = "String", @ReferenceToClassSameCompilationUnit = false]
| | | | +- VariableDeclaratorId[@Array = false, @ArrayDepth = 0, @ArrayType = false, @ExceptionBlockParameter = false, @ExplicitReceiverParameter = false, @Field = false, @Final = false, @ForeachVariable = false, @FormalParameter = false, @Image = "s", @LambdaParameter = false, @LocalVariable = false, @Name = "s", @PatternBinding = true, @ResourceDeclaration = false, @TypeInferred = false, @VariableName = "s"]
| | | +- Guard[]
| | | +- SwitchGuard[]
| | | +- Expression[@StandAlonePrimitive = false]
| | | +- PrimaryExpression[]
| | | +- PrimaryPrefix[@SuperModifier = false, @ThisModifier = false]
@ -285,7 +285,7 @@
| | | | | +- ReferenceType[@Array = false, @ArrayDepth = 0]
| | | | | +- ClassOrInterfaceType[@AnonymousClass = false, @Array = false, @ArrayDepth = 0, @Image = "Integer", @ReferenceToClassSameCompilationUnit = false]
| | | | +- VariableDeclaratorId[@Array = false, @ArrayDepth = 0, @ArrayType = false, @ExceptionBlockParameter = false, @ExplicitReceiverParameter = false, @Field = false, @Final = false, @ForeachVariable = false, @FormalParameter = false, @Image = "i", @LambdaParameter = false, @LocalVariable = false, @Name = "i", @PatternBinding = true, @ResourceDeclaration = false, @TypeInferred = false, @VariableName = "i"]
| | | +- Guard[]
| | | +- SwitchGuard[]
| | | +- Expression[@StandAlonePrimitive = false]
| | | +- EqualityExpression[@Image = "==", @Operator = "=="]
| | | +- PrimaryExpression[]
@ -314,7 +314,7 @@
| | | | | +- ReferenceType[@Array = false, @ArrayDepth = 0]
| | | | | +- ClassOrInterfaceType[@AnonymousClass = false, @Array = false, @ArrayDepth = 0, @Image = "Long", @ReferenceToClassSameCompilationUnit = false]
| | | | +- VariableDeclaratorId[@Array = false, @ArrayDepth = 0, @ArrayType = false, @ExceptionBlockParameter = false, @ExplicitReceiverParameter = false, @Field = false, @Final = false, @ForeachVariable = false, @FormalParameter = false, @Image = "l", @LambdaParameter = false, @LocalVariable = false, @Name = "l", @PatternBinding = true, @ResourceDeclaration = false, @TypeInferred = false, @VariableName = "l"]
| | | +- Guard[]
| | | +- SwitchGuard[]
| | | +- Expression[@StandAlonePrimitive = false]
| | | +- PrimaryExpression[]
| | | +- PrimaryPrefix[@SuperModifier = false, @ThisModifier = false]

View File

@ -139,7 +139,7 @@
| | +- RecordPattern[@ParenthesisDepth = 0]
| | +- ReferenceType[@Array = false, @ArrayDepth = 0]
| | | +- ClassOrInterfaceType[@AnonymousClass = false, @Array = false, @ArrayDepth = 0, @Image = "Point", @ReferenceToClassSameCompilationUnit = false]
| | +- RecordStructurePattern[]
| | +- ComponentPatternList[]
| | +- TypePattern[@ParenthesisDepth = 0]
| | | +- Type[@Array = false, @ArrayDepth = 0, @ArrayType = false, @TypeImage = "int"]
| | | | +- PrimitiveType[@Array = false, @ArrayDepth = 0, @Boolean = false, @Image = "int"]
@ -189,7 +189,7 @@
| | +- RecordPattern[@ParenthesisDepth = 0]
| | +- ReferenceType[@Array = false, @ArrayDepth = 0]
| | | +- ClassOrInterfaceType[@AnonymousClass = false, @Array = false, @ArrayDepth = 0, @Image = "Rectangle", @ReferenceToClassSameCompilationUnit = false]
| | +- RecordStructurePattern[]
| | +- ComponentPatternList[]
| | +- TypePattern[@ParenthesisDepth = 0]
| | | +- Type[@Array = false, @ArrayDepth = 0, @ArrayType = false, @TypeImage = "ColoredPoint"]
| | | | +- ReferenceType[@Array = false, @ArrayDepth = 0]
@ -239,11 +239,11 @@
| | +- RecordPattern[@ParenthesisDepth = 0]
| | +- ReferenceType[@Array = false, @ArrayDepth = 0]
| | | +- ClassOrInterfaceType[@AnonymousClass = false, @Array = false, @ArrayDepth = 0, @Image = "Rectangle", @ReferenceToClassSameCompilationUnit = false]
| | +- RecordStructurePattern[]
| | +- ComponentPatternList[]
| | +- RecordPattern[@ParenthesisDepth = 0]
| | | +- ReferenceType[@Array = false, @ArrayDepth = 0]
| | | | +- ClassOrInterfaceType[@AnonymousClass = false, @Array = false, @ArrayDepth = 0, @Image = "ColoredPoint", @ReferenceToClassSameCompilationUnit = false]
| | | +- RecordStructurePattern[]
| | | +- ComponentPatternList[]
| | | +- TypePattern[@ParenthesisDepth = 0]
| | | | +- Type[@Array = false, @ArrayDepth = 0, @ArrayType = false, @TypeImage = "Point"]
| | | | | +- ReferenceType[@Array = false, @ArrayDepth = 0]
@ -296,15 +296,15 @@
| | +- RecordPattern[@ParenthesisDepth = 0]
| | +- ReferenceType[@Array = false, @ArrayDepth = 0]
| | | +- ClassOrInterfaceType[@AnonymousClass = false, @Array = false, @ArrayDepth = 0, @Image = "Rectangle", @ReferenceToClassSameCompilationUnit = false]
| | +- RecordStructurePattern[]
| | +- ComponentPatternList[]
| | +- RecordPattern[@ParenthesisDepth = 0]
| | | +- ReferenceType[@Array = false, @ArrayDepth = 0]
| | | | +- ClassOrInterfaceType[@AnonymousClass = false, @Array = false, @ArrayDepth = 0, @Image = "ColoredPoint", @ReferenceToClassSameCompilationUnit = false]
| | | +- RecordStructurePattern[]
| | | +- ComponentPatternList[]
| | | +- RecordPattern[@ParenthesisDepth = 0]
| | | | +- ReferenceType[@Array = false, @ArrayDepth = 0]
| | | | | +- ClassOrInterfaceType[@AnonymousClass = false, @Array = false, @ArrayDepth = 0, @Image = "Point", @ReferenceToClassSameCompilationUnit = false]
| | | | +- RecordStructurePattern[]
| | | | +- ComponentPatternList[]
| | | | +- TypePattern[@ParenthesisDepth = 0]
| | | | | +- Type[@Array = false, @ArrayDepth = 0, @ArrayType = false, @TypeImage = "var"]
| | | | | | +- ReferenceType[@Array = false, @ArrayDepth = 0]
@ -385,7 +385,7 @@
| | | +- TypeArgument[@Wildcard = false]
| | | +- ReferenceType[@Array = false, @ArrayDepth = 0]
| | | +- ClassOrInterfaceType[@AnonymousClass = false, @Array = false, @ArrayDepth = 0, @Image = "Object", @ReferenceToClassSameCompilationUnit = false]
| | +- RecordStructurePattern[]
| | +- ComponentPatternList[]
| | +- TypePattern[@ParenthesisDepth = 0]
| | +- Type[@Array = false, @ArrayDepth = 0, @ArrayType = false, @TypeImage = "String"]
| | | +- ReferenceType[@Array = false, @ArrayDepth = 0]
@ -440,7 +440,7 @@
| | +- TypeArgument[@Wildcard = false]
| | +- ReferenceType[@Array = false, @ArrayDepth = 0]
| | +- ClassOrInterfaceType[@AnonymousClass = false, @Array = false, @ArrayDepth = 0, @Image = "String", @ReferenceToClassSameCompilationUnit = false]
| +- RecordStructurePattern[]
| +- ComponentPatternList[]
| +- TypePattern[@ParenthesisDepth = 0]
| +- Type[@Array = false, @ArrayDepth = 0, @ArrayType = false, @TypeImage = "var"]
| | +- ReferenceType[@Array = false, @ArrayDepth = 0]

View File

@ -157,7 +157,7 @@
| | | | | +- ReferenceType[@Array = false, @ArrayDepth = 0]
| | | | | +- ClassOrInterfaceType[@AnonymousClass = false, @Array = false, @ArrayDepth = 0, @Image = "Triangle", @ReferenceToClassSameCompilationUnit = true]
| | | | +- VariableDeclaratorId[@Array = false, @ArrayDepth = 0, @ArrayType = false, @ExceptionBlockParameter = false, @ExplicitReceiverParameter = false, @Field = false, @Final = false, @ForeachVariable = false, @FormalParameter = false, @Image = "t", @LambdaParameter = false, @LocalVariable = false, @Name = "t", @PatternBinding = true, @ResourceDeclaration = false, @TypeInferred = false, @VariableName = "t"]
| | | +- Guard[]
| | | +- SwitchGuard[]
| | | +- Expression[@StandAlonePrimitive = false]
| | | +- RelationalExpression[@Image = ">"]
| | | +- PrimaryExpression[]
@ -217,7 +217,7 @@
| | | | | +- ReferenceType[@Array = false, @ArrayDepth = 0]
| | | | | +- ClassOrInterfaceType[@AnonymousClass = false, @Array = false, @ArrayDepth = 0, @Image = "Triangle", @ReferenceToClassSameCompilationUnit = true]
| | | | +- VariableDeclaratorId[@Array = false, @ArrayDepth = 0, @ArrayType = false, @ExceptionBlockParameter = false, @ExplicitReceiverParameter = false, @Field = false, @Final = false, @ForeachVariable = false, @FormalParameter = false, @Image = "t", @LambdaParameter = false, @LocalVariable = false, @Name = "t", @PatternBinding = true, @ResourceDeclaration = false, @TypeInferred = false, @VariableName = "t"]
| | | +- Guard[]
| | | +- SwitchGuard[]
| | | +- Expression[@StandAlonePrimitive = false]
| | | +- RelationalExpression[@Image = ">"]
| | | +- PrimaryExpression[]