1*f585d8a3SJacky Wang /* 2*f585d8a3SJacky Wang * Copyright (C) 2017 The Dagger Authors. 3*f585d8a3SJacky Wang * 4*f585d8a3SJacky Wang * Licensed under the Apache License, Version 2.0 (the "License"); 5*f585d8a3SJacky Wang * you may not use this file except in compliance with the License. 6*f585d8a3SJacky Wang * You may obtain a copy of the License at 7*f585d8a3SJacky Wang * 8*f585d8a3SJacky Wang * http://www.apache.org/licenses/LICENSE-2.0 9*f585d8a3SJacky Wang * 10*f585d8a3SJacky Wang * Unless required by applicable law or agreed to in writing, software 11*f585d8a3SJacky Wang * distributed under the License is distributed on an "AS IS" BASIS, 12*f585d8a3SJacky Wang * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*f585d8a3SJacky Wang * See the License for the specific language governing permissions and 14*f585d8a3SJacky Wang * limitations under the License. 15*f585d8a3SJacky Wang */ 16*f585d8a3SJacky Wang 17*f585d8a3SJacky Wang package dagger.spi; 18*f585d8a3SJacky Wang 19*f585d8a3SJacky Wang import dagger.model.BindingGraph; 20*f585d8a3SJacky Wang import java.util.Collections; 21*f585d8a3SJacky Wang import java.util.Map; 22*f585d8a3SJacky Wang import java.util.Set; 23*f585d8a3SJacky Wang import javax.annotation.processing.Filer; 24*f585d8a3SJacky Wang import javax.annotation.processing.Messager; 25*f585d8a3SJacky Wang import javax.lang.model.util.Elements; 26*f585d8a3SJacky Wang import javax.lang.model.util.Types; 27*f585d8a3SJacky Wang 28*f585d8a3SJacky Wang /** 29*f585d8a3SJacky Wang * A pluggable visitor for {@link BindingGraph}. 30*f585d8a3SJacky Wang * 31*f585d8a3SJacky Wang * <p>Note: This is still experimental and will change. 32*f585d8a3SJacky Wang */ 33*f585d8a3SJacky Wang public interface BindingGraphPlugin { 34*f585d8a3SJacky Wang /** 35*f585d8a3SJacky Wang * Called once for each valid root binding graph encountered by the Dagger processor. May report 36*f585d8a3SJacky Wang * diagnostics using {@code diagnosticReporter}. 37*f585d8a3SJacky Wang */ visitGraph(BindingGraph bindingGraph, DiagnosticReporter diagnosticReporter)38*f585d8a3SJacky Wang void visitGraph(BindingGraph bindingGraph, DiagnosticReporter diagnosticReporter); 39*f585d8a3SJacky Wang 40*f585d8a3SJacky Wang /** 41*f585d8a3SJacky Wang * Initializes this plugin with a {@link Filer} that it can use to write Java or other files based 42*f585d8a3SJacky Wang * on the binding graph. This will be called once per instance of this plugin, before any graph is 43*f585d8a3SJacky Wang * {@linkplain #visitGraph(BindingGraph, DiagnosticReporter) visited}. 44*f585d8a3SJacky Wang * 45*f585d8a3SJacky Wang * @see javax.annotation.processing.ProcessingEnvironment#getFiler() 46*f585d8a3SJacky Wang */ initFiler(Filer filer)47*f585d8a3SJacky Wang default void initFiler(Filer filer) {} 48*f585d8a3SJacky Wang 49*f585d8a3SJacky Wang /** 50*f585d8a3SJacky Wang * Initializes this plugin with a {@link Types} instance. This will be called once per instance of 51*f585d8a3SJacky Wang * this plugin, before any graph is {@linkplain #visitGraph(BindingGraph, DiagnosticReporter) 52*f585d8a3SJacky Wang * visited}. 53*f585d8a3SJacky Wang * 54*f585d8a3SJacky Wang * @see javax.annotation.processing.ProcessingEnvironment#getTypeUtils() 55*f585d8a3SJacky Wang */ initTypes(Types types)56*f585d8a3SJacky Wang default void initTypes(Types types) {} 57*f585d8a3SJacky Wang 58*f585d8a3SJacky Wang /** 59*f585d8a3SJacky Wang * Initializes this plugin with a {@link Elements} instance. This will be called once per instance 60*f585d8a3SJacky Wang * of this plugin, before any graph is {@linkplain #visitGraph(BindingGraph, DiagnosticReporter) 61*f585d8a3SJacky Wang * visited}. 62*f585d8a3SJacky Wang * 63*f585d8a3SJacky Wang * @see javax.annotation.processing.ProcessingEnvironment#getElementUtils() 64*f585d8a3SJacky Wang */ initElements(Elements elements)65*f585d8a3SJacky Wang default void initElements(Elements elements) {} 66*f585d8a3SJacky Wang 67*f585d8a3SJacky Wang /** 68*f585d8a3SJacky Wang * Initializes this plugin with a filtered view of the options passed on the {@code javac} 69*f585d8a3SJacky Wang * command-line for all keys from {@link #supportedOptions()}. This will be called once per 70*f585d8a3SJacky Wang * instance of this plugin, before any graph is {@linkplain #visitGraph(BindingGraph, 71*f585d8a3SJacky Wang * DiagnosticReporter) visited}. 72*f585d8a3SJacky Wang * 73*f585d8a3SJacky Wang * @see javax.annotation.processing.ProcessingEnvironment#getOptions() 74*f585d8a3SJacky Wang */ initOptions(Map<String, String> options)75*f585d8a3SJacky Wang default void initOptions(Map<String, String> options) {} 76*f585d8a3SJacky Wang 77*f585d8a3SJacky Wang /** 78*f585d8a3SJacky Wang * Returns the annotation-processing options that this plugin uses to configure behavior. 79*f585d8a3SJacky Wang * 80*f585d8a3SJacky Wang * @see javax.annotation.processing.Processor#getSupportedOptions() 81*f585d8a3SJacky Wang */ supportedOptions()82*f585d8a3SJacky Wang default Set<String> supportedOptions() { 83*f585d8a3SJacky Wang return Collections.emptySet(); 84*f585d8a3SJacky Wang } 85*f585d8a3SJacky Wang 86*f585d8a3SJacky Wang /** 87*f585d8a3SJacky Wang * A distinguishing name of the plugin that will be used in diagnostics printed to the {@link 88*f585d8a3SJacky Wang * Messager}. By default, the {@linkplain Class#getCanonicalName() fully qualified name} of the 89*f585d8a3SJacky Wang * plugin is used. 90*f585d8a3SJacky Wang */ pluginName()91*f585d8a3SJacky Wang default String pluginName() { 92*f585d8a3SJacky Wang return getClass().getCanonicalName(); 93*f585d8a3SJacky Wang } 94*f585d8a3SJacky Wang 95*f585d8a3SJacky Wang /** 96*f585d8a3SJacky Wang * Perform any extra work after the plugin finished all its visiting. This will be called once per 97*f585d8a3SJacky Wang * instance of this plugin, after all graphs were {@linkplain #visitGraph(BindingGraph, 98*f585d8a3SJacky Wang * DiagnosticReporter) visited} 99*f585d8a3SJacky Wang */ onPluginEnd()100*f585d8a3SJacky Wang default void onPluginEnd() {} 101*f585d8a3SJacky Wang } 102