Skip to content

Commit

Permalink
Run superficial validation before running InjectValidator.
Browse files Browse the repository at this point in the history
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: 413439235
  • Loading branch information
bcorso authored and Dagger Team committed Dec 1, 2021
1 parent 361a24a commit e4c7dc4
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
7 changes: 7 additions & 0 deletions java/dagger/internal/codegen/validation/InjectValidator.java
Expand Up @@ -66,6 +66,7 @@ public final class InjectValidator implements ClearableCache {
private final DaggerTypes types;
private final DaggerElements elements;
private final CompilerOptions compilerOptions;
private final SuperficialValidator superficialValidator;
private final DependencyRequestValidator dependencyRequestValidator;
private final Optional<Diagnostic.Kind> privateAndStaticInjectionDiagnosticKind;
private final InjectionAnnotations injectionAnnotations;
Expand All @@ -77,6 +78,7 @@ public final class InjectValidator implements ClearableCache {
XProcessingEnv processingEnv,
DaggerTypes types,
DaggerElements elements,
SuperficialValidator superficialValidator,
DependencyRequestValidator dependencyRequestValidator,
CompilerOptions compilerOptions,
InjectionAnnotations injectionAnnotations,
Expand All @@ -86,6 +88,7 @@ public final class InjectValidator implements ClearableCache {
types,
elements,
compilerOptions,
superficialValidator,
dependencyRequestValidator,
Optional.empty(),
injectionAnnotations,
Expand All @@ -97,6 +100,7 @@ private InjectValidator(
DaggerTypes types,
DaggerElements elements,
CompilerOptions compilerOptions,
SuperficialValidator superficialValidator,
DependencyRequestValidator dependencyRequestValidator,
Optional<Kind> privateAndStaticInjectionDiagnosticKind,
InjectionAnnotations injectionAnnotations,
Expand All @@ -105,6 +109,7 @@ private InjectValidator(
this.types = types;
this.elements = elements;
this.compilerOptions = compilerOptions;
this.superficialValidator = superficialValidator;
this.dependencyRequestValidator = dependencyRequestValidator;
this.privateAndStaticInjectionDiagnosticKind = privateAndStaticInjectionDiagnosticKind;
this.injectionAnnotations = injectionAnnotations;
Expand All @@ -129,6 +134,7 @@ public InjectValidator whenGeneratingCode() {
types,
elements,
compilerOptions,
superficialValidator,
dependencyRequestValidator,
Optional.of(Diagnostic.Kind.ERROR),
injectionAnnotations,
Expand All @@ -140,6 +146,7 @@ public ValidationReport validate(XTypeElement typeElement) {
}

private ValidationReport validateUncached(XTypeElement typeElement) {
superficialValidator.throwIfNearestEnclosingTypeNotValid(typeElement);
ValidationReport.Builder builder = ValidationReport.about(typeElement);
builder.addSubreport(validateMembersInjectionType(typeElement));

Expand Down
1 change: 1 addition & 0 deletions javatests/dagger/functional/kotlin/BUILD
Expand Up @@ -41,6 +41,7 @@ kt_jvm_library(
":java_qualifier",
"//:dagger_with_compiler",
"//javatests/dagger/functional/kotlin/processor:annotation",
"//third_party/java/auto:factory",
],
)

Expand Down
@@ -0,0 +1,35 @@
/*
* 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.functional.kotlin

import com.google.auto.factory.AutoFactory
import dagger.Component
import javax.inject.Inject

// TODO(bcorso): Merge this into the test once we support kt_jvm_test
/** Defines kotlin classes for the associated test. */
object DependsOnGeneratedCodeClasses {

@Component
abstract class TestComponent {
abstract fun bar(): Bar
}

class Bar @Inject constructor(someClassFactory: DependsOnGeneratedCodeClasses_SomeClassFactory)

@AutoFactory class SomeClass
}
34 changes: 34 additions & 0 deletions javatests/dagger/functional/kotlin/DependsOnGeneratedCodeTest.java
@@ -0,0 +1,34 @@
/*
* 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.functional.kotlin;

import static com.google.common.truth.Truth.assertThat;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/**
* @see <a href="https://github.com/google/dagger/issues/3075">Issue #3075</a>
*/
@RunWith(JUnit4.class)
public class DependsOnGeneratedCodeTest {
@Test
public void testComponentDependsOnGeneratedCode() {
assertThat(DaggerDependsOnGeneratedCodeClasses_TestComponent.create().bar()).isNotNull();
}
}

0 comments on commit e4c7dc4

Please sign in to comment.