Annotation testing

This commit is contained in:
Katelyn Baker 2017-07-10 11:50:24 +01:00
parent c1cd7d6b79
commit fd3a827438
2 changed files with 33 additions and 5 deletions

View File

@ -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<out Any?>) : Field(field) {
override val nullabilityAnnotation = "Ljavax/annotations/NotNull;"
override val nullabilityAnnotation = "Ljavax/annotation/Nonnull;"
constructor(name: String, field: Class<out Any?>) : this(field) {
this.name = name
@ -141,7 +142,7 @@ class ClassCarpenter {
class NullableField(field: Class<out Any?>) : Field(field) {
override val nullabilityAnnotation = "Ljavax/annotations/Nullable;"
override val nullabilityAnnotation = "Ljavax/annotation/Nullable;"
constructor(name: String, field: Class<out Any?>) : this(field) {
if (field.isPrimitive) {

View File

@ -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)
}
}