Skip to content

Commit

Permalink
Add more Gradle test coverage for issue #3136.
Browse files Browse the repository at this point in the history
This CL adds tests to show how Dagger handles unrelated transitive annotations on the module and inject classes.

RELNOTES=N/A
PiperOrigin-RevId: 421078020
  • Loading branch information
bcorso authored and Dagger Team committed Jan 11, 2022
1 parent 006e59f commit 5ed67ca
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 12 deletions.
Expand Up @@ -20,6 +20,7 @@

import javax.inject.Inject;
import javax.inject.Singleton;
import library2.MySimpleTransitiveAnnotation;
import library2.MyTransitiveAnnotation;

/**
Expand All @@ -30,33 +31,42 @@
* the classpath. In most cases, Dagger shouldn't care that the annotation isn't on the classpath
*/
@Singleton
@MySimpleTransitiveAnnotation
@MyTransitiveAnnotation(VALUE)
public final class Foo extends FooBase {
@MySimpleTransitiveAnnotation
@MyTransitiveAnnotation(VALUE)
int nonDaggerField;

@MySimpleTransitiveAnnotation
// @MyTransitiveAnnotation(VALUE): Not supported on inject-fields yet.
@Inject int daggerField;

@MySimpleTransitiveAnnotation
@MyTransitiveAnnotation(VALUE)
Foo(@MyTransitiveAnnotation(VALUE) String str) {
Foo(@MySimpleTransitiveAnnotation @MyTransitiveAnnotation(VALUE) String str) {
super(str);
}

@MySimpleTransitiveAnnotation
@MyTransitiveAnnotation(VALUE)
@Inject
Foo(
@MySimpleTransitiveAnnotation
// @MyTransitiveAnnotation(VALUE): Not supported on inject-constructor parameters yet.
int i) {
super(i);
}

@MySimpleTransitiveAnnotation
@MyTransitiveAnnotation(VALUE)
void nonDaggerMethod(@MyTransitiveAnnotation(VALUE) int i) {}
void nonDaggerMethod(@MySimpleTransitiveAnnotation @MyTransitiveAnnotation(VALUE) int i) {}

@MySimpleTransitiveAnnotation
@MyTransitiveAnnotation(VALUE)
@Inject
void daggerMethod(
@MySimpleTransitiveAnnotation
// @MyTransitiveAnnotation(VALUE): Not supported on inject-method parameters yet.
int i) {}
}
Expand Up @@ -19,30 +19,39 @@
import static library2.MyTransitiveAnnotation.VALUE;

import javax.inject.Inject;
import library2.MySimpleTransitiveAnnotation;
import library2.MyTransitiveAnnotation;

/** A baseclass for {@link Foo}. */
@MySimpleTransitiveAnnotation
@MyTransitiveAnnotation(VALUE)
public class FooBase {
@MySimpleTransitiveAnnotation
@MyTransitiveAnnotation(VALUE)
int baseNonDaggerField;

@MySimpleTransitiveAnnotation
// @MyTransitiveAnnotation(VALUE): Not supported on inject-method parameters yet.
@Inject int baseDaggerField;

@MySimpleTransitiveAnnotation
@MyTransitiveAnnotation(VALUE)
FooBase(@MyTransitiveAnnotation(VALUE) String str) {}
FooBase(@MySimpleTransitiveAnnotation @MyTransitiveAnnotation(VALUE) String str) {}

@MySimpleTransitiveAnnotation
@MyTransitiveAnnotation(VALUE)
@Inject
FooBase(@MyTransitiveAnnotation(VALUE) int i) {}
FooBase(@MySimpleTransitiveAnnotation @MyTransitiveAnnotation(VALUE) int i) {}

@MySimpleTransitiveAnnotation
@MyTransitiveAnnotation(VALUE)
void baseNonDaggerMethod(@MyTransitiveAnnotation(VALUE) int i) {}
void baseNonDaggerMethod(@MySimpleTransitiveAnnotation @MyTransitiveAnnotation(VALUE) int i) {}

@MySimpleTransitiveAnnotation
@MyTransitiveAnnotation(VALUE)
@Inject
void baseDaggerMethod(
@MySimpleTransitiveAnnotation
// @MyTransitiveAnnotation(VALUE): Not supported on inject-method parameters yet.
int i) {}
}
@@ -0,0 +1,97 @@
/*
* 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 library1;

import static library2.MyTransitiveAnnotation.VALUE;

import dagger.Binds;
import dagger.Module;
import dagger.Provides;
import javax.inject.Inject;
import library2.MySimpleTransitiveAnnotation;
import library2.MyTransitiveAnnotation;

/**
* A class used to test that Dagger won't fail when non-dagger related annotations cannot be
* resolved.
*
* <p>During the compilation of {@code :app}, {@link MyTransitiveAnnotation} will no longer be on
* the classpath. In most cases, Dagger shouldn't care that the annotation isn't on the classpath
*/
@MySimpleTransitiveAnnotation
@MyTransitiveAnnotation(VALUE)
@Module(includes = {
MyComponentModule.MyAbstractModule.class
})
public final class MyComponentModule {
public static final class Dep {
@Inject Dep() {}
}

@MySimpleTransitiveAnnotation
@MyTransitiveAnnotation(VALUE)
@Module
interface MyAbstractModule {
@MySimpleTransitiveAnnotation
// @MyTransitiveAnnotation(VALUE): Not yet supported
@Binds
Number bindNumber(
@MySimpleTransitiveAnnotation
// @MyTransitiveAnnotation(VALUE): Not yet supported
int i);
}

@MySimpleTransitiveAnnotation
@MyTransitiveAnnotation(VALUE)
private final String nonDaggerField = "";

@MySimpleTransitiveAnnotation
@MyTransitiveAnnotation(VALUE)
MyComponentModule(
@MySimpleTransitiveAnnotation
@MyTransitiveAnnotation(VALUE)
Dep dep) {}

@MySimpleTransitiveAnnotation
// @MyTransitiveAnnotation(VALUE): Not yet supported
@Provides
int provideInt(
@MySimpleTransitiveAnnotation
// @MyTransitiveAnnotation(VALUE): Not yet supported
Dep dep) {
return 1;
}

@MySimpleTransitiveAnnotation
@MyTransitiveAnnotation(VALUE)
String nonDaggerMethod(
@MySimpleTransitiveAnnotation
@MyTransitiveAnnotation(VALUE)
String str) {
return str;
}


@MySimpleTransitiveAnnotation
@MyTransitiveAnnotation(VALUE)
static String nonDaggerStaticMethod(
@MySimpleTransitiveAnnotation
@MyTransitiveAnnotation(VALUE)
String str) {
return str;
}
}
@@ -0,0 +1,20 @@
/*
* Copyright (C) 2022 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 library2;

/** A simple annotation that is a transitive dependency of the app. */
public @interface MySimpleTransitiveAnnotation {}
Expand Up @@ -16,18 +16,13 @@

package app;

import dagger.BindsInstance;
import dagger.Component;
import javax.inject.Singleton;
import library1.Foo;
import library1.MyComponentModule;

@Singleton
@Component
@Component(modules = MyComponentModule.class)
interface MyComponent {
Foo foo();

@Component.Factory
interface Factory {
MyComponent create(@BindsInstance int i);
}
}

0 comments on commit 5ed67ca

Please sign in to comment.