# How to access \`extensions\` on any DSL object statically?

**URL:** <https://discuss.gradle.org/t/how-to-access-extensions-on-any-dsl-object-statically/28867>\
**Category:** Help/Discuss\
**Tags:** plugins\
**Created:** [September 29, 2018, 7:55pm UTC](https://discuss.gradle.org/t/how-to-access-extensions-on-any-dsl-object-statically/28867 "2018-09-29T19:55:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![TWiStErRob](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/twisterrob/32/7261_2.png) [@TWiStErRob](https://discuss.gradle.org/u/TWiStErRob)\
**Post date:** [September 29, 2018, 7:55pm UTC](https://discuss.gradle.org/t/how-to-access-extensions-on-any-dsl-object-statically/28867/1 "2018-09-29T19:55:21Z")

</div>

I’m trying to convert one of my plugins from Groovy to Kotlin and this is the last few lines of Groovy code in the project I cannot deal with:

```gradle
class Utils {
    /**
     * Call {@code extensions} property on the object dynamically.
     */
    static ExtensionContainer getExtensions(Object obj) {
        return obj.extensions
    }
}

```

Currently I have a workaround in Kotlin to help usages from Kotlin:

```gradle
/**
 * This should be `(this as dynamic).extensions as ExtensionContainer`, but `dynamic` is not allowed on JVM.
 * This version calls into Groovy so that Gradle's custom handlers (Decorated?) can respond correctly.
 */
internal val Any.extensions: ExtensionContainer get() = Utils.getExtensions(this)
// note: classpath for kotlinc is set up to see Groovy output so this is visible

```

I wonder how could I rewrite `Any.extensions` in pure Kotlin using statically typed public Groovy/Gradle APIs only.

An example usage:

```gradle
import org.gradle.kotlin.dsl.getByName
val git: GITPluginExtension = project
	// this one is using org.gradle.api.Project.getExtensions()
	.extensions.getByName<VCSPluginExtension>("vcs")
	// using my extension property as written above
	.extensions.create("git", GITPluginExtension::class.java)

```

---

<div class="post-metadata">

**Author:** ![rodrigo](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/rodrigo/32/3431_2.png) [@rodrigo](https://discuss.gradle.org/u/rodrigo)\
**Post date:** [September 30, 2018, 1:09pm UTC](https://discuss.gradle.org/t/how-to-access-extensions-on-any-dsl-object-statically/28867/2 "2018-09-30T13:09:49Z")

</div>

Hi Róbert,

The equivalent Kotlin extension would be:

```gradle
val Any.extensions get() = (this as org.gradle.api.plugins.ExtensionAware).extensions

```

Kind regards,  
Rodrigo

---

<div class="post-metadata">

**Author:** ![TWiStErRob](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/twisterrob/32/7261_2.png) [@TWiStErRob](https://discuss.gradle.org/u/TWiStErRob)\
**Post date:** [September 30, 2018, 3:45pm UTC](https://discuss.gradle.org/t/how-to-access-extensions-on-any-dsl-object-statically/28867/3 "2018-09-30T15:45:49Z")

</div>

Thanks Rodrigo, works like a charm!  
Such great pleasure to remove the last line of Groovy and `apply plugin: 'groovy'`.
