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.javac; 18 19 import androidx.room.compiler.processing.XMessager; 20 import androidx.room.compiler.processing.XProcessingEnv; 21 import com.sun.tools.javac.model.JavacElements; 22 import com.sun.tools.javac.model.JavacTypes; 23 import com.sun.tools.javac.util.Context; 24 import dagger.Binds; 25 import dagger.Module; 26 import dagger.Provides; 27 import dagger.internal.codegen.compileroption.CompilerOptions; 28 import javax.lang.model.util.Elements; // ALLOW_TYPES_ELEMENTS 29 import javax.lang.model.util.Types; // ALLOW_TYPES_ELEMENTS 30 31 /** 32 * A module that provides a {@link XMessager}, {@link XProcessingEnv}, and {@link CompilerOptions} 33 * for use in {@code javac} plugins. Requires a binding for the {@code javac} {@link Context}. 34 */ 35 @Module(includes = JavacPluginModule.BindsModule.class) 36 public final class JavacPluginModule { 37 @Module 38 interface BindsModule { 39 @Binds compilerOptions(JavacPluginCompilerOptions compilerOptions)40 CompilerOptions compilerOptions(JavacPluginCompilerOptions compilerOptions); 41 } 42 43 private final XProcessingEnv processingEnv; 44 JavacPluginModule(Context context)45 public JavacPluginModule(Context context) { 46 this(JavacElements.instance(context), JavacTypes.instance(context)); 47 } 48 JavacPluginModule(Elements elements, Types types)49 public JavacPluginModule(Elements elements, Types types) { 50 this.processingEnv = 51 XProcessingEnv.create(new JavacPluginProcessingEnvironment(elements, types)); 52 } 53 54 @Provides messager()55 XMessager messager() { 56 return processingEnv.getMessager(); 57 } 58 59 @Provides xProcessingEnv()60 XProcessingEnv xProcessingEnv() { 61 return processingEnv; 62 } 63 } 64