Skip to content

Commit

Permalink
Add a class retention @DaggerGenerated annotation to Dagger generated…
Browse files Browse the repository at this point in the history
… sources.

RELNOTES=N/A
PiperOrigin-RevId: 363251759
  • Loading branch information
bcorso authored and Dagger Team committed Mar 16, 2021
1 parent 3778ee2 commit ba1ad49
Show file tree
Hide file tree
Showing 27 changed files with 496 additions and 488 deletions.
30 changes: 30 additions & 0 deletions java/dagger/internal/DaggerGenerated.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (C) 2021 The Dagger Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package dagger.internal;

import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.CLASS;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

/** Annotates the top-level class of each Dagger generated source file. */
@Documented
@Retention(CLASS)
@Target(TYPE)
public @interface DaggerGenerated {}
2 changes: 2 additions & 0 deletions java/dagger/internal/codegen/base/SourceFileGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.TypeSpec;
import dagger.internal.DaggerGenerated;
import dagger.internal.codegen.javapoet.AnnotationSpecs;
import dagger.internal.codegen.javapoet.AnnotationSpecs.Suppression;
import dagger.internal.codegen.langmodel.DaggerElements;
Expand Down Expand Up @@ -90,6 +91,7 @@ public void generate(T input) throws SourceFileGenerationException {

private JavaFile buildJavaFile(T input, TypeSpec.Builder typeSpecBuilder) {
typeSpecBuilder.addOriginatingElement(originatingElement(input));
typeSpecBuilder.addAnnotation(DaggerGenerated.class);
Optional<AnnotationSpec> generatedAnnotation =
generatedAnnotation(elements, sourceVersion)
.map(
Expand Down
5 changes: 2 additions & 3 deletions javatests/dagger/internal/codegen/AssistedFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import static dagger.internal.codegen.CompilerMode.DEFAULT_MODE;
import static dagger.internal.codegen.CompilerMode.FAST_INIT_MODE;
import static dagger.internal.codegen.Compilers.compilerWithOptions;
import static dagger.internal.codegen.GeneratedLines.GENERATED_CODE_ANNOTATIONS;

import com.google.common.collect.ImmutableCollection;
import com.google.testing.compile.Compilation;
Expand Down Expand Up @@ -96,7 +95,7 @@ public void testAssistedFactory() {
JavaFileObject generatedComponent =
compilerMode
.javaFileBuilder("test.DaggerTestComponent")
.addLines("package test;", "", GENERATED_CODE_ANNOTATIONS)
.addLines("package test;", "", GeneratedLines.generatedAnnotations())
.addLinesIn(
FAST_INIT_MODE,
"final class DaggerTestComponent implements TestComponent {",
Expand Down Expand Up @@ -192,7 +191,7 @@ public void testAssistedFactoryCycle() {
JavaFileObject generatedComponent =
compilerMode
.javaFileBuilder("test.DaggerTestComponent")
.addLines("package test;", "", GENERATED_CODE_ANNOTATIONS)
.addLines("package test;", "", GeneratedLines.generatedAnnotations())
.addLinesIn(
FAST_INIT_MODE,
"final class DaggerTestComponent implements TestComponent {",
Expand Down
3 changes: 1 addition & 2 deletions javatests/dagger/internal/codegen/ComponentBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import static com.google.testing.compile.CompilationSubject.assertThat;
import static dagger.internal.codegen.Compilers.compilerWithOptions;
import static dagger.internal.codegen.GeneratedLines.GENERATED_CODE_ANNOTATIONS;
import static dagger.internal.codegen.binding.ComponentCreatorAnnotation.COMPONENT_BUILDER;
import static dagger.internal.codegen.binding.ErrorMessages.creatorMessagesFor;

Expand Down Expand Up @@ -88,7 +87,7 @@ public void testUsesBuildAndSetterNames() {
"",
"import dagger.internal.Preconditions;",
"",
GENERATED_CODE_ANNOTATIONS,
GeneratedLines.generatedAnnotations(),
"final class DaggerTestComponent implements TestComponent {",
" private static final class Builder implements TestComponent.Builder {",
" private TestModule testModule;",
Expand Down
18 changes: 7 additions & 11 deletions javatests/dagger/internal/codegen/ComponentCreatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
import static dagger.internal.codegen.Compilers.compilerWithOptions;
import static dagger.internal.codegen.Compilers.daggerCompiler;
import static dagger.internal.codegen.ComponentCreatorTest.CompilerType.JAVAC;
import static dagger.internal.codegen.GeneratedLines.GENERATED_CODE_ANNOTATIONS;
import static dagger.internal.codegen.GeneratedLines.IMPORT_GENERATED_ANNOTATION;
import static dagger.internal.codegen.binding.ComponentCreatorAnnotation.COMPONENT_BUILDER;
import static dagger.internal.codegen.binding.ComponentCreatorAnnotation.COMPONENT_FACTORY;
import static dagger.internal.codegen.binding.ComponentCreatorKind.BUILDER;
Expand Down Expand Up @@ -106,7 +104,7 @@ public void testEmptyCreator() {
"test.DaggerSimpleComponent",
"package test;",
"",
GENERATED_CODE_ANNOTATIONS,
GeneratedLines.generatedAnnotations(),
"final class DaggerSimpleComponent implements SimpleComponent {",
" private static final class Builder implements SimpleComponent.Builder {",
" @Override",
Expand Down Expand Up @@ -159,9 +157,9 @@ public void testCanInstantiateModulesUserCannotSet() {
"test.DaggerTestComponent",
"package test;",
"",
IMPORT_GENERATED_ANNOTATION,
GeneratedLines.generatedImports(),
"",
GENERATED_CODE_ANNOTATIONS,
GeneratedLines.generatedAnnotations(),
"final class DaggerTestComponent implements TestComponent {",
" private final TestModule testModule;",
"",
Expand Down Expand Up @@ -357,10 +355,9 @@ public void testCreatorWithBindsInstanceNoStaticCreateGenerated() {
.addLines(
"package test;",
"",
"import dagger.internal.Preconditions;",
IMPORT_GENERATED_ANNOTATION,
GeneratedLines.generatedImports("import dagger.internal.Preconditions;"),
"",
GENERATED_CODE_ANNOTATIONS,
GeneratedLines.generatedAnnotations(),
"final class DaggerSimpleComponent implements SimpleComponent {",
" private final Object object;",
"",
Expand Down Expand Up @@ -459,10 +456,9 @@ public void testCreatorWithPrimitiveBindsInstance() {
.addLines(
"package test;",
"",
"import dagger.internal.Preconditions;",
IMPORT_GENERATED_ANNOTATION,
GeneratedLines.generatedImports("import dagger.internal.Preconditions;"),
"",
GENERATED_CODE_ANNOTATIONS,
GeneratedLines.generatedAnnotations(),
"final class DaggerSimpleComponent implements SimpleComponent {",
" private final Integer i;",
"",
Expand Down
3 changes: 1 addition & 2 deletions javatests/dagger/internal/codegen/ComponentFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import static com.google.testing.compile.CompilationSubject.assertThat;
import static dagger.internal.codegen.Compilers.compilerWithOptions;
import static dagger.internal.codegen.GeneratedLines.GENERATED_CODE_ANNOTATIONS;
import static dagger.internal.codegen.binding.ComponentCreatorAnnotation.COMPONENT_FACTORY;
import static dagger.internal.codegen.binding.ErrorMessages.creatorMessagesFor;

Expand Down Expand Up @@ -87,7 +86,7 @@ public void testUsesParameterNames() {
"",
"import dagger.internal.Preconditions;",
"",
GENERATED_CODE_ANNOTATIONS,
GeneratedLines.generatedAnnotations(),
"final class DaggerTestComponent implements TestComponent {",
" private static final class Factory implements TestComponent.Factory {",
" @Override",
Expand Down
Loading

0 comments on commit ba1ad49

Please sign in to comment.