1 /* 2 * Copyright (C) 2015 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 com.google.common.base.Joiner; 20 import com.google.common.collect.ImmutableSet; 21 import java.util.stream.Collectors; 22 23 /** 24 * Common lines outputted during code generation. 25 */ 26 public final class GeneratedLines { 27 private static final String DAGGER_GENERATED_ANNOTATION = "@DaggerGenerated"; 28 29 private static final String GENERATED_ANNOTATION = 30 "@Generated(" 31 + "value = \"dagger.internal.codegen.ComponentProcessor\", " 32 + "comments = \"https://dagger.dev\")"; 33 34 private static final String SUPPRESS_WARNINGS_ANNOTATION = 35 "@SuppressWarnings({" 36 + "\"unchecked\", \"rawtypes\", \"KotlinInternal\", \"KotlinInternalInJava\", \"cast\", " 37 + "\"deprecation\"," 38 + "\"nullness:initialization.field.uninitialized\"" 39 + "})"; 40 41 private static final String IMPORT_DAGGER_GENERATED = "import dagger.internal.DaggerGenerated;"; 42 43 private static final String IMPORT_GENERATED_ANNOTATION = 44 isBeforeJava9() 45 ? "import javax.annotation.Generated;" 46 : "import javax.annotation.processing.Generated;"; 47 48 /** Returns a {@code String} of sorted imports. Includes generated imports automatically. */ generatedImports(String... extraImports)49 public static String generatedImports(String... extraImports) { 50 return ImmutableSet.<String>builder() 51 .add(IMPORT_DAGGER_GENERATED) 52 .add(IMPORT_GENERATED_ANNOTATION) 53 .add(extraImports) 54 .build() 55 .stream() 56 .sorted() 57 .collect(Collectors.joining("\n")); 58 } 59 60 /** Returns the annotations for a generated class. */ generatedAnnotations()61 public static String generatedAnnotations() { 62 return Joiner.on('\n') 63 .join(DAGGER_GENERATED_ANNOTATION, GENERATED_ANNOTATION, SUPPRESS_WARNINGS_ANNOTATION); 64 } 65 66 /** Returns the annotations for a generated class without {@code SuppressWarnings}. */ generatedAnnotationsWithoutSuppressWarnings()67 public static String generatedAnnotationsWithoutSuppressWarnings() { 68 return Joiner.on('\n').join(DAGGER_GENERATED_ANNOTATION, GENERATED_ANNOTATION); 69 } 70 71 static final String GENERATION_OPTIONS_ANNOTATION = "@GenerationOptions(fastInit = false)"; 72 isBeforeJava9()73 private static boolean isBeforeJava9() { 74 try { 75 Class.forName("java.lang.Module"); 76 return false; 77 } catch (ClassNotFoundException e) { 78 return true; 79 } 80 } 81 GeneratedLines()82 private GeneratedLines() {} 83 } 84