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.functional.spi; 18 19 import static javax.tools.StandardLocation.CLASS_OUTPUT; 20 21 import com.google.auto.service.AutoService; 22 import com.google.common.base.Joiner; 23 import com.squareup.javapoet.ClassName; 24 import dagger.model.BindingGraph; 25 import dagger.model.BindingGraph.ComponentNode; 26 import dagger.spi.BindingGraphPlugin; 27 import dagger.spi.DiagnosticReporter; 28 import java.io.IOException; 29 import java.io.UncheckedIOException; 30 import java.io.Writer; 31 import java.util.Properties; 32 import javax.annotation.processing.Filer; 33 34 @AutoService(BindingGraphPlugin.class) 35 public final class TestPlugin implements BindingGraphPlugin { 36 private Filer filer; 37 38 @Override initFiler(Filer filer)39 public void initFiler(Filer filer) { 40 this.filer = filer; 41 } 42 43 @Override visitGraph(BindingGraph bindingGraph, DiagnosticReporter diagnosticReporter)44 public void visitGraph(BindingGraph bindingGraph, DiagnosticReporter diagnosticReporter) { 45 Properties properties = new Properties(); 46 int i = 0; 47 for (ComponentNode node : bindingGraph.componentNodes()) { 48 properties.setProperty( 49 String.format("component[%s]", i++), node.componentPath().toString()); 50 } 51 52 write(bindingGraph, properties); 53 } 54 write(BindingGraph bindingGraph, Properties properties)55 private void write(BindingGraph bindingGraph, Properties properties) { 56 ClassName rootComponentName = 57 ClassName.get(bindingGraph.rootComponentNode().componentPath().currentComponent()); 58 try (Writer writer = 59 filer 60 .createResource( 61 CLASS_OUTPUT, 62 rootComponentName.packageName(), 63 Joiner.on('_').join(rootComponentName.simpleNames()) + ".properties") 64 .openWriter()) { 65 properties.store(writer, ""); 66 } catch (IOException e) { 67 throw new UncheckedIOException(e); 68 } 69 } 70 } 71