xref: /aosp_15_r20/external/dagger2/javatests/dagger/internal/codegen/CompilerMode.java (revision f585d8a307d0621d6060bd7e80091fdcbf94fe27)
1 /*
2  * Copyright (C) 2017 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.Preconditions.checkState;
20 
21 import com.google.common.base.Splitter;
22 import com.google.common.collect.FluentIterable;
23 import com.google.common.collect.ImmutableList;
24 import com.google.common.collect.ImmutableMap;
25 import java.util.List;
26 
27 /** The configuration options for compiler modes. */
28 // TODO(bcorso): Consider moving the java version into its own separate enum.
29 public enum CompilerMode {
30   DEFAULT_MODE,
31   DEFAULT_JAVA7_MODE("-source", "7", "-target", "7"),
32   FAST_INIT_MODE("-Adagger.fastInit=enabled"),
33   FAST_INIT_JAVA7_MODE("-Adagger.fastInit=enabled", "-source", "7", "-target", "7"),
34   ;
35 
36   /** Returns the compiler modes as a list of parameters for parameterized tests */
37   public static final ImmutableList<Object[]> TEST_PARAMETERS =
38       ImmutableList.copyOf(
39           new Object[][] {{CompilerMode.DEFAULT_MODE}, {CompilerMode.FAST_INIT_MODE}});
40 
41   private final ImmutableList<String> javacopts;
42 
CompilerMode(String... javacopts)43   private CompilerMode(String... javacopts) {
44     this.javacopts = ImmutableList.copyOf(javacopts);
45   }
46 
47   /**
48    * Returns the javacopts as a map of key-value pairs.
49    *
50    * @throws IllegalStateException if the javacopts are not of the form "-Akey=value".
51    */
processorOptions()52   public ImmutableMap<String, String> processorOptions() {
53     ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
54     for (String javacopt : javacopts) {
55       // Throw if there's a javacopt in this mode that is not an annotation processor option.
56       checkState(javacopt.startsWith("-A"));
57       List<String> splits = Splitter.on('=').splitToList(javacopt.substring(2));
58       checkState(splits.size() == 2);
59       builder.put(splits.get(0), splits.get(1));
60     }
61     return builder.buildOrThrow();
62   }
63 
64   /** Returns the javacopts for this compiler mode. */
javacopts()65   public FluentIterable<String> javacopts() {
66     return FluentIterable.from(javacopts);
67   }
68 }
69