xref: /aosp_15_r20/external/dagger2/javatests/dagger/internal/codegen/GeneratingProcessingStep.java (revision f585d8a307d0621d6060bd7e80091fdcbf94fe27)
1 /*
2  * Copyright (C) 2022 The Dagger Authors.
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 dagger.internal.codegen;
18 
19 import static androidx.room.compiler.processing.JavaPoetExtKt.addOriginatingElement;
20 
21 import androidx.room.compiler.processing.XElement;
22 import androidx.room.compiler.processing.XFiler;
23 import androidx.room.compiler.processing.XProcessingEnv;
24 import androidx.room.compiler.processing.XProcessingStep;
25 import com.google.common.collect.ImmutableSet;
26 import com.squareup.javapoet.JavaFile;
27 import com.squareup.javapoet.TypeName;
28 import com.squareup.javapoet.TypeSpec;
29 import java.util.Map;
30 import java.util.Set;
31 
32 /** A simple {@link XProcessingStep} that generates one source file. */
33 final class GeneratingProcessingStep implements XProcessingStep {
34   private final String pkgName;
35   private final TypeSpec typeSpec;
36 
37   // TODO(bcorso): Ideally we'd be able to pass in a Source rather than a TypeSpec for tests, but
38   // that would require XFiler supporting more generic writing of source files
GeneratingProcessingStep(String pkgName, TypeSpec typeSpec)39   GeneratingProcessingStep(String pkgName, TypeSpec typeSpec) {
40     this.pkgName = pkgName;
41     this.typeSpec = typeSpec;
42   }
43 
44   @Override
annotations()45   public final ImmutableSet<String> annotations() {
46     // TODO(b/249322175): Replace this with "*" after this bug is fixed.
47     // For now, we just trigger off of annotations in the other sources in the test, but ideally
48     // this should support "*" similar to javac's Processor.
49     return ImmutableSet.of("dagger.Component");
50   }
51 
52   @Override
process( XProcessingEnv env, Map<String, ? extends Set<? extends XElement>> elementsByAnnotation)53   public ImmutableSet<XElement> process(
54       XProcessingEnv env, Map<String, ? extends Set<? extends XElement>> elementsByAnnotation) {
55     String generatedClassName = String.format("%s.%s", pkgName, typeSpec.name);
56     if (env.findTypeElement(generatedClassName) == null) {
57       // Add an arbitrary orginating element, otherwise XProcessing will output a warning in KSP.
58       TypeSpec.Builder builder = typeSpec.toBuilder();
59       addOriginatingElement(builder, env.requireTypeElement(TypeName.OBJECT));
60       env.getFiler()
61           .write(JavaFile.builder(pkgName, builder.build()).build(), XFiler.Mode.Isolating);
62     }
63     return ImmutableSet.of();
64   }
65 
66   @Override
processOver( XProcessingEnv env, Map<String, ? extends Set<? extends XElement>> elementsByAnnotation)67   public void processOver(
68       XProcessingEnv env, Map<String, ? extends Set<? extends XElement>> elementsByAnnotation) {}
69 }
70