Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Kotlin DSL #25

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ buildscript {
}
dependencies {
classpath 'gradle.plugin.net.minecrell:licenser:0.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
}
}

Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ inceptionYear = 2016
# Build Settings
javaVersion = 1.8
groovyVersion = 2.5.8
kotlinVersion = 1.3.21
bombeVersion = 0.4.0-SNAPSHOT
junitVersion = 5.5.1
spockVersion = 1.3-groovy-2.5
2 changes: 1 addition & 1 deletion lorenz-dsl-groovy/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ dependencies {

javadoc {
options.links(
'http://docs.groovy-lang.org/2.5.8/html/gapi/'
"http://docs.groovy-lang.org/$groovyVersion/html/gapi/"
)
}
13 changes: 13 additions & 0 deletions lorenz-dsl-kotlin/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apply plugin: 'org.jetbrains.kotlin.jvm'

dependencies {
compile 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
compile project(':lorenz')
}

compileKotlin {
kotlinOptions.jvmTarget = javaVersion
}
compileTestKotlin {
kotlinOptions.jvmTarget = javaVersion
}
4 changes: 4 additions & 0 deletions lorenz-dsl-kotlin/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name = Lorenz-DSL-Kotlin
description = A DSL for easing use of Lorenz within Kotlin projects.
url = https://www.jamiemansfield.me/projects/lorenz
inceptionYear = 2019
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
* This file is part of Lorenz, licensed under the MIT License (MIT).
*
* Copyright (c) Jamie Mansfield <https://www.jamierocks.uk/>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package org.cadixdev.lorenz.dsl.kotlin

import org.cadixdev.bombe.type.FieldType
import org.cadixdev.bombe.type.MethodDescriptor
import org.cadixdev.lorenz.MappingSet
import org.cadixdev.lorenz.model.ClassMapping
import org.cadixdev.lorenz.model.ExtensionKey
import org.cadixdev.lorenz.model.FieldMapping
import org.cadixdev.lorenz.model.InnerClassMapping
import org.cadixdev.lorenz.model.Mapping
import org.cadixdev.lorenz.model.MethodMapping
import org.cadixdev.lorenz.model.MethodParameterMapping
import org.cadixdev.lorenz.model.TopLevelClassMapping

/**
* A DSL to simplify the creation of [MappingSet]s in Kotlin.
*
* @author Jamie Mansfield
* @since 0.6.0
*/
class MappingSetDsl(val mappings: MappingSet) {

companion object {
fun create(init: MappingSetDsl.() -> Unit): MappingSet {
val mappings = MappingSet.create()
val dsl = MappingSetDsl(mappings)
dsl.init()
return mappings
}
}

inline fun klass(name: String, init: ClassMappingDsl<TopLevelClassMapping, MappingSet>.() -> Unit): TopLevelClassMapping {
val mapping = this.mappings.getOrCreateTopLevelClassMapping(name)
val dsl = ClassMappingDsl(mapping)
dsl.init()
return mapping
}

}

/**
* A DSL to simplify the manipulation of [Mapping]s in Kotlin.
*
* @param T The type of the mapping
* @param P The type of parent of the mapping
* @author Jamie Mansfield
* @since 0.6.0
*/
open class MappingDsl<T : Mapping<T, P>, P>(val mapping: T) {

inline var deobf: String
get() = this.mapping.deobfuscatedName
set(value) {
this.mapping.deobfuscatedName = value
}

fun <K> extension(key: ExtensionKey<K>, value: K) {
this.mapping.set(key, value)
}

}

/**
* A DSL to simplify the manipulation of [ClassMapping]s in Kotlin.
*
* @param T The type of the class mapping
* @param P The type of parent of the class mapping
* @author Jamie Mansfield
* @since 0.6.0
*/
class ClassMappingDsl<T : ClassMapping<T, P>, P>(mapping: T) : MappingDsl<T, P>(mapping) {

inline fun field(name: String, type: FieldType?, init: MappingDsl<FieldMapping, ClassMapping<*, *>>.() -> Unit): FieldMapping {
val mapping = this.mapping.getOrCreateFieldMapping(name, type)
val dsl = MappingDsl(mapping)
dsl.init()
return mapping
}

inline fun field(name: String, type: String, init: MappingDsl<FieldMapping, ClassMapping<*, *>>.() -> Unit): FieldMapping {
return this.field(name, FieldType.of(type), init)
}

inline fun field(name: String, init: MappingDsl<FieldMapping, ClassMapping<*, *>>.() -> Unit): FieldMapping {
return this.field(name, null as FieldType?, init)
}

inline fun method(name: String, desc: MethodDescriptor, init: MethodMappingDsl.() -> Unit): MethodMapping {
val mapping = this.mapping.getOrCreateMethodMapping(name, desc)
val dsl = MethodMappingDsl(mapping)
dsl.init()
return mapping
}

inline fun method(name: String, desc: String, init: MethodMappingDsl.() -> Unit): MethodMapping {
return this.method(name, MethodDescriptor.of(desc), init)
}

inline fun klass(name: String, init: ClassMappingDsl<InnerClassMapping, ClassMapping<*, *>>.() -> Unit): InnerClassMapping {
val mapping = this.mapping.getOrCreateInnerClassMapping(name)
val dsl = ClassMappingDsl(mapping)
dsl.init()
return mapping
}

}

/**
* A DSL to simplify the manipulation of [MethodMapping]s in Kotlin.
*
* @author Jamie Mansfield
* @since 0.6.0
*/
class MethodMappingDsl(mapping: MethodMapping) : MappingDsl<MethodMapping, ClassMapping<*, *>>(mapping) {

inline fun param(index: Int, init: MappingDsl<MethodParameterMapping, MethodMapping>.() -> Unit): MethodParameterMapping {
val mapping = this.mapping.getOrCreateParameterMapping(index)
val dsl = MappingDsl(mapping)
dsl.init()
return mapping
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* This file is part of Lorenz, licensed under the MIT License (MIT).
*
* Copyright (c) Jamie Mansfield <https://www.jamierocks.uk/>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package org.cadixdev.lorenz.dsl.kotlin

import org.cadixdev.lorenz.io.MappingFormats
import org.cadixdev.lorenz.model.ExtensionKey

val EXTRA = ExtensionKey(String::class.java, "extra")

fun main() {
val mappings = MappingSetDsl.create {
klass("a") {
deobf = "Example"
extension(EXTRA, "Hello, World!")
}
klass("b") {
deobf = "Demo"

field("g") {
deobf = "name"
}

method("h", "(Z)Ljava/lang/String;") {
deobf = "getName"
param(0) {
deobf = "propagate"
}
}

klass("d") {
deobf = "Inner"
}
}
}

MappingFormats.TSRG.createWriter(System.out).use {
it.write(mappings)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
* @author Jamie Mansfield
* @since 0.2.0
*/
public abstract class AbstractClassMappingImpl<M extends ClassMapping, P>
public abstract class AbstractClassMappingImpl<M extends ClassMapping<M, P>, P>
extends AbstractMappingImpl<M, P>
implements ClassMapping<M, P> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
* @author Jamie Mansfield
* @since 0.2.0
*/
public abstract class AbstractMappingImpl<M extends Mapping, P> implements Mapping<M, P> {
public abstract class AbstractMappingImpl<M extends Mapping<M, P>, P> implements Mapping<M, P> {

private final MappingSet mappings;
private final Map<ExtensionKey<?>, Object> data = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
* @author Jamie Mansfield
* @since 0.2.0
*/
public abstract class AbstractMemberMappingImpl<M extends MemberMapping, P extends Mapping>
public abstract class AbstractMemberMappingImpl<M extends MemberMapping<M, P>, P extends Mapping>
extends AbstractMappingImpl<M, P>
implements MemberMapping<M, P> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
* @author Jamie Mansfield
* @since 0.1.0
*/
public interface ClassMapping<M extends ClassMapping, P> extends Mapping<M, P>, InheritanceCompletable {
public interface ClassMapping<M extends ClassMapping<M, P>, P> extends Mapping<M, P>, InheritanceCompletable {

/**
* {@inheritDoc}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
* @author Jamie Mansfield
* @since 0.1.0
*/
public interface Mapping<M extends Mapping, P> extends Reversible<M, P> {
public interface Mapping<M extends Mapping<M, P>, P> extends Reversible<M, P> {

/**
* Gets the obfuscated name of the member being represented.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
* @author Jamie Mansfield
* @since 0.1.0
*/
public interface MemberMapping<M extends MemberMapping, P extends Mapping> extends Mapping<M, P> {
public interface MemberMapping<M extends MemberMapping<M, P>, P extends Mapping> extends Mapping<M, P> {

/**
* Gets the parent {@link Mapping} of this member mapping.
Expand Down
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ rootProject.name = name
include 'lorenz'
include 'lorenz-asm'
include 'lorenz-dsl-groovy'
include 'lorenz-dsl-kotlin'
include 'lorenz-io-enigma'
include 'lorenz-io-gson'
include 'lorenz-io-jam'
Expand Down