• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   FAST_INIT_MODE("-Adagger.fastInit=enabled");
32 
33   /** Returns the compiler modes as a list of parameters for parameterized tests */
34   public static final ImmutableList<Object[]> TEST_PARAMETERS =
35       ImmutableList.copyOf(
36           new Object[][] {{CompilerMode.DEFAULT_MODE}, {CompilerMode.FAST_INIT_MODE}});
37 
38   private final ImmutableList<String> javacopts;
39 
CompilerMode(String... javacopts)40   private CompilerMode(String... javacopts) {
41     this.javacopts = ImmutableList.copyOf(javacopts);
42   }
43 
44   /**
45    * Returns the javacopts as a map of key-value pairs.
46    *
47    * @throws IllegalStateException if the javacopts are not of the form "-Akey=value".
48    */
processorOptions()49   public ImmutableMap<String, String> processorOptions() {
50     ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
51     for (String javacopt : javacopts) {
52       // Throw if there's a javacopt in this mode that is not an annotation processor option.
53       checkState(javacopt.startsWith("-A"));
54       List<String> splits = Splitter.on('=').splitToList(javacopt.substring(2));
55       checkState(splits.size() == 2);
56       builder.put(splits.get(0), splits.get(1));
57     }
58     return builder.buildOrThrow();
59   }
60 
61   /** Returns the javacopts for this compiler mode. */
javacopts()62   public FluentIterable<String> javacopts() {
63     return FluentIterable.from(javacopts);
64   }
65 }
66