xref: /aosp_15_r20/external/dagger2/javatests/dagger/internal/codegen/GeneratedLines.java (revision f585d8a307d0621d6060bd7e80091fdcbf94fe27)
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           + "})";
38 
39   private static final String IMPORT_DAGGER_GENERATED = "import dagger.internal.DaggerGenerated;";
40 
41   private static final String IMPORT_GENERATED_ANNOTATION =
42       isBeforeJava9()
43           ? "import javax.annotation.Generated;"
44           : "import javax.annotation.processing.Generated;";
45 
46   /** Returns a {@code String} of sorted imports. Includes generated imports automatically. */
generatedImports(String... extraImports)47   public static String generatedImports(String... extraImports) {
48     return ImmutableSet.<String>builder()
49         .add(IMPORT_DAGGER_GENERATED)
50         .add(IMPORT_GENERATED_ANNOTATION)
51         .add(extraImports)
52         .build()
53         .stream()
54         .sorted()
55         .collect(Collectors.joining("\n"));
56   }
57 
58   /** Returns the annotations for a generated class. */
generatedAnnotations()59   public static String generatedAnnotations() {
60     return Joiner.on('\n')
61         .join(DAGGER_GENERATED_ANNOTATION, GENERATED_ANNOTATION, SUPPRESS_WARNINGS_ANNOTATION);
62   }
63 
64   /** Returns the annotations for a generated class without {@code SuppressWarnings}. */
generatedAnnotationsWithoutSuppressWarnings()65   public static String generatedAnnotationsWithoutSuppressWarnings() {
66     return Joiner.on('\n').join(DAGGER_GENERATED_ANNOTATION, GENERATED_ANNOTATION);
67   }
68 
69   static final String GENERATION_OPTIONS_ANNOTATION = "@GenerationOptions(fastInit = false)";
70 
isBeforeJava9()71   private static boolean isBeforeJava9() {
72     try {
73       Class.forName("java.lang.Module");
74       return false;
75     } catch (ClassNotFoundException e) {
76       return true;
77     }
78   }
79 
GeneratedLines()80   private GeneratedLines() {}
81 }
82