• 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.android.processor;
18 
19 import static javax.tools.Diagnostic.Kind.ERROR;
20 import static net.ltgt.gradle.incap.IncrementalAnnotationProcessorType.ISOLATING;
21 
22 import com.google.auto.common.BasicAnnotationProcessor;
23 import com.google.auto.service.AutoService;
24 import com.google.common.base.Ascii;
25 import com.google.common.collect.ImmutableList;
26 import com.google.common.collect.ImmutableSet;
27 import com.google.googlejavaformat.java.filer.FormattingFiler;
28 import java.util.Set;
29 import javax.annotation.processing.Filer;
30 import javax.annotation.processing.Messager;
31 import javax.annotation.processing.Processor;
32 import javax.lang.model.SourceVersion;
33 import javax.lang.model.util.Elements;
34 import javax.lang.model.util.Types;
35 import net.ltgt.gradle.incap.IncrementalAnnotationProcessor;
36 
37 /**
38  * An {@linkplain javax.annotation.processing.Processor annotation processor} to verify usage of
39  * {@code dagger.android} code.
40  *
41  * <p>Additionally, if {@code -Adagger.android.experimentalUseStringKeys} is passed to the
42  * compilation, a file will be generated to support obfuscated injected Android types used with
43  * {@code @AndroidInjectionKey}. The fact that this is generated is deliberate: not all versions of
44  * ProGuard/R8 support {@code -identifiernamestring}, so we can't include a ProGuard file in the
45  * dagger-android artifact Instead, we generate the file in {@code META-INF/proguard} only when
46  * users enable the flag. They should only be enabling it if their shrinker supports those files,
47  * and any version that does so will also support {@code -identifiernamestring}. This was added to
48  * R8 in <a href="https://r8.googlesource.com/r8/+/389123dfcc11e6dda0eec31ab62e1b7eb0da80d2">May
49  * 2018</a>.
50  */
51 @IncrementalAnnotationProcessor(ISOLATING)
52 @AutoService(Processor.class)
53 public final class AndroidProcessor extends BasicAnnotationProcessor {
54   private static final String FLAG_EXPERIMENTAL_USE_STRING_KEYS =
55       "dagger.android.experimentalUseStringKeys";
56 
57   @Override
steps()58   protected Iterable<? extends Step> steps() {
59     Filer filer = new FormattingFiler(processingEnv.getFiler());
60     Messager messager = processingEnv.getMessager();
61     Elements elements = processingEnv.getElementUtils();
62     Types types = processingEnv.getTypeUtils();
63 
64     return ImmutableList.of(
65         new AndroidMapKeyValidator(elements, types, messager),
66         new ContributesAndroidInjectorGenerator(
67             new AndroidInjectorDescriptor.Validator(messager),
68             useStringKeys(),
69             filer,
70             elements,
71             processingEnv.getSourceVersion()));
72   }
73 
useStringKeys()74   private boolean useStringKeys() {
75     if (!processingEnv.getOptions().containsKey(FLAG_EXPERIMENTAL_USE_STRING_KEYS)) {
76       return false;
77     }
78     String flagValue = processingEnv.getOptions().get(FLAG_EXPERIMENTAL_USE_STRING_KEYS);
79     if (flagValue == null || Ascii.equalsIgnoreCase(flagValue, "true")) {
80       return true;
81     } else if (Ascii.equalsIgnoreCase(flagValue, "false")) {
82       return false;
83     } else {
84       processingEnv
85           .getMessager()
86           .printMessage(
87               ERROR,
88               String.format(
89                   "Unknown flag value: %s. %s must be set to either 'true' or 'false'.",
90                   flagValue, FLAG_EXPERIMENTAL_USE_STRING_KEYS));
91       return false;
92     }
93   }
94 
95   @Override
getSupportedOptions()96   public Set<String> getSupportedOptions() {
97     return ImmutableSet.of(FLAG_EXPERIMENTAL_USE_STRING_KEYS);
98   }
99 
100   @Override
getSupportedSourceVersion()101   public SourceVersion getSupportedSourceVersion() {
102     return SourceVersion.latestSupported();
103   }
104 }
105