Skip to content

Instantly share code, notes, and snippets.

@jamierocks
Last active September 2, 2019 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamierocks/169d03318b8783df1b9a65d81bfbd5e5 to your computer and use it in GitHub Desktop.
Save jamierocks/169d03318b8783df1b9a65d81bfbd5e5 to your computer and use it in GitHub Desktop.
/*
* 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.dsl;
import org.cadixdev.lorenz.model.ExtensionKey;
interface IMappingDsl {
void setDeobf(final String name);
<D> void extension(final ExtensionKey<D> key, final D value);
}
/*
* 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.dsl
import org.cadixdev.bombe.type.FieldType
import org.cadixdev.bombe.type.MethodDescriptor
import org.cadixdev.bombe.type.signature.FieldSignature
import org.cadixdev.bombe.type.signature.MethodSignature
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
static MappingSet create(@DelegatesTo(strategy = Closure.DELEGATE_FIRST, value = MappingSetDsl) final Closure script) {
final MappingSet mappings = MappingSet.create()
script.resolveStrategy = Closure.DELEGATE_FIRST
script.delegate = new MappingSetDsl(mappings)
script()
return mappings
}
class MappingSetDsl {
private final MappingSet mappings
MappingSetDsl(final MappingSet mappings) {
this.mappings = mappings
}
TopLevelClassMapping klass(final String name,
@DelegatesTo(strategy = Closure.DELEGATE_FIRST, value = ClassMappingDsl) final Closure script) {
final TopLevelClassMapping mapping = this.mappings.getOrCreateTopLevelClassMapping(name)
script.resolveStrategy = Closure.DELEGATE_FIRST
script.delegate = new ClassMappingDsl(mapping)
script()
return mapping
}
}
class MappingDsl<T extends Mapping> implements IMappingDsl {
protected final T mapping
MappingDsl(final T mapping) {
this.mapping = mapping
}
@Override
void setDeobf(final String name) {
this.mapping.setDeobfuscatedName(name)
}
@Override
<D> void extension(final ExtensionKey<D> key, final D value) {
this.mapping.set(key, value)
}
}
class ClassMappingDsl extends MappingDsl<ClassMapping> {
ClassMappingDsl(final ClassMapping mapping) {
super(mapping)
}
FieldMapping field(final String name, final FieldType type = null,
@DelegatesTo(strategy = Closure.DELEGATE_FIRST, value = Mappings) final Closure script) {
final FieldMapping mapping = this.mapping.getOrCreateFieldMapping(new FieldSignature(name, type))
script.resolveStrategy = Closure.DELEGATE_FIRST
script.delegate = new MappingDsl(mapping)
script()
return mapping
}
MethodMapping method(final String name, final MethodDescriptor desc,
@DelegatesTo(strategy = Closure.DELEGATE_FIRST, value = MethodMappingDsl) final Closure script) {
final MethodMapping mapping = this.mapping.getOrCreateMethodMapping(new MethodSignature(name, desc))
script.resolveStrategy = Closure.DELEGATE_FIRST
script.delegate = new MethodMappingDsl(mapping)
script()
return mapping
}
MethodMapping method(final String name, final String desc,
@DelegatesTo(strategy = Closure.DELEGATE_FIRST, value = MethodMappingDsl) final Closure script) {
this.method(name, MethodDescriptor.of(desc), script)
}
InnerClassMapping klass(final String name,
@DelegatesTo(strategy = Closure.DELEGATE_FIRST, value = ClassMappingDsl) final Closure script) {
final InnerClassMapping mapping = this.mapping.getOrCreateInnerClassMapping(name)
script.resolveStrategy = Closure.DELEGATE_FIRST
script.delegate = new ClassMappingDsl(mapping)
script()
return mapping
}
}
class MethodMappingDsl extends MappingDsl<MethodMapping> {
MethodMappingDsl(final MethodMapping mapping) {
super(mapping)
}
MethodParameterMapping param(final int index,
@DelegatesTo(strategy = Closure.DELEGATE_FIRST, value = Mappings) final Closure script) {
final MethodParameterMapping mapping = this.mapping.getOrCreateParameterMapping(index)
script.resolveStrategy = Closure.DELEGATE_FIRST
script.delegate = new MappingDsl(mapping)
script()
return mapping
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment