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 dagger.internal.codegen.ValidationType.NONE; 20 import static javax.tools.Diagnostic.Kind.NOTE; 21 22 import com.sun.tools.javac.model.JavacElements; 23 import com.sun.tools.javac.model.JavacTypes; 24 import com.sun.tools.javac.util.Context; 25 import dagger.Binds; 26 import dagger.Module; 27 import dagger.Provides; 28 import dagger.internal.codegen.langmodel.DaggerElements; 29 import dagger.internal.codegen.langmodel.DaggerTypes; 30 import javax.annotation.processing.Messager; 31 import javax.inject.Inject; 32 import javax.lang.model.element.AnnotationMirror; 33 import javax.lang.model.element.AnnotationValue; 34 import javax.lang.model.element.Element; 35 import javax.lang.model.element.TypeElement; 36 import javax.lang.model.util.Types; 37 import javax.tools.Diagnostic; 38 39 /** 40 * A module that provides a {@link BindingGraphFactory} and {@link ComponentDescriptorFactory} for 41 * use in {@code javac} plugins. Requires a binding for the {@code javac} {@link Context}. 42 */ 43 @Module(includes = InjectBindingRegistryModule.class) 44 abstract class JavacPluginModule { 45 @Provides compilerOptions()46 static CompilerOptions compilerOptions() { 47 return new CompilerOptions() { 48 @Override 49 boolean usesProducers() { 50 return true; 51 } 52 53 @Override 54 boolean fastInit() { 55 return false; 56 } 57 58 @Override 59 boolean formatGeneratedSource() { 60 return false; 61 } 62 63 @Override 64 boolean writeProducerNameInToken() { 65 return true; 66 } 67 68 @Override 69 Diagnostic.Kind nullableValidationKind() { 70 return NOTE; 71 } 72 73 @Override 74 Diagnostic.Kind privateMemberValidationKind() { 75 return NOTE; 76 } 77 78 @Override 79 Diagnostic.Kind staticMemberValidationKind() { 80 return NOTE; 81 } 82 83 @Override 84 boolean ignorePrivateAndStaticInjectionForComponent() { 85 return false; 86 } 87 88 @Override 89 ValidationType scopeCycleValidationType() { 90 return NONE; 91 } 92 93 @Override 94 boolean warnIfInjectionFactoryNotGeneratedUpstream() { 95 return false; 96 } 97 98 @Override 99 boolean headerCompilation() { 100 return false; 101 } 102 103 @Override 104 boolean aheadOfTimeSubcomponents() { 105 return false; 106 } 107 108 @Override 109 boolean forceUseSerializedComponentImplementations() { 110 return false; 111 } 112 113 @Override 114 boolean emitModifiableMetadataAnnotations() { 115 return false; 116 } 117 118 @Override 119 boolean useGradleIncrementalProcessing() { 120 return false; 121 } 122 123 @Override 124 ValidationType fullBindingGraphValidationType(TypeElement element) { 125 return NONE; 126 } 127 128 @Override 129 Diagnostic.Kind moduleHasDifferentScopesDiagnosticKind() { 130 return NOTE; 131 } 132 133 @Override 134 ValidationType explicitBindingConflictsWithInjectValidationType() { 135 return NONE; 136 } 137 }; 138 } 139 140 @Binds 141 abstract Messager messager(NullMessager nullMessager); 142 143 static final class NullMessager implements Messager { 144 145 @Inject 146 NullMessager() {} 147 148 @Override 149 public void printMessage(Diagnostic.Kind kind, CharSequence charSequence) {} 150 151 @Override 152 public void printMessage(Diagnostic.Kind kind, CharSequence charSequence, Element element) {} 153 154 @Override 155 public void printMessage( 156 Diagnostic.Kind kind, 157 CharSequence charSequence, 158 Element element, 159 AnnotationMirror annotationMirror) {} 160 161 @Override 162 public void printMessage( 163 Diagnostic.Kind kind, 164 CharSequence charSequence, 165 Element element, 166 AnnotationMirror annotationMirror, 167 AnnotationValue annotationValue) {} 168 } 169 170 @Provides 171 static DaggerElements daggerElements(Context javaContext) { 172 return new DaggerElements( 173 JavacElements.instance(javaContext), JavacTypes.instance(javaContext)); 174 } 175 176 @Provides 177 static DaggerTypes daggerTypes(Context javaContext, DaggerElements elements) { 178 return new DaggerTypes(JavacTypes.instance(javaContext), elements); 179 } 180 181 @Binds abstract Types types(DaggerTypes daggerTypes); 182 183 private JavacPluginModule() {} 184 } 185