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