From fd3a8274386fe6999086baf9d6fc080119e54c98 Mon Sep 17 00:00:00 2001 From: Katelyn Baker Date: Mon, 10 Jul 2017 11:50:24 +0100 Subject: [PATCH] Annotation testing --- .../serialization/carpenter/ClassCarpenter.kt | 11 ++++---- .../carpenter/ClassCarpenterTest.kt | 27 +++++++++++++++++++ 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/core/src/main/kotlin/net/corda/core/serialization/carpenter/ClassCarpenter.kt b/core/src/main/kotlin/net/corda/core/serialization/carpenter/ClassCarpenter.kt index 784cc5d0f5..2544727a9c 100644 --- a/core/src/main/kotlin/net/corda/core/serialization/carpenter/ClassCarpenter.kt +++ b/core/src/main/kotlin/net/corda/core/serialization/carpenter/ClassCarpenter.kt @@ -90,20 +90,21 @@ class ClassCarpenter { get() = if (this.field.isPrimitive) this.descriptor else "Ljava/lang/Object;" fun generateField(cw: ClassWriter) { + println ("generateField $name $nullabilityAnnotation") val fieldVisitor = cw.visitField(ACC_PROTECTED + ACC_FINAL, name, descriptor, null, null) - cw.visitAnnotation(nullabilityAnnotation, false).visitEnd() + fieldVisitor.visitAnnotation(nullabilityAnnotation, true).visitEnd() fieldVisitor.visitEnd() } fun addNullabilityAnnotation(mv: MethodVisitor) { - mv.visitAnnotation(nullabilityAnnotation, false) + mv.visitAnnotation(nullabilityAnnotation, true).visitEnd() } fun visitParameter(mv: MethodVisitor, idx: Int) { with(mv) { visitParameter(name, 0) if (!field.isPrimitive) { - visitParameterAnnotation(idx, nullabilityAnnotation, false).visitEnd() + visitParameterAnnotation(idx, nullabilityAnnotation, true).visitEnd() } } } @@ -113,7 +114,7 @@ class ClassCarpenter { } class NonNullableField(field: Class) : Field(field) { - override val nullabilityAnnotation = "Ljavax/annotations/NotNull;" + override val nullabilityAnnotation = "Ljavax/annotation/Nonnull;" constructor(name: String, field: Class) : this(field) { this.name = name @@ -141,7 +142,7 @@ class ClassCarpenter { class NullableField(field: Class) : Field(field) { - override val nullabilityAnnotation = "Ljavax/annotations/Nullable;" + override val nullabilityAnnotation = "Ljavax/annotation/Nullable;" constructor(name: String, field: Class) : this(field) { if (field.isPrimitive) { diff --git a/core/src/test/kotlin/net/corda/core/serialization/carpenter/ClassCarpenterTest.kt b/core/src/test/kotlin/net/corda/core/serialization/carpenter/ClassCarpenterTest.kt index ef9d75f640..6dc4f5b12a 100644 --- a/core/src/test/kotlin/net/corda/core/serialization/carpenter/ClassCarpenterTest.kt +++ b/core/src/test/kotlin/net/corda/core/serialization/carpenter/ClassCarpenterTest.kt @@ -5,6 +5,7 @@ import org.junit.Test import java.lang.reflect.Field import java.lang.reflect.Method import kotlin.test.assertEquals +import kotlin.test.assertTrue class ClassCarpenterTest { @@ -464,4 +465,30 @@ class ClassCarpenterTest { assertEquals("some pickles", arr2[0]) assertEquals("some fries", arr2[1]) } + + @Test + fun `nullable sets annotations`() { + val className = "iEnjoyJam" + val schema = ClassCarpenter.ClassSchema( + "gen.$className", + mapOf("a" to ClassCarpenter.NullableField(String::class.java), + "b" to ClassCarpenter.NonNullableField(String::class.java))) + + val clazz = cc.build(schema) + + assertEquals (2, clazz.declaredFields.size) + + assertEquals (1, clazz.getDeclaredField("a").annotations.size) + assertEquals (javax.annotation.Nullable::class.java, clazz.getDeclaredField("a").annotations[0].annotationClass.java) + + assertEquals (1, clazz.getDeclaredField("b").annotations.size) + assertEquals (javax.annotation.Nonnull::class.java, clazz.getDeclaredField("b").annotations[0].annotationClass.java) + + assertEquals (1, clazz.getMethod("getA").annotations.size) + assertEquals (javax.annotation.Nullable::class.java, clazz.getMethod("getA").annotations[0].annotationClass.java) + + assertEquals (1, clazz.getMethod("getB").annotations.size) + assertEquals (javax.annotation.Nonnull::class.java, clazz.getMethod("getB").annotations[0].annotationClass.java) + } + }