[go: nahoru, domu]

blob: 5a00fca9a6fda3ab905eb1383513665d06895c0d [file] [log] [blame]
Yigit Boyar949cae52020-07-07 08:12:37 -07001/*
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
17package androidx.room.processing
18
19import androidx.room.processing.javac.JavacElement
20import androidx.room.processing.javac.JavacProcessingEnv
21import com.google.auto.common.BasicAnnotationProcessor
22import com.google.auto.common.MoreElements
23import com.google.common.collect.SetMultimap
24import javax.lang.model.element.Element
25import javax.tools.Diagnostic
26import 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 */
34interface XProcessingStep {
Yigit Boyar4aff8762020-07-20 19:00:19 -070035 /**
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 Boyar949cae52020-07-07 08:12:37 -070043 fun process(
44 env: XProcessingEnv,
45 elementsByAnnotation: Map<KClass<out Annotation>, List<XTypeElement>>
46 ): Set<XTypeElement>
47
Yigit Boyar4aff8762020-07-20 19:00:19 -070048 /**
49 * The set of annotations processed by this step.
50 */
Yigit Boyar949cae52020-07-07 08:12:37 -070051 fun annotations(): Set<KClass<out Annotation>>
52
Yigit Boyar4aff8762020-07-20 19:00:19 -070053 /**
54 * Wraps current [XProcessingStep] into an Auto Common
55 * [BasicAnnotationProcessor.ProcessingStep].
56 */
Yigit Boyarb25a5682020-07-17 16:07:22 -070057 fun asAutoCommonProcessor(
Yigit Boyar949cae52020-07-07 08:12:37 -070058 env: XProcessingEnv
59 ): BasicAnnotationProcessor.ProcessingStep {
60 return JavacProcessingStepDelegate(
61 env = env as JavacProcessingEnv,
62 delegate = this
63 )
64 }
65}
66
67@Suppress("UnstableApiUsage")
68internal 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}