Skip to content

Commit 745d30c

Browse files
bcorsoDagger Team
authored andcommitted
Run superficial validation before running InjectValidator.
This ensures that we defer if a generated type has not been generated before trying to do validation on it. Fixes #3075 RELNOTES=Fixes #3075: ensures that we defer if a generated type has not been generated before trying to do validation on it PiperOrigin-RevId: 413542073
1 parent 361a24a commit 745d30c

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

java/dagger/internal/codegen/validation/InjectValidator.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public final class InjectValidator implements ClearableCache {
6666
private final DaggerTypes types;
6767
private final DaggerElements elements;
6868
private final CompilerOptions compilerOptions;
69+
private final SuperficialValidator superficialValidator;
6970
private final DependencyRequestValidator dependencyRequestValidator;
7071
private final Optional<Diagnostic.Kind> privateAndStaticInjectionDiagnosticKind;
7172
private final InjectionAnnotations injectionAnnotations;
@@ -77,6 +78,7 @@ public final class InjectValidator implements ClearableCache {
7778
XProcessingEnv processingEnv,
7879
DaggerTypes types,
7980
DaggerElements elements,
81+
SuperficialValidator superficialValidator,
8082
DependencyRequestValidator dependencyRequestValidator,
8183
CompilerOptions compilerOptions,
8284
InjectionAnnotations injectionAnnotations,
@@ -86,6 +88,7 @@ public final class InjectValidator implements ClearableCache {
8688
types,
8789
elements,
8890
compilerOptions,
91+
superficialValidator,
8992
dependencyRequestValidator,
9093
Optional.empty(),
9194
injectionAnnotations,
@@ -97,6 +100,7 @@ private InjectValidator(
97100
DaggerTypes types,
98101
DaggerElements elements,
99102
CompilerOptions compilerOptions,
103+
SuperficialValidator superficialValidator,
100104
DependencyRequestValidator dependencyRequestValidator,
101105
Optional<Kind> privateAndStaticInjectionDiagnosticKind,
102106
InjectionAnnotations injectionAnnotations,
@@ -105,6 +109,7 @@ private InjectValidator(
105109
this.types = types;
106110
this.elements = elements;
107111
this.compilerOptions = compilerOptions;
112+
this.superficialValidator = superficialValidator;
108113
this.dependencyRequestValidator = dependencyRequestValidator;
109114
this.privateAndStaticInjectionDiagnosticKind = privateAndStaticInjectionDiagnosticKind;
110115
this.injectionAnnotations = injectionAnnotations;
@@ -129,6 +134,7 @@ public InjectValidator whenGeneratingCode() {
129134
types,
130135
elements,
131136
compilerOptions,
137+
superficialValidator,
132138
dependencyRequestValidator,
133139
Optional.of(Diagnostic.Kind.ERROR),
134140
injectionAnnotations,
@@ -140,6 +146,7 @@ public ValidationReport validate(XTypeElement typeElement) {
140146
}
141147

142148
private ValidationReport validateUncached(XTypeElement typeElement) {
149+
superficialValidator.throwIfNearestEnclosingTypeNotValid(typeElement);
143150
ValidationReport.Builder builder = ValidationReport.about(typeElement);
144151
builder.addSubreport(validateMembersInjectionType(typeElement));
145152

javatests/dagger/functional/kotlin/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ kt_jvm_library(
4141
":java_qualifier",
4242
"//:dagger_with_compiler",
4343
"//javatests/dagger/functional/kotlin/processor:annotation",
44+
"//third_party/java/auto:factory",
4445
],
4546
)
4647

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (C) 2021 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.functional.kotlin
18+
19+
import com.google.auto.factory.AutoFactory
20+
import dagger.Component
21+
import javax.inject.Inject
22+
23+
// TODO(bcorso): Merge this into the test once we support kt_jvm_test
24+
/** Defines kotlin classes for the associated test. */
25+
object DependsOnGeneratedCodeClasses {
26+
@Component
27+
abstract class TestComponent {
28+
abstract fun bar(): Bar
29+
}
30+
31+
class Bar @Inject constructor(
32+
)
33+
34+
@AutoFactory class SomeClass
35+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (C) 2021 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.functional.kotlin;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
import org.junit.runners.JUnit4;
24+
25+
/**
26+
* @see <a href="https://github.com/google/dagger/issues/3075">Issue #3075</a>
27+
*/
28+
@RunWith(JUnit4.class)
29+
public class DependsOnGeneratedCodeTest {
30+
@Test
31+
public void testComponentDependsOnGeneratedCode() {
32+
assertThat(DaggerDependsOnGeneratedCodeClasses_TestComponent.create().bar()).isNotNull();
33+
}
34+
}

0 commit comments

Comments
 (0)