Skip to content

MsRandom/jvm-class-processing

Repository files navigation

jvm-class-extensions

Allows extending classes within the same compilation unit as a simple Gradle task, for example:

package some.random.pkg;

// import ...;

public final class BaseClass {
    @ImplementedByExtension
    public BaseClass(int size) {
        throw NotImplementedException("BaseClass::new was not implemented properly.");
    }

    @ImplementedByExtension
    public int size() {
        throw NotImplementedException("BaseClass::size was not implemented properly.");
    }
}
package some.random.pkg.extensions;

// import ...;

@ClassExtension(BaseClass.class)
class SomeExtension {
    private final int size;

    @ImplementsBaseElement
    public SomeExtension(int size) {
        this.size = size;
    }

    @ImplementsBaseElement
    public int size() {
        return size;
    }
}

The class file processing has to be implemented after the normal compilation of the source files, this can be done easily from within Gradle;

Kotlin DSL
classExtensions {
    registerForSourceSet(sourceSets.main.get(), "some.random.pkg.extensions")
    registerForSourceSet(sourceSets.main.get(), kotlin.sourceSets.main.get().kotlin, "some.random.pkg.extensions")
    registerForSourceSet(sourceSets.main.get(), sourceSets.main.get().groovy, "some.random.pkg.extensions")
}
Groovy DSL
classExtensions {
    registerForSourceSet sourceSets.main, "some.random.pkg.extensions"
    registerForSourceSet sourceSets.main, kotlin.sourceSets.main.kotlin, "some.random.pkg.extensions"
    registerForSourceSet sourceSets.main, sourceSets.main.groovy, "some.random.pkg.extensions"
}

About

Allows running replacing or adding elements to classes via external extensions, similar to Kotlin expect/actual

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published