Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use strong class reference for no-config glue class #32

Merged
merged 1 commit into from
Apr 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions MODULARIZATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class HttpClientImpl(private val okHttpClient: OkHttpClient) : HttpClient, HttpC
}
}
```
This class can live anywhere in the project or dependency tree.
This class can live anywhere in the project or dependency tree, but it has to be publicly available.

### Module annotations
To limit erroneous runtime configurations and simplify the configuration, it’s required to annotate a module implementation with the appropriate annotation from the `com.mapbox.base:annotations` artifact found in [this package](https://github.com/mapbox/mapbox-base-android/tree/master/annotations/src/main/java/com/mapbox/annotation):
Expand Down Expand Up @@ -123,10 +123,7 @@ object Mapbox_HttpClientModuleConfiguration {
val enableConfiguration: Boolean = false

@JvmStatic
val implPackage: String = "com.mapbox.maps.module.http"

@JvmStatic
val implClassName: String = "HttpClientImpl"
val implClass: Class<HttpClientImpl> = HttpClientImpl::class.java
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't it be?

Suggested change
val implClass: Class<HttpClientImpl> = HttpClientImpl::class.java
val implClass: Class<com.mapbox.maps.module.http.HttpClientImpl> = com.mapbox.maps.module.http.HttpClientImpl::class.java

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the generated class will have an import statement as well. This is just an example.

}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.google.auto.service.AutoService
import com.mapbox.annotation.*
import com.mapbox.annotation.module.MapboxModule
import com.squareup.kotlinpoet.*
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
import com.squareup.kotlinpoet.jvm.jvmStatic
import java.nio.file.Paths
import javax.annotation.processing.AbstractProcessor
Expand Down Expand Up @@ -136,17 +137,12 @@ internal class ModuleProviderGenerator : AbstractProcessor() {
} else {
// if configuration is disabled, generate only impl package and class paths for manual instantiation
typeBuilder.addProperty(
PropertySpec.builder(
MODULE_CONFIGURATION_DISABLED_PACKAGE, String::class)
.initializer("\"${module.implPackage}\"")
.jvmStatic()
.build())
typeBuilder.addProperty(
PropertySpec.builder(
MODULE_CONFIGURATION_DISABLED_CLASS, String::class)
.initializer("\"${module.implClassName}\"")
PropertySpec.builder(MODULE_CONFIGURATION_DISABLED_CLASS,
ClassName.bestGuess("java.lang.Class").parameterizedBy(ClassName.bestGuess("${module.implPackage}.${module.implClassName}")))
.initializer("${module.implClassName}::class.java")
.jvmStatic()
.build())
.build()
)
}

fileBuilder.addType(typeBuilder.build())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,9 @@ const val MODULE_CONFIGURATION_CLASS_NAME_FORMAT = "Mapbox_%sModuleConfiguration
const val MODULE_CONFIGURATION_ENABLE_CONFIGURATION = "enableConfiguration"

/**
* When configuration is disabled, module implementation package string variable name.
* When configuration is disabled, module implementation class reference variable name.
*/
const val MODULE_CONFIGURATION_DISABLED_PACKAGE = "implPackage"

/**
* When configuration is disabled, module implementation class name string variable name.
*/
const val MODULE_CONFIGURATION_DISABLED_CLASS = "implClassName"
const val MODULE_CONFIGURATION_DISABLED_CLASS = "implClass"

/**
* When configuration is enabled, configuration's module provider class name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import com.mapbox.annotation.MODULE_CONFIGURATION_CLASS_NAME_FORMAT
/**
* Annotation that marks an implementation class of the Mapbox module.
*
* The implementation class has to be public.
*
* For additional documentation and examples, see
* [MODULARIZATION.md](https://github.com/mapbox/mapbox-base-android/blob/master/MODULARIZATION.md).
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,8 @@ object MapboxModuleProvider {
}
} else {
// configuration was disabled, we should get the implementation's class and instantiate it
val implPackage =
configurationClass.getMethod(MODULE_CONFIGURATION_DISABLED_PACKAGE.asGetterFun()).invoke(null) as String
val implClassName =
configurationClass.getMethod(MODULE_CONFIGURATION_DISABLED_CLASS.asGetterFun()).invoke(null) as String
val implClass = Class.forName("$implPackage.$implClassName")
val implClass =
configurationClass.getMethod(MODULE_CONFIGURATION_DISABLED_CLASS.asGetterFun()).invoke(null) as Class<T>

var foundInstance: Any? = null
for (creator in instanceCreators) {
Expand Down