Rgfvfk Iff
Rgfvfk Iff

Reputation: 1609

Matching method name in kotlin/java

I was playing around with ObjectAnimator and I created a simple drawable.

// In my drawable file

fun setMyCustomParameter(...) {
    blah blah blah
}

To run the animation I have the following object animator:

val orreryAnimator = ObjectAnimator.ofObject(
            myDrawable,
            "MyCustomParameter",
            drawableEvaluator, // irrelevant
            startValues, // irrelevant 
            endValues // irrelevant
    )

Right now "MyCustomParameter" is a hardcoded string value that I match with my method setMyCustomParameter. The problem is that if I do not want to manually match it because if I accidentally misspell then the code will still run except the animation will have no effect. I want to ask is there a way to get the method setMycustomParameter as a string so that I can guarantee that even if I change the method name later on the code just runs without me having to remember changing the string value.

Upvotes: 1

Views: 86

Answers (1)

Alexey Romanov
Alexey Romanov

Reputation: 170723

You can: myDrawable::setMyCustomParameter will give you a reference to the method, which has a name: myDrawable::setMyCustomParameter.name. See Callable References in the documentation.

Also, if you have getMyCustomParameter and setMyCustomParameter, you may want to modify it to a property with custom getter and setter:

var myCustomParameter: Type
    get() = ...
    set(value: Type) { ... }

Upvotes: 2

Related Questions