# Exposing legacy getter/setter as Kotlin property

**URL:** <https://discuss.kotlinlang.org/t/exposing-legacy-getter-setter-as-kotlin-property/12008>\
**Category:** Uncategorized\
**Created:** [March 18, 2019, 8:46pm UTC](https://discuss.kotlinlang.org/t/exposing-legacy-getter-setter-as-kotlin-property/12008 "2019-03-18T20:46:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kirill.grouchnikov](https://avatars.discourse-cdn.com/v4/letter/k/a183cd/32.png) [@kirill.grouchnikov](https://discuss.kotlinlang.org/u/kirill.grouchnikov)\
**Post date:** [March 18, 2019, 8:46pm UTC](https://discuss.kotlinlang.org/t/exposing-legacy-getter-setter-as-kotlin-property/12008/1 "2019-03-18T20:46:10Z")

</div>

Suppose I have a legacy Java class like this:

```java
public class MyClass {
   private String text;

   public String getText() {
      return this.text;
   }

   public void setText(String text) {
       this.text = text;
   }
}

```

Now in Kotlin I can do this:

```kotlin
val obj = MyClass()
obj.text = "something"

```

And this `.text` gets resolved to the underlying setting (and getter).

But I can’t seem to find a way to work with such a property via reflection. Is it possible to somehow expose it to a function that expects `KProperty` or `KMutableProperty` via `MyClass::text` or `obj::text`?

I can kind of get there by extending `MyClass` in Kotlin and adding a “redirection” property:

```kotlin
class MyKotlinClass : MyClass() {
    var myText: String
        get() = this.text
        set(value) {
            this.text = value
        }
}

```

First it’s not ideal as I effectively duplicate all the properties and try to find names that make sense. Second, it doesn’t even compile anymore for a function that expects a `KProperty`.

If I use `MyKotlinClass::myText` I get “Type mismatch. Required: KProperty. Found: KMutableProperty1”. If I use `obj::myText` I get “Type mismatch. Required: KProperty. Found: KMutableProperty0”.

What’s the idiomatic way of exposing a getter/setter pair from the Java side as `KProperty`?

---

<div class="post-metadata">

**Author:** ![pdvrieze](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/pdvrieze/32/1882_2.png) [@pdvrieze](https://discuss.kotlinlang.org/u/pdvrieze)\
**Post date:** [March 27, 2019, 12:09pm UTC](https://discuss.kotlinlang.org/t/exposing-legacy-getter-setter-as-kotlin-property/12008/2 "2019-03-27T12:09:16Z")

</div>

Unfortunately this is an area where compatibility is somewhat limited. There are a number of options. Probably the cleanest one is to create an extension property that forwards to the getter and setter so that the property can be accessed transparently. The extension property getter and setter can even be inline.

---

<div class="post-metadata">

**Author:** ![kirill.grouchnikov](https://avatars.discourse-cdn.com/v4/letter/k/a183cd/32.png) [@kirill.grouchnikov](https://discuss.kotlinlang.org/u/kirill.grouchnikov)\
**Post date:** [March 27, 2019, 12:21pm UTC](https://discuss.kotlinlang.org/t/exposing-legacy-getter-setter-as-kotlin-property/12008/3 "2019-03-27T12:21:25Z")

</div>

From what I saw, extension properties can not be used with :: syntax

---

<div class="post-metadata">

**Author:** ![pdvrieze](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/pdvrieze/32/1882_2.png) [@pdvrieze](https://discuss.kotlinlang.org/u/pdvrieze)\
**Post date:** [March 28, 2019, 9:54pm UTC](https://discuss.kotlinlang.org/t/exposing-legacy-getter-setter-as-kotlin-property/12008/4 "2019-03-28T21:54:02Z")

</div>

If you want that, you will have to reference the getter instead, or create the lambda manually.
