6

I have a static method declared in Java:

class X {
    public static void foo(Y y) { … }
}

I would love to use this method as extension method for instances of type Y in Kotlin:

import X.foo
…
y.foo()

Is that possible? I have control over all source code in question, e.g. to add annotations.

1 Answer 1

8

I don't know of a way to automatically refer to these, but writing your own extension that just wraps the existing method should be possible...

fun Y.foo() = X.foo(this)
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, that is of course possible but I would like to avoid having to write my own extension method for every static method that could (theoretically) be used as an extension method in Kotlin.
@Bombe So don't write the static method. Just write the extension method and it will be accessible from Java as a static method.
@EugenPechanec it’s all part of porting code from Java to Kotlin. Sooner or later the original method will be gone anyway but in the mean time I’d really like to simply add an annotation and be able to use it from Kotlin as an extension method. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.