Yigit Boyar | 949cae5 | 2020-07-07 08:12:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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.processing |
| 18 | |
| 19 | import androidx.room.processing.javac.JavacElement |
| 20 | import androidx.room.processing.javac.JavacProcessingEnv |
| 21 | import com.google.auto.common.BasicAnnotationProcessor |
| 22 | import com.google.auto.common.MoreElements |
| 23 | import com.google.common.collect.SetMultimap |
| 24 | import javax.lang.model.element.Element |
| 25 | import javax.tools.Diagnostic |
| 26 | import kotlin.reflect.KClass |
| 27 | |
| 28 | /** |
| 29 | * Specialized processing step which only supports annotations on TypeElements. |
| 30 | * |
| 31 | * We can generalize it but for now, Room only needs annotations on TypeElements to start |
| 32 | * processing. |
| 33 | */ |
| 34 | interface XProcessingStep { |
Yigit Boyar | 4aff876 | 2020-07-20 19:00:19 -0700 | [diff] [blame^] | 35 | /** |
| 36 | * The implementation of processing logic for the step. It is guaranteed that the keys in |
| 37 | * [elementsByAnnotation] will be a subset of the set returned by [annotations]. |
| 38 | * |
| 39 | * @return the elements (a subset of the values of [elementsByAnnotation]) that this step |
| 40 | * is unable to process, possibly until a later processing round. These elements will be |
| 41 | * passed back to this step at the next round of processing. |
| 42 | */ |
Yigit Boyar | 949cae5 | 2020-07-07 08:12:37 -0700 | [diff] [blame] | 43 | fun process( |
| 44 | env: XProcessingEnv, |
| 45 | elementsByAnnotation: Map<KClass<out Annotation>, List<XTypeElement>> |
| 46 | ): Set<XTypeElement> |
| 47 | |
Yigit Boyar | 4aff876 | 2020-07-20 19:00:19 -0700 | [diff] [blame^] | 48 | /** |
| 49 | * The set of annotations processed by this step. |
| 50 | */ |
Yigit Boyar | 949cae5 | 2020-07-07 08:12:37 -0700 | [diff] [blame] | 51 | fun annotations(): Set<KClass<out Annotation>> |
| 52 | |
Yigit Boyar | 4aff876 | 2020-07-20 19:00:19 -0700 | [diff] [blame^] | 53 | /** |
| 54 | * Wraps current [XProcessingStep] into an Auto Common |
| 55 | * [BasicAnnotationProcessor.ProcessingStep]. |
| 56 | */ |
Yigit Boyar | b25a568 | 2020-07-17 16:07:22 -0700 | [diff] [blame] | 57 | fun asAutoCommonProcessor( |
Yigit Boyar | 949cae5 | 2020-07-07 08:12:37 -0700 | [diff] [blame] | 58 | env: XProcessingEnv |
| 59 | ): BasicAnnotationProcessor.ProcessingStep { |
| 60 | return JavacProcessingStepDelegate( |
| 61 | env = env as JavacProcessingEnv, |
| 62 | delegate = this |
| 63 | ) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | @Suppress("UnstableApiUsage") |
| 68 | internal class JavacProcessingStepDelegate( |
| 69 | val env: JavacProcessingEnv, |
| 70 | val delegate: XProcessingStep |
| 71 | ) : BasicAnnotationProcessor.ProcessingStep { |
| 72 | override fun process( |
| 73 | elementsByAnnotation: SetMultimap<Class<out Annotation>, Element> |
| 74 | ): Set<Element> { |
| 75 | val converted = mutableMapOf<KClass<out Annotation>, List<XTypeElement>>() |
| 76 | annotations().forEach { annotation -> |
| 77 | val elements = elementsByAnnotation[annotation].mapNotNull { element -> |
| 78 | if (MoreElements.isType(element)) { |
| 79 | env.wrapTypeElement(MoreElements.asType(element)) |
| 80 | } else { |
| 81 | env.delegate.messager.printMessage( |
| 82 | Diagnostic.Kind.ERROR, |
| 83 | "Unsupported element type: ${element.kind}", |
| 84 | element |
| 85 | ) |
| 86 | null |
| 87 | } |
| 88 | } |
| 89 | converted[annotation.kotlin] = elements |
| 90 | } |
| 91 | val result = delegate.process(env, converted) |
| 92 | return result.map { |
| 93 | (it as JavacElement).element |
| 94 | }.toSet() |
| 95 | } |
| 96 | |
| 97 | override fun annotations(): Set<Class<out Annotation>> { |
| 98 | return delegate.annotations().mapTo(mutableSetOf()) { |
| 99 | it.java |
| 100 | } |
| 101 | } |
| 102 | } |