Add a test for annotated array type

This commit is contained in:
Clément Fournier committed 2022-11-29 16:17:16 +01:00
1 parent dd657bad4a
commit 71b4e4459e
4 files changed
+35 -9

No files matched your search

@@ -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) {
@@ -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<? extends Annotation> annotationType() {
@@ -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
@@ -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() }
}