Skip to content

Commit

Permalink
[java] Support RecordConstructorDeclaration as AnyTypeBodyDeclaration
Browse files Browse the repository at this point in the history
  • Loading branch information
adangel committed Feb 28, 2020
1 parent 7d3df99 commit 2ace55f
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ enum DeclarationKind {
/** See {@link ASTAnnotationTypeDeclaration}. */
ANNOTATION,
/** No child, {@link #getDeclarationNode()} will return null. */
EMPTY
EMPTY,
/** See {@link ASTRecordConstructorDeclaration}. */
RECORD_CONSTRUCTOR
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public interface ASTAnyTypeDeclaration extends TypeNode, JavaQualifiableNode, Ac
*/
@Deprecated
enum TypeKind {
CLASS, INTERFACE, ENUM, ANNOTATION;
CLASS, INTERFACE, ENUM, ANNOTATION, RECORD;


public String getPrintableName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*
*/
@Experimental
public class ASTRecordConstructorDeclaration extends AbstractJavaAccessNode {
public class ASTRecordConstructorDeclaration extends AbstractJavaAccessNode implements ASTAnyTypeBodyDeclaration {
ASTRecordConstructorDeclaration(int id) {
super(id);
}
Expand All @@ -36,4 +36,14 @@ public class ASTRecordConstructorDeclaration extends AbstractJavaAccessNode {
public Object jjtAccept(JavaParserVisitor visitor, Object data) {
return visitor.visit(this, data);
}

@Override
public ASTRecordConstructorDeclaration getDeclarationNode() {
return this;
}

@Override
public DeclarationKind getKind() {
return DeclarationKind.RECORD_CONSTRUCTOR;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,12 @@ public Object jjtAccept(JavaParserVisitor visitor, Object data) {

@Override
public TypeKind getTypeKind() {
return null;
return TypeKind.RECORD;
}

@Override
public List<ASTAnyTypeBodyDeclaration> getDeclarations() {
// TODO Auto-generated method stub
return null;
return getFirstChildOfType(ASTRecordBody.class).findChildrenOfType(ASTAnyTypeBodyDeclaration.class);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import net.sourceforge.pmd.lang.java.ast.ASTFormalParameters;
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTMethodOrConstructorDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTRecordDeclaration;

/**
* @author Clément Fournier
Expand Down Expand Up @@ -70,6 +71,8 @@ public static String kindName(ASTAnyTypeDeclaration decl) {
return "annotation";
} else if (decl instanceof ASTEnumDeclaration) {
return "enum";
} else if (decl instanceof ASTRecordDeclaration) {
return "record";
}
return "class";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,13 @@ private static boolean isGetterOrSetter(ASTMethodDeclaration node) {
.getNode()
.getFirstParentOfType(ASTFieldDeclaration.class);

// the field might be null in record types - the fields for the record components are synthetic
if (field != null) {
Matcher matcher = FIELD_NAME_PATTERN.matcher(field.getVariableName());
String varName = matcher.find() ? matcher.group(1) : field.getVariableName();

Matcher matcher = FIELD_NAME_PATTERN.matcher(field.getVariableName());
String varName = matcher.find() ? matcher.group(1) : field.getVariableName();

fieldNames.put(varName, field.getFirstChildOfType(ASTType.class).getTypeImage());
fieldNames.put(varName, field.getFirstChildOfType(ASTType.class).getTypeImage());
}
}

return isGetter(node, fieldNames) || isSetter(node, fieldNames);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ public void innerRecords() {
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.assertTrue(complex.getChild(1).getChild(0).getChild(1) instanceof ASTConstructorDeclaration);
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());
Expand All @@ -132,6 +134,7 @@ public void innerRecords() {
List<ASTRecordConstructorDeclaration> rangeConstructors = range.findDescendantsOfType(ASTRecordConstructorDeclaration.class);
Assert.assertEquals(1, rangeConstructors.size());
Assert.assertEquals("Range", rangeConstructors.get(0).getImage());
Assert.assertEquals(2, range.getDeclarations().size());

ASTRecordDeclaration varRec = recordDecls.get(3);
Assert.assertEquals("VarRec", varRec.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public MyComplex(@MyAnnotation int real, int imaginary) {
this.real = real;
this.imaginary = imaginary;
}
public record Nested(int a) {};
public record Nested(int a) {}
}


Expand All @@ -31,11 +31,15 @@ public record Range(int lo, int hi) {
if (lo > hi) /* referring here to the implicit constructor parameters */
throw new IllegalArgumentException(String.format("(%d,%d)", lo, hi));
}

public void foo() { }
}

public record VarRec(@Nullable @Deprecated String @Nullable ... x) {}

public record ArrayRec(int x[]) {}

public record EmptyRec() {}
public record EmptyRec() {
public void foo() { }
}
}

0 comments on commit 2ace55f

Please sign in to comment.