xref: /aosp_15_r20/external/dagger2/javatests/dagger/internal/codegen/Compilers.java (revision f585d8a307d0621d6060bd7e80091fdcbf94fe27)
1 /*
2  * Copyright (C) 2016 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 com.google.common.base.StandardSystemProperty.JAVA_CLASS_PATH;
20 import static com.google.common.base.StandardSystemProperty.PATH_SEPARATOR;
21 import static com.google.testing.compile.Compiler.javac;
22 import static java.util.stream.Collectors.collectingAndThen;
23 import static java.util.stream.Collectors.toList;
24 
25 import com.google.auto.value.processor.AutoAnnotationProcessor;
26 import com.google.common.base.Splitter;
27 import com.google.common.collect.ImmutableList;
28 import com.google.testing.compile.Compiler;
29 import java.io.File;
30 import java.util.Arrays;
31 import javax.annotation.processing.Processor;
32 
33 /** {@link Compiler} instances for testing Dagger. */
34 public final class Compilers {
35   private static final String GUAVA = "guava";
36 
37   static final ImmutableList<File> CLASS_PATH_WITHOUT_GUAVA_OPTION =
38       Splitter.on(PATH_SEPARATOR.value()).splitToList(JAVA_CLASS_PATH.value()).stream()
39           .filter(jar -> !jar.contains(GUAVA))
40           // Remove Bazel's runner deploy jar which leaks Guava classes into the classpath and
41           // the compile testing tests.
42           .filter(jar -> !jar.contains("Runner_deploy.jar"))
43           // Remove the kotlin-compiler jar which leaks Guava classes into the classpath and
44           // the compile testing tests.
45           .filter(jar -> !jar.contains("kotlin-compiler.jar"))
46           .map(File::new)
47           .collect(collectingAndThen(toList(), ImmutableList::copyOf));
48 
49   static final ImmutableList<String> DEFAULT_JAVACOPTS =
50       ImmutableList.of(
51           "-Adagger.experimentalDaggerErrorMessages=enabled");
52 
53   /**
54    * Returns a compiler that runs the Dagger and {@code @AutoAnnotation} processors, along with
55    * extras.
56    */
daggerCompiler(Processor... extraProcessors)57   public static Compiler daggerCompiler(Processor... extraProcessors) {
58     ImmutableList.Builder<Processor> processors = ImmutableList.builder();
59     processors.add(new ComponentProcessor(), new AutoAnnotationProcessor());
60     processors.add(extraProcessors);
61     return javac().withProcessors(processors.build()).withOptions(DEFAULT_JAVACOPTS);
62   }
63 
compilerWithOptions(CompilerMode... compilerModes)64   public static Compiler compilerWithOptions(CompilerMode... compilerModes) {
65     ImmutableList.Builder<String> options = ImmutableList.builder();
66     for (CompilerMode compilerMode : compilerModes) {
67       options = options.addAll(compilerMode.javacopts());
68     }
69     return compilerWithOptions(options.build());
70   }
71 
compilerWithOptions(String... options)72   public static Compiler compilerWithOptions(String... options) {
73     return compilerWithOptions(Arrays.asList(options));
74   }
75 
compilerWithOptions(Iterable<String> options)76   public static Compiler compilerWithOptions(Iterable<String> options) {
77     return daggerCompiler()
78         .withOptions(ImmutableList.builder().addAll(DEFAULT_JAVACOPTS).addAll(options).build());
79   }
80 
Compilers()81   private Compilers() {}
82 }
83