• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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           .map(File::new)
44           .collect(collectingAndThen(toList(), ImmutableList::copyOf));
45 
46   static final ImmutableList<String> DEFAULT_JAVACOPTS =
47       ImmutableList.of("-Adagger.experimentalDaggerErrorMessages=enabled");
48 
49   /**
50    * Returns a compiler that runs the Dagger and {@code @AutoAnnotation} processors, along with
51    * extras.
52    */
daggerCompiler(Processor... extraProcessors)53   public static Compiler daggerCompiler(Processor... extraProcessors) {
54     ImmutableList.Builder<Processor> processors = ImmutableList.builder();
55     processors.add(new ComponentProcessor(), new AutoAnnotationProcessor());
56     processors.add(extraProcessors);
57     return javac().withProcessors(processors.build()).withOptions(DEFAULT_JAVACOPTS);
58   }
59 
compilerWithOptions(CompilerMode... compilerModes)60   public static Compiler compilerWithOptions(CompilerMode... compilerModes) {
61     ImmutableList.Builder<String> options = ImmutableList.builder();
62     for (CompilerMode compilerMode : compilerModes) {
63       options = options.addAll(compilerMode.javacopts());
64     }
65     return compilerWithOptions(options.build());
66   }
67 
compilerWithOptions(String... options)68   public static Compiler compilerWithOptions(String... options) {
69     return compilerWithOptions(Arrays.asList(options));
70   }
71 
compilerWithOptions(Iterable<String> options)72   public static Compiler compilerWithOptions(Iterable<String> options) {
73     return daggerCompiler()
74         .withOptions(ImmutableList.builder().addAll(DEFAULT_JAVACOPTS).addAll(options).build());
75   }
76 
Compilers()77   private Compilers() {}
78 }
79