1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package androidx.room.testing 18 19 import javax.annotation.processing.AbstractProcessor 20 import javax.annotation.processing.RoundEnvironment 21 import javax.annotation.processing.SupportedSourceVersion 22 import javax.lang.model.SourceVersion 23 import javax.lang.model.element.TypeElement 24 import kotlin.reflect.KClass 25 26 @SupportedSourceVersion(SourceVersion.RELEASE_8)// test are compiled w/ J_8 27 class TestProcessor(val handlers: List<(TestInvocation) -> Boolean>, 28 val annotations: MutableSet<String>) : AbstractProcessor() { 29 var count = 0 processnull30 override fun process( 31 annotations: MutableSet<out TypeElement>, roundEnv: RoundEnvironment): Boolean { 32 return handlers.getOrNull(count++)?.invoke( 33 TestInvocation(processingEnv, annotations, roundEnv)) ?: true 34 } 35 getSupportedAnnotationTypesnull36 override fun getSupportedAnnotationTypes(): MutableSet<String> { 37 return annotations 38 } 39 40 class Builder { 41 private var handlers = arrayListOf<(TestInvocation) -> Boolean>() 42 private var annotations = mutableSetOf<String>() nextRunHandlernull43 fun nextRunHandler(f: (TestInvocation) -> Boolean): Builder { 44 handlers.add(f) 45 return this 46 } 47 forAnnotationsnull48 fun forAnnotations(vararg klasses: KClass<*>): Builder { 49 annotations.addAll(klasses.map { it.java.canonicalName }) 50 return this 51 } 52 buildnull53 fun build(): TestProcessor { 54 if (annotations.isEmpty()) { 55 throw IllegalStateException("must provide at least 1 annotation") 56 } 57 if (handlers.isEmpty()) { 58 throw IllegalStateException("must provide at least 1 handler") 59 } 60 return TestProcessor(handlers, annotations) 61 } 62 } 63 64 companion object { buildernull65 fun builder(): Builder = Builder() 66 } 67 } 68