in kotlin, how to access protected static member in parent class from sub class - kotlin

It is code worked in java but after convert to kotlin it does not compile.
Having a base class which has some defines as static protected member in the companion object:
abstract class ParentClass {
companion object {
#JvmField
final protected val SERVICE_TYPE_A = "the_service_type_a"
}
}
and the child class:
class ChildClass: ParentClass {
public override fun getServiceType(): String {
return SERVICE_TYPE_A. //<== got compile error
}
}
it does not compile.
how to access a parent class static protected member from subclass?

You need to use #JvmStatic instead as follows:
abstract class ParentClass {
companion object {
#JvmStatic
protected val SERVICE_TYPE_A = "the_service_type_a"
}
abstract fun getServiceType(): String
}
The final keyword in SERVICE_TYPE_A is redundant since everything is final by default in Kotlin. This also mean that if you want ParentClass to be extended, then you need to explicitly define it as open.
Then your ChildClass would look as follows:
class ChildClass: ParentClass() {
override fun getServiceType(): String {
return SERVICE_TYPE_A
}
}

Related

Kotlin concrete class extending from abstract class and interface, with the interface using a method implemented in the abstract class

I want to ask a question that I have some clues about, but I don't want to influence the answers I will get. I have the following class hierarchy:
abstract class MyAbstractClass {
fun displayStuff(id: String) {
println("My id is $id.")
}
}
interface MyInterface {
fun displayThis() {
displayStuff("some-value")
}
fun displayStuff(id: String) // Not implemented here
}
class MyConcreteClass(): MyAbstractClass(), MyInterface {
fun doStuff() {
displayThis()
}
}
fun main() {
val result = MyConcreteClass()
result.doStuff()
result.displayStuff("id")
}
What's wrong with this design, and how do you suggest I fix it?
It would probably not be a bad idea to extract the displayStuff into another interface. Then MyAbstractClass and MyInterface can both derive from the same interface.
One overrides the displayStuff function, hence providing something like an abstract base implementation for the interface.
The other one is using the function in a specific way, thereby extending the functionality of the interface.
interface DisplayStuff {
fun displayStuff(id: String)
}
abstract class MyAbstractClass: DisplayStuff {
override fun displayStuff(id: String) = println("My id is $id.")
}
interface MyInterface : DisplayStuff {
fun displayThis() = displayStuff("some-value")
}

Map abstract type in Kotlin with MapStruct

I'm trying to map an abstract class with Mapstruct in Kotlin, but getting the following error:
AnimalMapper.java: error: The return type AnimalOutput is an abstract class or interface. Provide a non abstract / non interface result type or a factory method.
My implementation:
#Mapper(componentModel = "jsr330")
interface AnimalMapper {
fun mapToDogOutput(dogInput: DogInput): DogOutput
fun mapToCatOutput(catInput: CatInput): CatOutput
fun mapToAnimalOutput(animalInput: AnimalInput): AnimalOutput {
when (animalInput) {
is DogInput -> mapToDogOutput(animalInput)
is CatInput -> mapToCatOutput(animalInput)
}
throw RuntimeException("Unsupported animal type");
}
}
sealed class AnimalInput {
abstract val name: String
}
data class CatInput(
override val name: String,
val catProperty: Int,
) : AnimalInput()
data class DogInput(
override val name: String,
val dogProperty: Float,
) : AnimalInput()
sealed class AnimalOutput {
abstract val name: String
}
data class CatOutput(
override val name: String,
val catProperty: Int,
) : AnimalOutput()
data class DogOutput(
override val name: String,
val dogProperty: Float,
) : AnimalOutput()
In my old java project, this corresponding implementation works as expected:
#Mapper(componentModel = "jsr330")
interface AnimalMapper {
DogOutput mapToDogOutput(DogInput dogInput);
CatOutput mapToCatOutput(CatInput catInput);
default AnimalOutput mapToAnimalOutput(AnimalInput animalInput) {
if (animalInput instanceof DogInput) {
return mapToDogOutput((DogInput) animalInput);
}
if (animalInput instanceof CatInput) {
return mapToCatOutput((CatInput) animalInput);
}
throw new RuntimeException("Unsupported animal type");
}
}
public abstract class AnimalInput {
public String name;
}
public abstract class CatInput extends AnimalInput {
public String name;
public int catProperty;
}
public abstract class DogInput extends AnimalInput {
public String name;
public float dogProperty;
}
public abstract class AnimalOutput {
public String name;
}
public abstract class CatOutput extends AnimalOutput {
public String name;
public int catProperty;
}
public abstract class DogOutput extends AnimalOutput {
public String name;
public float dogProperty;
}
I'm using the following dependency versions:
mapstructVersion: 1.4.2.Final
kotlinVersion: 1.5.21 (jvmTarget 16)
Anyone have an idea, how to fix this issue in my Kotlin project?
The reason why this is not working in Kotlin is due to the fact that the generated code by Kotlin marks the mapToAnimalOutput method as an abstract method.
You'll need to use #JvmDefault on that method in order for Kotlin to generate the correct modifiers for the method.

Dagger #Provides in Kotlin

I'm trying to understand Dagger. I created applicationInjector class :
class BaseApplication : DaggerApplication() {
override fun applicationInjector(): AndroidInjector<out DaggerApplication>? {
return DaggerAppComponent.builder().application(this)?.build()
}
}
And here's my AppComponent
#Component(
modules = [AndroidSupportInjectionModule::class,
ActivityBuilderModules::class]
)
interface AppComponent : AndroidInjector<BaseApplication> {
#Component.Builder
interface Builder {
#BindsInstance
fun application(application: Application?): Builder?
fun build(): AppComponent?
}
}
Now what I want to do is to to inject simple String to Activity (really basic, right ?)
In Java it works like this :
#Module
abstract class ActivityBuilderModules {
#ContributesAndroidInjector
abstract fun contributeAuthActivity() : AuthActivity
//JAVA
#Provides
public static String provideTestString() {
return "TEST "
}
however we don't have static function in Kotlin, right ? And it needs to be static cause I'm getting an error :
error: com.example.kotlintests.di.ActivityBuilderModules is abstract and has instance #Provides methods. Consider making the methods static or including a non-abstract subclass of the module instead.
public abstract interface AppComponent extends dagger.android.AndroidInjector<com.example.kotlintests.BaseApplication> {
I tried with package level function but it didn't work. How can I add provideTestString function in Kotlin ?

This not referring to the original object using Kotlins class delegation

I am confused how delegation works in Kotlin. Wikipedia says:
With language-level support for delegation, this is done implicitly by having self in the delegate refer to the original (sending) object, not the delegate (receiving object).
Given the following Code:
interface BaseInterface {
fun print()
}
open class Base() : BaseInterface {
override fun print() { println(this) }
}
class Forwarded() {
private val base = Base()
fun print() { base.print() }
}
class Inherited() : Base() {}
class Delegated(delegate: BaseInterface) : BaseInterface by delegate
fun main(args: Array<String>) {
print("Forwarded: ")
Forwarded().print();
print("Inherited: ")
Inherited().print();
print("Delegated: ")
Delegated(Base()).print();
}
I get this output:
Forwarded: Base#7440e464
Inherited: Inherited#49476842
Delegated: Base#78308db1
I'd expect Delegated to return Delegated because self/this should refer to the original object. Do I get it wrong or is Kotlins delegation different?
Kotlin delegation is very simple - it generates all interface methods and implicitly invokes it on delegated object, except for methods explicitly overriden by the user.
Your example is functionally the same as:
class Delegated(delegate: BaseInterface) : BaseInterface{
// when generating bytecode kotlin assigns delegate object to internal final variable
// that is not visible at compile time
private val d = delegate
override fun print(){
d.print()
}
}
So it's pretty clear why it prints Base.
I think this is easiest to understand if we look at the decompiled Java bytecode this gets compiled into:
You can do this by going to Tools > Kotlin > Show Kotlin Bytecode and then clicking Decompile
public final class Delegated implements BaseInterface {
// $FF: synthetic field
private final BaseInterface $$delegate_0;
public Delegated(#NotNull BaseInterface delegate) {
Intrinsics.checkParameterIsNotNull(delegate, "delegate");
super();
this.$$delegate_0 = delegate;
}
public void print() {
this.$$delegate_0.print();
}
}
So when you do interface delegation what happens is that Kotlin creates field for the delegate named $$delegate_0 and adds methods in your delegating class which will operate on $$delegate_0. You can have multiple delegates as well, they will get their own fields. There is one caveat though: you can't access $$delegate_0 directly, not even if you make it a var like this:
class Delegated(var delegate: BaseInterface) : BaseInterface by delegate
This will compile to:
public final class Delegated implements BaseInterface {
#NotNull
private BaseInterface delegate;
// $FF: synthetic field
private final BaseInterface $$delegate_0;
#NotNull
public final BaseInterface getDelegate() {
return this.delegate;
}
public final void setDelegate(#NotNull BaseInterface var1) {
Intrinsics.checkParameterIsNotNull(var1, "<set-?>");
this.delegate = var1;
}
public Delegated(#NotNull BaseInterface delegate) {
Intrinsics.checkParameterIsNotNull(delegate, "delegate");
super();
this.$$delegate_0 = delegate;
this.delegate = delegate;
}
public void print() {
this.$$delegate_0.print();
}
}
sadly. I've written about this topic here.

Class is not abstract and does not implement abstract base class member

I'm confused by this Kotlin error associated with providing an implementation for an abstract class that has been imported from a maven package.
I have a maven library that is written in Kotlin and exposes an abstract class called APIGatewayRequestHandler. In my app that imports the library, I provide an implementation of the abstract class:
class GetWelcomeMessageHandler : APIGatewayRequestHandler<WelcomeMessage>()
fun handleAPIGatewayRequest(input: com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent, context: com.amazonaws.services.lambda.runtime.Context?): WelcomeMessage {
return WelcomeMessage()
}
}
The decompiled abstract class from the library looks like this:
public abstract class APIGatewayRequestHandler<T> public constructor() : com.amazonaws.services.lambda.runtime.RequestHandler<com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent, T> {
public abstract fun handleAPIGatewayRequest(input: com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent, context: com.amazonaws.services.lambda.runtime.Context?): T
public open fun handleRequest(input: com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent?, context: com.amazonaws.services.lambda.runtime.Context?): T {
/* compiled code */
}
}
I get the following error:
Class 'GetWelcomeMessageHandler' is not abstract and does not implement abstract base class member
public abstract fun handleAPIGatewayRequest(input: APIGatewayProxyRequestEvent, context: Context?): WelcomeMessage
I think you're just missing some override keywords. Namely, your abstract class should have it on the handleRequest method:
public abstract class APIGatewayRequestHandler<T> public constructor() : com.amazonaws.services.lambda.runtime.RequestHandler<com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent, T> {
public abstract fun handleAPIGatewayRequest(input: com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent, context: com.amazonaws.services.lambda.runtime.Context?): T
public override fun handleRequest(input: com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent?, context: com.amazonaws.services.lambda.runtime.Context?): T {
/* compiled code */
}
}
And then your GetWelcomeMessageHandler should have it on its handleAPIGatewayRequest method:
class GetWelcomeMessageHandler : APIGatewayRequestHandler<WelcomeMessage>() { // <-- This curly brace was also missing
override fun handleAPIGatewayRequest(input: com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent, context: com.amazonaws.services.lambda.runtime.Context?): WelcomeMessage {
return WelcomeMessage()
}
}