Skip to content

Commit

Permalink
Also generate proxy module classes for Kotlin classes that are marked…
Browse files Browse the repository at this point in the history
… as `internal`, which are analogous to package-private Java modules.

RELNOTES=N/A
PiperOrigin-RevId: 365865718
  • Loading branch information
groakley authored and Dagger Team committed Mar 30, 2021
1 parent b42731b commit 41ebd99
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.TypeName;
import dagger.hilt.processor.internal.ClassNames;
import dagger.hilt.processor.internal.KotlinMetadataUtils;
import dagger.hilt.processor.internal.Processors;
import java.util.Optional;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;

Expand Down Expand Up @@ -63,9 +63,11 @@ final ClassName generatedClassName() {
* Returns an Optional PkgPrivateMetadata requiring Hilt processing, otherwise returns an empty
* Optional.
*/
static Optional<PkgPrivateMetadata> of(Elements elements, Element element, ClassName annotation) {
static Optional<PkgPrivateMetadata> of(
Elements elements, TypeElement element, ClassName annotation) {
// If this is a public element no wrapping is needed
if (effectiveVisibilityOfElement(element) == Visibility.PUBLIC) {
if (effectiveVisibilityOfElement(element) == Visibility.PUBLIC
&& !KotlinMetadataUtils.getMetadataUtil().isVisibilityInternal(element)) {
return Optional.empty();
}

Expand Down
9 changes: 9 additions & 0 deletions java/dagger/internal/codegen/kotlin/KotlinMetadataUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,15 @@ public boolean isVisibilityPrivate(TypeElement typeElement) {
&& metadataFactory.create(typeElement).classMetadata().flags(IS_PRIVATE);
}

/**
* Returns {@code true} if the given type element was declared {@code internal} in its Kotlin
* source.
*/
public boolean isVisibilityInternal(TypeElement type) {
return hasMetadata(type)
&& metadataFactory.create(type).classMetadata().flags(Flag.IS_INTERNAL);
}

/**
* Returns {@code true} if the given executable element was declared {@code internal} in its
* Kotlin source.
Expand Down
19 changes: 19 additions & 0 deletions javatests/dagger/hilt/android/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,25 @@ kt_android_library(
],
)

android_local_test(
name = "InternalKtModuleTest",
size = "small",
srcs = ["InternalKtModuleTest.java"],
manifest_values = {
"minSdkVersion": "14",
},
deps = [
"//:android_local_test_exports",
"//:dagger_with_compiler",
"//java/dagger/hilt:entry_point",
"//java/dagger/hilt:install_in",
"//java/dagger/hilt/android:package_info",
"//java/dagger/hilt/android/testing:hilt_android_test",
"//javatests/dagger/hilt/testmodules",
"@google_bazel_common//third_party/java/truth",
],
)

android_local_test(
name = "DefaultViewModelFactoryTest",
srcs = ["DefaultViewModelFactoryTest.java"],
Expand Down
50 changes: 50 additions & 0 deletions javatests/dagger/hilt/android/InternalKtModuleTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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.hilt.android;

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

import android.os.Build;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import dagger.hilt.android.testing.HiltAndroidRule;
import dagger.hilt.android.testing.HiltAndroidTest;
import dagger.hilt.android.testing.HiltTestApplication;
import javax.inject.Inject;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;

@HiltAndroidTest
@RunWith(AndroidJUnit4.class)
@Config(sdk = Build.VERSION_CODES.P, application = HiltTestApplication.class)
public final class InternalKtModuleTest {
@Rule public final HiltAndroidRule rule = new HiltAndroidRule(this);

@Inject String string;

@Before
public void setUp() {
rule.inject();
}

@Test
public void testBindingFromInternalKtModule() {
assertThat(string).isEqualTo("expected_string_value");
}
}
37 changes: 37 additions & 0 deletions javatests/dagger/hilt/testmodules/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# 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.
#
# Description:
# Builds and run tests related to AggregatedDepsProcessor.

load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_android_library")

package(default_visibility = ["//:src"])

kt_android_library(
name = "testmodules",
testonly = True,
srcs = glob(["*.kt"]),
deps = [
"//:dagger_with_compiler",
"//java/dagger/hilt:install_in",
"//java/dagger/hilt/components",
"@google_bazel_common//third_party/java/jsr330_inject",
],
)

filegroup(
name = "srcs_filegroup",
srcs = glob(["*"]),
)
34 changes: 34 additions & 0 deletions javatests/dagger/hilt/testmodules/InternalKtTestModule.kt
Original file line number Diff line number Diff line change
@@ -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.hilt.testmodules

import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

/** Module for [dagger.hilt.android.InternalKtModuleTest]. */
@Module
@InstallIn(SingletonComponent::class)
internal abstract class InternalKtTestModule private constructor() {
companion object {
@Provides
@Singleton
fun provideString(): String = "expected_string_value"
}
}

0 comments on commit 41ebd99

Please sign in to comment.