From f698a2e7aa3de0851e353d17a1b491d4bb6e911c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Mon, 2 Mar 2020 22:18:13 +0100 Subject: [PATCH] Small API improvements for records --- pmd-java/etc/grammar/Java.jjt | 2 +- .../pmd/lang/java/ast/ASTRecordComponent.java | 4 +- .../lang/java/ast/ASTRecordComponentList.java | 13 +++++- .../ast/ASTRecordConstructorDeclaration.java | 11 +++-- .../lang/java/ast/ASTRecordDeclaration.java | 8 +--- .../pmd/lang/java/ast/Java14PreviewTest.java | 43 +++++++++++-------- 6 files changed, 48 insertions(+), 33 deletions(-) diff --git a/pmd-java/etc/grammar/Java.jjt b/pmd-java/etc/grammar/Java.jjt index 2f4db088d17..80186c015eb 100644 --- a/pmd-java/etc/grammar/Java.jjt +++ b/pmd-java/etc/grammar/Java.jjt @@ -1171,7 +1171,7 @@ void RecordConstructorDeclaration(): modifiers = Modifiers() { jjtThis.setModifiers(modifiers); } [TypeParameters()] { jjtThis.setImage(token.image); } - "{" ( BlockStatement() )* "}" + Block() } void TypeParameters(): 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 4c9943e2589..b19f983d06f 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 @@ -20,7 +20,7 @@ * */ @Experimental -public final class ASTRecordComponent extends AbstractJavaNode { +public final class ASTRecordComponent extends AbstractJavaAnnotatableNode { private boolean varargs; ASTRecordComponent(int id) { @@ -48,7 +48,7 @@ public ASTType getTypeNode() { return getFirstChildOfType(ASTType.class); } - public ASTVariableDeclaratorId getVariableDeclaratorId() { + public ASTVariableDeclaratorId getVarId() { return getFirstChildOfType(ASTVariableDeclaratorId.class); } } 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 8139e687cb2..9bf152df080 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 @@ -5,6 +5,8 @@ package net.sourceforge.pmd.lang.java.ast; +import java.util.Iterator; + import net.sourceforge.pmd.annotation.Experimental; /** @@ -17,7 +19,7 @@ * */ @Experimental -public final class ASTRecordComponentList extends AbstractJavaNode { +public final class ASTRecordComponentList extends AbstractJavaNode implements Iterable { ASTRecordComponentList(int id) { super(id); } @@ -30,4 +32,13 @@ public final class ASTRecordComponentList extends AbstractJavaNode { public Object jjtAccept(JavaParserVisitor visitor, Object data) { return visitor.visit(this, data); } + + public int size() { + return getNumChildren(); + } + + @Override + public Iterator iterator() { + return new NodeChildrenIterator<>(this, ASTRecordComponent.class); + } } 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 9d5fb279d51..f262de9ea9f 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,12 +12,11 @@ * *
  *
- * RecordConstructorDeclaration ::=  ({@linkplain ASTTypeAnnotation TypeAnnotation})*
- *                                   {@linkplain ASTModifiers Modifiers}
+ * RecordConstructorDeclaration ::=  ({@linkplain ASTAnnotation Annotation})*
+ *                                   RecordModifiers
  *                                   {@linkplain ASTTypeParameters TypeParameters}?
  *                                   <IDENTIFIER>
- *                                   ( "throws" {@linkplain ASTNameList NameList} )?
- *                                   "{" ( {@linkplain ASTBlockStatement ASTBlockStatement} )* "}"
+ *                                   {@link ASTBlock Block}
  *
  * 
* @@ -46,4 +45,8 @@ public ASTRecordConstructorDeclaration getDeclarationNode() { public DeclarationKind getKind() { return DeclarationKind.RECORD_CONSTRUCTOR; } + + public ASTBlock getBody() { + return getFirstChildOfType(ASTBlock.class); + } } 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 1d84939d8c9..553190dd74d 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 @@ -57,11 +57,7 @@ public boolean isFindBoundary() { return isNested(); } - public List getRecordComponents() { - return getFirstChildOfType(ASTRecordComponentList.class).findChildrenOfType(ASTRecordComponent.class); - } - - public String getName() { - return getImage(); + public ASTRecordComponentList getComponentList() { + return getFirstChildOfType(ASTRecordComponentList.class); } } 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 be98bbb9d5b..b137fe5c2db 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 @@ -100,8 +100,8 @@ public void recordPoint() { List components = recordDecl.getFirstChildOfType(ASTRecordComponentList.class) .findChildrenOfType(ASTRecordComponent.class); Assert.assertEquals(2, components.size()); - Assert.assertEquals("x", components.get(0).getVariableDeclaratorId().getImage()); - Assert.assertEquals("y", components.get(1).getVariableDeclaratorId().getImage()); + Assert.assertEquals("x", components.get(0).getVarId().getImage()); + Assert.assertEquals("y", components.get(1).getVarId().getImage()); } @Test(expected = ParseException.class) @@ -123,21 +123,21 @@ public void innerRecords() { Assert.assertEquals(7, recordDecls.size()); ASTRecordDeclaration complex = recordDecls.get(0); - Assert.assertEquals("MyComplex", complex.getName()); + Assert.assertEquals("MyComplex", complex.getSimpleName()); Assert.assertTrue(complex.isNested()); - Assert.assertEquals(0, complex.getRecordComponents().get(0).findChildrenOfType(ASTAnnotation.class).size()); - Assert.assertEquals(1, complex.getRecordComponents().get(1).findChildrenOfType(ASTAnnotation.class).size()); + Assert.assertEquals(0, getComponent(complex, 0).findChildrenOfType(ASTAnnotation.class).size()); + Assert.assertEquals(1, getComponent(complex, 1).findChildrenOfType(ASTAnnotation.class).size()); Assert.assertEquals(2, complex.getDeclarations().size()); Assert.assertTrue(complex.getDeclarations().get(0).getChild(1) instanceof ASTConstructorDeclaration); Assert.assertTrue(complex.getDeclarations().get(1).getChild(0) instanceof ASTRecordDeclaration); ASTRecordDeclaration nested = recordDecls.get(1); - Assert.assertEquals("Nested", nested.getName()); + Assert.assertEquals("Nested", nested.getSimpleName()); Assert.assertTrue(nested.isNested()); ASTRecordDeclaration range = recordDecls.get(2); - Assert.assertEquals("Range", range.getName()); - Assert.assertEquals(2, range.getRecordComponents().size()); + Assert.assertEquals("Range", range.getSimpleName()); + Assert.assertEquals(2, range.getComponentList().size()); List rangeConstructors = range.findDescendantsOfType(ASTRecordConstructorDeclaration.class); Assert.assertEquals(1, rangeConstructors.size()); Assert.assertEquals("Range", rangeConstructors.get(0).getImage()); @@ -145,27 +145,32 @@ public void innerRecords() { Assert.assertEquals(2, range.getDeclarations().size()); ASTRecordDeclaration varRec = recordDecls.get(3); - Assert.assertEquals("VarRec", varRec.getName()); - Assert.assertEquals("x", varRec.getRecordComponents().get(0).getVariableDeclaratorId().getImage()); - Assert.assertTrue(varRec.getRecordComponents().get(0).isVarargs()); - Assert.assertEquals(2, varRec.getRecordComponents().get(0).findChildrenOfType(ASTAnnotation.class).size()); - Assert.assertEquals(1, varRec.getRecordComponents().get(0).getTypeNode().findDescendantsOfType(ASTAnnotation.class).size()); + Assert.assertEquals("VarRec", varRec.getSimpleName()); + Assert.assertEquals("x", getComponent(varRec, 0).getVarId().getImage()); + Assert.assertTrue(getComponent(varRec, 0).isVarargs()); + Assert.assertEquals(2, getComponent(varRec, 0).findChildrenOfType(ASTAnnotation.class).size()); + Assert.assertEquals(1, getComponent(varRec, 0).getTypeNode().findDescendantsOfType(ASTAnnotation.class).size()); ASTRecordDeclaration arrayRec = recordDecls.get(4); - Assert.assertEquals("ArrayRec", arrayRec.getName()); - Assert.assertEquals("x", arrayRec.getRecordComponents().get(0).getVariableDeclaratorId().getImage()); - Assert.assertTrue(arrayRec.getRecordComponents().get(0).getVariableDeclaratorId().hasArrayType()); + Assert.assertEquals("ArrayRec", arrayRec.getSimpleName()); + Assert.assertEquals("x", getComponent(arrayRec, 0).getVarId().getImage()); + Assert.assertTrue(getComponent(arrayRec, 0).getVarId().hasArrayType()); ASTRecordDeclaration emptyRec = recordDecls.get(5); - Assert.assertEquals("EmptyRec", emptyRec.getName()); - Assert.assertEquals(0, emptyRec.getRecordComponents().size()); + Assert.assertEquals("EmptyRec", emptyRec.getSimpleName()); + Assert.assertEquals(0, emptyRec.getComponentList().size()); ASTRecordDeclaration personRec = recordDecls.get(6); - Assert.assertEquals("PersonRecord", personRec.getName()); + Assert.assertEquals("PersonRecord", personRec.getSimpleName()); ASTImplementsList impl = personRec.getFirstChildOfType(ASTImplementsList.class); Assert.assertEquals(2, impl.findChildrenOfType(ASTClassOrInterfaceType.class).size()); } + private ASTRecordComponent getComponent(ASTRecordDeclaration arrayRec, int index) { + return (ASTRecordComponent) arrayRec.getComponentList().getChild(index); + } + + @Test(expected = ParseException.class) public void recordIsARestrictedIdentifier() { java14p.parse("public class record {}");