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 com.google.common.base.Preconditions.checkArgument; 20 21 import com.google.common.collect.ImmutableList; 22 import com.google.testing.compile.JavaFileObjects; 23 import java.util.Collections; 24 import java.util.HashSet; 25 import java.util.Set; 26 import javax.tools.JavaFileObject; 27 28 /** 29 * A fluent API to build a {@link JavaFileObject} appropriate for a current set of settings, such as 30 * compiler mode. 31 * 32 * <p>After creating a builder, you can add lines to the file. Call {@link #addLines(String...)} to 33 * add lines irrespective of the settings. If you want to add different lines for different possible 34 * settings, call {@link #addLinesIf(Object, String...)} to add those lines only if the given 35 * setting has been added via {@link #withSetting(Object)} or {@link #withSettings(Object...)}. 36 */ 37 final class JavaFileBuilder { 38 private final String qualifiedName; 39 private final Set<Object> settings = new HashSet<>(); 40 41 private final ImmutableList.Builder<String> sourceLines = ImmutableList.builder(); 42 43 /** Creates a builder for a file whose top level type has a given qualified name. */ JavaFileBuilder(String qualifiedName)44 JavaFileBuilder(String qualifiedName) { 45 checkArgument(!qualifiedName.isEmpty()); 46 this.qualifiedName = qualifiedName; 47 } 48 49 // TODO(cgdecker): Get rid of the special constructor/method for CompilerMode 50 51 /** Creates a builder for a file whose top level type has a given qualified name. */ JavaFileBuilder(CompilerMode compilerMode, String qualifiedName)52 JavaFileBuilder(CompilerMode compilerMode, String qualifiedName) { 53 this(qualifiedName); 54 settings.add(compilerMode); 55 } 56 57 /** Adds the given setting as one that code should be generated for. */ withSetting(Object setting)58 JavaFileBuilder withSetting(Object setting) { 59 this.settings.add(setting); 60 return this; 61 } 62 63 /** Adds the given settings as one that code should be generated for. */ withSettings(Object s1, Object s2, Object... more)64 JavaFileBuilder withSettings(Object s1, Object s2, Object... more) { 65 settings.add(s1); 66 settings.add(s2); 67 Collections.addAll(settings, more); 68 return this; 69 } 70 71 /** Adds lines no matter what the {@link CompilerMode} is. */ addLines(String... lines)72 JavaFileBuilder addLines(String... lines) { 73 sourceLines.add(lines); 74 return this; 75 } 76 77 /** Adds lines if in the given mode. */ addLinesIn(CompilerMode mode, String... lines)78 JavaFileBuilder addLinesIn(CompilerMode mode, String... lines) { 79 return addLinesIf(mode, lines); 80 } 81 82 /** Adds lines if in the given setting is set. */ addLinesIf(Object setting, String... lines)83 JavaFileBuilder addLinesIf(Object setting, String... lines) { 84 if (settings.contains(setting)) { 85 sourceLines.add(lines); 86 } 87 return this; 88 } 89 90 /** Builds the {@link JavaFileObject}. */ build()91 JavaFileObject build() { 92 return JavaFileObjects.forSourceLines(qualifiedName, sourceLines.build()); 93 } 94 } 95