Skip to content

Commit

Permalink
Small API improvements for records
Browse files Browse the repository at this point in the history
  • Loading branch information
oowekyala committed Mar 2, 2020
1 parent 1b3254f commit f698a2e
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 33 deletions.
2 changes: 1 addition & 1 deletion pmd-java/etc/grammar/Java.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -1171,7 +1171,7 @@ void RecordConstructorDeclaration():
modifiers = Modifiers() { jjtThis.setModifiers(modifiers); }
[TypeParameters()]
<IDENTIFIER> { jjtThis.setImage(token.image); }
"{" ( BlockStatement() )* "}"
Block()
}

void TypeParameters():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* </pre>
*/
@Experimental
public final class ASTRecordComponent extends AbstractJavaNode {
public final class ASTRecordComponent extends AbstractJavaAnnotatableNode {
private boolean varargs;

ASTRecordComponent(int id) {
Expand Down Expand Up @@ -48,7 +48,7 @@ public ASTType getTypeNode() {
return getFirstChildOfType(ASTType.class);
}

public ASTVariableDeclaratorId getVariableDeclaratorId() {
public ASTVariableDeclaratorId getVarId() {
return getFirstChildOfType(ASTVariableDeclaratorId.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

package net.sourceforge.pmd.lang.java.ast;

import java.util.Iterator;

import net.sourceforge.pmd.annotation.Experimental;

/**
Expand All @@ -17,7 +19,7 @@
* </pre>
*/
@Experimental
public final class ASTRecordComponentList extends AbstractJavaNode {
public final class ASTRecordComponentList extends AbstractJavaNode implements Iterable<ASTRecordComponent> {
ASTRecordComponentList(int id) {
super(id);
}
Expand All @@ -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<ASTRecordComponent> iterator() {
return new NodeChildrenIterator<>(this, ASTRecordComponent.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@
*
* <pre class="grammar">
*
* RecordConstructorDeclaration ::= ({@linkplain ASTTypeAnnotation TypeAnnotation})*
* {@linkplain ASTModifiers Modifiers}
* RecordConstructorDeclaration ::= ({@linkplain ASTAnnotation Annotation})*
* RecordModifiers
* {@linkplain ASTTypeParameters TypeParameters}?
* &lt;IDENTIFIER&gt;
* ( "throws" {@linkplain ASTNameList NameList} )?
* "{" ( {@linkplain ASTBlockStatement ASTBlockStatement} )* "}"
* {@link ASTBlock Block}
*
* </pre>
*
Expand Down Expand Up @@ -46,4 +45,8 @@ public ASTRecordConstructorDeclaration getDeclarationNode() {
public DeclarationKind getKind() {
return DeclarationKind.RECORD_CONSTRUCTOR;
}

public ASTBlock getBody() {
return getFirstChildOfType(ASTBlock.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,7 @@ public boolean isFindBoundary() {
return isNested();
}

public List<ASTRecordComponent> getRecordComponents() {
return getFirstChildOfType(ASTRecordComponentList.class).findChildrenOfType(ASTRecordComponent.class);
}

public String getName() {
return getImage();
public ASTRecordComponentList getComponentList() {
return getFirstChildOfType(ASTRecordComponentList.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ public void recordPoint() {
List<ASTRecordComponent> 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)
Expand All @@ -123,49 +123,54 @@ 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<ASTRecordConstructorDeclaration> rangeConstructors = range.findDescendantsOfType(ASTRecordConstructorDeclaration.class);
Assert.assertEquals(1, rangeConstructors.size());
Assert.assertEquals("Range", rangeConstructors.get(0).getImage());
Assert.assertTrue(rangeConstructors.get(0).getChild(0) instanceof ASTAnnotation);
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 {}");
Expand Down

0 comments on commit f698a2e

Please sign in to comment.