From 71b4e4459ee584bc653424786a0c2d9c255927f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Tue, 29 Nov 2022 16:17:16 +0100 Subject: [PATCH] Add a test for annotated array type --- .../pmd/lang/java/types/TypeOps.java | 7 ++++- .../symbols/internal/TypeAnnotTestUtil.java | 8 +++--- .../pmd/lang/java/types/TypeCreationDsl.kt | 26 ++++++++++++++++--- .../internal/infer/StandaloneTypesTest.kt | 3 ++- 4 files changed, 35 insertions(+), 9 deletions(-) diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/TypeOps.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/TypeOps.java index f496f6eef2..ada9c6a2e6 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/TypeOps.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/TypeOps.java @@ -130,10 +130,15 @@ public final class TypeOps { @Override public Boolean visit(JTypeMirror t, JTypeMirror s) { - // for primitive & sentinel types + // for sentinel types return t == s; } + @Override + public Boolean visitPrimitive(JPrimitiveType t, JTypeMirror s) { + return s.isPrimitive(t.getKind()); + } + @Override public Boolean visitClass(JClassType t, JTypeMirror s) { if (s instanceof JClassType) { diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/symbols/internal/TypeAnnotTestUtil.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/symbols/internal/TypeAnnotTestUtil.java index 25dd36d46c..5e656596cc 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/symbols/internal/TypeAnnotTestUtil.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/symbols/internal/TypeAnnotTestUtil.java @@ -52,15 +52,15 @@ public class TypeAnnotTestUtil { assertThat(t.getTypeAnnotations(), Matchers.hasItems(annots.stream().map(TypeAnnotTestUtil::matchesAnnot).toArray(Matcher[]::new))); } - static final class AnnotAImpl implements ClassWithTypeAnnotationsInside.A { + public static final class AnnotAImpl implements ClassWithTypeAnnotationsInside.A { private final int val; - AnnotAImpl(int val) { + public AnnotAImpl(int val) { this.val = val; } - AnnotAImpl() { + public AnnotAImpl() { this.val = 1; // the default declared in interface } @@ -80,7 +80,7 @@ public class TypeAnnotTestUtil { } } - static final class AnnotBImpl implements ClassWithTypeAnnotationsInside.B { + public static final class AnnotBImpl implements ClassWithTypeAnnotationsInside.B { @Override public Class annotationType() { diff --git a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/types/TypeCreationDsl.kt b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/types/TypeCreationDsl.kt index eea7c68ec3..551f8c1ef6 100644 --- a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/types/TypeCreationDsl.kt +++ b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/types/TypeCreationDsl.kt @@ -7,7 +7,13 @@ package net.sourceforge.pmd.lang.java.types import net.sourceforge.pmd.lang.java.ast.JavaNode +import net.sourceforge.pmd.lang.java.ast.NodeParsingCtx +import net.sourceforge.pmd.lang.java.ast.ParserTestSpec import net.sourceforge.pmd.lang.java.symbols.JClassSymbol +import net.sourceforge.pmd.lang.java.symbols.SymbolicValue +import net.sourceforge.pmd.lang.java.symbols.SymbolicValue.SymAnnot +import net.sourceforge.pmd.lang.java.symbols.internal.TypeAnnotTestUtil +import net.sourceforge.pmd.lang.java.symbols.testdata.ClassWithTypeAnnotationsInside import net.sourceforge.pmd.lang.java.types.TypeOps.isSameType import kotlin.reflect.KClass @@ -52,14 +58,26 @@ interface TypeDslMixin { val void get() = ts.NO_TYPE + infix fun JTypeMirror.withAnnot(a: SymAnnot) = this.addAnnotation(a) + + val `@A`: SymAnnotDsl + get() = SymAnnotDsl(SymbolicValue.of(ts, TypeAnnotTestUtil.AnnotAImpl()) as SymAnnot) + + class SymAnnotDsl(delegate: SymAnnot) : SymAnnot by delegate { + + operator fun invoke(t: JTypeMirror): JTypeMirror = t.addAnnotation(this) + override fun toString(): String = "@$binaryName" + } + + /** intersection */ operator fun JTypeMirror.times(t: JTypeMirror): JTypeMirror = - ts.glb(listOf(this, t)) + ts.glb(listOf(this, t)) // for some tests we assert whether the intersection is flattened, which doesn't work if we use `a * b * c` fun glb(t1: JTypeMirror, t2: JTypeMirror, vararg tail: JTypeMirror): JTypeMirror = - // flatten - ts.glb(listOf(t1, t2, *tail)) + // flatten + ts.glb(listOf(t1, t2, *tail)) // for some tests we assert whether the intersection is flattened, which doesn't work if we use `a * b * c` fun lub(vararg tail: JTypeMirror): JTypeMirror = @@ -106,6 +124,8 @@ interface TypeDslMixin { } +/** See [TypeDslMixin.@A]. */ +val ParserTestSpec.GroupTestCtx.VersionedTestCtx.ImplicitNodeParsingCtx<*>.AnnotA get() = "@" + ClassWithTypeAnnotationsInside.A::class.java.canonicalName class TypeDslOf(override val ts: TypeSystem) : TypeDslMixin diff --git a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/types/internal/infer/StandaloneTypesTest.kt b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/types/internal/infer/StandaloneTypesTest.kt index 31940dc400..77e32d92f4 100644 --- a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/types/internal/infer/StandaloneTypesTest.kt +++ b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/types/internal/infer/StandaloneTypesTest.kt @@ -47,7 +47,7 @@ class StandaloneTypesTest : ProcessorTestSpec({ } } - parserTest("Test array length") { + parserTest("f:Test array length") { inContext(ExpressionParsingCtx) { @@ -66,6 +66,7 @@ class StandaloneTypesTest : ProcessorTestSpec({ } "new int[0].length" should matchArrayLength { int } + "new $AnnotA int[0].length" should matchArrayLength { `@A`(int) } "new String[0].length" should matchArrayLength { gen.t_String } "new String[0][].length" should matchArrayLength { gen.t_String.toArray() } }