Skip to content

Commit

Permalink
Avoid clashes with nested type defined in super Interface/Class.
Browse files Browse the repository at this point in the history
RELNOTES=n/a
PiperOrigin-RevId: 418837115
  • Loading branch information
wanyingd1996 authored and Dagger Team committed Dec 29, 2021
1 parent 8bb1d1a commit 8ff7d6b
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 2 deletions.
8 changes: 6 additions & 2 deletions java/dagger/internal/codegen/javapoet/TypeSpecs.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,13 @@ public static TypeSpec.Builder addSupertype(
public static TypeSpec.Builder addSupertype(TypeSpec.Builder typeBuilder, TypeElement supertype) {
switch (supertype.getKind()) {
case CLASS:
return typeBuilder.superclass(ClassName.get(supertype));
return typeBuilder
.superclass(ClassName.get(supertype))
.avoidClashesWithNestedClasses(supertype);
case INTERFACE:
return typeBuilder.addSuperinterface(ClassName.get(supertype));
return typeBuilder
.addSuperinterface(ClassName.get(supertype))
.avoidClashesWithNestedClasses(supertype);
default:
throw new AssertionError(supertype + " is neither a class nor an interface.");
}
Expand Down
61 changes: 61 additions & 0 deletions javatests/dagger/functional/ComponentNestedTypeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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;

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

import dagger.Component;
import dagger.Module;
import dagger.Provides;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Regression test for b/212604806. */
@RunWith(JUnit4.class)
public final class ComponentNestedTypeTest {
@Component(modules = TestModule.class)
interface TestComponent {

// Dagger generated component implementation that extends TestComponent will implement this
// method, so the component implementation will keep a reference to the {@link
// dagger.functional.sub.NestedType}. The reference to {@link dagger.functional.sub.NestedType}
// may collide with the NestedType defined inside of TestComponent, because javapoet may strip
// the package prefix of the type as it does not have enough information about the super
// class/interfaces.
dagger.functional.sub.NestedType nestedType();

interface NestedType {}
}

public static final class SomeType implements dagger.functional.sub.NestedType {}

@Module
static final class TestModule {
@Provides
static dagger.functional.sub.NestedType provideSomeType() {
return new SomeType();
}
}

@Test
public void typeNameWontClashWithNestedTypeName() {
TestComponent component =
DaggerComponentNestedTypeTest_TestComponent.builder().testModule(new TestModule()).build();
assertThat(component.nestedType()).isNotNull();
}
}
21 changes: 21 additions & 0 deletions javatests/dagger/functional/sub/NestedType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* 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.sub;

// Move this back to dagger.functional package once the javapoet clash bug gets fixed.
// https://github.com/square/javapoet/issues/860
public interface NestedType {}

0 comments on commit 8ff7d6b

Please sign in to comment.