From 2ace55f635a9ddd655d932737c11266b3858207c Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Fri, 28 Feb 2020 18:59:16 +0100 Subject: [PATCH] [java] Support RecordConstructorDeclaration as AnyTypeBodyDeclaration --- .../pmd/lang/java/ast/ASTAnyTypeBodyDeclaration.java | 4 +++- .../pmd/lang/java/ast/ASTAnyTypeDeclaration.java | 2 +- .../java/ast/ASTRecordConstructorDeclaration.java | 12 +++++++++++- .../pmd/lang/java/ast/ASTRecordDeclaration.java | 5 ++--- .../lang/java/ast/internal/PrettyPrintingUtil.java | 3 +++ .../multifile/signature/JavaOperationSignature.java | 10 ++++++---- .../pmd/lang/java/ast/Java14PreviewTest.java | 5 ++++- .../java/ast/jdkversiontests/java14/Records.java | 8 ++++++-- 8 files changed, 36 insertions(+), 13 deletions(-) diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnyTypeBodyDeclaration.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnyTypeBodyDeclaration.java index 5196b3f9934..74465075ae7 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnyTypeBodyDeclaration.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnyTypeBodyDeclaration.java @@ -58,7 +58,9 @@ enum DeclarationKind { /** See {@link ASTAnnotationTypeDeclaration}. */ ANNOTATION, /** No child, {@link #getDeclarationNode()} will return null. */ - EMPTY + EMPTY, + /** See {@link ASTRecordConstructorDeclaration}. */ + RECORD_CONSTRUCTOR } } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnyTypeDeclaration.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnyTypeDeclaration.java index 9ceaa09653e..2c39b1dbf81 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnyTypeDeclaration.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnyTypeDeclaration.java @@ -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() { 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 bbdcbc28301..9ab85f673b3 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 @@ -23,7 +23,7 @@ * */ @Experimental -public class ASTRecordConstructorDeclaration extends AbstractJavaAccessNode { +public class ASTRecordConstructorDeclaration extends AbstractJavaAccessNode implements ASTAnyTypeBodyDeclaration { ASTRecordConstructorDeclaration(int id) { super(id); } @@ -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; + } } 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 aeb882f8845..6cc2355ef48 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 @@ -44,13 +44,12 @@ public Object jjtAccept(JavaParserVisitor visitor, Object data) { @Override public TypeKind getTypeKind() { - return null; + return TypeKind.RECORD; } @Override public List getDeclarations() { - // TODO Auto-generated method stub - return null; + return getFirstChildOfType(ASTRecordBody.class).findChildrenOfType(ASTAnyTypeBodyDeclaration.class); } @Override diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/internal/PrettyPrintingUtil.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/internal/PrettyPrintingUtil.java index c79eb94ddfd..ca16c0d4736 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/internal/PrettyPrintingUtil.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/internal/PrettyPrintingUtil.java @@ -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 @@ -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"; } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/multifile/signature/JavaOperationSignature.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/multifile/signature/JavaOperationSignature.java index 2c20589f93a..a41a066c1e0 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/multifile/signature/JavaOperationSignature.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/multifile/signature/JavaOperationSignature.java @@ -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); 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 8b1645e6608..78013f6c00c 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 @@ -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()); @@ -132,6 +134,7 @@ public void innerRecords() { List 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()); diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java14/Records.java b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java14/Records.java index 4135ed7044d..cdd93fc7cd1 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java14/Records.java +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java14/Records.java @@ -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) {} } @@ -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() { } + } }