# Extension with a nested lazy property

**URL:** <https://discuss.gradle.org/t/extension-with-a-nested-lazy-property/41380>\
**Category:** Help/Discuss\
**Created:** [November 7, 2021, 10:23am UTC](https://discuss.gradle.org/t/extension-with-a-nested-lazy-property/41380 "2021-11-07T10:23:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![radudh](https://avatars.discourse-cdn.com/v4/letter/r/898d66/32.png) [@radudh](https://discuss.gradle.org/u/radudh)\
**Post date:** [November 7, 2021, 10:23am UTC](https://discuss.gradle.org/t/extension-with-a-nested-lazy-property/41380/1 "2021-11-07T10:23:46Z")

</div>

I want to build an extension that has a nested property inside.  
I’ve tried to use lazy properties and latest guidelines but it doesn’t work, so I think I’m missing something.

In the current form, the script crashes on evaluation, with message:

```gradle
> Failed to apply plugin class 'MyPlugin'.
   > Could not create an instance of type MyExtension.
      > Could not generate a decorated class for type MyExtension.
         > Cannot have abstract method MyExtension.getDevTarget().

```

I suspect the problem is in the line  
`abstract DevTarget getDevTarget()`  
Maybe the field is not managed? The correct syntax is different?

Gradle version is 6.9.1, java is 8, language is Groovy.  
The simplified code is below:

```gradle
apply plugin: MyPlugin

abstract class DevTarget {
    abstract Property<String> getUrl()
    abstract Property<String> getUser()
}

abstract class MyExtension {
    abstract DevTarget getDevTarget()
}

class MyPlugin implements Plugin<Project> {

    void apply(Project project) {
        project.extensions.create("myx", MyExtension)
    }
}

// test config
myx {
    devTarget {
        url = 'dev url'
        user = 'dev user'
    }
}

task show {
    doLast {
        println "DevTarget: ${myx.devTarget.url.get()}"
    }
}

```

---

<div class="post-metadata">

**Author:** ![Vampire](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/vampire/32/9082_2.png) [@Vampire](https://discuss.gradle.org/u/Vampire)\
**Post date:** [November 8, 2021, 10:26am UTC](https://discuss.gradle.org/t/extension-with-a-nested-lazy-property/41380/2 "2021-11-08T10:26:21Z")

</div>

```groovy
abstract class MyExtension {
    @Nested
    abstract DevTarget getDevTarget()
    def devTarget(Action<DevTarget> action) {
        action.execute(devTarget)
    }
}

```

---

<div class="post-metadata">

**Author:** ![radudh](https://avatars.discourse-cdn.com/v4/letter/r/898d66/32.png) [@radudh](https://discuss.gradle.org/u/radudh)\
**Post date:** [November 8, 2021, 12:44pm UTC](https://discuss.gradle.org/t/extension-with-a-nested-lazy-property/41380/3 "2021-11-08T12:44:48Z")

</div>

Indeed, this works, thank you.

Is this the best solution? I can live with it, but I wonder if there doesn’t exist a managed type that can solve this problem in a more concise way, without the @Nested annotation and the Action method:

`abstract SomeManagedType<DevTarget> getDevTarget()`

---

<div class="post-metadata">

**Author:** ![Vampire](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/vampire/32/9082_2.png) [@Vampire](https://discuss.gradle.org/u/Vampire)\
**Post date:** [November 8, 2021, 2:02pm UTC](https://discuss.gradle.org/t/extension-with-a-nested-lazy-property/41380/4 "2021-11-08T14:02:54Z")

</div>

At least none I’m aware of.
