repo
stringclasses
1k values
file_url
stringlengths
96
373
file_path
stringlengths
11
294
content
stringlengths
0
32.8k
language
stringclasses
1 value
license
stringclasses
6 values
commit_sha
stringclasses
1k values
retrieved_at
stringdate
2026-01-04 14:45:56
2026-01-04 18:30:23
truncated
bool
2 classes
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/kotlin/instruction/method/TupleToEmitter.java
src/main/java/org/spongepowered/despector/emitter/kotlin/instruction/method/TupleToEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.kotlin.instruction.method; import org.spongepowered.despector.ast.generic.TypeSignature; import org.spongepowered.despector.ast.stmt.invoke.StaticMethodInvoke; import org.spongepowered.despector.emitter.java.JavaEmitterContext; /** * An emitter for kotlin tuple creation methods. */ public class TupleToEmitter implements SpecialMethodEmitter<StaticMethodInvoke> { @Override public boolean emit(JavaEmitterContext ctx, StaticMethodInvoke arg, TypeSignature type) { if (arg.getParameters().length != 2) { return false; } ctx.emit(arg.getParameters()[0], null); ctx.printString(" to "); ctx.emit(arg.getParameters()[1], null); return true; } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/kotlin/instruction/method/StringConcatEmitter.java
src/main/java/org/spongepowered/despector/emitter/kotlin/instruction/method/StringConcatEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.kotlin.instruction.method; import com.google.common.collect.Lists; import org.spongepowered.despector.ast.generic.ClassTypeSignature; import org.spongepowered.despector.ast.generic.TypeSignature; import org.spongepowered.despector.ast.insn.Instruction; import org.spongepowered.despector.ast.insn.cst.StringConstant; import org.spongepowered.despector.ast.insn.var.LocalAccess; import org.spongepowered.despector.ast.stmt.invoke.InstanceMethodInvoke; import org.spongepowered.despector.ast.stmt.invoke.New; import org.spongepowered.despector.ast.stmt.invoke.StaticMethodInvoke; import org.spongepowered.despector.emitter.java.JavaEmitterContext; import java.util.List; /** * A special method emitter to convert a string builder chain into a string * concatenation with addition operators. */ public class StringConcatEmitter implements SpecialMethodEmitter<InstanceMethodInvoke> { @Override public boolean emit(JavaEmitterContext ctx, InstanceMethodInvoke arg, TypeSignature type) { // We detect and collapse string builder chains used to perform // string concatentation into simple "foo" + "bar" form boolean valid = true; Instruction callee = arg.getCallee(); List<Instruction> constants = Lists.newArrayList(); // We add all the constants to the front of this list as we have to // replay them in the reverse of the ordering that we will encounter // them in while (callee != null) { if (callee instanceof InstanceMethodInvoke) { InstanceMethodInvoke call = (InstanceMethodInvoke) callee; if (call.getParameters().length == 1) { constants.add(0, call.getParameters()[0]); callee = call.getCallee(); continue; } } else if (callee instanceof New) { New ref = (New) callee; if ("Ljava/lang/StringBuilder;".equals(ref.getType().getDescriptor())) { if (ref.getParameters().length == 1) { Instruction initial = ref.getParameters()[0]; if (initial instanceof StaticMethodInvoke) { StaticMethodInvoke valueof = (StaticMethodInvoke) initial; if (valueof.getMethodName().equals("valueOf") && valueof.getOwner().equals("Ljava/lang/String;")) { Instruction internal = valueof.getParameters()[0]; if (internal instanceof StringConstant) { initial = internal; } else if (internal instanceof LocalAccess) { LocalAccess local = (LocalAccess) internal; if (local.getLocal().getType().equals("Ljava/lang/String;")) { initial = local; } } } } constants.add(0, initial); } break; } valid = false; break; } valid = false; } if (valid) { boolean in_string = false; for (int i = 0; i < constants.size(); i++) { Instruction next = constants.get(i); if (next instanceof StringConstant) { if (!in_string) { if (i == 0) { ctx.printString("\""); } else { ctx.printString(" + \""); } in_string = true; } // TODO escape string ctx.printString(((StringConstant) next).getConstant()); continue; } else if (next instanceof LocalAccess) { if (!in_string && i < constants.size() - 1 && constants.get(i + 1) instanceof StringConstant) { in_string = true; ctx.printString("\""); } if (in_string) { ctx.printString("$"); ctx.printString(((LocalAccess) next).getLocal().getName()); } else { ctx.markWrapPoint(); ctx.printString(" + "); ctx.printString(((LocalAccess) next).getLocal().getName()); if (i < constants.size() - 1) { ctx.markWrapPoint(); ctx.printString(" + "); } } continue; } else if (next instanceof StaticMethodInvoke) { StaticMethodInvoke mth = (StaticMethodInvoke) next; if ("Lkotlin/text/StringsKt;".equals(mth.getOwner()) && "replace$default".equals(mth.getMethodName())) { if (!in_string) { if (i == 0) { ctx.printString("\""); } else { ctx.markWrapPoint(); ctx.printString(" + \""); } in_string = true; } ctx.printString("${"); ctx.emit(mth.getParameters()[0], ClassTypeSignature.STRING); ctx.printString(".replace("); ctx.emit(mth.getParameters()[1], ClassTypeSignature.STRING); ctx.printString(", "); ctx.emit(mth.getParameters()[2], ClassTypeSignature.STRING); ctx.printString(")}"); continue; } } if (in_string) { ctx.printString("\""); ctx.markWrapPoint(); ctx.printString(" + "); in_string = false; } ctx.emit(constants.get(i), ClassTypeSignature.STRING); if (i < constants.size() - 1) { ctx.markWrapPoint(); ctx.printString(" + "); } } if (in_string) { ctx.printString("\""); } return true; } return false; } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/kotlin/instruction/method/ListContainsEmitter.java
src/main/java/org/spongepowered/despector/emitter/kotlin/instruction/method/ListContainsEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.kotlin.instruction.method; import org.spongepowered.despector.ast.generic.TypeSignature; import org.spongepowered.despector.ast.stmt.invoke.InstanceMethodInvoke; import org.spongepowered.despector.emitter.java.JavaEmitterContext; /** * An emitter for kotlin collection contains methods. */ public class ListContainsEmitter implements SpecialMethodEmitter<InstanceMethodInvoke> { @Override public boolean emit(JavaEmitterContext ctx, InstanceMethodInvoke arg, TypeSignature type) { if (arg.getParameters().length != 1) { return false; } ctx.emit(arg.getParameters()[0], null); ctx.printString(" in "); ctx.emit(arg.getCallee(), null); return true; } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/kotlin/instruction/method/KotlinStaticMethodInvokeEmitter.java
src/main/java/org/spongepowered/despector/emitter/kotlin/instruction/method/KotlinStaticMethodInvokeEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.kotlin.instruction.method; import org.spongepowered.despector.ast.generic.ClassTypeSignature; import org.spongepowered.despector.ast.generic.TypeSignature; import org.spongepowered.despector.ast.insn.Instruction; import org.spongepowered.despector.ast.insn.cst.IntConstant; import org.spongepowered.despector.ast.insn.misc.NewArray; import org.spongepowered.despector.ast.stmt.invoke.StaticMethodInvoke; import org.spongepowered.despector.emitter.java.JavaEmitterContext; import org.spongepowered.despector.emitter.java.instruction.StaticMethodInvokeEmitter; import org.spongepowered.despector.util.TypeHelper; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; /** * An emitter for kotlin static methods. */ public class KotlinStaticMethodInvokeEmitter extends StaticMethodInvokeEmitter { private static final Set<String> IGNORED_METHODS = new HashSet<>(); private static final Set<String> NO_CALLEE = new HashSet<>(); private static final Map<String, SpecialMethodEmitter<StaticMethodInvoke>> SPECIAL = new HashMap<>(); static { IGNORED_METHODS.add("Ljava/lang/Integer;valueOf"); IGNORED_METHODS.add("Ljava/lang/String;valueOf"); NO_CALLEE.add("Lkotlin/collections/CollectionsKt;"); NO_CALLEE.add("Lkotlin/collections/MapsKt;"); SPECIAL.put("Lkotlin/TuplesKt;to", new TupleToEmitter()); SPECIAL.put("Lkotlin/jvm/internal/Intrinsics;areEqual", new EqualityEmitter()); } @Override public void emit(JavaEmitterContext ctx, StaticMethodInvoke arg, TypeSignature type) { String key = arg.getOwner() + arg.getMethodName(); SpecialMethodEmitter<StaticMethodInvoke> special = SPECIAL.get(key); if (special != null && special.emit(ctx, arg, type)) { return; } if (IGNORED_METHODS.contains(key) && arg.getParameters().length == 1) { ctx.emit(arg.getParameters()[0], ClassTypeSignature.of(arg.getReturnType())); return; } String owner = TypeHelper.descToType(arg.getOwner()); if (arg.getMethodName().startsWith("access$") && ctx.getType() != null) { if (replaceSyntheticAccessor(ctx, arg, owner)) { return; } } if (arg.getMethodName().endsWith("$default")) { callDefaultMethod(ctx, arg); return; } if (!NO_CALLEE.contains(arg.getOwner()) && (ctx.getType() == null || !owner.equals(ctx.getType().getName()))) { ctx.emitTypeName(owner); ctx.printString("."); } ctx.printString(arg.getMethodName()); List<String> param_types = TypeHelper.splitSig(arg.getMethodDescription()); ctx.printString("("); for (int i = 0; i < arg.getParameters().length; i++) { Instruction param = arg.getParameters()[i]; if (i == arg.getParameters().length - 1 && param instanceof NewArray) { NewArray varargs = (NewArray) param; for (int o = 0; o < varargs.getInitializer().length; o++) { ctx.markWrapPoint(); ctx.emit(varargs.getInitializer()[o], varargs.getType()); if (o < varargs.getInitializer().length - 1) { ctx.printString(", "); } } break; } ctx.emit(param, ClassTypeSignature.of(param_types.get(i))); if (i < arg.getParameters().length - 1) { ctx.printString(", "); ctx.markWrapPoint(); } } ctx.printString(")"); } /** * Unwraps a call to a default method to determine the default parameter * values. */ public void callDefaultMethod(JavaEmitterContext ctx, StaticMethodInvoke call) { Instruction callee = call.getParameters()[0]; int set = ((IntConstant) call.getParameters()[call.getParameters().length - 2]).getConstant(); int total_args = call.getParameters().length - 3; ctx.emit(callee, null); ctx.printString("."); ctx.printString(call.getMethodName().substring(0, call.getMethodName().length() - 8)); List<String> param_types = TypeHelper.splitSig(call.getMethodDescription()); ctx.printString("("); boolean first = true; for (int i = 0; i < total_args; i++) { if ((set & (1 << i)) != 0) { continue; } if (!first) { first = false; ctx.printString(", "); ctx.markWrapPoint(); } Instruction param = call.getParameters()[i + 1]; if (i == total_args - 1 && param instanceof NewArray) { NewArray varargs = (NewArray) param; for (int o = 0; o < varargs.getInitializer().length; o++) { ctx.markWrapPoint(); ctx.emit(varargs.getInitializer()[o], varargs.getType()); if (o < varargs.getInitializer().length - 1) { ctx.printString(", "); } } break; } ctx.emit(param, ClassTypeSignature.of(param_types.get(i))); } ctx.printString(")"); } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/kotlin/instruction/method/package-info.java
src/main/java/org/spongepowered/despector/emitter/kotlin/instruction/method/package-info.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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. */ @org.spongepowered.despector.util.NonnullByDefault package org.spongepowered.despector.emitter.kotlin.instruction.method;
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/kotlin/instruction/method/MapPutEmitter.java
src/main/java/org/spongepowered/despector/emitter/kotlin/instruction/method/MapPutEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.kotlin.instruction.method; import org.spongepowered.despector.ast.generic.TypeSignature; import org.spongepowered.despector.ast.stmt.invoke.InstanceMethodInvoke; import org.spongepowered.despector.emitter.java.JavaEmitterContext; /** * An emitter for kotlin map assignments. */ public class MapPutEmitter implements SpecialMethodEmitter<InstanceMethodInvoke> { @Override public boolean emit(JavaEmitterContext ctx, InstanceMethodInvoke arg, TypeSignature type) { if (arg.getParameters().length != 2) { return false; } ctx.emit(arg.getCallee(), null); ctx.printString("["); ctx.emit(arg.getParameters()[0], null); ctx.printString("] = "); ctx.emit(arg.getParameters()[1], null); return true; } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/kotlin/instruction/method/KotlinInstanceMethodInvokeEmitter.java
src/main/java/org/spongepowered/despector/emitter/kotlin/instruction/method/KotlinInstanceMethodInvokeEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.kotlin.instruction.method; import org.spongepowered.despector.ast.generic.ClassTypeSignature; import org.spongepowered.despector.ast.generic.TypeSignature; import org.spongepowered.despector.ast.insn.Instruction; import org.spongepowered.despector.ast.insn.misc.NewArray; import org.spongepowered.despector.ast.insn.var.LocalAccess; import org.spongepowered.despector.ast.stmt.invoke.InstanceMethodInvoke; import org.spongepowered.despector.emitter.java.JavaEmitterContext; import org.spongepowered.despector.emitter.java.instruction.InstanceMethodInvokeEmitter; import org.spongepowered.despector.util.TypeHelper; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; /** * An emitter for kotlin instance method invoke instructions. */ public class KotlinInstanceMethodInvokeEmitter extends InstanceMethodInvokeEmitter { private static final Set<String> NO_CALLEE = new HashSet<>(); private static final Set<String> NO_PARAMS = new HashSet<>(); private static final Map<String, SpecialMethodEmitter<InstanceMethodInvoke>> SPECIAL = new HashMap<>(); static { NO_CALLEE.add("Ljava/io/PrintStream;println"); NO_CALLEE.add("Ljava/io/PrintStream;print"); NO_PARAMS.add("Ljava/lang/String;length"); SPECIAL.put("Ljava/lang/StringBuilder;toString", new StringConcatEmitter()); SPECIAL.put("Ljava/util/List;contains", new ListContainsEmitter()); MapGetEmitter map_get = new MapGetEmitter(); SPECIAL.put("Ljava/util/Map;get", map_get); SPECIAL.put("Ljava/util/HashMap;get", map_get); SPECIAL.put("Ljava/lang/String;charAt", map_get); // TODO operator overloading, any get method can be simplified in this way MapPutEmitter map_put = new MapPutEmitter(); SPECIAL.put("Ljava/util/Map;put", map_put); SPECIAL.put("Ljava/util/HashMap;put", map_put); } @Override public void emit(JavaEmitterContext ctx, InstanceMethodInvoke arg, TypeSignature type) { String key = arg.getOwner() + arg.getMethodName(); SpecialMethodEmitter<InstanceMethodInvoke> special = SPECIAL.get(key); if (special != null && special.emit(ctx, arg, type)) { return; } if (arg.getMethodName().equals("<init>")) { if (ctx.getType() != null) { if (arg.getOwnerName().equals(ctx.getType().getName())) { ctx.printString("this"); } else { ctx.printString("super"); } } else { ctx.printString("super"); } } else { if (!NO_CALLEE.contains(key)) { if (arg.getCallee() instanceof LocalAccess && ctx.getMethod() != null && !ctx.getMethod().isStatic()) { LocalAccess local = (LocalAccess) arg.getCallee(); if (local.getLocal().getIndex() == 0) { if (ctx.getType() != null && !arg.getOwnerName().equals(ctx.getType().getName())) { ctx.printString("super."); } } else { ctx.emit(local, null); ctx.printString("."); } } else { ctx.emit(arg.getCallee(), ClassTypeSignature.of(arg.getOwner())); ctx.printString("."); } } ctx.printString(arg.getMethodName()); } if (NO_PARAMS.contains(key)) { return; } ctx.printString("("); List<String> param_types = TypeHelper.splitSig(arg.getMethodDescription()); for (int i = 0; i < arg.getParameters().length; i++) { Instruction param = arg.getParameters()[i]; if (i == arg.getParameters().length - 1 && param instanceof NewArray) { NewArray varargs = (NewArray) param; for (int o = 0; o < varargs.getInitializer().length; o++) { ctx.emit(varargs.getInitializer()[o], varargs.getType()); if (o < varargs.getInitializer().length - 1) { ctx.printString(", "); ctx.markWrapPoint(); } } break; } ctx.emit(param, ClassTypeSignature.of(param_types.get(i))); if (i < arg.getParameters().length - 1) { ctx.printString(", "); ctx.markWrapPoint(); } } ctx.printString(")"); } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/kotlin/instruction/method/EqualityEmitter.java
src/main/java/org/spongepowered/despector/emitter/kotlin/instruction/method/EqualityEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.kotlin.instruction.method; import org.spongepowered.despector.ast.generic.TypeSignature; import org.spongepowered.despector.ast.stmt.invoke.StaticMethodInvoke; import org.spongepowered.despector.emitter.java.JavaEmitterContext; /** * An emitter for kotlin equals methods. */ public class EqualityEmitter implements SpecialMethodEmitter<StaticMethodInvoke> { @Override public boolean emit(JavaEmitterContext ctx, StaticMethodInvoke arg, TypeSignature type) { if (arg.getParameters().length != 2) { return false; } ctx.emit(arg.getParameters()[0], null); ctx.printString(" == "); ctx.emit(arg.getParameters()[1], null); return true; } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/kotlin/instruction/method/SpecialMethodEmitter.java
src/main/java/org/spongepowered/despector/emitter/kotlin/instruction/method/SpecialMethodEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.kotlin.instruction.method; import org.spongepowered.despector.ast.generic.TypeSignature; import org.spongepowered.despector.ast.stmt.invoke.MethodInvoke; import org.spongepowered.despector.emitter.java.JavaEmitterContext; /** * A special method emitter for custom handling of certain methods. */ public interface SpecialMethodEmitter<T extends MethodInvoke> { /** * Emits the given method. */ boolean emit(JavaEmitterContext ctx, T arg, TypeSignature type); }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/kotlin/instruction/method/MapGetEmitter.java
src/main/java/org/spongepowered/despector/emitter/kotlin/instruction/method/MapGetEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.kotlin.instruction.method; import org.spongepowered.despector.ast.generic.TypeSignature; import org.spongepowered.despector.ast.stmt.invoke.InstanceMethodInvoke; import org.spongepowered.despector.emitter.java.JavaEmitterContext; /** * An emitter for kotlin map getters. */ public class MapGetEmitter implements SpecialMethodEmitter<InstanceMethodInvoke> { @Override public boolean emit(JavaEmitterContext ctx, InstanceMethodInvoke arg, TypeSignature type) { if (arg.getParameters().length != 1) { return false; } ctx.emit(arg.getCallee(), null); ctx.printString("["); ctx.emit(arg.getParameters()[0], null); ctx.printString("]"); return true; } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/kotlin/special/KotlinRangeEmitter.java
src/main/java/org/spongepowered/despector/emitter/kotlin/special/KotlinRangeEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.kotlin.special; import org.spongepowered.despector.ast.generic.TypeSignature; import org.spongepowered.despector.ast.insn.condition.AndCondition; import org.spongepowered.despector.ast.insn.condition.CompareCondition; import org.spongepowered.despector.ast.insn.condition.Condition; import org.spongepowered.despector.ast.insn.cst.IntConstant; import org.spongepowered.despector.ast.insn.misc.Ternary; import org.spongepowered.despector.emitter.java.JavaEmitterContext; import org.spongepowered.despector.transform.matcher.ConditionMatcher; import org.spongepowered.despector.transform.matcher.InstructionMatcher; public class KotlinRangeEmitter { public static final ConditionMatcher<?> RANGE_MATCHER = ConditionMatcher.and() .operand(ConditionMatcher.compare() .left(InstructionMatcher.intConstant().build()) .build()) .operand(ConditionMatcher.compare() .right(InstructionMatcher.intConstant().build()) .build()) .build(); public static boolean checkRange(JavaEmitterContext ctx, Condition cond, boolean inverse) { if (!RANGE_MATCHER.matches(cond)) { return false; } AndCondition range = (AndCondition) cond; CompareCondition low = (CompareCondition) range.getOperands().get(0); CompareCondition high = (CompareCondition) range.getOperands().get(1); if (!low.getRight().equals(high.getLeft())) { return false; } ctx.emit(low.getRight(), null); ctx.printString(" "); ctx.printString("!", inverse); ctx.printString("in "); TypeSignature type = low.getRight().inferType(); ctx.emit(low.getLeft(), type); ctx.printString(".."); ctx.emit(high.getRight(), type); return true; } public static boolean checkRangeTernary(JavaEmitterContext ctx, Ternary ternary) { if (!(ternary.getFalseValue() instanceof IntConstant) || !(ternary.getTrueValue() instanceof IntConstant)) { return false; } int tr = ((IntConstant) ternary.getTrueValue()).getConstant(); int fl = ((IntConstant) ternary.getFalseValue()).getConstant(); if (tr == 0 && fl == 1) { return checkRange(ctx, ternary.getCondition(), true); } else if (tr == 1 && fl == 0) { return checkRange(ctx, ternary.getCondition(), false); } return false; } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/kotlin/special/KotlinCompanionClassEmitter.java
src/main/java/org/spongepowered/despector/emitter/kotlin/special/KotlinCompanionClassEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.kotlin.special; import org.spongepowered.despector.ast.Annotation; import org.spongepowered.despector.ast.generic.TypeSignature; import org.spongepowered.despector.ast.stmt.Statement; import org.spongepowered.despector.ast.stmt.StatementBlock; import org.spongepowered.despector.ast.stmt.StatementBlock.Type; import org.spongepowered.despector.ast.stmt.invoke.InvokeStatement; import org.spongepowered.despector.ast.stmt.invoke.StaticMethodInvoke; import org.spongepowered.despector.ast.stmt.misc.Return; import org.spongepowered.despector.ast.type.ClassEntry; import org.spongepowered.despector.ast.type.MethodEntry; import org.spongepowered.despector.emitter.SpecialEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; import org.spongepowered.despector.emitter.kotlin.KotlinEmitterUtil; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * An emitter for kotlin companion classes. */ public class KotlinCompanionClassEmitter implements SpecialEmitter { /** * Emits the given companion class. */ public void emit(JavaEmitterContext ctx, ClassEntry type) { ctx.printIndentation(); ctx.printString("companion object {"); ctx.newLine().indent(); Map<String, Field> fields = new HashMap<>(); for (MethodEntry mth : type.getMethods()) { if ("<init>".equals(mth.getName())) { continue; } if (mth.getName().startsWith("get")) { String name = mth.getName().substring(3); name = name.substring(0, 1).toLowerCase() + name.substring(1); Field fld = fields.get(name); if (fld == null) { fld = new Field(); fld.name = name; fields.put(name, fld); } fld.type = mth.getReturnType(); fld.getter = new StatementBlock(Type.METHOD); for (Statement stmt : mth.getInstructions().getStatements()) { if (stmt instanceof InvokeStatement) { InvokeStatement istmt = (InvokeStatement) stmt; if (istmt.getInstruction() instanceof StaticMethodInvoke) { StaticMethodInvoke invoke = (StaticMethodInvoke) istmt.getInstruction(); if (invoke.getOwner().equals("Lkotlin/jvm/internal/Intrinsics;")) { continue; } } } fld.getter.append(stmt); } } } for (MethodEntry mth : type.getStaticMethods()) { if (mth.getName().endsWith("$annotations")) { String name = mth.getName().substring(0, mth.getName().length() - 12); Field fld = fields.get(name); if (fld == null) { fld = new Field(); fld.name = name; fields.put(name, fld); } fld.annotations.addAll(mth.getAnnotations()); } } for (Field fld : fields.values()) { for (Annotation anno : fld.annotations) { ctx.printIndentation(); ctx.emit(anno); ctx.newLine(); } ctx.printIndentation(); ctx.printString("val "); ctx.printString(fld.name); ctx.printString(": "); KotlinEmitterUtil.emitType(ctx, fld.type); if (fld.getter != null) { ctx.newLine(); ctx.indent(); ctx.printIndentation(); ctx.printString("get()"); if (fld.getter.getStatementCount() == 1) { ctx.printString(" = "); Statement value = fld.getter.getStatement(0); if (value instanceof Return) { ctx.emit(((Return) value).getValue().get(), fld.type); } else { ctx.emit(value, false); } ctx.newLine(); } else { ctx.printString(" = {"); ctx.newLine(); ctx.indent(); for (Statement stmt : fld.getter.getStatements()) { ctx.printIndentation(); ctx.emit(stmt, ctx.usesSemicolons()); ctx.newLine(); } ctx.dedent(); ctx.printIndentation(); ctx.printString("}"); ctx.newLine(); } } } ctx.dedent(); ctx.printIndentation(); ctx.printString("}"); ctx.newLine(); } /** * A representation of a field. */ private static class Field { public List<Annotation> annotations = new ArrayList<>(); public String name; public TypeSignature type; public StatementBlock getter; public Field() { } } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/kotlin/special/package-info.java
src/main/java/org/spongepowered/despector/emitter/kotlin/special/package-info.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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. */ @org.spongepowered.despector.util.NonnullByDefault package org.spongepowered.despector.emitter.kotlin.special;
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/kotlin/special/KotlinGenericsEmitter.java
src/main/java/org/spongepowered/despector/emitter/kotlin/special/KotlinGenericsEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.kotlin.special; import org.spongepowered.despector.ast.generic.ClassTypeSignature; import org.spongepowered.despector.ast.generic.GenericClassTypeSignature; import org.spongepowered.despector.ast.generic.TypeSignature; import org.spongepowered.despector.ast.generic.TypeVariableSignature; import org.spongepowered.despector.ast.generic.VoidTypeSignature; import org.spongepowered.despector.emitter.java.JavaEmitterContext; import org.spongepowered.despector.emitter.java.special.GenericsEmitter; import org.spongepowered.despector.emitter.kotlin.KotlinEmitterUtil; /** * An emitter for kotlin generics. */ public class KotlinGenericsEmitter extends GenericsEmitter { @Override public void emitTypeSignature(JavaEmitterContext ctx, TypeSignature sig, boolean is_varargs) { if (sig instanceof TypeVariableSignature) { int array_depth = 0; String type = ((TypeVariableSignature) sig).getIdentifier(); while (type.startsWith("[")) { array_depth++; type = type.substring(1); ctx.printString("Array<"); } ctx.printString(type.substring(1, type.length() - 1)); for (int i = 0; i < array_depth; i++) { ctx.printString(">"); } } else if (sig instanceof ClassTypeSignature) { ClassTypeSignature cls = (ClassTypeSignature) sig; int array_depth = 0; String type = cls.getType(); while (type.startsWith("[")) { array_depth++; type = type.substring(1); ctx.printString("Array<"); } KotlinEmitterUtil.emitType(ctx, type); for (int i = 0; i < array_depth; i++) { ctx.printString(">"); } } else if (sig instanceof GenericClassTypeSignature) { GenericClassTypeSignature cls = (GenericClassTypeSignature) sig; int array_depth = 0; String type = cls.getType(); while (type.startsWith("[")) { array_depth++; type = type.substring(1); ctx.printString("Array<"); } KotlinEmitterUtil.emitType(ctx, type); emitTypeArguments(ctx, cls.getArguments()); for (int i = 0; i < array_depth; i++) { ctx.printString(">"); } } else if (sig instanceof VoidTypeSignature) { ctx.printString("void"); } } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/kotlin/special/KotlinDataClassEmitter.java
src/main/java/org/spongepowered/despector/emitter/kotlin/special/KotlinDataClassEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.kotlin.special; import org.spongepowered.despector.ast.generic.TypeSignature; import org.spongepowered.despector.ast.insn.Instruction; import org.spongepowered.despector.ast.insn.var.LocalAccess; import org.spongepowered.despector.ast.stmt.Statement; import org.spongepowered.despector.ast.stmt.assign.FieldAssignment; import org.spongepowered.despector.ast.stmt.assign.LocalAssignment; import org.spongepowered.despector.ast.stmt.branch.If; import org.spongepowered.despector.ast.type.ClassEntry; import org.spongepowered.despector.ast.type.FieldEntry; import org.spongepowered.despector.ast.type.MethodEntry; import org.spongepowered.despector.ast.type.TypeEntry.InnerClassInfo; import org.spongepowered.despector.emitter.SpecialEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; import org.spongepowered.despector.emitter.kotlin.KotlinEmitterUtil; import java.util.LinkedHashMap; import java.util.Map; /** * An emitter for kotlin data classes. */ public class KotlinDataClassEmitter implements SpecialEmitter { /** * Emits the given data class. */ public void emit(JavaEmitterContext ctx, ClassEntry type) { ctx.printIndentation(); ctx.printString("data class "); InnerClassInfo inner_info = null; if (type.isInnerClass() && ctx.getOuterType() != null) { inner_info = ctx.getOuterType().getInnerClassInfo(type.getName()); } if (inner_info != null) { ctx.printString(inner_info.getSimpleName()); } else { String name = type.getName().replace('/', '.'); if (name.indexOf('.') != -1) { name = name.substring(name.lastIndexOf('.') + 1, name.length()); } name = name.replace('$', '.'); ctx.printString(name); } ctx.printString("("); ctx.newLine(); ctx.indent(); Map<String, DataField> fields = new LinkedHashMap<>(); for (FieldEntry fld : type.getFields()) { DataField data = new DataField(); data.name = fld.getName(); data.type = fld.getType(); data.is_final = fld.isFinal(); fields.put(data.name, data); } DataField[] fields_ordered = new DataField[fields.size()]; int longest = 0; MethodEntry ctor = null; MethodEntry ctor_smaller = null; for (MethodEntry mth : type.getMethods()) { if ("<init>".equals(mth.getName()) && mth.getParamTypes().size() > longest) { longest = mth.getParamTypes().size(); ctor_smaller = ctor; ctor = mth; } } if (ctor_smaller == null) { ctor_smaller = ctor; } for (Statement stmt : ctor_smaller.getInstructions().getStatements()) { if (!(stmt instanceof FieldAssignment)) { continue; } FieldAssignment assign = (FieldAssignment) stmt; DataField data = fields.get(assign.getFieldName()); LocalAccess local = (LocalAccess) assign.getValue(); fields_ordered[local.getLocal().getIndex() - 1] = data; } if (ctor_smaller != ctor) { for (Statement stmt : ctor.getInstructions().getStatements()) { if (!(stmt instanceof If)) { continue; } LocalAssignment assign = (LocalAssignment) ((If) stmt).getBody().getStatement(0); fields_ordered[assign.getLocal().getIndex() - 1].default_val = assign.getValue(); } } for (int i = 0; i < fields_ordered.length; i++) { DataField fld = fields_ordered[i]; ctx.printIndentation(); if(fld.is_final) { ctx.printString("val "); } else { ctx.printString("var "); } ctx.printString(fld.name); ctx.printString(": "); KotlinEmitterUtil.emitType(ctx, fld.type); if (fld.default_val != null) { ctx.printString(" = "); ctx.emit(fld.default_val, fld.type); } if (i != fields_ordered.length - 1) { ctx.printString(","); } ctx.newLine(); } ctx.dedent(); ctx.printIndentation(); ctx.printString(")"); ctx.newLine(); } /** * A data field. */ private static class DataField { public String name; public TypeSignature type; public boolean is_final; public Instruction default_val; public DataField() { } } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/kotlin/special/KotlinPackageEmitter.java
src/main/java/org/spongepowered/despector/emitter/kotlin/special/KotlinPackageEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.kotlin.special; import org.spongepowered.despector.emitter.java.JavaEmitterContext; import org.spongepowered.despector.emitter.java.special.PackageEmitter; /** * Emits a kotlin package. */ public class KotlinPackageEmitter extends PackageEmitter { @Override public void emitPackage(JavaEmitterContext ctx, String pkg) { ctx.printString("package "); ctx.printString(pkg); } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/kotlin/statement/KotlinForEmitter.java
src/main/java/org/spongepowered/despector/emitter/kotlin/statement/KotlinForEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.kotlin.statement; import org.spongepowered.despector.ast.Locals.LocalInstance; import org.spongepowered.despector.ast.generic.ClassTypeSignature; import org.spongepowered.despector.ast.insn.Instruction; import org.spongepowered.despector.ast.stmt.assign.LocalAssignment; import org.spongepowered.despector.ast.stmt.branch.For; import org.spongepowered.despector.ast.stmt.invoke.StaticMethodInvoke; import org.spongepowered.despector.emitter.java.JavaEmitterContext; import org.spongepowered.despector.emitter.java.statement.ForEmitter; import org.spongepowered.despector.transform.matcher.ConditionMatcher; import org.spongepowered.despector.transform.matcher.InstructionMatcher; import org.spongepowered.despector.transform.matcher.MatchContext; import org.spongepowered.despector.transform.matcher.StatementMatcher; /** * An emitter for kotlin for loops. */ public class KotlinForEmitter extends ForEmitter { private static final StatementMatcher<For> CHAR_ITERATOR = StatementMatcher.forLoop() .init(MatchContext.storeLocal("loop_iterator", StatementMatcher.localAssign() .value(InstructionMatcher.staticInvoke() .owner("Lkotlin/text/StringsKt;") .name("iterator") .desc("(Ljava/lang/CharSequence;)Lkotlin/collections/CharIterator;") .build()) .build())) .condition(ConditionMatcher.bool() .value(InstructionMatcher.instanceMethodInvoke() .name("hasNext") .desc("()Z") .callee(InstructionMatcher.localAccess() .fromContext("loop_iterator") .build()) .build()) .build()) .incr(StatementMatcher.NONE) .body(0, StatementMatcher.localAssign() .value(InstructionMatcher.instanceMethodInvoke() .owner("Lkotlin/collections/CharIterator;") .name("nextChar") .callee(InstructionMatcher.localAccess() .fromContext("loop_iterator") .build()) .build()) .build()) .build(); @Override public void emit(JavaEmitterContext ctx, For loop, boolean semicolon) { if (checkCharIterator(ctx, loop)) { return; } super.emit(ctx, loop, semicolon); } /** * Checks if the given for loop is an iterator over characters in a string. */ public boolean checkCharIterator(JavaEmitterContext ctx, For loop) { if (!CHAR_ITERATOR.matches(MatchContext.create(), loop)) { return false; } LocalInstance local = ((LocalAssignment) loop.getBody().getStatement(0)).getLocal(); Instruction str = ((StaticMethodInvoke) ((LocalAssignment) loop.getInit()).getValue()).getParameters()[0]; ctx.printString("for ("); ctx.printString(local.getName()); ctx.printString(" in "); ctx.emit(InstructionMatcher.unwrapCast(str), ClassTypeSignature.STRING); ctx.printString(") {"); ctx.newLine(); ctx.indent(); ctx.emitBody(loop.getBody(), 1); ctx.dedent(); ctx.newLine(); ctx.printIndentation(); ctx.printString("}"); return true; } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/kotlin/statement/package-info.java
src/main/java/org/spongepowered/despector/emitter/kotlin/statement/package-info.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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. */ @org.spongepowered.despector.util.NonnullByDefault package org.spongepowered.despector.emitter.kotlin.statement;
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/kotlin/statement/KotlinInvokeEmitter.java
src/main/java/org/spongepowered/despector/emitter/kotlin/statement/KotlinInvokeEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.kotlin.statement; import org.spongepowered.despector.ast.insn.Instruction; import org.spongepowered.despector.ast.stmt.invoke.InstanceMethodInvoke; import org.spongepowered.despector.ast.stmt.invoke.InvokeStatement; import org.spongepowered.despector.ast.stmt.invoke.StaticMethodInvoke; import org.spongepowered.despector.emitter.StatementEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; /** * An emitter kotlin invoke statements. */ public class KotlinInvokeEmitter implements StatementEmitter<JavaEmitterContext, InvokeStatement> { @Override public void emit(JavaEmitterContext ctx, InvokeStatement insn, boolean semicolon) { Instruction i = insn.getInstruction(); if (i instanceof InstanceMethodInvoke) { InstanceMethodInvoke mth = (InstanceMethodInvoke) i; if (mth.getMethodName().equals("<init>") && mth.getParameters().length == 0) { return; } } else if (i instanceof StaticMethodInvoke) { StaticMethodInvoke mth = (StaticMethodInvoke) i; if ("Lkotlin/jvm/internal/Intrinsics;".equals(mth.getOwner())) { return; } } ctx.emit(i, null); } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/kotlin/statement/KotlinForEachEmitter.java
src/main/java/org/spongepowered/despector/emitter/kotlin/statement/KotlinForEachEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.kotlin.statement; import org.spongepowered.despector.ast.Locals.LocalInstance; import org.spongepowered.despector.ast.insn.Instruction; import org.spongepowered.despector.ast.stmt.assign.LocalAssignment; import org.spongepowered.despector.ast.stmt.branch.ForEach; import org.spongepowered.despector.ast.stmt.invoke.InstanceMethodInvoke; import org.spongepowered.despector.emitter.StatementEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; import org.spongepowered.despector.transform.matcher.InstructionMatcher; import org.spongepowered.despector.transform.matcher.MatchContext; import org.spongepowered.despector.transform.matcher.StatementMatcher; /** * An emitter for kotlin for-each loops. */ public class KotlinForEachEmitter implements StatementEmitter<JavaEmitterContext, ForEach> { private static final StatementMatcher<ForEach> MAP_ITERATOR = MatchContext.storeLocal("loop_value", StatementMatcher.forEach() .value(InstructionMatcher.instanceMethodInvoke() .name("entrySet") .desc("()Ljava/lang/Set;") .build()) .body(0, StatementMatcher.localAssign() .value(InstructionMatcher.instanceMethodInvoke() .name("getKey") .desc("()Ljava/lang/Object;") .callee(InstructionMatcher.localAccess() .fromContext("loop_value") .build()) .autoUnwrap() .build()) .build()) .body(1, StatementMatcher.localAssign() .value(InstructionMatcher.instanceMethodInvoke() .name("getValue") .desc("()Ljava/lang/Object;") .callee(InstructionMatcher.localAccess() .fromContext("loop_value") .build()) .autoUnwrap() .build()) .build()) .build()); @Override public void emit(JavaEmitterContext ctx, ForEach loop, boolean semicolon) { if (checkMapIterator(ctx, loop)) { return; } ctx.printString("for ("); LocalInstance local = loop.getValueAssignment(); ctx.printString(local.getName()); ctx.printString(" in "); ctx.emit(loop.getCollectionValue(), null); ctx.printString(") {"); ctx.newLine(); if (!loop.getBody().getStatements().isEmpty()) { ctx.indent(); ctx.emitBody(loop.getBody()); ctx.dedent(); ctx.newLine(); } ctx.printIndentation(); ctx.printString("}"); } /** * Checks if the given for-each loop is over a map iterator. */ public boolean checkMapIterator(JavaEmitterContext ctx, ForEach loop) { if (!MAP_ITERATOR.matches(MatchContext.create(), loop)) { return false; } LocalAssignment key_assign = (LocalAssignment) loop.getBody().getStatement(0); LocalAssignment value_assign = (LocalAssignment) loop.getBody().getStatement(1); Instruction collection = ((InstanceMethodInvoke) loop.getCollectionValue()).getCallee(); ctx.printString("for (("); ctx.printString(key_assign.getLocal().getName()); ctx.printString(", "); ctx.printString(value_assign.getLocal().getName()); ctx.printString(") in "); ctx.emit(collection, null); ctx.printString(") {"); ctx.newLine(); if (!loop.getBody().getStatements().isEmpty()) { ctx.indent(); ctx.emitBody(loop.getBody(), 2); ctx.dedent(); ctx.newLine(); } ctx.printIndentation(); ctx.printString("}"); return true; } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/kotlin/statement/KotlinLocalAssignmentEmitter.java
src/main/java/org/spongepowered/despector/emitter/kotlin/statement/KotlinLocalAssignmentEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.kotlin.statement; import org.spongepowered.despector.ast.Locals.LocalInstance; import org.spongepowered.despector.ast.generic.TypeSignature; import org.spongepowered.despector.ast.stmt.assign.LocalAssignment; import org.spongepowered.despector.emitter.java.JavaEmitterContext; import org.spongepowered.despector.emitter.java.statement.LocalAssignmentEmitter; import org.spongepowered.despector.emitter.kotlin.KotlinEmitterUtil; /** * An emitter for kotlin local assignments. */ public class KotlinLocalAssignmentEmitter extends LocalAssignmentEmitter { @Override public void emit(JavaEmitterContext ctx, LocalAssignment insn, boolean semicolon) { TypeSignature type = insn.getLocal().getType(); if (type == null) { type = insn.getValue().inferType(); insn.getLocal().setType(type); } if (!insn.getLocal().getLocal().isParameter() && !ctx.isDefined(insn.getLocal())) { if (insn.getLocal().isEffectivelyFinal()) { ctx.printString("val "); } else { ctx.printString("var "); } ctx.printString(insn.getLocal().getName()); ctx.printString(": "); LocalInstance local = insn.getLocal(); KotlinEmitterUtil.emitType(ctx, local.getType()); ctx.markDefined(insn.getLocal()); } else { ctx.printString(insn.getLocal().getName()); if (checkOperator(ctx, insn, insn.getValue(), semicolon)) { return; } } ctx.printString(" = "); ctx.markWrapPoint(); ctx.emit(insn.getValue(), insn.getLocal().getType()); } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/kotlin/condition/KotlinCompareConditionEmitter.java
src/main/java/org/spongepowered/despector/emitter/kotlin/condition/KotlinCompareConditionEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.kotlin.condition; import org.spongepowered.despector.ast.insn.condition.CompareCondition; import org.spongepowered.despector.ast.insn.condition.CompareCondition.CompareOperator; import org.spongepowered.despector.ast.insn.misc.NumberCompare; import org.spongepowered.despector.emitter.ConditionEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; /** * An emitter for kotlin compare conditions. */ public class KotlinCompareConditionEmitter implements ConditionEmitter<JavaEmitterContext, CompareCondition> { @Override public void emit(JavaEmitterContext ctx, CompareCondition compare) { if (compare.getLeft() instanceof NumberCompare) { NumberCompare cmp = (NumberCompare) compare.getLeft(); ctx.emit(cmp.getLeftOperand(), null); ctx.markWrapPoint(); if (compare.getOperator() == CompareOperator.EQUAL) { ctx.printString(" === "); } else { ctx.printString(compare.getOperator().asString()); } ctx.emit(cmp.getRightOperand(), null); return; } ctx.emit(compare.getLeft(), null); ctx.markWrapPoint(); if (compare.getOperator() == CompareOperator.EQUAL) { ctx.printString(" === "); } else { ctx.printString(compare.getOperator().asString()); } ctx.emit(compare.getRight(), null); } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/kotlin/condition/package-info.java
src/main/java/org/spongepowered/despector/emitter/kotlin/condition/package-info.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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. */ @org.spongepowered.despector.util.NonnullByDefault package org.spongepowered.despector.emitter.kotlin.condition;
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/kotlin/condition/KotlinBooleanConditionEmitter.java
src/main/java/org/spongepowered/despector/emitter/kotlin/condition/KotlinBooleanConditionEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.kotlin.condition; import org.spongepowered.despector.ast.generic.ClassTypeSignature; import org.spongepowered.despector.ast.insn.condition.BooleanCondition; import org.spongepowered.despector.ast.insn.misc.InstanceOf; import org.spongepowered.despector.emitter.java.JavaEmitterContext; import org.spongepowered.despector.emitter.java.condition.BooleanConditionEmitter; /** * An emitter for kotlin boolean conditions. */ public class KotlinBooleanConditionEmitter extends BooleanConditionEmitter { @Override public void emit(JavaEmitterContext ctx, BooleanCondition bool) { if (checkConstant(ctx, bool)) { return; } if (bool.getConditionValue() instanceof InstanceOf) { InstanceOf arg = (InstanceOf) bool.getConditionValue(); ctx.emit(arg.getCheckedValue(), null); ctx.printString(" !is "); ctx.emitType(arg.getType().getDescriptor()); return; } if (bool.isInverse()) { ctx.printString("!"); } if (bool.isInverse() && bool.getConditionValue() instanceof InstanceOf) { ctx.printString("("); ctx.emit(bool.getConditionValue(), ClassTypeSignature.BOOLEAN); ctx.printString(")"); } else { ctx.emit(bool.getConditionValue(), ClassTypeSignature.BOOLEAN); } } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/kotlin/condition/KotlinInverseConditionEmitter.java
src/main/java/org/spongepowered/despector/emitter/kotlin/condition/KotlinInverseConditionEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.kotlin.condition; import org.spongepowered.despector.ast.insn.condition.InverseCondition; import org.spongepowered.despector.emitter.java.JavaEmitterContext; import org.spongepowered.despector.emitter.java.condition.InverseConditionEmitter; import org.spongepowered.despector.emitter.kotlin.special.KotlinRangeEmitter; public class KotlinInverseConditionEmitter extends InverseConditionEmitter { @Override public void emit(JavaEmitterContext ctx, InverseCondition inv) { if (KotlinRangeEmitter.checkRange(ctx, inv.getConditionValue(), true)) { return; } super.emit(ctx, inv); } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/JavaEmitterContext.java
src/main/java/org/spongepowered/despector/emitter/java/JavaEmitterContext.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; import com.google.common.collect.Sets; import org.spongepowered.despector.ast.Annotation; import org.spongepowered.despector.ast.AstEntry; import org.spongepowered.despector.ast.Locals.LocalInstance; import org.spongepowered.despector.ast.generic.TypeSignature; import org.spongepowered.despector.ast.insn.Instruction; import org.spongepowered.despector.ast.insn.condition.Condition; import org.spongepowered.despector.ast.stmt.Statement; import org.spongepowered.despector.ast.stmt.StatementBlock; import org.spongepowered.despector.ast.stmt.branch.DoWhile; import org.spongepowered.despector.ast.stmt.branch.For; import org.spongepowered.despector.ast.stmt.branch.ForEach; import org.spongepowered.despector.ast.stmt.branch.If; import org.spongepowered.despector.ast.stmt.branch.Switch; import org.spongepowered.despector.ast.stmt.branch.TryCatch; import org.spongepowered.despector.ast.stmt.branch.While; import org.spongepowered.despector.ast.stmt.misc.Return; import org.spongepowered.despector.ast.type.FieldEntry; import org.spongepowered.despector.ast.type.InterfaceEntry; import org.spongepowered.despector.ast.type.TypeEntry; import org.spongepowered.despector.emitter.AbstractEmitterContext; import org.spongepowered.despector.emitter.AstEmitter; import org.spongepowered.despector.emitter.ConditionEmitter; import org.spongepowered.despector.emitter.InstructionEmitter; import org.spongepowered.despector.emitter.StatementEmitter; import org.spongepowered.despector.emitter.format.EmitterFormat; import org.spongepowered.despector.emitter.format.EmitterFormat.BracePosition; import org.spongepowered.despector.emitter.format.EmitterFormat.WrappingStyle; import org.spongepowered.despector.emitter.java.special.AnnotationEmitter; import org.spongepowered.despector.emitter.java.special.GenericsEmitter; import org.spongepowered.despector.emitter.java.special.PackageEmitter; import org.spongepowered.despector.emitter.java.special.PackageInfoEmitter; import org.spongepowered.despector.util.TypeHelper; import java.io.IOException; import java.io.Writer; import java.util.HashSet; import java.util.Set; /** * A holder for the current context of a type being emitted. */ public class JavaEmitterContext extends AbstractEmitterContext { private final ImportManager import_manager = new ImportManager(); private EmitterFormat format; private Writer output; private Set<LocalInstance> defined_locals = Sets.newHashSet(); private int indentation = 0; private int offs = 0; private boolean semicolons = true; private int line_length = 0; private int wrap_point = -1; private StringBuilder line_buffer = new StringBuilder(); private boolean is_wrapped = false; private final Set<Class<? extends Statement>> block_statements = new HashSet<>(); public JavaEmitterContext(Writer output, EmitterFormat format) { this.output = output; this.format = format; this.block_statements.add(DoWhile.class); this.block_statements.add(While.class); this.block_statements.add(For.class); this.block_statements.add(ForEach.class); this.block_statements.add(If.class); this.block_statements.add(Switch.class); this.block_statements.add(TryCatch.class); } /** * Gets the emitter format. */ public EmitterFormat getFormat() { return this.format; } /** * Gets if semicolons should be emitted after statements. */ public boolean usesSemicolons() { return this.semicolons; } /** * Sets if semicolons should be emitted after statements. */ public void setSemicolons(boolean state) { this.semicolons = state; } /** * Gets if the given local instance has been defined previously in this * method. */ public boolean isDefined(LocalInstance local) { return this.defined_locals.contains(local); } /** * Sets if the given local instance as defined. */ public void markDefined(LocalInstance local) { this.defined_locals.add(checkNotNull(local, "local")); } public void resetDefinedLocals() { this.defined_locals.clear(); } /** * Marks the given statement type as a block statement. */ public void markBlockStatement(Class<? extends Statement> type) { this.block_statements.add(checkNotNull(type, "type")); } /** * Gets the import manager. */ public ImportManager getImportManager() { return this.import_manager; } /** * Emits the given type as an outer type. */ public void emitOuterType(TypeEntry type) { if (type.getName().endsWith("package-info")) { PackageInfoEmitter emitter = this.set.getSpecialEmitter(PackageInfoEmitter.class); emitter.emit(this, (InterfaceEntry) type); return; } // reset and calculate imports for this type. this.import_manager.reset(); this.import_manager.calculateImports(type); this.outer_type = type; PackageEmitter pkg_emitter = this.set.getSpecialEmitter(PackageEmitter.class); String pkg = this.outer_type.getName(); int last = pkg.lastIndexOf('/'); if (last != -1) { for (int i = 0; i < this.format.blank_lines_before_package; i++) { newLine(); } pkg = pkg.substring(0, last).replace('/', '.'); pkg_emitter.emitPackage(this, pkg); newLine(); } int lines = Math.max(this.format.blank_lines_after_package, this.format.blank_lines_before_imports); for (int i = 0; i < lines; i++) { newLine(); } this.import_manager.emitImports(this); emit(type); this.outer_type = null; } /** * Emits the given ast entry. */ @SuppressWarnings("unchecked") public <T extends AstEntry> boolean emit(T obj) { if (obj instanceof TypeEntry) { TypeEntry type = (TypeEntry) obj; if (type.isSynthetic()) { return false; } this.type = (TypeEntry) obj; } else if (obj instanceof FieldEntry) { this.field = (FieldEntry) obj; } AstEmitter<AbstractEmitterContext, T> emitter = (AstEmitter<AbstractEmitterContext, T>) this.set.getAstEmitter(obj.getClass()); if (emitter == null) { throw new IllegalArgumentException("No emitter for ast entry " + obj.getClass().getName()); } boolean state = emitter.emit(this, obj); if (obj instanceof TypeEntry) { this.type = this.outer_type; } else if (obj instanceof FieldEntry) { this.field = null; } return state; } /** * Emits the given body. */ public JavaEmitterContext emitBody(StatementBlock instructions) { emitBody(instructions, 0); return this; } /** * Emits the given body starting at the given start index. */ public JavaEmitterContext emitBody(StatementBlock instructions, int start) { boolean last_success = false; boolean should_indent = true; for (int i = start; i < instructions.getStatements().size(); i++) { Statement insn = instructions.getStatements().get(i); if (insn instanceof Return && !((Return) insn).getValue().isPresent() && instructions.getType() == StatementBlock.Type.METHOD && i == instructions.getStatements().size() - 1) { break; } if (last_success) { newLine(); } last_success = true; if (should_indent) { printIndentation(); } should_indent = true; int mark = this.offs; emit(insn, this.semicolons); if (this.block_statements.contains(insn.getClass())) { if (i < instructions.getStatementCount() - 1) { if (instructions.getType() == StatementBlock.Type.METHOD && i == instructions.getStatementCount() - 2) { Statement ret = instructions.getStatement(instructions.getStatementCount() - 1); if (ret instanceof Return && ((Return) ret).getValue().isPresent()) { newLine(); } } else { newLine(); } } } if (this.offs == mark) { should_indent = false; last_success = false; } } return this; } /** * Emits the given statement. */ @SuppressWarnings("unchecked") public <T extends Statement> JavaEmitterContext emit(T obj, boolean semicolon) { StatementEmitter<AbstractEmitterContext, T> emitter = (StatementEmitter<AbstractEmitterContext, T>) this.set.getStatementEmitter(obj.getClass()); if (emitter == null) { throw new IllegalArgumentException("No emitter for statement " + obj.getClass().getName()); } Statement last = getStatement(); setStatement(obj); emitter.emit(this, obj, semicolon); setStatement(last); return this; } /** * Emits the given instruction. */ @SuppressWarnings("unchecked") public <T extends Instruction> JavaEmitterContext emit(T obj, TypeSignature type) { InstructionEmitter<AbstractEmitterContext, T> emitter = (InstructionEmitter<AbstractEmitterContext, T>) this.set.getInstructionEmitter(obj.getClass()); if (emitter == null) { throw new IllegalArgumentException("No emitter for instruction " + obj.getClass().getName()); } this.insn_stack.push(obj); if (type == null) { type = obj.inferType(); } emitter.emit(this, obj, type); this.insn_stack.pop(); return this; } /** * Emits the given condition. */ @SuppressWarnings("unchecked") public <T extends Condition> JavaEmitterContext emit(T condition) { ConditionEmitter<AbstractEmitterContext, T> emitter = (ConditionEmitter<AbstractEmitterContext, T>) this.set.getConditionEmitter(condition.getClass()); if (emitter == null) { throw new IllegalArgumentException("No emitter for condition " + condition.getClass().getName()); } emitter.emit(this, condition); return this; } /** * Emits the given annotation. */ public JavaEmitterContext emit(Annotation anno) { AnnotationEmitter emitter = this.set.getSpecialEmitter(AnnotationEmitter.class); if (emitter == null) { throw new IllegalArgumentException("No emitter for annotations"); } emitter.emit(this, anno); return this; } /** * Increases the indentation level by one. */ public JavaEmitterContext indent() { this.indentation++; return this; } /** * Decreases the indentation level by one. */ public JavaEmitterContext dedent() { this.indentation--; return this; } /** * Prints the required indentation for the current indentation level. */ public JavaEmitterContext printIndentation() { if (this.format.indent_with_spaces) { for (int i = 0; i < this.indentation * this.format.indentation_size; i++) { printString(" "); } } else { for (int i = 0; i < this.indentation; i++) { printString("\t"); } } return this; } /** * Gets the string for the given type descriptor taking imports into * account. */ public String getType(String name) { return getTypeName(TypeHelper.descToType(name)); } /** * Gets the string for the given type internal name taking imports into * account. */ public String getTypeName(String name) { if (name.endsWith("[]")) { String n = getTypeName(name.substring(0, name.length() - 2)); if (this.format.insert_space_before_opening_bracket_in_array_type_reference) { n += " "; } if (this.format.insert_space_between_brackets_in_array_type_reference) { n += "[ ]"; } else { n += "[]"; } return n; } if (name.indexOf('/') != -1) { if (this.import_manager.checkImport(name)) { name = name.substring(name.lastIndexOf('/') + 1); } else if (this.type != null) { String this_package = ""; String target_package = name; String this_name = this.type.getName(); String outer_name = null; if (this_name.indexOf('/') != -1) { this_package = this_name.substring(0, this_name.lastIndexOf('/')); outer_name = this_name.substring(this_package.length() + 1); if (outer_name.indexOf('$') != -1) { outer_name = outer_name.substring(0, outer_name.indexOf('$')); } target_package = name.substring(0, name.lastIndexOf('/')); } if (this_package.equals(target_package)) { name = name.substring(name.lastIndexOf('/') + 1); if (name.startsWith(outer_name + "$")) { name = name.substring(outer_name.length() + 1); } } } } return name.replace('/', '.').replace('$', '.'); } /** * Emits the given type signature taking imports into account. */ public JavaEmitterContext emitType(TypeSignature sig, boolean varargs) { GenericsEmitter generics = this.set.getSpecialEmitter(GenericsEmitter.class); generics.emitTypeSignature(this, sig, varargs); return this; } /** * Emits the given type descriptor taking imports into account. */ public JavaEmitterContext emitType(String name) { emitTypeName(TypeHelper.descToType(name)); return this; } /** * Emits the given type internal name taking imports into account. */ public JavaEmitterContext emitTypeName(String name) { printString(getTypeName(name)); return this; } /** * Inserts `count` new lines. */ public JavaEmitterContext newLine(int count) { for (int i = 0; i < count; i++) { newLine(); } return this; } /** * Inserts a new line. */ public JavaEmitterContext newLine() { __newLine(); if (this.is_wrapped) { for (int i = 0; i < this.format.continuation_indentation; i++) { dedent(); } this.is_wrapped = false; } return this; } /** * Flushes the line buffer to the output. */ public void flush() { try { this.output.write(this.line_buffer.toString()); } catch (IOException e) { e.printStackTrace(); } } /** * inserts a new line without resetting wrapping. */ private void __newLine() { flush(); this.offs += 1; try { this.output.write('\n'); } catch (IOException e) { e.printStackTrace(); } this.line_length = 0; this.wrap_point = -1; this.line_buffer.setLength(0); } /** * Inserts a new line and indents. */ public void newIndentedLine() { newLine(); printIndentation(); } /** * Gets the length of the current line. */ public int getCurrentLength() { return this.line_length; } /** * Prints the given string to the output. */ public JavaEmitterContext printString(String line) { checkArgument(line.indexOf('\n') == -1); this.offs += this.line_buffer.length(); this.line_length += line.length(); this.line_buffer.append(line); if (this.line_length > this.format.line_split) { if (this.wrap_point != -1) { String existing = this.line_buffer.toString(); String next = existing.substring(this.wrap_point); this.line_buffer.setLength(this.wrap_point); this.wrap_point = -1; newLine(); if (!this.is_wrapped) { this.is_wrapped = true; for (int i = 0; i < this.format.continuation_indentation; i++) { indent(); } } printIndentation(); printString(next); } } return this; } public JavaEmitterContext printStringf(String line, Object... args) { String s = String.format(line, args); return printString(s); } /** * Prints the given string if the condition is met. */ public JavaEmitterContext printString(String line, boolean condition) { if (condition) { printString(line); } return this; } /** * Marks the current line posittion as a possible line break point. */ public JavaEmitterContext markWrapPoint() { markWrapPoint(WrappingStyle.WRAP_WHEN_NEEDED, 0); return this; } /** * Wraps the current line at the current position. */ private void wrap(boolean indent) { __newLine(); if (!this.is_wrapped && indent) { this.is_wrapped = true; for (int i = 0; i < this.format.continuation_indentation; i++) { indent(); } } printIndentation(); } /** * Marks the current line posittion as a possible line break point depending * on the given wrapping style and index. */ public JavaEmitterContext markWrapPoint(WrappingStyle style, int index) { switch (style) { case DO_NOT_WRAP: break; case WRAP_ALL: wrap(false); break; case WRAP_ALL_AND_INDENT: wrap(true); break; case WRAP_ALL_EXCEPT_FIRST: if (index != 0) { wrap(true); } break; case WRAP_FIRST_OR_NEEDED: if (index == 0) { wrap(true); } else { this.wrap_point = this.line_length; } break; case WRAP_WHEN_NEEDED: this.wrap_point = this.line_length; break; default: break; } return this; } /** * Emits an opening brace with a position depending on the given * {@link BracePosition}. */ public void emitBrace(BracePosition pos, boolean wrapped, boolean with_space) { switch (pos) { case NEXT_LINE: newLine(); printIndentation(); printString("{"); indent(); break; case NEXT_LINE_ON_WRAP: if (wrapped) { newLine(); printIndentation(); } else if (with_space) { printString(" "); } printString("{"); indent(); break; case NEXT_LINE_SHIFTED: newLine(); indent(); printIndentation(); printString("{"); break; case SAME_LINE: default: if (with_space) { printString(" "); } printString("{"); indent(); break; } } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/JavaEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/JavaEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java; import org.spongepowered.despector.ast.type.TypeEntry; import org.spongepowered.despector.emitter.Emitter; import org.spongepowered.despector.emitter.Emitters; import org.spongepowered.despector.parallel.Timing; /** * A java source emitter. */ public class JavaEmitter implements Emitter<JavaEmitterContext> { @Override public void setup(JavaEmitterContext ctx) { ctx.setSemicolons(true); ctx.setEmitterSet(Emitters.JAVA_SET); } @Override public void emit(JavaEmitterContext ctx, TypeEntry type) { setup(ctx); long emitting_start = System.nanoTime(); ctx.emitOuterType(type); Timing.time_emitting += System.nanoTime() - emitting_start; } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/ImportManager.java
src/main/java/org/spongepowered/despector/emitter/java/ImportManager.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java; import com.google.common.collect.Lists; import org.spongepowered.despector.ast.Annotation; import org.spongepowered.despector.ast.Annotation.EnumConstant; import org.spongepowered.despector.ast.Locals.LocalInstance; import org.spongepowered.despector.ast.generic.ClassTypeSignature; import org.spongepowered.despector.ast.generic.GenericClassTypeSignature; import org.spongepowered.despector.ast.generic.TypeArgument; import org.spongepowered.despector.ast.generic.TypeParameter; import org.spongepowered.despector.ast.generic.TypeSignature; import org.spongepowered.despector.ast.insn.InstructionVisitor; import org.spongepowered.despector.ast.insn.cst.DoubleConstant; import org.spongepowered.despector.ast.insn.cst.FloatConstant; import org.spongepowered.despector.ast.insn.cst.IntConstant; import org.spongepowered.despector.ast.insn.cst.LongConstant; import org.spongepowered.despector.ast.insn.cst.NullConstant; import org.spongepowered.despector.ast.insn.cst.StringConstant; import org.spongepowered.despector.ast.insn.cst.TypeConstant; import org.spongepowered.despector.ast.insn.misc.Cast; import org.spongepowered.despector.ast.insn.misc.InstanceOf; import org.spongepowered.despector.ast.insn.misc.MultiNewArray; import org.spongepowered.despector.ast.insn.misc.NewArray; import org.spongepowered.despector.ast.insn.misc.NumberCompare; import org.spongepowered.despector.ast.insn.misc.Ternary; import org.spongepowered.despector.ast.insn.op.NegativeOperator; import org.spongepowered.despector.ast.insn.op.Operator; import org.spongepowered.despector.ast.insn.var.ArrayAccess; import org.spongepowered.despector.ast.insn.var.InstanceFieldAccess; import org.spongepowered.despector.ast.insn.var.LocalAccess; import org.spongepowered.despector.ast.insn.var.StaticFieldAccess; import org.spongepowered.despector.ast.stmt.invoke.Lambda; import org.spongepowered.despector.ast.stmt.invoke.MethodReference; import org.spongepowered.despector.ast.stmt.invoke.InstanceMethodInvoke; import org.spongepowered.despector.ast.stmt.invoke.New; import org.spongepowered.despector.ast.stmt.invoke.StaticMethodInvoke; import org.spongepowered.despector.ast.type.FieldEntry; import org.spongepowered.despector.ast.type.MethodEntry; import org.spongepowered.despector.ast.type.TypeEntry; import org.spongepowered.despector.ast.type.TypeEntry.InnerClassInfo; import org.spongepowered.despector.util.TypeHelper; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Set; /** * A manager which handles determining which types should be imported and * whether a given type is already imported. */ public class ImportManager { private final List<String> implicit_imports = new ArrayList<>(); private final Set<String> imports = new HashSet<>(); private final Set<TypeEntry> checked = new HashSet<>(); public ImportManager() { addImplicitImport("java/lang/"); } /** * Clears all imports from this manager. */ public void reset() { this.imports.clear(); this.checked.clear(); } /** * Adds the given package prefix as an implicit import. Implicit imports are * not emitted at the top of the class but are still considered present. * * <p>An example of an implicit import is {@code 'java/lang/'} which is * always considered as imported in java code.</p> */ public void addImplicitImport(String i) { if (!this.implicit_imports.contains(i)) { this.implicit_imports.add(i); } } /** * Traverses the given type and determines what types should be imported for * it. */ public void calculateImports(TypeEntry type) { if (this.checked.contains(type)) { return; } this.checked.add(type); ImportWalker walker = new ImportWalker(); for (Annotation anno : type.getAnnotations()) { check(anno); } for (String i : type.getInterfaces()) { add("L" + i + ";"); } for (MethodEntry method : type.getStaticMethods()) { check(method, walker); } for (MethodEntry method : type.getMethods()) { check(method, walker); } for (FieldEntry field : type.getStaticFields()) { check(field); } for (FieldEntry field : type.getFields()) { check(field); } for (InnerClassInfo info : type.getInnerClasses()) { TypeEntry inner = type.getSource().get(info.getName()); if (inner != null && inner != type) { calculateImports(inner); } } } /** * Adds the given type descriptor to the list of imports. */ void add(String desc) { if (desc.startsWith("[")) { add(desc.substring(1)); return; } if (!desc.startsWith("L")) { return; } String type = TypeHelper.descToType(desc); if (type.indexOf('$') != -1) { type = type.substring(0, type.indexOf('$')); } for (String implicit : this.implicit_imports) { if (type.startsWith(implicit) && type.lastIndexOf('/') <= implicit.length()) { return; } } this.imports.add(type); } private void check(Annotation anno) { add("L" + anno.getType().getName() + ";"); for (String key : anno.getKeys()) { Object val = anno.getValue(key); checkAnnotationValue(val); } } private void checkAnnotationValue(Object val) { if (val instanceof ClassTypeSignature) { add(((ClassTypeSignature) val).getDescriptor()); } else if (val instanceof GenericClassTypeSignature) { add(((ClassTypeSignature) val).getDescriptor()); } else if (val instanceof EnumConstant) { add(((EnumConstant) val).getEnumType()); } else if (val instanceof List) { for (Object obj : (List<?>) val) { checkAnnotationValue(obj); } } } private void check(MethodEntry method, ImportWalker walker) { for (Annotation anno : method.getAnnotations()) { check(anno); } if (!method.isAbstract() && method.getInstructions() != null) { method.getInstructions().accept(walker); } check(method.getReturnType()); for (TypeSignature param : method.getParamTypes()) { check(param); } for (TypeSignature ex : method.getMethodSignature().getThrowsSignature()) { check(ex); } for (TypeParameter arg : method.getMethodSignature().getTypeParameters()) { if (arg.getClassBound() != null) { check(arg.getClassBound()); } for (TypeSignature sig : arg.getInterfaceBounds()) { check(sig); } } } private void check(FieldEntry field) { for (Annotation anno : field.getAnnotations()) { check(anno); } check(field.getType()); // Field initializer is still within the ctor and will be walked with // the methods } void check(TypeSignature sig) { if (sig instanceof ClassTypeSignature) { ClassTypeSignature cls = (ClassTypeSignature) sig; add(cls.getDescriptor()); } else if (sig instanceof GenericClassTypeSignature) { GenericClassTypeSignature cls = (GenericClassTypeSignature) sig; add(cls.getDescriptor()); for (TypeArgument param : cls.getArguments()) { check(param.getSignature()); } } } /** * Checks if the given type is imported. */ public boolean checkImport(String type) { if (type.indexOf('$') != -1) { type = type.substring(0, type.indexOf('$')); } if (TypeHelper.isPrimative(type)) { return true; } for (String implicit : this.implicit_imports) { if (type.startsWith(implicit)) { return true; } } return this.imports.contains(type); } /** * Emits the imports to the given emitter context. */ public void emitImports(JavaEmitterContext ctx) { for (Iterator<String> it = this.imports.iterator(); it.hasNext();) { String i = it.next(); if (i.equals(ctx.getOuterType().getName())) { it.remove(); } } List<String> imports = Lists.newArrayList(this.imports); for (int i = 0; i < ctx.getFormat().import_order.size(); i++) { String group = ctx.getFormat().import_order.get(i); if (group.startsWith("/#")) { // don't have static imports yet continue; } List<String> group_imports = Lists.newArrayList(); for (Iterator<String> it = imports.iterator(); it.hasNext();) { String import_ = it.next(); if (import_.startsWith(group)) { group_imports.add(import_); it.remove(); } } Collections.sort(group_imports); for (String import_ : group_imports) { ctx.printString("import "); ctx.printString(import_.replace('/', '.')); if (ctx.usesSemicolons()) { ctx.printString(";"); } ctx.newLine(); } if (!group_imports.isEmpty() && i < ctx.getFormat().import_order.size() - 1) { for (int o = 0; o < ctx.getFormat().blank_lines_between_import_groups; o++) { ctx.newLine(); } } } for (int i = 0; i < ctx.getFormat().blank_lines_after_imports - 1; i++) { ctx.newLine(); } } /** * A visitor to gather types needing to be imported. */ private class ImportWalker implements InstructionVisitor { public ImportWalker() { } @Override public void visitCast(Cast cast) { ImportManager.this.check(cast.getType()); } @Override public void visitLocalInstance(LocalInstance local) { ImportManager.this.check(local.getType()); } @Override public void visitTypeConstant(TypeConstant cst) { ImportManager.this.add(cst.getConstant().getDescriptor()); } @Override public void visitNew(New ne) { ImportManager.this.check(ne.getType()); } @Override public void visitArrayAccess(ArrayAccess insn) { } @Override public void visitDoubleConstant(DoubleConstant insn) { } @Override public void visitDynamicInvoke(Lambda insn) { } @Override public void visitFloatConstant(FloatConstant insn) { } @Override public void visitInstanceFieldAccess(InstanceFieldAccess insn) { } @Override public void visitInstanceMethodInvoke(InstanceMethodInvoke insn) { } @Override public void visitInstanceOf(InstanceOf insn) { } @Override public void visitIntConstant(IntConstant insn) { } @Override public void visitLocalAccess(LocalAccess insn) { } @Override public void visitLongConstant(LongConstant insn) { } @Override public void visitNegativeOperator(NegativeOperator insn) { } @Override public void visitNewArray(NewArray insn) { } @Override public void visitNullConstant(NullConstant insn) { } @Override public void visitNumberCompare(NumberCompare insn) { } @Override public void visitOperator(Operator insn) { } @Override public void visitStaticFieldAccess(StaticFieldAccess insn) { } @Override public void visitStaticMethodInvoke(StaticMethodInvoke insn) { } @Override public void visitStringConstant(StringConstant insn) { } @Override public void visitTernary(Ternary insn) { } @Override public void visitMultiNewArray(MultiNewArray insn) { } @Override public void visitMethodReference(MethodReference methodReference) { } } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/type/EnumEntryEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/type/EnumEntryEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.type; import com.google.common.collect.Lists; import com.google.common.collect.Sets; import org.spongepowered.despector.ast.AccessModifier; import org.spongepowered.despector.ast.Annotation; import org.spongepowered.despector.ast.generic.ClassTypeSignature; import org.spongepowered.despector.ast.stmt.Statement; import org.spongepowered.despector.ast.stmt.assign.StaticFieldAssignment; import org.spongepowered.despector.ast.stmt.invoke.New; import org.spongepowered.despector.ast.type.EnumEntry; import org.spongepowered.despector.ast.type.FieldEntry; import org.spongepowered.despector.ast.type.MethodEntry; import org.spongepowered.despector.ast.type.TypeEntry; import org.spongepowered.despector.ast.type.TypeEntry.InnerClassInfo; import org.spongepowered.despector.config.ConfigManager; import org.spongepowered.despector.emitter.AstEmitter; import org.spongepowered.despector.emitter.format.EmitterFormat.BracePosition; import org.spongepowered.despector.emitter.format.EmitterFormat.WrappingStyle; import org.spongepowered.despector.emitter.java.JavaEmitterContext; import org.spongepowered.despector.util.TypeHelper; import java.util.Collection; import java.util.Iterator; import java.util.List; import java.util.Set; /** * An emitter for enum types. */ public class EnumEntryEmitter implements AstEmitter<JavaEmitterContext, EnumEntry> { @Override public boolean emit(JavaEmitterContext ctx, EnumEntry type) { ctx.printIndentation(); for (Annotation anno : type.getAnnotations()) { ctx.emit(anno); if (ctx.getFormat().insert_new_line_after_annotation_on_type) { ctx.newLine(); ctx.printIndentation(); } else { ctx.printString(" "); } } ctx.printString(type.getAccessModifier().asString()); if (type.getAccessModifier() != AccessModifier.PACKAGE_PRIVATE) { ctx.printString(" "); } ctx.printString("enum "); InnerClassInfo inner_info = null; if (type.isInnerClass() && ctx.getOuterType() != null) { inner_info = ctx.getOuterType().getInnerClassInfo(type.getName()); } if (inner_info != null) { ctx.printString(inner_info.getSimpleName()); } else { String name = type.getName().replace('/', '.'); if (name.indexOf('.') != -1) { name = name.substring(name.lastIndexOf('.') + 1, name.length()); } name = name.replace('$', '.'); ctx.printString(name); } if (!type.getInterfaces().isEmpty()) { ctx.printString(" "); ctx.markWrapPoint(ctx.getFormat().alignment_for_superinterfaces_in_enum_declaration, 0); ctx.printString("implements "); for (int i = 0; i < type.getInterfaces().size(); i++) { ctx.emitType(type.getInterfaces().get(i)); if (i < type.getInterfaces().size() - 1) { ctx.printString(" ", ctx.getFormat().insert_space_before_comma_in_superinterfaces); ctx.printString(","); ctx.printString(" ", ctx.getFormat().insert_space_after_comma_in_superinterfaces); ctx.markWrapPoint(ctx.getFormat().alignment_for_superinterfaces_in_enum_declaration, i + 1); } } } ctx.emitBrace(ctx.getFormat().brace_position_for_enum_declaration, false, ctx.getFormat().insert_space_before_opening_brace_in_enum_declaration); ctx.newLine(ctx.getFormat().blank_lines_before_first_class_body_declaration + 1); // we look through the class initializer to find the enum constant // initializers so that we can emit those specially before the rest of // the class contents. MethodEntry clinit = type.getStaticMethod("<clinit>"); List<Statement> remaining = Lists.newArrayList(); Set<String> found = Sets.newHashSet(); if (clinit != null && clinit.getInstructions() != null) { Iterator<Statement> initializers = clinit.getInstructions().getStatements().iterator(); boolean first = true; int index = 0; while (initializers.hasNext()) { Statement next = initializers.next(); if (!(next instanceof StaticFieldAssignment)) { break; } StaticFieldAssignment assign = (StaticFieldAssignment) next; if (assign.getFieldName().contains("$VALUES")) { continue; } if (!TypeHelper.descToType(assign.getOwnerType()).equals(type.getName()) || !(assign.getValue() instanceof New)) { remaining.add(assign); break; } if (!first) { ctx.printString(" ", ctx.getFormat().insert_space_before_comma_in_enum_declarations); ctx.printString(","); ctx.markWrapPoint(ctx.getFormat().alignment_for_enum_constants, index); if (ctx.getFormat().alignment_for_enum_constants == WrappingStyle.DO_NOT_WRAP) { ctx.printString(" ", ctx.getFormat().insert_space_after_comma_in_enum_declarations); } } else { ctx.printIndentation(); } New val = (New) assign.getValue(); ctx.printString(assign.getFieldName()); found.add(assign.getFieldName()); if (val.getParameters().length != 2) { ctx.printString(" ", ctx.getFormat().insert_space_before_opening_paren_in_enum_constant); ctx.printString("("); ctx.printString(" ", ctx.getFormat().insert_space_after_opening_paren_in_enum_constant); List<String> args = TypeHelper.splitSig(val.getCtorDescription()); for (int i = 2; i < val.getParameters().length; i++) { ctx.emit(val.getParameters()[i], ClassTypeSignature.of(args.get(i))); if (i < val.getParameters().length - 1) { ctx.printString(" ", ctx.getFormat().insert_space_before_comma_in_enum_constant_arguments); ctx.printString(","); ctx.printString(" ", ctx.getFormat().insert_space_after_comma_in_enum_constant_arguments); } } ctx.printString(" ", ctx.getFormat().insert_space_before_closing_paren_in_enum_constant); ctx.printString(")"); } first = false; index++; } if (!first) { ctx.printString(";"); ctx.newLine(); } // We store any remaining statements to be emitted later while (initializers.hasNext()) { remaining.add(initializers.next()); } } if (!found.isEmpty()) { ctx.newLine(); } if (!type.getStaticFields().isEmpty()) { boolean at_least_one = false; for (FieldEntry field : type.getStaticFields()) { if (field.isSynthetic()) { // Skip the values array. if (ConfigManager.getConfig().emitter.emit_synthetics) { ctx.printIndentation(); ctx.printString("// Synthetic"); ctx.newLine(); } else { continue; } } if (found.contains(field.getName())) { // Skip the fields for any of the enum constants that we // found earlier. continue; } ctx.printIndentation(); ctx.emit(field); ctx.printString(";"); ctx.newLine(); at_least_one = true; } if (at_least_one) { ctx.newLine(); } } if (!remaining.isEmpty()) { // if we found any additional statements in the class initializer // while looking for enum constants we emit them here ctx.printIndentation(); ctx.printString("static {"); ctx.newLine(); ctx.indent(); for (Statement stmt : remaining) { ctx.printIndentation(); ctx.emit(stmt, ctx.usesSemicolons()); ctx.newLine(); } ctx.dedent(); ctx.printIndentation(); ctx.printString("}"); ctx.newLine(); } if (!type.getStaticMethods().isEmpty()) { for (MethodEntry mth : type.getStaticMethods()) { if (mth.getName().equals("valueOf") || mth.getName().equals("values") || mth.getName().equals("<clinit>")) { // Can skip these boilerplate methods and the class // initializer continue; } else if (mth.isSynthetic()) { if (ConfigManager.getConfig().emitter.emit_synthetics) { ctx.printIndentation(); ctx.printString("// Synthetic"); if (mth.isBridge()) { ctx.printString(" - Bridge"); } ctx.newLine(); } else { continue; } } ctx.emit(mth); ctx.newLine(); ctx.newLine(); } } if (!type.getFields().isEmpty()) { for (FieldEntry field : type.getFields()) { if (field.isSynthetic()) { if (ConfigManager.getConfig().emitter.emit_synthetics) { ctx.printIndentation(); ctx.printString("// Synthetic"); ctx.newLine(); } else { continue; } } ctx.printIndentation(); ctx.emit(field); ctx.printString(";"); ctx.newLine(); } ctx.newLine(); } if (!type.getMethods().isEmpty()) { for (MethodEntry mth : type.getMethods()) { if (mth.isSynthetic()) { if (ConfigManager.getConfig().emitter.emit_synthetics) { ctx.printIndentation(); ctx.printString("// Synthetic"); if (mth.isBridge()) { ctx.printString(" - Bridge"); } ctx.newLine(); } else { continue; } } if (mth.getName().equals("<init>") && mth.getInstructions().getStatements().size() == 2) { // If the initializer contains only two statement (which // will be the invoke of the super constructor and the void // return) then we can // skip emitting it continue; } ctx.emit(mth); ctx.newLine(); ctx.newLine(); } } Collection<InnerClassInfo> inners = type.getInnerClasses(); for (InnerClassInfo inner : inners) { if (inner.getOuterName() == null || !inner.getOuterName().equals(type.getName())) { continue; } TypeEntry inner_type = type.getSource().get(inner.getName()); ctx.newLine(); ctx.emit(inner_type); } if (ctx.getFormat().brace_position_for_enum_declaration == BracePosition.NEXT_LINE_SHIFTED) { ctx.printIndentation(); ctx.printString("}"); ctx.newLine(); ctx.dedent(); } else { ctx.dedent(); ctx.printIndentation(); ctx.printString("}"); ctx.newLine(); } return true; } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/type/package-info.java
src/main/java/org/spongepowered/despector/emitter/java/type/package-info.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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. */ @org.spongepowered.despector.util.NonnullByDefault package org.spongepowered.despector.emitter.java.type;
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/type/InterfaceEntryEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/type/InterfaceEntryEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.type; import org.spongepowered.despector.ast.AccessModifier; import org.spongepowered.despector.ast.Annotation; import org.spongepowered.despector.ast.type.FieldEntry; import org.spongepowered.despector.ast.type.InterfaceEntry; import org.spongepowered.despector.ast.type.MethodEntry; import org.spongepowered.despector.ast.type.TypeEntry; import org.spongepowered.despector.ast.type.TypeEntry.InnerClassInfo; import org.spongepowered.despector.config.ConfigManager; import org.spongepowered.despector.emitter.AstEmitter; import org.spongepowered.despector.emitter.format.EmitterFormat.BracePosition; import org.spongepowered.despector.emitter.java.JavaEmitterContext; import org.spongepowered.despector.emitter.java.special.GenericsEmitter; import java.util.Collection; /** * An emitter for interface types. */ public class InterfaceEntryEmitter implements AstEmitter<JavaEmitterContext, InterfaceEntry> { @Override public boolean emit(JavaEmitterContext ctx, InterfaceEntry type) { ctx.printIndentation(); for (Annotation anno : type.getAnnotations()) { ctx.emit(anno); if (ctx.getFormat().insert_new_line_after_annotation_on_type) { ctx.newLine(); ctx.printIndentation(); } else { ctx.printString(" "); } } InnerClassInfo inner_info = null; if (type.isInnerClass() && ctx.getOuterType() != null) { inner_info = ctx.getOuterType().getInnerClassInfo(type.getName()); } String name = null; if (inner_info != null) { name = inner_info.getSimpleName(); } else { name = type.getName().replace('/', '.'); if (name.indexOf('.') != -1) { name = name.substring(name.lastIndexOf('.') + 1, name.length()); } name = name.replace('$', '.'); } if (!(name.contains(".") && inner_info == null && type.getAccessModifier() == AccessModifier.PUBLIC)) { ctx.printString(type.getAccessModifier().asString()); if (type.getAccessModifier() != AccessModifier.PACKAGE_PRIVATE) { ctx.printString(" "); } } ctx.printString("interface "); ctx.printString(name); GenericsEmitter generics = ctx.getEmitterSet().getSpecialEmitter(GenericsEmitter.class); if (type.getSignature() != null) { generics.emitTypeParameters(ctx, type.getSignature().getParameters()); } if (!type.getInterfaces().isEmpty()) { ctx.printString(" extends "); for (int i = 0; i < type.getInterfaces().size(); i++) { ctx.emitType(type.getInterfaces().get(i)); if (i < type.getInterfaces().size() - 1) { ctx.printString(" ", ctx.getFormat().insert_space_before_comma_in_superinterfaces); ctx.printString(","); ctx.printString(" ", ctx.getFormat().insert_space_after_comma_in_superinterfaces); ctx.markWrapPoint(); } } } ctx.emitBrace(ctx.getFormat().brace_position_for_type_declaration, false, ctx.getFormat().insert_space_before_opening_brace_in_type_declaration); ctx.newLine(ctx.getFormat().blank_lines_before_first_class_body_declaration + 1); if (!type.getStaticFields().isEmpty()) { boolean at_least_one = false; for (FieldEntry field : type.getStaticFields()) { if (field.isSynthetic()) { if (ConfigManager.getConfig().emitter.emit_synthetics) { ctx.printIndentation(); ctx.printString("// Synthetic"); ctx.newLine(); } else { continue; } } at_least_one = true; ctx.printIndentation(); ctx.emit(field); ctx.printString(";"); ctx.newLine(); } if (at_least_one) { ctx.newLine(); } } if (!type.getStaticMethods().isEmpty()) { for (MethodEntry mth : type.getStaticMethods()) { if (mth.isSynthetic()) { if (ConfigManager.getConfig().emitter.emit_synthetics) { ctx.printIndentation(); ctx.printString("// Synthetic"); if (mth.isBridge()) { ctx.printString(" - Bridge"); } ctx.newLine(); } else { continue; } } ctx.emit(mth); ctx.newLine(); ctx.newLine(); } } if (!type.getMethods().isEmpty()) { for (MethodEntry mth : type.getMethods()) { if (mth.isSynthetic()) { if (ConfigManager.getConfig().emitter.emit_synthetics) { ctx.printIndentation(); ctx.printString("// Synthetic"); if (mth.isBridge()) { ctx.printString(" - Bridge"); } ctx.newLine(); } else { continue; } } // TODO need something for emitting 'default' for default // methods ctx.emit(mth); ctx.newLine(); ctx.newLine(); } } Collection<InnerClassInfo> inners = type.getInnerClasses(); for (InnerClassInfo inner : inners) { if (inner.getOuterName() == null || !inner.getOuterName().equals(type.getName())) { continue; } TypeEntry inner_type = type.getSource().get(inner.getName()); ctx.newLine(); ctx.emit(inner_type); } if (ctx.getFormat().brace_position_for_type_declaration == BracePosition.NEXT_LINE_SHIFTED) { ctx.printIndentation(); ctx.printString("}"); ctx.dedent(); } else { ctx.dedent(); ctx.printIndentation(); ctx.printString("}"); } ctx.newLine(); return true; } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/type/ClassEntryEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/type/ClassEntryEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.type; import org.spongepowered.despector.ast.AccessModifier; import org.spongepowered.despector.ast.Annotation; import org.spongepowered.despector.ast.insn.Instruction; import org.spongepowered.despector.ast.stmt.Statement; import org.spongepowered.despector.ast.stmt.assign.FieldAssignment; import org.spongepowered.despector.ast.stmt.assign.StaticFieldAssignment; import org.spongepowered.despector.ast.type.ClassEntry; import org.spongepowered.despector.ast.type.FieldEntry; import org.spongepowered.despector.ast.type.MethodEntry; import org.spongepowered.despector.ast.type.TypeEntry; import org.spongepowered.despector.ast.type.TypeEntry.InnerClassInfo; import org.spongepowered.despector.config.ConfigManager; import org.spongepowered.despector.emitter.AstEmitter; import org.spongepowered.despector.emitter.format.EmitterFormat.BracePosition; import org.spongepowered.despector.emitter.java.JavaEmitterContext; import org.spongepowered.despector.emitter.java.special.GenericsEmitter; import org.spongepowered.despector.util.AstUtil; import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; /** * An emitter of class types. */ public class ClassEntryEmitter implements AstEmitter<JavaEmitterContext, ClassEntry> { @Override public boolean emit(JavaEmitterContext ctx, ClassEntry type) { ctx.printIndentation(); for (Annotation anno : type.getAnnotations()) { ctx.emit(anno); if (ctx.getFormat().insert_new_line_after_annotation_on_type) { ctx.newLine(); ctx.printIndentation(); } else { ctx.printString(" "); } } InnerClassInfo inner_info = null; if (type.isInnerClass() && ctx.getOuterType() != null) { inner_info = ctx.getOuterType().getInnerClassInfo(type.getName()); } ctx.printString(type.getAccessModifier().asString()); if (type.getAccessModifier() != AccessModifier.PACKAGE_PRIVATE) { ctx.printString(" "); } if (inner_info != null && inner_info.isStatic()) { ctx.printString("static "); } if ((inner_info != null && inner_info.isFinal()) || type.isFinal()) { ctx.printString("final "); } if (inner_info != null && inner_info.isAbstract()) { ctx.printString("abstract "); } ctx.printString("class "); if (inner_info != null) { ctx.printString(inner_info.getSimpleName()); } else { String name = type.getName().replace('/', '.'); if (name.indexOf('.') != -1) { name = name.substring(name.lastIndexOf('.') + 1, name.length()); } name = name.replace('$', '.'); ctx.printString(name); } GenericsEmitter generics = ctx.getEmitterSet().getSpecialEmitter(GenericsEmitter.class); if (type.getSignature() != null) { generics.emitTypeParameters(ctx, type.getSignature().getParameters()); } if (!type.getSuperclass().equals("Ljava/lang/Object;")) { ctx.printString(" "); ctx.markWrapPoint(ctx.getFormat().alignment_for_superclass_in_type_declaration, 0); ctx.printString("extends "); ctx.emitTypeName(type.getSuperclassName()); if (type.getSignature() != null && type.getSignature().getSuperclassSignature() != null) { generics.emitTypeArguments(ctx, type.getSignature().getSuperclassSignature().getArguments()); } } if (!type.getInterfaces().isEmpty()) { ctx.printString(" "); ctx.markWrapPoint(ctx.getFormat().alignment_for_superinterfaces_in_type_declaration, 0); ctx.printString("implements "); for (int i = 0; i < type.getInterfaces().size(); i++) { ctx.emitType(type.getInterfaces().get(i)); if (type.getSignature() != null) { generics.emitTypeArguments(ctx, type.getSignature().getInterfaceSignatures().get(i).getArguments()); } if (i < type.getInterfaces().size() - 1) { ctx.printString(" ", ctx.getFormat().insert_space_before_comma_in_superinterfaces); ctx.printString(","); ctx.printString(" ", ctx.getFormat().insert_space_after_comma_in_superinterfaces); ctx.markWrapPoint(ctx.getFormat().alignment_for_superinterfaces_in_type_declaration, i + 1); } } } ctx.emitBrace(ctx.getFormat().brace_position_for_type_declaration, false, ctx.getFormat().insert_space_before_opening_brace_in_type_declaration); ctx.newLine(ctx.getFormat().blank_lines_before_first_class_body_declaration + 1); // Ordering is static fields -> static methods -> instance fields -> // instance methods emitStaticFields(ctx, type); emitStaticMethods(ctx, type); emitFields(ctx, type); emitMethods(ctx, type); Collection<InnerClassInfo> inners = type.getInnerClasses(); for (InnerClassInfo inner : inners) { if (inner.getOuterName() == null || !inner.getOuterName().equals(type.getName())) { continue; } TypeEntry inner_type = type.getSource().get(inner.getName()); if (inner_type != null) { ctx.newLine(); ctx.emit(inner_type); } } if (ctx.getFormat().brace_position_for_type_declaration == BracePosition.NEXT_LINE_SHIFTED) { ctx.printIndentation(); ctx.printString("}"); ctx.dedent(); } else { ctx.dedent(); ctx.printIndentation(); ctx.printString("}"); } ctx.newLine(); return true; } /** * Emits the static fields for the given type. */ public void emitStaticFields(JavaEmitterContext ctx, ClassEntry type) { if (!type.getStaticFields().isEmpty()) { Map<String, Instruction> static_initializers = new HashMap<>(); MethodEntry static_init = type.getStaticMethod("<clinit>"); if (static_init != null && static_init.getInstructions() != null) { for (Statement stmt : static_init.getInstructions().getStatements()) { if (!(stmt instanceof StaticFieldAssignment)) { break; } StaticFieldAssignment assign = (StaticFieldAssignment) stmt; if (!assign.getOwnerName().equals(type.getName())) { break; } static_initializers.put(assign.getFieldName(), assign.getValue()); } } boolean at_least_one = false; for (FieldEntry field : type.getStaticFields()) { if (field.isSynthetic()) { if (ConfigManager.getConfig().emitter.emit_synthetics) { ctx.printIndentation(); ctx.printString("// Synthetic"); ctx.newLine(); } else { continue; } } at_least_one = true; ctx.printIndentation(); ctx.emit(field); if (static_initializers.containsKey(field.getName())) { ctx.printString(" = "); ctx.emit(static_initializers.get(field.getName()), field.getType()); } ctx.printString(";"); ctx.newLine(); } if (at_least_one) { ctx.newLine(); } } } /** * Emits the static methods for the given type. */ public void emitStaticMethods(JavaEmitterContext ctx, ClassEntry type) { if (!type.getStaticMethods().isEmpty()) { for (MethodEntry mth : type.getStaticMethods()) { if (mth.isSynthetic()) { if (ConfigManager.getConfig().emitter.emit_synthetics) { ctx.printIndentation(); ctx.printString("// Synthetic"); if (mth.isBridge()) { ctx.printString(" - Bridge"); } ctx.newLine(); } else { continue; } } if (ctx.emit(mth)) { ctx.newLine(); ctx.newLine(); } } } } /** * Emits the instance fields for the given type. */ public void emitFields(JavaEmitterContext ctx, ClassEntry type) { if (!type.getFields().isEmpty()) { Map<FieldEntry, Instruction> initializers = new HashMap<>(); List<MethodEntry> inits = type.getMethods().stream().filter((m) -> m.getName().equals("<init>")).collect(Collectors.toList()); MethodEntry main = null; if (inits.size() == 1) { main = inits.get(0); } if (main != null && main.getInstructions() != null) { for (int i = 1; i < main.getInstructions().getStatements().size(); i++) { Statement next = main.getInstructions().getStatements().get(i); if (!(next instanceof FieldAssignment)) { break; } FieldAssignment assign = (FieldAssignment) next; if (!type.getName().equals(assign.getOwnerName())) { break; } if (AstUtil.references(assign.getValue(), null)) { break; } assign.setInitializer(true); FieldEntry fld = type.getField(assign.getFieldName()); initializers.put(fld, assign.getValue()); } } boolean at_least_one = false; for (FieldEntry field : type.getFields()) { if (field.isSynthetic()) { if (ConfigManager.getConfig().emitter.emit_synthetics) { ctx.printIndentation(); ctx.printString("// Synthetic"); ctx.newLine(); } else { continue; } } at_least_one = true; ctx.printIndentation(); ctx.emit(field); Instruction init = initializers.get(field); if (init != null) { ctx.setField(field); if (ctx.getFormat().align_type_members_on_columns && ctx.getType() != null) { int len = FieldEntryEmitter.getMaxNameLength(ctx, ctx.getType().getFields()); len -= field.getName().length(); len++; for (int i = 0; i < len; i++) { ctx.printString(" "); } } else { ctx.printString(" "); } ctx.printString("= "); ctx.emit(init, field.getType()); ctx.setField(null); } ctx.printString(";"); ctx.newLine(); } if (at_least_one) { ctx.newLine(); } } } /** * Emits the instance fields for the given type. */ public void emitMethods(JavaEmitterContext ctx, ClassEntry type) { if (!type.getMethods().isEmpty()) { for (MethodEntry mth : type.getMethods()) { if (mth.isSynthetic()) { if (ConfigManager.getConfig().emitter.emit_synthetics) { ctx.printIndentation(); ctx.printString("// Synthetic"); if (mth.isBridge()) { ctx.printString(" - Bridge"); } ctx.newLine(); } else { continue; } } if (ctx.emit(mth)) { ctx.newLine(); ctx.newLine(); } } } } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/type/FieldEntryEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/type/FieldEntryEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.type; import org.spongepowered.despector.ast.Annotation; import org.spongepowered.despector.ast.type.FieldEntry; import org.spongepowered.despector.config.ConfigManager; import org.spongepowered.despector.emitter.AstEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; import org.spongepowered.despector.emitter.java.special.GenericsEmitter; import java.util.Collection; /** * An emitter for field entries. */ public class FieldEntryEmitter implements AstEmitter<JavaEmitterContext, FieldEntry> { @Override public boolean emit(JavaEmitterContext ctx, FieldEntry ast) { for (Annotation anno : ast.getAnnotations()) { ctx.emit(anno); if (ctx.getFormat().insert_new_line_after_annotation_on_field) { ctx.newLine(); ctx.printIndentation(); } else { ctx.printString(" "); } } ctx.printString(ast.getAccessModifier().asString()); if (!ast.getAccessModifier().asString().isEmpty()) { ctx.printString(" "); } if (ast.isStatic()) { ctx.printString("static "); } if (ast.isFinal()) { ctx.printString("final "); } GenericsEmitter generics = ctx.getEmitterSet().getSpecialEmitter(GenericsEmitter.class); generics.emitTypeSignature(ctx, ast.getType(), false); if (ctx.getFormat().align_type_members_on_columns && ctx.getType() != null) { int len = getMaxTypeLength(ctx, ast.isStatic() ? ctx.getType().getStaticFields() : ctx.getType().getFields()); len -= getTypeLength(ctx, ast); len++; for (int i = 0; i < len; i++) { ctx.printString(" "); } } else { ctx.printString(" "); } ctx.printString(ast.getName()); return true; } private static int getMaxTypeLength(JavaEmitterContext ctx, Collection<FieldEntry> fields) { int max = 0; for (FieldEntry fld : fields) { if (fld.isSynthetic() && !ConfigManager.getConfig().emitter.emit_synthetics) { continue; } max = Math.max(max, getTypeLength(ctx, fld)); } return max; } public static int getMaxNameLength(JavaEmitterContext ctx, Collection<FieldEntry> fields) { int max = 0; for (FieldEntry fld : fields) { if (fld.isSynthetic() && !ConfigManager.getConfig().emitter.emit_synthetics) { continue; } max = Math.max(max, fld.getName().length()); } return max; } private static int getTypeLength(JavaEmitterContext ctx, FieldEntry field) { int length = 0; length += field.getAccessModifier().asString().length(); if (length > 0) { length++; } if (field.isStatic()) { length += 7; } if (field.isFinal()) { length += 6; } length += GenericsEmitter.getLength(ctx, field.getType()); length++; return length; } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/type/MethodEntryEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/type/MethodEntryEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.type; import org.spongepowered.despector.ast.AccessModifier; import org.spongepowered.despector.ast.Annotation; import org.spongepowered.despector.ast.Locals.Local; import org.spongepowered.despector.ast.Locals.LocalInstance; import org.spongepowered.despector.ast.generic.MethodSignature; import org.spongepowered.despector.ast.generic.TypeSignature; import org.spongepowered.despector.ast.stmt.Statement; import org.spongepowered.despector.ast.stmt.StatementBlock; import org.spongepowered.despector.ast.stmt.assign.FieldAssignment; import org.spongepowered.despector.ast.stmt.assign.StaticFieldAssignment; import org.spongepowered.despector.ast.stmt.invoke.InstanceMethodInvoke; import org.spongepowered.despector.ast.stmt.invoke.InvokeStatement; import org.spongepowered.despector.ast.stmt.misc.Return; import org.spongepowered.despector.ast.type.EnumEntry; import org.spongepowered.despector.ast.type.InterfaceEntry; import org.spongepowered.despector.ast.type.MethodEntry; import org.spongepowered.despector.emitter.AstEmitter; import org.spongepowered.despector.emitter.format.EmitterFormat.BracePosition; import org.spongepowered.despector.emitter.java.JavaEmitterContext; import org.spongepowered.despector.emitter.java.special.GenericsEmitter; import org.spongepowered.despector.util.TypeHelper; import java.util.List; /** * An emitter for methods. */ public class MethodEntryEmitter implements AstEmitter<JavaEmitterContext, MethodEntry> { @Override public boolean emit(JavaEmitterContext ctx, MethodEntry method) { if (method.getName().equals("<clinit>")) { int start = 0; if (method.getInstructions() != null) { for (Statement stmt : method.getInstructions().getStatements()) { if (!(stmt instanceof StaticFieldAssignment)) { break; } StaticFieldAssignment assign = (StaticFieldAssignment) stmt; if (!assign.getOwnerName().equals(method.getOwnerName())) { break; } start++; } // only need one less as we can ignore the return at the end if (start == method.getInstructions().getStatements().size() - 1) { return false; } } ctx.setMethod(method); ctx.printString("static {"); ctx.newLine(); ctx.indent(); ctx.resetDefinedLocals(); if (method.getInstructions() == null) { ctx.printIndentation(); ctx.printString("// Error decompiling block"); printReturn(ctx, method.getReturnType()); } else { ctx.emitBody(method.getInstructions(), start); } ctx.newLine(); ctx.dedent(); ctx.printIndentation(); ctx.printString("}"); ctx.setMethod(null); return true; } if ("<init>".equals(method.getName()) && method.getAccessModifier() == AccessModifier.PUBLIC && method.getParamTypes().isEmpty() && method.getInstructions().getStatements().size() == 2) { // TODO this could omit somewhere the ctor is purely passing // constants to the super ctor return false; } ctx.printIndentation(); for (Annotation anno : method.getAnnotations()) { ctx.emit(anno); if (ctx.getFormat().insert_new_line_after_annotation_on_method) { ctx.newLine(); ctx.printIndentation(); } else { ctx.printString(" "); } } ctx.setMethod(method); if (!(ctx.getType() instanceof InterfaceEntry) && !(ctx.getType() instanceof EnumEntry && method.getName().equals("<init>"))) { ctx.printString(method.getAccessModifier().asString()); if (method.getAccessModifier() != AccessModifier.PACKAGE_PRIVATE) { ctx.printString(" "); } } GenericsEmitter generics = ctx.getEmitterSet().getSpecialEmitter(GenericsEmitter.class); MethodSignature sig = method.getMethodSignature(); if ("<init>".equals(method.getName())) { String name = method.getOwnerName(); name = name.substring(Math.max(name.lastIndexOf('/'), name.lastIndexOf('$')) + 1); ctx.printString(name); } else { if (method.isStatic()) { ctx.printString("static "); } if (method.isFinal()) { ctx.printString("final "); } if (method.isAbstract() && !(ctx.getType() instanceof InterfaceEntry)) { ctx.printString("abstract "); } if (sig != null) { if (!sig.getTypeParameters().isEmpty()) { generics.emitTypeParameters(ctx, sig.getTypeParameters()); ctx.printString(" "); } generics.emitTypeSignature(ctx, sig.getReturnType(), false); } else { ctx.emitType(method.getReturnType(), false); } ctx.printString(" "); ctx.printString(method.getName()); } ctx.printString(" ", ctx.getFormat().insert_space_before_opening_paren_in_method_declaration); ctx.printString("("); StatementBlock block = method.getInstructions(); int start = 0; List<String> param_types = TypeHelper.splitSig(method.getDescription()); if ("<init>".equals(method.getName()) && ctx.getType() instanceof EnumEntry) { // If this is an enum type then we skip the first two ctor // parameters // (which are the index and name of the enum constant) start += 2; } if (start >= param_types.size()) { ctx.printString(" ", ctx.getFormat().insert_space_between_empty_parens_in_method_declaration); } int param_index = start; if (!method.isStatic()) { param_index++; } // TODO support synthetic parameters properly, their types will be // missing from the signature, need to offset types when emitting for (int i = start; i < method.getParamTypes().size(); i++) { boolean varargs = method.isVarargs() && i == param_types.size() - 1; if (block == null) { if (sig != null) { // interfaces have no lvt for parameters, need to get // generic types from the method signature generics.emitTypeSignature(ctx, sig.getParameters().get(i), varargs); } else { if (method.getParamTypes().size() != param_types.size()) { ctx.emitType(param_types.get(i)); } else { ctx.emitType(method.getParamTypes().get(i), varargs); } } ctx.printString(" "); ctx.printString("local" + param_index); } else { Local local = method.getLocals().getLocal(param_index); LocalInstance insn = local.getParameterInstance(); if(insn == null) { ctx.printString("<error-type> local" + i); } else { for (Annotation anno : insn.getAnnotations()) { ctx.emit(anno); if (ctx.getFormat().insert_new_line_after_annotation_on_parameter) { ctx.indent(); ctx.indent(); ctx.newLine(); ctx.printIndentation(); ctx.dedent(); ctx.dedent(); } else { ctx.printString(" "); } } generics.emitTypeSignature(ctx, insn.getType(), varargs); ctx.printString(" "); ctx.printString(insn.getName()); } } if (i < param_types.size() - 1) { ctx.printString(" ", ctx.getFormat().insert_space_before_comma_in_method_declaration_parameters); ctx.printString(","); ctx.printString(" ", ctx.getFormat().insert_space_after_comma_in_method_declaration_parameters); ctx.markWrapPoint(ctx.getFormat().alignment_for_parameters_in_method_declaration, i + 1); } String desc = param_types.get(i); param_index++; if ("D".equals(desc) || "J".equals(desc)) { param_index++; } } ctx.printString(" ", ctx.getFormat().insert_space_before_closing_paren_in_method_declaration); ctx.printString(")"); if (!method.getMethodSignature().getThrowsSignature().isEmpty()) { ctx.printString(" throws "); boolean first = true; for (TypeSignature ex : method.getMethodSignature().getThrowsSignature()) { if (!first) { ctx.printString(" ", ctx.getFormat().insert_space_before_comma_in_method_declaration_throws); ctx.printString(","); ctx.printString(" ", ctx.getFormat().insert_space_after_comma_in_method_declaration_throws); } else { first = false; } generics.emitTypeSignature(ctx, ex, false); } } if (!method.isAbstract()) { ctx.emitBrace(ctx.getFormat().brace_position_for_method_declaration, false, ctx.getFormat().insert_space_before_opening_brace_in_method_declaration); ctx.newLine(); if (block == null) { ctx.printIndentation(); ctx.printString("// Error decompiling block"); printReturn(ctx, method.getReturnType()); ctx.newLine(); } else if (isEmpty(block, "<init>".equals(method.getName()))) { if (ctx.getFormat().insert_new_line_in_empty_method_body) { ctx.newLine(); } } else { ctx.emitBody(block); ctx.newLine(); } if (ctx.getFormat().brace_position_for_method_declaration == BracePosition.NEXT_LINE_SHIFTED) { ctx.printIndentation(); ctx.printString("}"); ctx.dedent(); } else { ctx.dedent(); ctx.printIndentation(); ctx.printString("}"); } } else { ctx.printString(";"); } ctx.setMethod(null); return true; } protected boolean isEmpty(StatementBlock block, boolean is_init) { if (block.getStatementCount() == 0) { return true; } else if (block.getStatementCount() == 1 && block.getStatement(0) instanceof Return) { Return ret = (Return) block.getStatement(0); return !ret.getValue().isPresent(); } else if (is_init && block.getStatementCount() >= 2 && block.getStatement(0) instanceof InvokeStatement && block.getStatement(block.getStatementCount() - 1) instanceof Return) { InvokeStatement invoke = (InvokeStatement) block.getStatement(0); if (!(invoke.getInstruction() instanceof InstanceMethodInvoke)) { return false; } for (int i = 1; i < block.getStatementCount() - 1; i++) { Statement s = block.getStatement(i); if (!(s instanceof FieldAssignment)) { return false; } FieldAssignment assign = (FieldAssignment) s; if (!assign.isInitializer()) { return false; } } InstanceMethodInvoke ctor = (InstanceMethodInvoke) invoke.getInstruction(); Return ret = (Return) block.getStatement(block.getStatementCount() - 1); return !ret.getValue().isPresent() && ctor.getParameters().length == 0; } return false; } protected static void printReturn(JavaEmitterContext ctx, TypeSignature type) { char f = type.getDescriptor().charAt(0); if (f == 'V') { return; } ctx.newLine(); ctx.printIndentation(); switch (f) { case 'I': case 'S': case 'B': case 'C': ctx.printString("return 0;"); break; case 'J': ctx.printString("return 0L;"); break; case 'F': ctx.printString("return 0.0f;"); break; case 'D': ctx.printString("return 0.0;"); break; case 'Z': ctx.printString("return false;"); break; case 'L': ctx.printString("return null;"); break; default: throw new IllegalStateException("Malformed return type " + type); } } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/type/AnnotationEntryEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/type/AnnotationEntryEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.type; import org.spongepowered.despector.ast.AccessModifier; import org.spongepowered.despector.ast.Annotation; import org.spongepowered.despector.ast.Locals.Local; import org.spongepowered.despector.ast.Locals.LocalInstance; import org.spongepowered.despector.ast.generic.MethodSignature; import org.spongepowered.despector.ast.stmt.StatementBlock; import org.spongepowered.despector.ast.type.AnnotationEntry; import org.spongepowered.despector.ast.type.FieldEntry; import org.spongepowered.despector.ast.type.MethodEntry; import org.spongepowered.despector.ast.type.TypeEntry; import org.spongepowered.despector.ast.type.TypeEntry.InnerClassInfo; import org.spongepowered.despector.config.ConfigManager; import org.spongepowered.despector.emitter.AstEmitter; import org.spongepowered.despector.emitter.format.EmitterFormat.BracePosition; import org.spongepowered.despector.emitter.java.JavaEmitterContext; import org.spongepowered.despector.emitter.java.special.AnnotationEmitter; import org.spongepowered.despector.emitter.java.special.GenericsEmitter; import java.util.Collection; /** * An emitter for interface types. */ public class AnnotationEntryEmitter implements AstEmitter<JavaEmitterContext, AnnotationEntry> { @Override public boolean emit(JavaEmitterContext ctx, AnnotationEntry type) { ctx.printIndentation(); for (Annotation anno : type.getAnnotations()) { ctx.emit(anno); if (ctx.getFormat().insert_new_line_after_annotation_on_type) { ctx.newLine(); ctx.printIndentation(); } else { ctx.printString(" "); } } InnerClassInfo inner_info = null; if (type.isInnerClass() && ctx.getOuterType() != null) { inner_info = ctx.getOuterType().getInnerClassInfo(type.getName()); } String name = null; if (inner_info != null) { name = inner_info.getSimpleName(); } else { name = type.getName().replace('/', '.'); if (name.indexOf('.') != -1) { name = name.substring(name.lastIndexOf('.') + 1, name.length()); } name = name.replace('$', '.'); } if (!(name.contains(".") && inner_info == null && type.getAccessModifier() == AccessModifier.PUBLIC)) { ctx.printString(type.getAccessModifier().asString()); if (type.getAccessModifier() != AccessModifier.PACKAGE_PRIVATE) { ctx.printString(" "); } } ctx.printString("@interface "); ctx.printString(name); GenericsEmitter generics = ctx.getEmitterSet().getSpecialEmitter(GenericsEmitter.class); if (type.getSignature() != null) { generics.emitTypeParameters(ctx, type.getSignature().getParameters()); } ctx.emitBrace(ctx.getFormat().brace_position_for_type_declaration, false, ctx.getFormat().insert_space_before_opening_brace_in_type_declaration); ctx.newLine(ctx.getFormat().blank_lines_before_first_class_body_declaration + 1); if (!type.getStaticFields().isEmpty()) { boolean at_least_one = false; for (FieldEntry field : type.getStaticFields()) { if (field.isSynthetic()) { if (ConfigManager.getConfig().emitter.emit_synthetics) { ctx.printIndentation(); ctx.printString("// Synthetic"); ctx.newLine(); } else { continue; } } at_least_one = true; ctx.printIndentation(); ctx.emit(field); ctx.printString(";"); ctx.newLine(); } if (at_least_one) { ctx.newLine(); } } if (!type.getStaticMethods().isEmpty()) { for (MethodEntry mth : type.getStaticMethods()) { if (mth.isSynthetic()) { if (ConfigManager.getConfig().emitter.emit_synthetics) { ctx.printIndentation(); ctx.printString("// Synthetic"); if (mth.isBridge()) { ctx.printString(" - Bridge"); } ctx.newLine(); } else { continue; } } ctx.emit(mth); ctx.newLine(); ctx.newLine(); } } if (!type.getMethods().isEmpty()) { for (MethodEntry mth : type.getMethods()) { if (mth.isSynthetic()) { if (ConfigManager.getConfig().emitter.emit_synthetics) { ctx.printIndentation(); ctx.printString("// Synthetic"); if (mth.isBridge()) { ctx.printString(" - Bridge"); } ctx.newLine(); } else { continue; } } ctx.printIndentation(); for (Annotation anno : mth.getAnnotations()) { ctx.emit(anno); if (ctx.getFormat().insert_new_line_after_annotation_on_method) { ctx.newLine(); ctx.printIndentation(); } else { ctx.printString(" "); } } ctx.printString(mth.getAccessModifier().asString()); if (mth.getAccessModifier() != AccessModifier.PACKAGE_PRIVATE) { ctx.printString(" "); } MethodSignature sig = mth.getMethodSignature(); if (sig != null) { if (!sig.getTypeParameters().isEmpty()) { generics.emitTypeParameters(ctx, sig.getTypeParameters()); ctx.printString(" "); } generics.emitTypeSignature(ctx, sig.getReturnType(), false); } else { ctx.emitType(mth.getReturnType(), false); } ctx.printString(" "); ctx.printString(mth.getName()); ctx.printString(" ", ctx.getFormat().insert_space_before_opening_paren_in_method_declaration); ctx.printString("("); StatementBlock block = mth.getInstructions(); if (!mth.getParamTypes().isEmpty()) { ctx.printString(" ", ctx.getFormat().insert_space_between_empty_parens_in_method_declaration); } int param_index = 1; for (int i = 0; i < mth.getParamTypes().size(); i++) { boolean varargs = mth.isVarargs() && i == mth.getParamTypes().size() - 1; if (block == null) { if (sig != null) { // interfaces have no lvt for parameters, need to // get generic types from the method signature generics.emitTypeSignature(ctx, sig.getParameters().get(i), varargs); } else { ctx.emitType(mth.getParamTypes().get(i), varargs); } ctx.printString(" "); ctx.printString("local" + param_index); } else { Local local = mth.getLocals().getLocal(param_index); LocalInstance insn = local.getParameterInstance(); for (Annotation anno : insn.getAnnotations()) { ctx.emit(anno); if (ctx.getFormat().insert_new_line_after_annotation_on_parameter) { ctx.indent(); ctx.indent(); ctx.newLine(); ctx.printIndentation(); ctx.dedent(); ctx.dedent(); } else { ctx.printString(" "); } } generics.emitTypeSignature(ctx, insn.getType(), varargs); ctx.printString(" "); ctx.printString(insn.getName()); } if (i < mth.getParamTypes().size() - 1) { ctx.printString(" ", ctx.getFormat().insert_space_before_comma_in_method_declaration_parameters); ctx.printString(","); ctx.printString(" ", ctx.getFormat().insert_space_after_comma_in_method_declaration_parameters); ctx.markWrapPoint(ctx.getFormat().alignment_for_parameters_in_method_declaration, i + 1); } String desc = mth.getParamTypes().get(i).getDescriptor(); param_index++; if ("D".equals(desc) || "J".equals(desc)) { param_index++; } } ctx.printString(" ", ctx.getFormat().insert_space_before_closing_paren_in_method_declaration); ctx.printString(")"); if (mth.getAnnotationValue() != null) { ctx.printString(" default "); AnnotationEmitter.emitValue(ctx, mth.getAnnotationValue()); } ctx.printString(";"); ctx.newLine(); ctx.newLine(); } } Collection<InnerClassInfo> inners = type.getInnerClasses(); for (InnerClassInfo inner : inners) { if (inner.getOuterName() == null || !inner.getOuterName().equals(type.getName())) { continue; } TypeEntry inner_type = type.getSource().get(inner.getName()); ctx.newLine(); ctx.emit(inner_type); } if (ctx.getFormat().brace_position_for_type_declaration == BracePosition.NEXT_LINE_SHIFTED) { ctx.printIndentation(); ctx.printString("}"); ctx.dedent(); } else { ctx.dedent(); ctx.printIndentation(); ctx.printString("}"); } ctx.newLine(); return true; } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/instruction/ArrayLoadEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/instruction/ArrayLoadEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.instruction; import org.spongepowered.despector.ast.generic.ClassTypeSignature; import org.spongepowered.despector.ast.generic.TypeSignature; import org.spongepowered.despector.ast.insn.var.ArrayAccess; import org.spongepowered.despector.emitter.InstructionEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; /** * An emitter for an array access instruction. */ public class ArrayLoadEmitter implements InstructionEmitter<JavaEmitterContext, ArrayAccess> { @Override public void emit(JavaEmitterContext ctx, ArrayAccess arg, TypeSignature type) { ctx.emit(arg.getArrayVar(), null); ctx.printString(" ", ctx.getFormat().insert_space_before_opening_bracket_in_array_reference); ctx.printString("["); ctx.emit(arg.getIndex(), ClassTypeSignature.INT); ctx.printString(" ", ctx.getFormat().insert_space_before_closing_bracket_in_array_reference); ctx.printString("]"); } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/instruction/CastEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/instruction/CastEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.instruction; import org.spongepowered.despector.ast.generic.TypeSignature; import org.spongepowered.despector.ast.insn.misc.Cast; import org.spongepowered.despector.ast.insn.op.Operator; import org.spongepowered.despector.emitter.InstructionEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; /** * An emitter of cast instructions. */ public class CastEmitter implements InstructionEmitter<JavaEmitterContext, Cast> { @Override public void emit(JavaEmitterContext ctx, Cast arg, TypeSignature type) { TypeSignature inferred = arg.getValue().inferType(); if (inferred.equals(arg.getType())) { ctx.emit(arg.getValue(), arg.getType()); return; } boolean operator = arg.getValue() instanceof Operator; ctx.printString("("); if (!operator) { ctx.printString("("); } ctx.emitType(arg.getType(), false); ctx.printString(") "); if (operator) { ctx.printString("("); } ctx.emit(arg.getValue(), null); ctx.printString(")"); } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/instruction/TypeConstantEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/instruction/TypeConstantEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.instruction; import org.spongepowered.despector.ast.generic.TypeSignature; import org.spongepowered.despector.ast.insn.cst.TypeConstant; import org.spongepowered.despector.emitter.InstructionEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; /** * An emitter for type constants. */ public class TypeConstantEmitter implements InstructionEmitter<JavaEmitterContext, TypeConstant> { @Override public void emit(JavaEmitterContext ctx, TypeConstant arg, TypeSignature type) { ctx.emitTypeName(arg.getConstant().getName()); ctx.printString(".class"); } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/instruction/LongConstantEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/instruction/LongConstantEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.instruction; import org.spongepowered.despector.ast.generic.TypeSignature; import org.spongepowered.despector.ast.insn.cst.LongConstant; import org.spongepowered.despector.emitter.InstructionEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; /** * An emitter for long constants. */ public class LongConstantEmitter implements InstructionEmitter<JavaEmitterContext, LongConstant> { @Override public void emit(JavaEmitterContext ctx, LongConstant arg, TypeSignature type) { ctx.printString(String.valueOf(arg.getConstant())); } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/instruction/package-info.java
src/main/java/org/spongepowered/despector/emitter/java/instruction/package-info.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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. */ @org.spongepowered.despector.util.NonnullByDefault package org.spongepowered.despector.emitter.java.instruction;
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/instruction/CompareEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/instruction/CompareEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.instruction; import org.spongepowered.despector.ast.generic.ClassTypeSignature; import org.spongepowered.despector.ast.generic.TypeSignature; import org.spongepowered.despector.ast.insn.misc.NumberCompare; import org.spongepowered.despector.emitter.InstructionEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; /** * An emitter for a number compare. */ public class CompareEmitter implements InstructionEmitter<JavaEmitterContext, NumberCompare> { @Override public void emit(JavaEmitterContext ctx, NumberCompare arg, TypeSignature type) { if (arg.getRightOperand().inferType().equals(ClassTypeSignature.INT) && arg.getLeftOperand().inferType().equals(ClassTypeSignature.INT)) { ctx.printString("Integer.signum("); ctx.emit(arg.getRightOperand(), arg.inferType()); ctx.printString(" - "); ctx.emit(arg.getLeftOperand(), arg.inferType()); ctx.printString(")"); } else { ctx.printString("Float.compare("); ctx.emit(arg.getRightOperand(), arg.inferType()); ctx.printString(", "); ctx.emit(arg.getLeftOperand(), arg.inferType()); ctx.printString(")"); } } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/instruction/FieldAccessEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/instruction/FieldAccessEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.instruction; import org.spongepowered.despector.ast.generic.ClassTypeSignature; import org.spongepowered.despector.ast.generic.TypeSignature; import org.spongepowered.despector.ast.insn.Instruction; import org.spongepowered.despector.ast.insn.var.FieldAccess; import org.spongepowered.despector.ast.insn.var.InstanceFieldAccess; import org.spongepowered.despector.ast.insn.var.LocalAccess; import org.spongepowered.despector.ast.insn.var.StaticFieldAccess; import org.spongepowered.despector.config.ConfigManager; import org.spongepowered.despector.emitter.InstructionEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; import org.spongepowered.despector.util.TypeHelper; /** * An emitter for instance field accesses. */ public class FieldAccessEmitter implements InstructionEmitter<JavaEmitterContext, FieldAccess> { @Override public void emit(JavaEmitterContext ctx, FieldAccess arg, TypeSignature type) { if (arg instanceof StaticFieldAccess) { if (ctx.getType() == null || !((StaticFieldAccess) arg).getOwnerName().equals(ctx.getType().getName())) { ctx.emitTypeName(((StaticFieldAccess) arg).getOwnerName()); ctx.printString("."); } ctx.printString(arg.getFieldName()); } else if (arg instanceof InstanceFieldAccess) { if (TypeHelper.isAnonClass(arg.getOwnerName()) && arg.getFieldName().startsWith("val$")) { ctx.printString(arg.getFieldName().substring(4)); return; } Instruction owner = ((InstanceFieldAccess) arg).getFieldOwner(); // TODO check if there is a local in scope with the same name as the // field and we need this regardless if (ConfigManager.getConfig().emitter.emit_this_for_fields || !(owner instanceof LocalAccess) || ((LocalAccess) owner).getLocal().getIndex() != 0 || ctx.getMethod() == null || ctx.getMethod().isStatic()) { ctx.emit(owner, ClassTypeSignature.of(arg.getOwnerType())); ctx.printString("."); } ctx.printString(arg.getFieldName()); } else { throw new UnsupportedOperationException(); } } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/instruction/NewEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/instruction/NewEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.instruction; import org.spongepowered.despector.ast.generic.ClassTypeSignature; import org.spongepowered.despector.ast.generic.GenericClassTypeSignature; import org.spongepowered.despector.ast.generic.TypeSignature; import org.spongepowered.despector.ast.insn.Instruction; import org.spongepowered.despector.ast.stmt.assign.LocalAssignment; import org.spongepowered.despector.ast.stmt.invoke.New; import org.spongepowered.despector.ast.type.ClassEntry; import org.spongepowered.despector.ast.type.TypeEntry; import org.spongepowered.despector.emitter.InstructionEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; import org.spongepowered.despector.emitter.java.special.AnonymousClassEmitter; import org.spongepowered.despector.util.TypeHelper; import java.util.List; /** * An emitter for a new instruction. */ public class NewEmitter implements InstructionEmitter<JavaEmitterContext, New> { @Override public void emit(JavaEmitterContext ctx, New arg, TypeSignature type) { if (arg.getType().getName().contains("$")) { String last = arg.getType().getName(); int last_inner_class = last.lastIndexOf('$'); last = last.substring(last_inner_class + 1); if (last.matches("[0-9]+")) { TypeEntry anon_type = ctx.getType().getSource().get(arg.getType().getName()); if (anon_type != null) { AnonymousClassEmitter emitter = ctx.getEmitterSet().getSpecialEmitter(AnonymousClassEmitter.class); emitter.emit(ctx, (ClassEntry) anon_type, arg); return; } System.err.println("Missing TypeEntry for anon type " + arg.getType()); } } ctx.printString("new "); ctx.emitType(arg.getType(), false); if (ctx.getField() != null && ctx.getField().getType().hasArguments()) { ctx.printString("<>"); } else if (ctx.getStatement() != null && ctx.getStatement() instanceof LocalAssignment) { LocalAssignment assign = (LocalAssignment) ctx.getStatement(); TypeSignature sig = assign.getLocal().getType(); if (sig != null && sig instanceof GenericClassTypeSignature && !((GenericClassTypeSignature) sig).getArguments().isEmpty()) { ctx.printString("<>"); } } ctx.printString("("); List<String> args = TypeHelper.splitSig(arg.getCtorDescription()); for (int i = 0; i < arg.getParameters().length; i++) { Instruction param = arg.getParameters()[i]; ctx.markWrapPoint(); ctx.emit(param, ClassTypeSignature.of(args.get(i))); if (i < arg.getParameters().length - 1) { ctx.printString(", "); } } ctx.printString(")"); } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/instruction/DynamicInvokeEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/instruction/DynamicInvokeEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.instruction; import org.spongepowered.despector.ast.Locals.Local; import org.spongepowered.despector.ast.Locals.LocalInstance; import org.spongepowered.despector.ast.generic.TypeSignature; import org.spongepowered.despector.ast.stmt.StatementBlock; import org.spongepowered.despector.ast.stmt.invoke.Lambda; import org.spongepowered.despector.ast.stmt.misc.Return; import org.spongepowered.despector.ast.type.MethodEntry; import org.spongepowered.despector.ast.type.TypeEntry; import org.spongepowered.despector.emitter.InstructionEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; import org.spongepowered.despector.util.TypeHelper; /** * An emitter for a dynamic invoke. */ public class DynamicInvokeEmitter implements InstructionEmitter<JavaEmitterContext, Lambda> { @Override public void emit(JavaEmitterContext ctx, Lambda arg, TypeSignature type) { TypeEntry owner = ctx.getType().getSource().get(TypeHelper.descToType(arg.getLambdaOwner())); MethodEntry method = owner.getStaticMethod(arg.getLambdaMethod(), arg.getLambdaDescription()); if (method == null) { method = owner.getMethod(arg.getLambdaMethod()); if (method == null) { throw new IllegalStateException(); } } StatementBlock block = method.getInstructions(); ctx.printString("("); for (int i = 0; i < method.getParamTypes().size(); i++) { if (block == null) { ctx.printString("local" + i); } else { Local local = method.getLocals().getLocal(i); LocalInstance insn = local.getParameterInstance(); ctx.printString(insn.getName()); } if (i < method.getParamTypes().size() - 1) { ctx.printString(", "); } } ctx.printString(") ->"); ctx.printString(" ", ctx.getFormat().insert_space_after_lambda_arrow); if (block.getStatementCount() == 1) { Return ret = (Return) block.getStatement(0); ctx.emit(ret.getValue().get(), method.getReturnType()); return; } else if (block.getStatementCount() == 2) { if (block.getStatement(1) instanceof Return && !((Return) block.getStatement(1)).getValue().isPresent()) { ctx.emit(block.getStatement(0), false); return; } } ctx.printString("{"); ctx.newLine().indent(); ctx.emitBody(block); ctx.newLine().dedent().printIndentation(); ctx.printString("}"); } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/instruction/NewArrayEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/instruction/NewArrayEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.instruction; import org.spongepowered.despector.ast.generic.ClassTypeSignature; import org.spongepowered.despector.ast.generic.TypeSignature; import org.spongepowered.despector.ast.insn.misc.NewArray; import org.spongepowered.despector.emitter.InstructionEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; /** * An emitter for a new array instanciation instruction. */ public class NewArrayEmitter implements InstructionEmitter<JavaEmitterContext, NewArray> { @Override public void emit(JavaEmitterContext ctx, NewArray arg, TypeSignature type) { ctx.printString("new "); ctx.emitType(arg.getType().getDescriptor()); if (arg.getInitializer() == null || arg.getInitializer().length == 0) { ctx.printString("["); ctx.printString(" ", ctx.getFormat().insert_space_after_opening_bracket_in_array_allocation_expression); ctx.emit(arg.getSize(), ClassTypeSignature.INT); ctx.printString(" ", ctx.getFormat().insert_space_before_closing_bracket_in_array_allocation_expression); ctx.printString("]"); } else { ctx.printString("[]"); ctx.emitBrace(ctx.getFormat().brace_position_for_array_initializer, false, true); ctx.printString(" ", ctx.getFormat().insert_space_after_opening_brace_in_array_initializer); ctx.markWrapPoint(ctx.getFormat().alignment_for_expressions_in_array_initializer, 0); if (ctx.getFormat().insert_new_line_after_opening_brace_in_array_initializer) { ctx.newLine(); ctx.printIndentation(); } for (int i = 0; i < arg.getInitializer().length; i++) { ctx.emit(arg.getInitializer()[i], arg.getType()); if (i < arg.getInitializer().length - 1) { ctx.printString(" ", ctx.getFormat().insert_space_before_comma_in_array_initializer); ctx.printString(","); ctx.printString(" ", ctx.getFormat().insert_space_after_comma_in_array_initializer); ctx.markWrapPoint(ctx.getFormat().alignment_for_expressions_in_array_initializer, i + 1); } } if (ctx.getFormat().insert_new_line_before_closing_brace_in_array_initializer) { ctx.newLine(); ctx.printIndentation(); } switch (ctx.getFormat().brace_position_for_array_initializer) { case NEXT_LINE: ctx.dedent(); ctx.newLine(); ctx.printIndentation(); ctx.printString("}"); break; case NEXT_LINE_ON_WRAP: ctx.dedent(); ctx.newLine(); ctx.printIndentation(); ctx.printString("}"); break; case NEXT_LINE_SHIFTED: ctx.newLine(); ctx.printIndentation(); ctx.printString("}"); ctx.dedent(); break; case SAME_LINE: default: ctx.printString("}"); ctx.dedent(); break; } } } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/instruction/DoubleConstantEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/instruction/DoubleConstantEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.instruction; import org.spongepowered.despector.ast.generic.TypeSignature; import org.spongepowered.despector.ast.insn.cst.DoubleConstant; import org.spongepowered.despector.emitter.InstructionEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; /** * An emitter for a double constant. */ public class DoubleConstantEmitter implements InstructionEmitter<JavaEmitterContext, DoubleConstant> { @Override public void emit(JavaEmitterContext ctx, DoubleConstant arg, TypeSignature type) { ctx.printString(String.valueOf(arg.getConstant())); } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/instruction/MethodReferenceEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/instruction/MethodReferenceEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.instruction; import org.spongepowered.despector.ast.generic.ClassTypeSignature; import org.spongepowered.despector.ast.generic.TypeSignature; import org.spongepowered.despector.ast.stmt.invoke.MethodReference; import org.spongepowered.despector.emitter.InstructionEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; public class MethodReferenceEmitter implements InstructionEmitter<JavaEmitterContext, MethodReference> { @Override public void emit(JavaEmitterContext ctx, MethodReference arg, TypeSignature type) { ctx.emit(arg.getOwnerVal(), ClassTypeSignature.of(arg.getLambdaOwner())); ctx.printString("::"); ctx.printString(arg.getLambdaMethod()); } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/instruction/FloatConstantEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/instruction/FloatConstantEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.instruction; import org.spongepowered.despector.ast.generic.TypeSignature; import org.spongepowered.despector.ast.insn.cst.FloatConstant; import org.spongepowered.despector.emitter.InstructionEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; /** * An emitter for 32-bit floating point constants. */ public class FloatConstantEmitter implements InstructionEmitter<JavaEmitterContext, FloatConstant> { @Override public void emit(JavaEmitterContext ctx, FloatConstant arg, TypeSignature type) { ctx.printString(String.valueOf(arg.getConstant()) + "F"); } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/instruction/MultiNewArrayEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/instruction/MultiNewArrayEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.instruction; import org.spongepowered.despector.ast.generic.ClassTypeSignature; import org.spongepowered.despector.ast.generic.TypeSignature; import org.spongepowered.despector.ast.insn.Instruction; import org.spongepowered.despector.ast.insn.misc.MultiNewArray; import org.spongepowered.despector.emitter.InstructionEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; /** * An emitter for a new array instanciation instruction. */ public class MultiNewArrayEmitter implements InstructionEmitter<JavaEmitterContext, MultiNewArray> { @Override public void emit(JavaEmitterContext ctx, MultiNewArray arg, TypeSignature type) { ctx.printString("new "); String desc = arg.getType().getDescriptor(); for (int i = 0; i < arg.getSizes().length; i++) { if (!desc.startsWith("[")) { throw new IllegalStateException(); } desc = desc.substring(1); } ctx.emitType(desc); for (Instruction size : arg.getSizes()) { ctx.printString("["); ctx.printString(" ", ctx.getFormat().insert_space_after_opening_bracket_in_array_allocation_expression); ctx.emit(size, ClassTypeSignature.INT); ctx.printString(" ", ctx.getFormat().insert_space_before_closing_bracket_in_array_allocation_expression); ctx.printString("]"); } } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/instruction/TernaryEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/instruction/TernaryEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.instruction; import org.spongepowered.despector.ast.generic.ClassTypeSignature; import org.spongepowered.despector.ast.generic.TypeSignature; import org.spongepowered.despector.ast.insn.condition.CompareCondition; import org.spongepowered.despector.ast.insn.cst.IntConstant; import org.spongepowered.despector.ast.insn.misc.Ternary; import org.spongepowered.despector.emitter.InstructionEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; /** * An emitter for ternary instructions. */ public class TernaryEmitter implements InstructionEmitter<JavaEmitterContext, Ternary> { @Override public void emit(JavaEmitterContext ctx, Ternary ternary, TypeSignature type) { if (type == ClassTypeSignature.BOOLEAN && checkBooleanExpression(ctx, ternary)) { return; } if (ternary.getCondition() instanceof CompareCondition) { ctx.printString("("); ctx.emit(ternary.getCondition()); ctx.printString(")"); } else { ctx.emit(ternary.getCondition()); } ctx.markWrapPoint(); ctx.printString(" ? "); ctx.emit(ternary.getTrueValue(), type); ctx.markWrapPoint(); ctx.printString(" : "); ctx.emit(ternary.getFalseValue(), type); } /** * Checks if the given ternary can be simplified to a boolean expression. */ public boolean checkBooleanExpression(JavaEmitterContext ctx, Ternary ternary) { if (ternary.getTrueValue() instanceof IntConstant && ternary.getFalseValue() instanceof IntConstant) { int tr = ((IntConstant) ternary.getTrueValue()).getConstant(); int fl = ((IntConstant) ternary.getFalseValue()).getConstant(); if (tr == 0 && fl == 0) { ctx.printString("false"); } else if (tr == 1 && fl == 1) { ctx.printString("true"); } else if (tr == 0) { ctx.printString("!"); ctx.printString(" ", ctx.getFormat().insert_space_after_unary_operator); ctx.printString("("); ctx.emit(ternary.getCondition()); ctx.printString(")"); } else if (fl == 0) { ctx.emit(ternary.getCondition()); } else { return false; } return true; } else if (ternary.getTrueValue() instanceof IntConstant) { if (((IntConstant) ternary.getTrueValue()).getConstant() == 0) { // !a && b ctx.printString("!"); ctx.printString(" ", ctx.getFormat().insert_space_after_unary_operator); ctx.printString("("); ctx.emit(ternary.getCondition()); ctx.printString(") && "); if (ternary.getFalseValue() instanceof Ternary) { ctx.printString("("); ctx.emit(ternary.getFalseValue(), ClassTypeSignature.BOOLEAN); ctx.printString(")"); } else { ctx.emit(ternary.getFalseValue(), ClassTypeSignature.BOOLEAN); } } else { ctx.emit(ternary.getCondition()); ctx.markWrapPoint(); ctx.printString(" || "); if (ternary.getFalseValue() instanceof Ternary) { ctx.printString("("); ctx.emit(ternary.getFalseValue(), ClassTypeSignature.BOOLEAN); ctx.printString(")"); } else { ctx.emit(ternary.getFalseValue(), ClassTypeSignature.BOOLEAN); } } return true; } else if (ternary.getFalseValue() instanceof IntConstant) { if (((IntConstant) ternary.getFalseValue()).getConstant() == 0) { // !a && b ctx.printString("!"); ctx.printString(" ", ctx.getFormat().insert_space_after_unary_operator); ctx.printString("("); ctx.emit(ternary.getCondition()); ctx.printString(") && "); if (ternary.getTrueValue() instanceof Ternary) { ctx.printString("("); ctx.emit(ternary.getTrueValue(), ClassTypeSignature.BOOLEAN); ctx.printString(")"); } else { ctx.emit(ternary.getTrueValue(), ClassTypeSignature.BOOLEAN); } } else { ctx.emit(ternary.getCondition()); ctx.markWrapPoint(); ctx.printString(" || "); if (ternary.getTrueValue() instanceof Ternary) { ctx.printString("("); ctx.emit(ternary.getTrueValue(), ClassTypeSignature.BOOLEAN); ctx.printString(")"); } else { ctx.emit(ternary.getTrueValue(), ClassTypeSignature.BOOLEAN); } } return true; } return false; } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/instruction/OperatorEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/instruction/OperatorEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.instruction; import org.spongepowered.despector.ast.generic.TypeSignature; import org.spongepowered.despector.ast.insn.op.Operator; import org.spongepowered.despector.emitter.InstructionEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; /** * An emitter for an operator. */ public class OperatorEmitter implements InstructionEmitter<JavaEmitterContext, Operator> { @Override public void emit(JavaEmitterContext ctx, Operator arg, TypeSignature type) { if (arg.getLeftOperand() instanceof Operator) { Operator left = (Operator) arg.getLeftOperand(); if (arg.getOperator().getPrecedence() > left.getOperator().getPrecedence()) { ctx.printString("("); } ctx.emit(arg.getLeftOperand(), null); if (arg.getOperator().getPrecedence() > left.getOperator().getPrecedence()) { ctx.printString(")"); } } else { ctx.emit(arg.getLeftOperand(), null); } ctx.markWrapPoint(); ctx.printString(" ", ctx.getFormat().insert_space_before_binary_operator); ctx.printString(arg.getOperator().getSymbol()); ctx.printString(" ", ctx.getFormat().insert_space_after_binary_operator); if (arg.getRightOperand() instanceof Operator) { Operator right = (Operator) arg.getRightOperand(); if (arg.getOperator().getPrecedence() > right.getOperator().getPrecedence()) { ctx.printString("("); } ctx.emit(arg.getRightOperand(), null); if (arg.getOperator().getPrecedence() > right.getOperator().getPrecedence()) { ctx.printString(")"); } } else { ctx.emit(arg.getRightOperand(), null); } } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/instruction/IntConstantEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/instruction/IntConstantEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.instruction; import org.spongepowered.despector.ast.generic.ClassTypeSignature; import org.spongepowered.despector.ast.generic.TypeSignature; import org.spongepowered.despector.ast.insn.cst.IntConstant; import org.spongepowered.despector.emitter.InstructionEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; /** * An emitter for integer constants. */ public class IntConstantEmitter implements InstructionEmitter<JavaEmitterContext, IntConstant> { @Override public void emit(JavaEmitterContext ctx, IntConstant arg, TypeSignature type) { // Some basic constant replacement, TODO should probably make this // better int cst = arg.getConstant(); if (cst == Integer.MAX_VALUE) { ctx.printString("Integer.MAX_VALUE"); return; } else if (cst == Integer.MIN_VALUE) { ctx.printString("Integer.MIN_VALUE"); return; } if (type == ClassTypeSignature.BOOLEAN) { if (cst == 0) { ctx.printString("false"); } else { ctx.printString("true"); } return; } else if (type == ClassTypeSignature.CHAR) { char c = (char) cst; if (c < 127 && c > 31) { ctx.printString("'" + c + "'"); return; } else if (c >= 127) { ctx.printString("'\\u" + Integer.toHexString(cst).toLowerCase() + "'"); return; } } switch (arg.getFormat()) { case BINARY: { ctx.printString("0b"); ctx.printString(Integer.toBinaryString(cst)); break; } case OCTAL: ctx.printString("0"); ctx.printString(Integer.toOctalString(cst)); break; case HEXADECIMAL: ctx.printString("0x"); String str = Integer.toHexString(cst); for (int i = str.length(); i < 8; i++) { ctx.printString("0"); } ctx.printString(str); break; default: case DECIMAL: ctx.printString(String.valueOf(cst)); break; } } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/instruction/StringConstantEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/instruction/StringConstantEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.instruction; import org.spongepowered.despector.ast.generic.TypeSignature; import org.spongepowered.despector.ast.insn.cst.StringConstant; import org.spongepowered.despector.config.ConfigManager; import org.spongepowered.despector.emitter.InstructionEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; /** * An emitter for string constants. */ public class StringConstantEmitter implements InstructionEmitter<JavaEmitterContext, StringConstant> { /** * Escapes the given string. */ public static String escape(String text) { StringBuilder str = new StringBuilder(); for (int i = 0; i < text.length(); i++) { char n = text.charAt(i); if (n == '\n') { str.append("\\n"); } else if (n == '\\') { str.append("\\\\"); } else if (n == '\t') { str.append("\\t"); } else if (n == '\r') { str.append("\\r"); } else if (n == '\b') { str.append("\\b"); } else if (n == '\f') { str.append("\\f"); } else if (n == '\'') { str.append("\\'"); } else if (n == '\"') { str.append("\\\""); } else if (n == '\0') { str.append("\\0"); } else { str.append(n); } } return str.toString(); } @Override public void emit(JavaEmitterContext ctx, StringConstant arg, TypeSignature type) { if (arg.getConstant().contains("\n") && ConfigManager.getConfig().kotlin.replace_mulit_line_strings) { ctx.printString("\"\"\""); String[] lines = arg.getConstant().split("\n"); for (int i = 0; i < lines.length; i++) { ctx.printString(lines[i]); if (i < lines.length - 1 || arg.getConstant().endsWith("\n")) { ctx.newLine(); } } ctx.printString("\"\"\""); return; } ctx.printString("\""); ctx.printString(escape(arg.getConstant())); ctx.printString("\""); } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/instruction/NegativeEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/instruction/NegativeEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.instruction; import org.spongepowered.despector.ast.generic.TypeSignature; import org.spongepowered.despector.ast.insn.op.NegativeOperator; import org.spongepowered.despector.ast.insn.op.Operator; import org.spongepowered.despector.emitter.InstructionEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; /** * An emitter for a unary negative operator. */ public class NegativeEmitter implements InstructionEmitter<JavaEmitterContext, NegativeOperator> { @Override public void emit(JavaEmitterContext ctx, NegativeOperator arg, TypeSignature type) { ctx.printString("-"); ctx.printString(" ", ctx.getFormat().insert_space_after_unary_operator); if (arg.getOperand() instanceof Operator) { ctx.printString("("); ctx.emit(arg.getOperand(), null); ctx.printString(")"); } else { ctx.emit(arg.getOperand(), null); } } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/instruction/NullConstantEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/instruction/NullConstantEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.instruction; import org.spongepowered.despector.ast.generic.TypeSignature; import org.spongepowered.despector.ast.insn.cst.NullConstant; import org.spongepowered.despector.emitter.InstructionEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; /** * An emitter for the null constant. */ public class NullConstantEmitter implements InstructionEmitter<JavaEmitterContext, NullConstant> { @Override public void emit(JavaEmitterContext ctx, NullConstant arg, TypeSignature type) { ctx.printString("null"); } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/instruction/InstanceOfEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/instruction/InstanceOfEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.instruction; import org.spongepowered.despector.ast.generic.TypeSignature; import org.spongepowered.despector.ast.insn.misc.InstanceOf; import org.spongepowered.despector.emitter.InstructionEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; /** * An emitter for an instanceof check. */ public class InstanceOfEmitter implements InstructionEmitter<JavaEmitterContext, InstanceOf> { @Override public void emit(JavaEmitterContext ctx, InstanceOf arg, TypeSignature type) { ctx.emit(arg.getCheckedValue(), null); ctx.printString(" instanceof "); ctx.emitType(arg.getType().getDescriptor()); } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/instruction/InstanceMethodInvokeEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/instruction/InstanceMethodInvokeEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.instruction; import com.google.common.collect.Lists; import org.spongepowered.despector.ast.generic.ClassTypeSignature; import org.spongepowered.despector.ast.generic.TypeSignature; import org.spongepowered.despector.ast.insn.Instruction; import org.spongepowered.despector.ast.insn.cst.StringConstant; import org.spongepowered.despector.ast.insn.misc.NewArray; import org.spongepowered.despector.ast.insn.var.LocalAccess; import org.spongepowered.despector.ast.stmt.invoke.InstanceMethodInvoke; import org.spongepowered.despector.ast.stmt.invoke.New; import org.spongepowered.despector.ast.stmt.invoke.StaticMethodInvoke; import org.spongepowered.despector.ast.type.MethodEntry; import org.spongepowered.despector.ast.type.TypeEntry; import org.spongepowered.despector.config.ConfigManager; import org.spongepowered.despector.emitter.InstructionEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; import org.spongepowered.despector.util.TypeHelper; import java.util.List; /** * An emitter for instance method invoke instructions. */ public class InstanceMethodInvokeEmitter implements InstructionEmitter<JavaEmitterContext, InstanceMethodInvoke> { @Override public void emit(JavaEmitterContext ctx, InstanceMethodInvoke arg, TypeSignature type) { if (arg.getOwner().equals("Ljava/lang/StringBuilder;") && arg.getMethodName().equals("toString")) { if (replaceStringConcat(ctx, arg)) { return; } } if (arg.getMethodName().equals("<init>")) { if (ctx.getType() != null) { if (arg.getOwnerName().equals(ctx.getType().getName())) { ctx.printString("this"); } else { ctx.printString("super"); } } else { ctx.printString("super"); } } else { if (arg.getCallee() instanceof LocalAccess && ctx.getMethod() != null && !ctx.getMethod().isStatic()) { LocalAccess local = (LocalAccess) arg.getCallee(); if (local.getLocal().getIndex() == 0) { if (ctx.getType() != null && !arg.getOwnerName().equals(ctx.getType().getName())) { ctx.printString("super."); } else if (ConfigManager.getConfig().emitter.emit_this_for_methods) { ctx.printString("this."); } } else { ctx.emit(local, null); ctx.markWrapPoint(); ctx.printString("."); } } else { ctx.emit(arg.getCallee(), ClassTypeSignature.of(arg.getOwner())); ctx.markWrapPoint(); ctx.printString("."); } ctx.printString(arg.getMethodName()); } boolean is_varargs = false; TypeEntry target = ctx.getType().getSource().get(arg.getOwnerName()); if (target != null) { MethodEntry mth = target.getMethod(arg.getMethodName(), arg.getMethodDescription()); // TODO need to search up superclasses if not found if (mth != null) { is_varargs = mth.isVarargs(); } } ctx.printString("("); List<String> param_types = TypeHelper.splitSig(arg.getMethodDescription()); for (int i = 0; i < arg.getParameters().length; i++) { Instruction param = arg.getParameters()[i]; if (is_varargs && i == arg.getParameters().length - 1 && param instanceof NewArray) { NewArray varargs = (NewArray) param; if (varargs.getInitializer() != null) { for (int o = 0; o < varargs.getInitializer().length; o++) { ctx.emit(varargs.getInitializer()[o], varargs.getType()); if (o < varargs.getInitializer().length - 1) { ctx.printString(", "); ctx.markWrapPoint(); } } break; } } ctx.emit(param, ClassTypeSignature.of(param_types.get(i))); if (i < arg.getParameters().length - 1) { ctx.printString(", "); ctx.markWrapPoint(); } } ctx.printString(")"); } protected boolean replaceStringConcat(JavaEmitterContext ctx, InstanceMethodInvoke arg) { // We detect and collapse string builder chains used to perform // string concatentation into simple "foo" + "bar" form boolean valid = true; Instruction callee = arg.getCallee(); List<Instruction> constants = Lists.newArrayList(); // We add all the constants to the front of this list as we have to // replay them in the reverse of the ordering that we will encounter // them in while (callee != null) { if (callee instanceof InstanceMethodInvoke) { InstanceMethodInvoke call = (InstanceMethodInvoke) callee; if (call.getParameters().length == 1) { constants.add(0, call.getParameters()[0]); callee = call.getCallee(); continue; } } else if (callee instanceof New) { New ref = (New) callee; if ("Ljava/lang/StringBuilder;".equals(ref.getType().getDescriptor())) { if (ref.getParameters().length == 1) { Instruction initial = ref.getParameters()[0]; if (initial instanceof StaticMethodInvoke) { StaticMethodInvoke valueof = (StaticMethodInvoke) initial; if (valueof.getMethodName().equals("valueOf") && valueof.getOwner().equals("Ljava/lang/String;")) { Instruction internal = valueof.getParameters()[0]; if (internal instanceof StringConstant) { initial = internal; } else if (internal instanceof LocalAccess) { LocalAccess local = (LocalAccess) internal; if (local.getLocal().getType() == ClassTypeSignature.STRING) { initial = local; } } } } constants.add(0, initial); } break; } valid = false; break; } else if (callee instanceof LocalAccess) { valid = false; break; } valid = false; } if (valid) { for (int i = 0; i < constants.size(); i++) { ctx.emit(constants.get(i), ClassTypeSignature.STRING); if (i < constants.size() - 1) { ctx.markWrapPoint(); ctx.printString(" + "); } } return true; } return false; } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/instruction/StaticMethodInvokeEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/instruction/StaticMethodInvokeEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.instruction; import org.spongepowered.despector.ast.generic.ClassTypeSignature; import org.spongepowered.despector.ast.generic.TypeSignature; import org.spongepowered.despector.ast.generic.VoidTypeSignature; import org.spongepowered.despector.ast.insn.Instruction; import org.spongepowered.despector.ast.insn.misc.NewArray; import org.spongepowered.despector.ast.insn.var.FieldAccess; import org.spongepowered.despector.ast.insn.var.InstanceFieldAccess; import org.spongepowered.despector.ast.insn.var.StaticFieldAccess; import org.spongepowered.despector.ast.stmt.Statement; import org.spongepowered.despector.ast.stmt.assign.FieldAssignment; import org.spongepowered.despector.ast.stmt.assign.InstanceFieldAssignment; import org.spongepowered.despector.ast.stmt.assign.StaticFieldAssignment; import org.spongepowered.despector.ast.stmt.invoke.StaticMethodInvoke; import org.spongepowered.despector.ast.stmt.misc.Return; import org.spongepowered.despector.ast.type.MethodEntry; import org.spongepowered.despector.ast.type.TypeEntry; import org.spongepowered.despector.emitter.InstructionEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; import org.spongepowered.despector.util.TypeHelper; import java.util.List; /** * An emitter for static method invoke instructions. */ public class StaticMethodInvokeEmitter implements InstructionEmitter<JavaEmitterContext, StaticMethodInvoke> { @Override public void emit(JavaEmitterContext ctx, StaticMethodInvoke arg, TypeSignature type) { String owner = TypeHelper.descToType(arg.getOwner()); if (arg.getMethodName().startsWith("access$") && ctx.getType() != null) { if (replaceSyntheticAccessor(ctx, arg, owner)) { return; } } if (ctx.getType() == null || !owner.equals(ctx.getType().getName())) { ctx.emitTypeName(owner); ctx.printString("."); } boolean is_varargs = false; TypeEntry target = ctx.getType().getSource().get(arg.getOwnerName()); if (target != null) { MethodEntry mth = target.getStaticMethod(arg.getMethodName(), arg.getMethodDescription()); if (mth != null) { is_varargs = mth.isVarargs(); } } ctx.printString(arg.getMethodName()); List<String> param_types = TypeHelper.splitSig(arg.getMethodDescription()); ctx.printString("("); for (int i = 0; i < arg.getParameters().length; i++) { Instruction param = arg.getParameters()[i]; if (is_varargs && i == arg.getParameters().length - 1 && param instanceof NewArray) { NewArray varargs = (NewArray) param; if (varargs.getInitializer() != null) { for (int o = 0; o < varargs.getInitializer().length; o++) { ctx.emit(varargs.getInitializer()[o], varargs.getType()); if (o < varargs.getInitializer().length - 1) { ctx.printString(", "); ctx.markWrapPoint(); } } break; } } ctx.emit(param, ClassTypeSignature.of(param_types.get(i))); if (i < arg.getParameters().length - 1) { ctx.printString(", "); ctx.markWrapPoint(); } } ctx.printString(")"); } protected boolean replaceSyntheticAccessor(JavaEmitterContext ctx, StaticMethodInvoke arg, String owner) { // synthetic accessor // we resolve these to the field that they are accessing directly TypeEntry owner_type = ctx.getType().getSource().get(owner); if (owner_type != null) { MethodEntry accessor = owner_type.getStaticMethod(arg.getMethodName()); Statement stmt0 = accessor.getInstructions().getStatements().get(0); if (accessor.getReturnType().equals(VoidTypeSignature.VOID)) { if(!(stmt0 instanceof FieldAssignment)) { return false; } // setter FieldAssignment assign = (FieldAssignment) stmt0; FieldAssignment replacement = null; if (arg.getParameters().length == 2) { replacement = new InstanceFieldAssignment(assign.getFieldName(), assign.getFieldDescription(), assign.getOwnerType(), arg.getParameters()[0], arg.getParameters()[1]); } else { replacement = new StaticFieldAssignment(assign.getFieldName(), assign.getFieldDescription(), assign.getOwnerType(), arg.getParameters()[0]); } ctx.emit(replacement, ctx.usesSemicolons()); return true; } if(!(stmt0 instanceof Return)) { return false; } // getter Return ret = (Return) stmt0; FieldAccess getter = (FieldAccess) ret.getValue().get(); FieldAccess replacement = null; if (arg.getParameters().length == 1) { replacement = new InstanceFieldAccess(getter.getFieldName(), getter.getTypeDescriptor(), getter.getOwnerType(), arg.getParameters()[0]); } else { replacement = new StaticFieldAccess(getter.getFieldName(), getter.getTypeDescriptor(), getter.getOwnerType()); } ctx.emit(replacement, null); return true; } return false; } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/instruction/LocalAccessEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/instruction/LocalAccessEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.instruction; import org.spongepowered.despector.ast.generic.TypeSignature; import org.spongepowered.despector.ast.insn.var.LocalAccess; import org.spongepowered.despector.emitter.InstructionEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; /** * An emitter for a local access. */ public class LocalAccessEmitter implements InstructionEmitter<JavaEmitterContext, LocalAccess> { @Override public void emit(JavaEmitterContext ctx, LocalAccess arg, TypeSignature type) { ctx.printString(arg.getLocal().getName()); } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/special/AnnotationEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/special/AnnotationEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.special; import org.spongepowered.despector.ast.Annotation; import org.spongepowered.despector.ast.Annotation.EnumConstant; import org.spongepowered.despector.ast.generic.ClassTypeSignature; import org.spongepowered.despector.emitter.SpecialEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; import org.spongepowered.despector.emitter.java.instruction.StringConstantEmitter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.function.BiConsumer; /** * An emitter for annotations. */ public class AnnotationEmitter implements SpecialEmitter { private static final Map<Class<?>, BiConsumer<JavaEmitterContext, Object>> value_emitters = new HashMap<>(); static { value_emitters.put(Boolean.class, (ctx, value) -> ctx.printString(String.valueOf(value))); value_emitters.put(Integer.class, (ctx, value) -> ctx.printString(String.valueOf(value))); value_emitters.put(int[].class, (ctx, value) -> { int[] values = (int[]) value; ctx.printString("{"); for (int i = 0; i < values.length; i++) { if (i > 0) { ctx.printString(", "); } ctx.printString(String.valueOf(values[i])); } ctx.printString("}"); }); value_emitters.put(ArrayList.class, (ctx, value) -> { List<?> list = (List<?>) value; if (list.isEmpty()) { ctx.printString("{}"); } else if (list.size() == 1) { emitValue(ctx, list.get(0)); } else { ctx.printString("{"); for (int i = 0; i < list.size(); i++) { if (i != 0) { ctx.printString(", "); } emitValue(ctx, list.get(i)); } ctx.printString("}"); } }); value_emitters.put(String.class, (ctx, value) -> { ctx.printString("\""); ctx.printString(StringConstantEmitter.escape((String) value)); ctx.printString("\""); }); value_emitters.put(String[].class, (ctx, value) -> { String[] values = (String[]) value; ctx.printString("{"); for (int i = 0; i < values.length; i++) { if (i > 0) { ctx.printString(", "); } ctx.printString("\""); ctx.printString(StringConstantEmitter.escape(values[i])); ctx.printString("\""); } ctx.printString("}"); }); value_emitters.put(EnumConstant.class, (ctx, value) -> { EnumConstant e = (EnumConstant) value; ctx.emitType(e.getEnumType()); ctx.printString("."); ctx.printString(e.getConstantName()); }); value_emitters.put(ClassTypeSignature.class, (ctx, value) -> ctx.emitTypeName(((ClassTypeSignature) value).getName())); } public static void emitValue(JavaEmitterContext ctx, Object value) { BiConsumer<JavaEmitterContext, Object> emitter = value_emitters.get(value.getClass()); if (emitter == null) { throw new IllegalStateException("Unknown annotation value type in emitter: " + value.getClass().getName()); } emitter.accept(ctx, value); } /** * Emits the given annotation. */ public void emit(JavaEmitterContext ctx, Annotation annotation) { ctx.printString("@"); ctx.emitTypeName(annotation.getType().getName()); if (annotation.getKeys().isEmpty()) { return; } else if (annotation.getKeys().size() == 1 && "value".equals(annotation.getKeys().iterator().next())) { ctx.printString("("); emitValue(ctx, annotation.getValue("value")); ctx.printString(" ", ctx.getFormat().insert_space_before_closing_paren_in_annotation); ctx.printString(")"); } else { ctx.printString("("); boolean first = true; for (String key : annotation.getKeys()) { if (!first) { ctx.printString(", "); } first = false; ctx.printString(key); ctx.printString(" = "); Object value = annotation.getValue(key); emitValue(ctx, value); } ctx.printString(" ", ctx.getFormat().insert_space_before_closing_paren_in_annotation); ctx.printString(")"); } } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/special/PackageEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/special/PackageEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.special; import org.spongepowered.despector.emitter.SpecialEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; /** * An emitter for a package declaration. */ public class PackageEmitter implements SpecialEmitter { /** * Emits the given package. */ public void emitPackage(JavaEmitterContext ctx, String pkg) { ctx.printString("package "); ctx.printString(pkg); ctx.printString(";"); } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/special/PackageInfoEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/special/PackageInfoEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.special; import org.spongepowered.despector.ast.Annotation; import org.spongepowered.despector.ast.generic.ClassTypeSignature; import org.spongepowered.despector.ast.type.InterfaceEntry; import org.spongepowered.despector.emitter.SpecialEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; import java.util.List; /** * An emitter for a package-info type. */ public class PackageInfoEmitter implements SpecialEmitter { /** * Emits the given package-info class. */ public void emit(JavaEmitterContext ctx, InterfaceEntry info) { for (Annotation anno : info.getAnnotations()) { emitAnnotation(ctx, anno); if (ctx.getFormat().insert_new_line_after_annotation_on_package) { ctx.newLine(); } else { ctx.printString(" "); } } String pkg = info.getName(); int last = pkg.lastIndexOf('/'); if (last != -1) { pkg = pkg.substring(0, last).replace('/', '.'); ctx.printString("package "); ctx.printString(pkg); ctx.printString(";"); ctx.newLine(); } } // TODO add some settings to the annotation emitter / emitter context to // disable import tracking private void emitValue(JavaEmitterContext ctx, Object value) { if (value instanceof List) { List<?> list = (List<?>) value; if (list.isEmpty()) { ctx.printString("{}"); } else if (list.size() == 1) { emitValue(ctx, list.get(0)); } else { for (int i = 0; i < list.size(); i++) { if (i != 0) { ctx.printString(", "); } emitValue(ctx, list.get(i)); } } } else if (value instanceof ClassTypeSignature) { ctx.printString(((ClassTypeSignature) value).getClassName()); } else { throw new IllegalStateException("Unknown annotation value type in emitter: " + value.getClass().getName()); } } /** * Emits the given annotation with fully-qualified names. */ public void emitAnnotation(JavaEmitterContext ctx, Annotation annotation) { ctx.printString("@"); ctx.printString(annotation.getType().getName().replace('/', '.')); if (annotation.getKeys().isEmpty()) { return; } else if (annotation.getKeys().size() == 1 && "value".equals(annotation.getKeys().iterator().next())) { ctx.printString("("); emitValue(ctx, annotation.getValue("value")); ctx.printString(")"); } else { ctx.printString("("); boolean first = true; for (String key : annotation.getKeys()) { if (first) { first = false; ctx.printString(", "); } ctx.printString(key); ctx.printString(" = "); Object value = annotation.getValue(key); emitValue(ctx, value); } ctx.printString(")"); } } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/special/AnonymousClassEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/special/AnonymousClassEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.special; import static com.google.common.base.Preconditions.checkArgument; import org.spongepowered.despector.ast.generic.ClassTypeSignature; import org.spongepowered.despector.ast.generic.TypeSignature; import org.spongepowered.despector.ast.insn.Instruction; import org.spongepowered.despector.ast.stmt.invoke.New; import org.spongepowered.despector.ast.type.ClassEntry; import org.spongepowered.despector.ast.type.FieldEntry; import org.spongepowered.despector.ast.type.MethodEntry; import org.spongepowered.despector.ast.type.TypeEntry; import org.spongepowered.despector.emitter.SpecialEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; import org.spongepowered.despector.emitter.java.type.ClassEntryEmitter; import org.spongepowered.despector.util.TypeHelper; import java.util.List; /** * An emitter for anonymous classes. */ public class AnonymousClassEmitter implements SpecialEmitter { /** * Emits the given anonymous class. */ public void emit(JavaEmitterContext ctx, ClassEntry type, New new_insn) { checkArgument(type.isAnonType()); TypeEntry old_outer = ctx.getOuterType(); TypeEntry old_type = ctx.getType(); ctx.setOuterType(old_type); ctx.setType(type); TypeSignature actual_type = null; if (type.getSuperclassName().equals("java/lang/Object")) { if (type.getSignature() != null) { actual_type = type.getSignature().getInterfaceSignatures().get(0); } else { actual_type = ClassTypeSignature.of(type.getInterfaces().get(0)); } } else { if (type.getSignature() != null) { actual_type = type.getSignature().getSuperclassSignature(); } else { actual_type = ClassTypeSignature.of(type.getSuperclass()); } } ctx.printString("new "); ctx.emitType(actual_type, false); int syn_field_count = 0; for (FieldEntry fld : type.getFields()) { if ((fld.getName().startsWith("val$") || fld.getName().startsWith("this$")) && fld.isSynthetic()) { syn_field_count++; } } ctx.printString("("); List<String> param_types = TypeHelper.splitSig(new_insn.getCtorDescription()); for (int i = 0; i < new_insn.getParameters().length - syn_field_count; i++) { Instruction param = new_insn.getParameters()[i]; ctx.emit(param, ClassTypeSignature.of(param_types.get(i))); if (i < new_insn.getParameters().length - 1 - syn_field_count) { ctx.printString(", "); } } ctx.printString(") {"); ctx.newLine(); ctx.indent(); ctx.indent(); ClassEntryEmitter emitter = (ClassEntryEmitter) (Object) ctx.getEmitterSet().getAstEmitter(ClassEntry.class); emitter.emitStaticFields(ctx, type); emitter.emitStaticMethods(ctx, type); emitter.emitFields(ctx, type); emitMethods(ctx, type); ctx.dedent(); ctx.dedent(); ctx.printIndentation(); ctx.printString("}"); ctx.setOuterType(old_outer); ctx.setType(old_type); } /** * Emits the methods in the given anonymous class. */ public void emitMethods(JavaEmitterContext ctx, ClassEntry type) { if (!type.getMethods().isEmpty()) { for (MethodEntry mth : type.getMethods()) { if (mth.isSynthetic()) { continue; } if (mth.getName().equals("<init>")) { // TODO need to check if ctor has extra things that still // need to be emitted? continue; } if (ctx.emit(mth)) { ctx.newLine(); ctx.newLine(); } } } } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/special/GenericsEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/special/GenericsEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.special; import org.spongepowered.despector.ast.generic.ClassTypeSignature; import org.spongepowered.despector.ast.generic.GenericClassTypeSignature; import org.spongepowered.despector.ast.generic.TypeArgument; import org.spongepowered.despector.ast.generic.TypeParameter; import org.spongepowered.despector.ast.generic.TypeSignature; import org.spongepowered.despector.ast.generic.TypeVariableSignature; import org.spongepowered.despector.ast.generic.VoidTypeSignature; import org.spongepowered.despector.emitter.SpecialEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; import java.util.List; /** * An emitter for various generic signatures. */ public class GenericsEmitter implements SpecialEmitter { /** * Emits the given {@link TypeParameter}. */ public void emitTypeParameter(JavaEmitterContext ctx, TypeParameter param) { ctx.printString(param.getIdentifier()); boolean had_superclass = false; superclass: if (param.getClassBound() != null) { if (param.getClassBound() instanceof ClassTypeSignature) { ClassTypeSignature cls = (ClassTypeSignature) param.getClassBound(); if (cls.getDescriptor().equals("Ljava/lang/Object;")) { break superclass; } } else if (param.getClassBound() instanceof GenericClassTypeSignature) { GenericClassTypeSignature cls = (GenericClassTypeSignature) param.getClassBound(); if (cls.getDescriptor().equals("Ljava/lang/Object;")) { break superclass; } } ctx.printString(" extends "); emitTypeSignature(ctx, param.getClassBound(), false); had_superclass = true; } for (TypeSignature ibound : param.getInterfaceBounds()) { if (!had_superclass) { ctx.printString(" extends "); } else { ctx.printString(" & "); } emitTypeSignature(ctx, ibound, false); } } /** * Emits the given type. */ public void emitTypeSignature(JavaEmitterContext ctx, TypeSignature sig, boolean is_varargs) { if (sig instanceof TypeVariableSignature) { int array_depth = 0; String type = ((TypeVariableSignature) sig).getIdentifier(); while (type.startsWith("[")) { array_depth++; type = type.substring(1); } ctx.printString(type.substring(1, type.length() - 1)); if (is_varargs && array_depth > 0) { array_depth--; } for (int i = 0; i < array_depth; i++) { ctx.printString(" ", ctx.getFormat().insert_space_before_opening_bracket_in_array_type_reference); ctx.printString("["); ctx.printString(" ", ctx.getFormat().insert_space_between_brackets_in_array_type_reference); ctx.printString("]"); } if (is_varargs && array_depth >= 0) { ctx.printString("..."); } } else if (sig instanceof ClassTypeSignature) { ClassTypeSignature cls = (ClassTypeSignature) sig; int array_depth = 0; String type = cls.getType(); while (type.startsWith("[")) { array_depth++; type = type.substring(1); } ctx.emitType(type); if (is_varargs && array_depth > 0) { array_depth--; } for (int i = 0; i < array_depth; i++) { ctx.printString(" ", ctx.getFormat().insert_space_before_opening_bracket_in_array_type_reference); ctx.printString("["); ctx.printString(" ", ctx.getFormat().insert_space_between_brackets_in_array_type_reference); ctx.printString("]"); } if (is_varargs && array_depth >= 0) { ctx.printString("..."); } } else if (sig instanceof GenericClassTypeSignature) { GenericClassTypeSignature cls = (GenericClassTypeSignature) sig; int array_depth = 0; String type = cls.getType(); while (type.startsWith("[")) { array_depth++; type = type.substring(1); } ctx.emitType(type); emitTypeArguments(ctx, cls.getArguments()); if (is_varargs && array_depth > 0) { array_depth--; } for (int i = 0; i < array_depth; i++) { ctx.printString(" ", ctx.getFormat().insert_space_before_opening_bracket_in_array_type_reference); ctx.printString("["); ctx.printString(" ", ctx.getFormat().insert_space_between_brackets_in_array_type_reference); ctx.printString("]"); } if (is_varargs && array_depth >= 0) { ctx.printString("..."); } } else if (sig instanceof VoidTypeSignature) { ctx.printString("void"); } } /** * Emits the given set of {@link TypeParameter}s. */ public void emitTypeParameters(JavaEmitterContext ctx, List<TypeParameter> parameters) { if (parameters.isEmpty()) { return; } ctx.printString("<"); boolean first = true; for (TypeParameter param : parameters) { if (!first) { ctx.printString(" ", ctx.getFormat().insert_space_before_comma_in_method_invocation_arguments); ctx.printString(","); ctx.printString(" ", ctx.getFormat().insert_space_after_comma_in_method_invocation_arguments); } first = false; emitTypeParameter(ctx, param); } ctx.printString(">"); } /** * Emits the given {@link TypeArgument}. */ public void emitTypeArgument(JavaEmitterContext ctx, TypeArgument arg) { switch (arg.getWildcard()) { case NONE: emitTypeSignature(ctx, arg.getSignature(), false); break; case EXTENDS: ctx.printString("? extends "); emitTypeSignature(ctx, arg.getSignature(), false); break; case SUPER: ctx.printString("? super "); emitTypeSignature(ctx, arg.getSignature(), false); break; case STAR: ctx.printString("?"); break; default: break; } } /** * Emits the given set of {@link TypeArgument}s. */ public void emitTypeArguments(JavaEmitterContext ctx, List<TypeArgument> arguments) { if (arguments.isEmpty()) { return; } ctx.printString("<"); boolean first = true; for (TypeArgument param : arguments) { if (!first) { ctx.printString(", "); } first = false; emitTypeArgument(ctx, param); } ctx.printString(">"); } /** * Gets the length of the given {@link TypeSignature} when its emitted. */ public static int getLength(JavaEmitterContext ctx, TypeSignature type) { if (type instanceof VoidTypeSignature) { return 4; } if (type instanceof ClassTypeSignature) { ClassTypeSignature cls = (ClassTypeSignature) type; return ctx.getType(cls.getDescriptor()).length(); } if (type instanceof GenericClassTypeSignature) { GenericClassTypeSignature cls = (GenericClassTypeSignature) type; int length = ctx.getType(cls.getDescriptor()).length(); if (!cls.getArguments().isEmpty()) { for (TypeArgument arg : cls.getArguments()) { length += 2; switch (arg.getWildcard()) { case NONE: length += getLength(ctx, arg.getSignature()); break; case EXTENDS: length += 10; length += getLength(ctx, arg.getSignature()); break; case SUPER: length += 8; length += getLength(ctx, arg.getSignature()); break; case STAR: length++; break; default: break; } } } return length; } if (type instanceof TypeVariableSignature) { return ((TypeVariableSignature) type).getIdentifierName().length(); } return 0; } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/statement/IfEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/statement/IfEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.statement; import org.spongepowered.despector.ast.stmt.StatementBlock; import org.spongepowered.despector.ast.stmt.branch.If; import org.spongepowered.despector.ast.stmt.branch.If.Elif; import org.spongepowered.despector.ast.stmt.branch.If.Else; import org.spongepowered.despector.emitter.StatementEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; /** * An emitter for if statements. */ public class IfEmitter implements StatementEmitter<JavaEmitterContext, If> { @Override public void emit(JavaEmitterContext ctx, If insn, boolean semicolon) { ctx.printString("if"); ctx.printString(" ", ctx.getFormat().insert_space_before_opening_paren_in_if); ctx.printString("("); ctx.printString(" ", ctx.getFormat().insert_space_after_opening_paren_in_if); ctx.emit(insn.getCondition()); ctx.printString(" ", ctx.getFormat().insert_space_before_closing_paren_in_if); ctx.printString(") {"); ctx.newLine(); if (!insn.getBody().getStatements().isEmpty()) { ctx.indent(); ctx.emitBody(insn.getBody()); ctx.dedent(); ctx.newLine(); } ctx.printIndentation(); Else else_ = insn.getElseBlock(); for (int i = 0; i < insn.getElifBlocks().size(); i++) { Elif elif = insn.getElifBlocks().get(i); ctx.printString("} else if"); ctx.printString(" ", ctx.getFormat().insert_space_before_opening_paren_in_if); ctx.printString("("); ctx.emit(elif.getCondition()); ctx.printString(") {"); ctx.newLine(); ctx.indent(); ctx.emitBody(elif.getBody()); ctx.dedent(); ctx.newLine(); ctx.printIndentation(); } if (else_ == null || else_.getBody().getStatements().isEmpty()) { ctx.printString("}"); } else { StatementBlock else_block = else_.getBody(); if (ctx.getFormat().insert_new_line_before_else_in_if_statement) { ctx.printString("}"); ctx.newIndentedLine(); ctx.printString("else {"); } else { ctx.printString("} else {"); } ctx.newLine(); if (!else_block.getStatements().isEmpty()) { ctx.indent(); ctx.emitBody(else_block); ctx.dedent(); ctx.newLine(); } ctx.printIndentation(); ctx.printString("}"); } } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/statement/CommentEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/statement/CommentEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.statement; import org.spongepowered.despector.ast.stmt.misc.Comment; import org.spongepowered.despector.emitter.StatementEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; /** * An emitter for comments. */ public class CommentEmitter implements StatementEmitter<JavaEmitterContext, Comment> { @Override public void emit(JavaEmitterContext ctx, Comment stmt, boolean semicolon) { if (stmt.getCommentText().isEmpty()) { return; } if (stmt.getCommentText().size() == 1) { ctx.printString("// "); ctx.printString(stmt.getCommentText().get(0)); } else { ctx.printString("/*"); ctx.newLine(); for (String line : stmt.getCommentText()) { ctx.printIndentation(); ctx.printString(" * "); ctx.printString(line); ctx.newLine(); } ctx.printIndentation(); ctx.printString(" */"); } } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/statement/FieldAssignmentEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/statement/FieldAssignmentEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.statement; import org.spongepowered.despector.ast.generic.ClassTypeSignature; import org.spongepowered.despector.ast.insn.Instruction; import org.spongepowered.despector.ast.insn.cst.IntConstant; import org.spongepowered.despector.ast.insn.op.Operator; import org.spongepowered.despector.ast.insn.var.InstanceFieldAccess; import org.spongepowered.despector.ast.insn.var.LocalAccess; import org.spongepowered.despector.ast.stmt.assign.FieldAssignment; import org.spongepowered.despector.ast.stmt.assign.InstanceFieldAssignment; import org.spongepowered.despector.ast.stmt.assign.StaticFieldAssignment; import org.spongepowered.despector.emitter.StatementEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; /** * An emitter for field assignments. */ public class FieldAssignmentEmitter implements StatementEmitter<JavaEmitterContext, FieldAssignment> { @Override public void emit(JavaEmitterContext ctx, FieldAssignment insn, boolean semicolon) { if (insn.isInitializer()) { return; } if (insn instanceof StaticFieldAssignment) { if (ctx.getType() != null && !((StaticFieldAssignment) insn).getOwnerType().equals(ctx.getType().getDescriptor())) { ctx.emitTypeName(((StaticFieldAssignment) insn).getOwnerName()); ctx.printString("."); } } else if (insn instanceof InstanceFieldAssignment) { ctx.emit(((InstanceFieldAssignment) insn).getOwner(), ClassTypeSignature.of(insn.getOwnerType())); ctx.printString("."); } ctx.printString(insn.getFieldName()); Instruction val = insn.getValue(); if (checkOperator(ctx, insn, val, semicolon)) { return; } ctx.printString(" = "); ctx.emit(val, insn.getFieldDescription()); if (semicolon) { ctx.printString(";"); } } protected boolean checkOperator(JavaEmitterContext ctx, FieldAssignment insn, Instruction val, boolean semicolon) { if (val instanceof Operator) { Instruction left = ((Operator) val).getLeftOperand(); Instruction right = ((Operator) val).getRightOperand(); String op = " " + ((Operator) val).getOperator() + "= "; if (left instanceof InstanceFieldAccess) { InstanceFieldAccess left_field = (InstanceFieldAccess) left; Instruction owner = left_field.getFieldOwner(); if (owner instanceof LocalAccess && ((LocalAccess) owner).getLocal().getIndex() == 0) { // If the field assign is of the form 'field = field + x' // where + is any operator then we collapse it to the '+=' // form of the assignment. if (left_field.getFieldName().equals(insn.getFieldName())) { ctx.printString(op); if (insn.getFieldDescription().equals("Z")) { if (val instanceof IntConstant) { IntConstant cst = (IntConstant) insn.getValue(); if (cst.getConstant() == 1) { ctx.printString("true"); } else { ctx.printString("false"); } if (semicolon) { ctx.printString(";"); } return true; } } ctx.emit(right, insn.getFieldDescription()); if (semicolon) { ctx.printString(";"); } return true; } } } } return false; } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/statement/package-info.java
src/main/java/org/spongepowered/despector/emitter/java/statement/package-info.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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. */ @org.spongepowered.despector.util.NonnullByDefault package org.spongepowered.despector.emitter.java.statement;
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/statement/BreakEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/statement/BreakEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.statement; import org.spongepowered.despector.ast.stmt.branch.Break; import org.spongepowered.despector.emitter.StatementEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; /** * An emitter of a control flow break. */ public class BreakEmitter implements StatementEmitter<JavaEmitterContext, Break> { @Override public void emit(JavaEmitterContext ctx, Break stmt, boolean semicolon) { // TODO labels if nested. ctx.printString(stmt.getType().getKeyword()); if (semicolon) { ctx.printString(";"); } } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/statement/ThrowEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/statement/ThrowEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.statement; import org.spongepowered.despector.ast.stmt.misc.Throw; import org.spongepowered.despector.emitter.StatementEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; /** * An emitter for a throw statement. */ public class ThrowEmitter implements StatementEmitter<JavaEmitterContext, Throw> { @Override public void emit(JavaEmitterContext ctx, Throw insn, boolean semicolon) { ctx.printString("throw "); ctx.emit(insn.getException(), null); if (semicolon) { ctx.printString(";"); } } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/statement/ArrayAssignmentEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/statement/ArrayAssignmentEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.statement; import org.spongepowered.despector.ast.generic.ClassTypeSignature; import org.spongepowered.despector.ast.stmt.assign.ArrayAssignment; import org.spongepowered.despector.emitter.StatementEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; /** * An emitter for array assignments. */ public class ArrayAssignmentEmitter implements StatementEmitter<JavaEmitterContext, ArrayAssignment> { @Override public void emit(JavaEmitterContext ctx, ArrayAssignment insn, boolean semicolon) { ctx.emit(insn.getArray(), null); ctx.printString("["); ctx.emit(insn.getIndex(), ClassTypeSignature.INT); ctx.printString("] = "); ctx.emit(insn.getValue(), null); if (semicolon) { ctx.printString(";"); } } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/statement/ForEachEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/statement/ForEachEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.statement; import org.spongepowered.despector.ast.Locals.LocalInstance; import org.spongepowered.despector.ast.stmt.branch.ForEach; import org.spongepowered.despector.emitter.StatementEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; import org.spongepowered.despector.emitter.java.special.GenericsEmitter; /** * An emitter for for-each loops. */ public class ForEachEmitter implements StatementEmitter<JavaEmitterContext, ForEach> { @Override public void emit(JavaEmitterContext ctx, ForEach loop, boolean semicolon) { ctx.printString("for"); ctx.printString(" ", ctx.getFormat().insert_space_before_opening_paren_in_for); ctx.printString("("); ctx.printString(" ", ctx.getFormat().insert_space_after_opening_paren_in_for); LocalInstance local = loop.getValueAssignment(); GenericsEmitter generics = ctx.getEmitterSet().getSpecialEmitter(GenericsEmitter.class); generics.emitTypeSignature(ctx, local.getType(), false); ctx.printString(" "); ctx.printString(local.getName()); ctx.printString(" ", ctx.getFormat().insert_space_before_colon_in_for); ctx.printString(":"); ctx.printString(" ", ctx.getFormat().insert_space_after_colon_in_for); ctx.emit(loop.getCollectionValue(), null); ctx.printString(") {"); ctx.newLine(); if (!loop.getBody().getStatements().isEmpty()) { ctx.indent(); ctx.emitBody(loop.getBody()); ctx.dedent(); ctx.newLine(); } ctx.printIndentation(); ctx.printString("}"); } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/statement/DoWhileEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/statement/DoWhileEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.statement; import org.spongepowered.despector.ast.stmt.branch.DoWhile; import org.spongepowered.despector.emitter.StatementEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; /** * An emitter for do while loops. */ public class DoWhileEmitter implements StatementEmitter<JavaEmitterContext, DoWhile> { @Override public void emit(JavaEmitterContext ctx, DoWhile loop, boolean semicolon) { ctx.printString("do {"); ctx.newLine(); if (!loop.getBody().getStatements().isEmpty()) { ctx.indent(); ctx.emitBody(loop.getBody()); ctx.dedent(); ctx.newLine(); } ctx.printIndentation(); ctx.printString("} while ("); ctx.emit(loop.getCondition()); ctx.printString(")"); if (semicolon) { ctx.printString(";"); } } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/statement/InvokeEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/statement/InvokeEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.statement; import org.spongepowered.despector.ast.insn.Instruction; import org.spongepowered.despector.ast.stmt.invoke.InstanceMethodInvoke; import org.spongepowered.despector.ast.stmt.invoke.InvokeStatement; import org.spongepowered.despector.emitter.StatementEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; /** * An emitter for invoke statements. */ public class InvokeEmitter implements StatementEmitter<JavaEmitterContext, InvokeStatement> { @Override public void emit(JavaEmitterContext ctx, InvokeStatement insn, boolean semicolon) { Instruction i = insn.getInstruction(); if (i instanceof InstanceMethodInvoke) { InstanceMethodInvoke mth = (InstanceMethodInvoke) i; if (mth.getMethodName().equals("<init>") && (mth.getParameters().length == 0 || mth.getOwner().equals("Ljava/lang/Enum;"))) { return; } } ctx.emit(i, null); if (semicolon) { ctx.printString(";"); } } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/statement/LocalAssignmentEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/statement/LocalAssignmentEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.statement; import org.spongepowered.despector.ast.Locals.LocalInstance; import org.spongepowered.despector.ast.insn.Instruction; import org.spongepowered.despector.ast.insn.cst.IntConstant; import org.spongepowered.despector.ast.insn.op.Operator; import org.spongepowered.despector.ast.insn.var.LocalAccess; import org.spongepowered.despector.ast.stmt.assign.LocalAssignment; import org.spongepowered.despector.emitter.StatementEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; import org.spongepowered.despector.emitter.java.special.GenericsEmitter; /** * An emitter for local assignments. */ public class LocalAssignmentEmitter implements StatementEmitter<JavaEmitterContext, LocalAssignment> { @Override public void emit(JavaEmitterContext ctx, LocalAssignment insn, boolean semicolon) { if (!insn.getLocal().getLocal().isParameter() && !ctx.isDefined(insn.getLocal())) { LocalInstance local = insn.getLocal(); GenericsEmitter generics = ctx.getEmitterSet().getSpecialEmitter(GenericsEmitter.class); generics.emitTypeSignature(ctx, local.getType(), false); ctx.printString(" "); ctx.markDefined(insn.getLocal()); } else { Instruction val = insn.getValue(); if (checkOperator(ctx, insn, val, semicolon)) { return; } } ctx.printString(insn.getLocal().getName()); ctx.printString(" = "); ctx.emit(insn.getValue(), insn.getLocal().getType()); if (semicolon) { ctx.printString(";"); } } protected boolean checkOperator(JavaEmitterContext ctx, LocalAssignment insn, Instruction val, boolean semicolon) { if (val instanceof Operator) { Instruction left = ((Operator) val).getLeftOperand(); Instruction right = ((Operator) val).getRightOperand(); String op = " " + ((Operator) val).getOperator().getSymbol() + "= "; if (left instanceof LocalAccess) { LocalAccess left_field = (LocalAccess) left; // If the field assign is of the form 'field = field + x' // where + is any operator then we collapse it to the '+=' // form of the assignment. if (left_field.getLocal().getIndex() == insn.getLocal().getIndex()) { ctx.printString(insn.getLocal().getName()); ctx.printString(op); if ("Z".equals(insn.getLocal().getType())) { if (val instanceof IntConstant) { IntConstant cst = (IntConstant) insn.getValue(); if (cst.getConstant() == 1) { ctx.printString("true"); } else { ctx.printString("false"); } if (semicolon) { ctx.printString(";"); } return true; } } ctx.emit(right, insn.getLocal().getType()); if (semicolon) { ctx.printString(";"); } return true; } } } return false; } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/statement/SwitchEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/statement/SwitchEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.statement; import com.google.common.collect.Maps; import org.spongepowered.despector.ast.generic.ClassTypeSignature; import org.spongepowered.despector.ast.insn.cst.IntConstant; import org.spongepowered.despector.ast.insn.var.ArrayAccess; import org.spongepowered.despector.ast.insn.var.FieldAccess; import org.spongepowered.despector.ast.insn.var.StaticFieldAccess; import org.spongepowered.despector.ast.stmt.Statement; import org.spongepowered.despector.ast.stmt.assign.ArrayAssignment; import org.spongepowered.despector.ast.stmt.branch.Switch; import org.spongepowered.despector.ast.stmt.branch.Switch.Case; import org.spongepowered.despector.ast.stmt.branch.TryCatch; import org.spongepowered.despector.ast.stmt.invoke.InstanceMethodInvoke; import org.spongepowered.despector.ast.stmt.invoke.StaticMethodInvoke; import org.spongepowered.despector.ast.type.MethodEntry; import org.spongepowered.despector.ast.type.TypeEntry; import org.spongepowered.despector.emitter.StatementEmitter; import org.spongepowered.despector.emitter.format.EmitterFormat.BracePosition; import org.spongepowered.despector.emitter.java.JavaEmitterContext; import java.util.Map; /** * An emitter for switch statements. */ public class SwitchEmitter implements StatementEmitter<JavaEmitterContext, Switch> { private Map<Integer, String> buildSwitchTable(MethodEntry mth, String field) { Map<Integer, String> table = Maps.newHashMap(); for (Statement stmt : mth.getInstructions().getStatements()) { if (stmt instanceof TryCatch) { TryCatch next = (TryCatch) stmt; ArrayAssignment assign = (ArrayAssignment) next.getTryBlock().getStatements().get(0); int jump_index = ((IntConstant) assign.getValue()).getConstant(); InstanceMethodInvoke ordinal = (InstanceMethodInvoke) assign.getIndex(); StaticFieldAccess callee = (StaticFieldAccess) ordinal.getCallee(); if (field == null) { table.put(jump_index, callee.getFieldName()); } else { FieldAccess fld = (FieldAccess) assign.getArray(); if (fld.getFieldName().equals(field)) { table.put(jump_index, callee.getFieldName()); } } } } return table; } @Override public void emit(JavaEmitterContext ctx, Switch tswitch, boolean semicolon) { Map<Integer, String> table = null; ctx.printString("switch ("); ctx.printString(" ", ctx.getFormat().insert_space_after_opening_paren_in_switch); boolean synthetic = false; if (tswitch.getSwitchVar() instanceof ArrayAccess) { ArrayAccess var = (ArrayAccess) tswitch.getSwitchVar(); if (var.getArrayVar() instanceof StaticMethodInvoke) { StaticMethodInvoke arg = (StaticMethodInvoke) var.getArrayVar(); if (arg.getMethodName().contains("$SWITCH_TABLE$") && ctx.getType() != null) { MethodEntry mth = ctx.getType().getStaticMethod(arg.getMethodName(), arg.getMethodDescription()); table = buildSwitchTable(mth, null); String enum_type = arg.getMethodName().substring("$SWITCH_TABLE$".length()).replace('$', '/'); ctx.emit(((InstanceMethodInvoke) var.getIndex()).getCallee(), ClassTypeSignature.of("L" + enum_type + ";")); synthetic = true; } } else if (var.getArrayVar() instanceof StaticFieldAccess) { StaticFieldAccess arg = (StaticFieldAccess) var.getArrayVar(); if (arg.getFieldName().startsWith("$SwitchMap") && ctx.getType() != null) { TypeEntry owner = ctx.getType().getSource().get(arg.getOwnerName()); if(owner != null) { MethodEntry mth = owner.getStaticMethod("<clinit>"); table = buildSwitchTable(mth, arg.getFieldName()); } String enum_type = arg.getFieldName().substring("$SwitchMap/".length()).replace('$', '/'); ctx.emit(((InstanceMethodInvoke) var.getIndex()).getCallee(), ClassTypeSignature.of("L" + enum_type + ";")); synthetic = true; } } } if (!synthetic) { ctx.emit(tswitch.getSwitchVar(), ClassTypeSignature.INT); } ctx.printString(" ", ctx.getFormat().insert_space_before_closing_paren_in_switch); ctx.printString(")"); ctx.emitBrace(ctx.getFormat().brace_position_for_switch, false, ctx.getFormat().insert_space_before_opening_brace_in_switch); if (!ctx.getFormat().indent_switchstatements_compare_to_switch) { ctx.dedent(); } ctx.newLine(); for (Case cs : tswitch.getCases()) { for (int i = 0; i < cs.getIndices().size(); i++) { ctx.printIndentation(); ctx.printString("case "); int index = cs.getIndices().get(i); if (table != null) { String label = table.get(index); if (label == null) { ctx.printString(String.valueOf(index)); } else { ctx.printString(label); } } else { ctx.printString(String.valueOf(cs.getIndices().get(i))); } ctx.printString(" ", ctx.getFormat().insert_space_before_colon_in_case); ctx.printString(":"); ctx.printString(" ", ctx.getFormat().insert_space_after_colon_in_case); ctx.newLine(); } if (cs.isDefault()) { ctx.printIndentation(); ctx.printString("default"); ctx.printString(" ", ctx.getFormat().insert_space_before_colon_in_case); ctx.printString(":"); ctx.printString(" ", ctx.getFormat().insert_space_after_colon_in_case); ctx.newLine(); } if (ctx.getFormat().indent_switchstatements_compare_to_cases) { ctx.indent(); } ctx.emitBody(cs.getBody()); if (!cs.getBody().getStatements().isEmpty()) { ctx.newLine(); } if (cs.doesBreak()) { if (!ctx.getFormat().indent_breaks_compare_to_cases && ctx.getFormat().indent_switchstatements_compare_to_cases) { ctx.dedent(); } ctx.printIndentation(); ctx.printString("break;"); ctx.newLine(); } if ((!cs.doesBreak() || ctx.getFormat().indent_breaks_compare_to_cases) && ctx.getFormat().indent_switchstatements_compare_to_cases) { ctx.dedent(); } } if (ctx.getFormat().indent_switchstatements_compare_to_switch) { ctx.dedent(); } if (ctx.getFormat().brace_position_for_switch == BracePosition.NEXT_LINE_SHIFTED) { ctx.indent(); ctx.printIndentation(); ctx.printString("}"); ctx.dedent(); } else { ctx.printIndentation(); ctx.printString("}"); } } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/statement/ForEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/statement/ForEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.statement; import org.spongepowered.despector.ast.stmt.branch.For; import org.spongepowered.despector.emitter.StatementEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; /** * An emitter for for loops. */ public class ForEmitter implements StatementEmitter<JavaEmitterContext, For> { @Override public void emit(JavaEmitterContext ctx, For loop, boolean semicolon) { ctx.printString("for"); ctx.printString(" ", ctx.getFormat().insert_space_before_opening_paren_in_for); ctx.printString("("); ctx.printString(" ", ctx.getFormat().insert_space_after_opening_paren_in_for); if (loop.getInit() != null) { ctx.emit(loop.getInit(), false); } ctx.printString("; "); ctx.emit(loop.getCondition()); ctx.printString("; "); if (loop.getIncr() != null) { ctx.emit(loop.getIncr(), false); } ctx.printString(") {"); ctx.newLine(); if (!loop.getBody().getStatements().isEmpty()) { ctx.indent(); ctx.emitBody(loop.getBody()); ctx.dedent(); ctx.newLine(); } ctx.printIndentation(); ctx.printString("}"); } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/statement/ReturnEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/statement/ReturnEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.statement; import org.spongepowered.despector.ast.generic.TypeSignature; import org.spongepowered.despector.ast.stmt.misc.Return; import org.spongepowered.despector.emitter.StatementEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; /** * An emitter for a return statement. */ public class ReturnEmitter implements StatementEmitter<JavaEmitterContext, Return> { @Override public void emit(JavaEmitterContext ctx, Return insn, boolean semicolon) { ctx.printString("return"); if (insn.getValue().isPresent()) { TypeSignature type = null; if (ctx.getMethod() != null) { type = ctx.getMethod().getReturnType(); } ctx.printString(" "); ctx.emit(insn.getValue().get(), type); } if (semicolon) { ctx.printString(";"); } } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/statement/WhileEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/statement/WhileEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.statement; import org.spongepowered.despector.ast.stmt.branch.While; import org.spongepowered.despector.emitter.StatementEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; /** * An emitter for a while statement. */ public class WhileEmitter implements StatementEmitter<JavaEmitterContext, While> { @Override public void emit(JavaEmitterContext ctx, While loop, boolean semicolon) { ctx.printString("while"); ctx.printString(" ", ctx.getFormat().insert_space_before_opening_paren_in_while); ctx.printString("("); ctx.printString(" ", ctx.getFormat().insert_space_after_opening_paren_in_while); ctx.emit(loop.getCondition()); ctx.printString(" ", ctx.getFormat().insert_space_before_closing_paren_in_while); ctx.printString(") {"); ctx.newLine(); if (!loop.getBody().getStatements().isEmpty()) { ctx.indent(); ctx.emitBody(loop.getBody()); ctx.dedent(); ctx.newLine(); } ctx.printIndentation(); ctx.printString("}"); } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/statement/TryCatchEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/statement/TryCatchEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.statement; import org.spongepowered.despector.ast.stmt.branch.TryCatch; import org.spongepowered.despector.ast.stmt.branch.TryCatch.CatchBlock; import org.spongepowered.despector.emitter.StatementEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; public class TryCatchEmitter implements StatementEmitter<JavaEmitterContext, TryCatch> { @Override public void emit(JavaEmitterContext ctx, TryCatch try_block, boolean semicolon) { ctx.printString("try {"); ctx.newLine(); ctx.indent(); ctx.emitBody(try_block.getTryBlock()); ctx.dedent(); ctx.newLine(); for (CatchBlock c : try_block.getCatchBlocks()) { ctx.printIndentation(); if (ctx.getFormat().insert_new_line_before_catch_in_try_statement) { ctx.printString("}"); ctx.newIndentedLine(); ctx.printString("catch ("); } else { ctx.printString("} catch ("); } for (int i = 0; i < c.getExceptions().size(); i++) { ctx.emitType(c.getExceptions().get(i)); if (i < c.getExceptions().size() - 1) { ctx.printString(" | "); } } ctx.printString(" "); if (c.getExceptionLocal() != null) { ctx.printString(c.getExceptionLocal().getName()); ctx.isDefined(c.getExceptionLocal()); } else { ctx.printString(c.getDummyName()); } ctx.printString(") {"); ctx.newLine(); ctx.indent(); ctx.emitBody(c.getBlock()); ctx.dedent(); ctx.newLine(); } ctx.printIndentation(); ctx.printString("}"); } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/statement/IncrementEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/statement/IncrementEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.statement; import org.spongepowered.despector.ast.stmt.misc.Increment; import org.spongepowered.despector.emitter.StatementEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; /** * An emitter for increment statements. */ public class IncrementEmitter implements StatementEmitter<JavaEmitterContext, Increment> { @Override public void emit(JavaEmitterContext ctx, Increment insn, boolean semicolon) { ctx.printString(insn.getLocal().getName()); if (insn.getIncrementValue() == 1) { ctx.printString("++"); if (semicolon) { ctx.printString(";"); } return; } else if (insn.getIncrementValue() == -1) { ctx.printString("--"); if (semicolon) { ctx.printString(";"); } return; } ctx.printString(" += "); ctx.printString(String.valueOf(insn.getIncrementValue())); if (semicolon) { ctx.printString(";"); } } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/condition/AndConditionEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/condition/AndConditionEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.condition; import org.spongepowered.despector.ast.insn.condition.AndCondition; import org.spongepowered.despector.ast.insn.condition.Condition; import org.spongepowered.despector.ast.insn.condition.OrCondition; import org.spongepowered.despector.emitter.ConditionEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; /** * An emitter for and conditions. */ public class AndConditionEmitter implements ConditionEmitter<JavaEmitterContext, AndCondition> { @Override public void emit(JavaEmitterContext ctx, AndCondition and) { for (int i = 0; i < and.getOperands().size(); i++) { Condition cond = and.getOperands().get(i); if (cond instanceof OrCondition) { ctx.printString("("); ctx.emit(cond); ctx.printString(")"); } else { ctx.emit(cond); } if (i < and.getOperands().size() - 1) { ctx.markWrapPoint(); ctx.printString(" && "); } } } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/condition/CompareConditionEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/condition/CompareConditionEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.condition; import org.spongepowered.despector.ast.insn.condition.CompareCondition; import org.spongepowered.despector.ast.insn.misc.NumberCompare; import org.spongepowered.despector.emitter.ConditionEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; /** * An emitter for a compare condition. */ public class CompareConditionEmitter implements ConditionEmitter<JavaEmitterContext, CompareCondition> { @Override public void emit(JavaEmitterContext ctx, CompareCondition compare) { if (compare.getLeft() instanceof NumberCompare) { NumberCompare cmp = (NumberCompare) compare.getLeft(); ctx.emit(cmp.getLeftOperand(), null); ctx.markWrapPoint(); ctx.printString(compare.getOperator().asString()); ctx.emit(cmp.getRightOperand(), null); return; } ctx.emit(compare.getLeft(), null); ctx.markWrapPoint(); ctx.printString(compare.getOperator().asString()); ctx.emit(compare.getRight(), null); } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/condition/package-info.java
src/main/java/org/spongepowered/despector/emitter/java/condition/package-info.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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. */ @org.spongepowered.despector.util.NonnullByDefault package org.spongepowered.despector.emitter.java.condition;
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/condition/BooleanConditionEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/condition/BooleanConditionEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.condition; import org.spongepowered.despector.ast.generic.ClassTypeSignature; import org.spongepowered.despector.ast.insn.Instruction; import org.spongepowered.despector.ast.insn.condition.BooleanCondition; import org.spongepowered.despector.ast.insn.cst.IntConstant; import org.spongepowered.despector.ast.insn.misc.InstanceOf; import org.spongepowered.despector.ast.insn.misc.Ternary; import org.spongepowered.despector.ast.insn.op.Operator; import org.spongepowered.despector.emitter.ConditionEmitter; import org.spongepowered.despector.emitter.InstructionEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; import org.spongepowered.despector.emitter.java.instruction.TernaryEmitter; /** * An emitter for boolean conditions. */ public class BooleanConditionEmitter implements ConditionEmitter<JavaEmitterContext, BooleanCondition> { @Override public void emit(JavaEmitterContext ctx, BooleanCondition bool) { if (checkConstant(ctx, bool)) { return; } if (bool.isInverse()) { ctx.printString("!"); } emit(ctx, bool.getConditionValue(), bool.isInverse()); } protected void emit(JavaEmitterContext ctx, Instruction val, boolean parens_needed) { if (parens_needed && val instanceof InstanceOf) { ctx.printString("("); ctx.emit(val, ClassTypeSignature.BOOLEAN); ctx.printString(")"); } else { ctx.emit(val, ClassTypeSignature.BOOLEAN); } } protected boolean checkConstant(JavaEmitterContext ctx, BooleanCondition bool) { Instruction val = bool.getConditionValue(); if (val.inferType() == ClassTypeSignature.INT) { if (val instanceof IntConstant) { IntConstant cst = (IntConstant) val; if (cst.getConstant() == 0) { ctx.printString("false"); } else { ctx.printString("true"); } return true; } if (val instanceof Ternary) { Ternary ternary = (Ternary) val; InstructionEmitter<JavaEmitterContext, Ternary> emitter = ctx.getEmitterSet().getInstructionEmitter(Ternary.class); if (emitter instanceof TernaryEmitter) { if (((TernaryEmitter) emitter).checkBooleanExpression(ctx, ternary)) { return true; } } } if (val instanceof Operator) { ctx.printString("("); ctx.emit(val, ClassTypeSignature.INT); ctx.printString(")"); } else { ctx.emit(val, ClassTypeSignature.INT); } if (bool.isInverse()) { ctx.printString(" == 0"); } else { ctx.printString(" != 0"); } return true; } return false; } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/condition/OrConditionEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/condition/OrConditionEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.condition; import org.spongepowered.despector.ast.insn.condition.OrCondition; import org.spongepowered.despector.emitter.ConditionEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; /** * An emitter for an or condition. */ public class OrConditionEmitter implements ConditionEmitter<JavaEmitterContext, OrCondition> { @Override public void emit(JavaEmitterContext ctx, OrCondition and) { for (int i = 0; i < and.getOperands().size(); i++) { ctx.emit(and.getOperands().get(i)); if (i < and.getOperands().size() - 1) { ctx.markWrapPoint(); ctx.printString(" || "); } } } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/java/condition/InverseConditionEmitter.java
src/main/java/org/spongepowered/despector/emitter/java/condition/InverseConditionEmitter.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.java.condition; import org.spongepowered.despector.ast.generic.ClassTypeSignature; import org.spongepowered.despector.ast.insn.condition.BooleanCondition; import org.spongepowered.despector.ast.insn.condition.CompareCondition; import org.spongepowered.despector.ast.insn.condition.Condition; import org.spongepowered.despector.ast.insn.condition.InverseCondition; import org.spongepowered.despector.emitter.ConditionEmitter; import org.spongepowered.despector.emitter.java.JavaEmitterContext; /** * An emitter for an inverse condition. */ public class InverseConditionEmitter implements ConditionEmitter<JavaEmitterContext, InverseCondition> { @Override public void emit(JavaEmitterContext ctx, InverseCondition inv) { Condition cond = inv.getConditionValue(); if (cond instanceof InverseCondition) { ctx.emit(((InverseCondition) cond).getConditionValue()); return; } else if (cond instanceof BooleanCondition) { BooleanCondition bool = (BooleanCondition) cond; if (!bool.isInverse()) { ctx.printString("!"); } ctx.emit(bool.getConditionValue(), ClassTypeSignature.BOOLEAN); return; } else if (cond instanceof CompareCondition) { CompareCondition compare = (CompareCondition) cond; ctx.emit(compare.getLeft(), null); ctx.printString(compare.getOperator().inverse().asString()); ctx.emit(compare.getRight(), null); return; } ctx.printString("!"); ctx.printString(" ", ctx.getFormat().insert_space_after_unary_operator); ctx.printString("("); ctx.emit(cond); ctx.printString(")"); } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/format/package-info.java
src/main/java/org/spongepowered/despector/emitter/format/package-info.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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. */ @org.spongepowered.despector.util.NonnullByDefault package org.spongepowered.despector.emitter.format;
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/format/EclipseFormatLoader.java
src/main/java/org/spongepowered/despector/emitter/format/EclipseFormatLoader.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.format; import com.google.common.collect.Maps; import org.spongepowered.despector.emitter.format.EmitterFormat.BracePosition; import org.spongepowered.despector.emitter.format.EmitterFormat.WrappingStyle; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.nio.file.Path; import java.util.Map; import java.util.function.BiConsumer; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; /** * A loader for loading an eclipse formatter configuration. */ public final class EclipseFormatLoader implements FormatLoader { public static final EclipseFormatLoader instance = new EclipseFormatLoader(); private static final Map<String, BiConsumer<EmitterFormat, String>> settings_handlers = Maps.newHashMap(); private static WrappingStyle wrapFromVal(int val) { val /= 16; if (val < 0 || val >= WrappingStyle.values().length) { return WrappingStyle.DO_NOT_WRAP; } return WrappingStyle.values()[val]; } private static BracePosition braceFromVal(String val) { if ("next_line".equals(val)) { return BracePosition.NEXT_LINE; } else if ("next_line_shifted".equals(val)) { return BracePosition.NEXT_LINE_SHIFTED; } else if ("next_line_on_wrap".equals(val)) { return BracePosition.NEXT_LINE_ON_WRAP; } return BracePosition.SAME_LINE; } static { // @formatter:off BiConsumer<EmitterFormat, String> noop = (f, v)->{}; settings_handlers.put("org.eclipse.jdt.core.compiler.source", noop); settings_handlers.put("org.eclipse.jdt.core.formatter.continuation_indentation", (f, v) -> f.continuation_indentation = Integer.parseInt(v)); settings_handlers.put("org.eclipse.jdt.core.formatter.lineSplit", (f, v) -> f.line_split = Integer.parseInt(v)); settings_handlers.put("org.eclipse.jdt.core.formatter.indentation.size", (f, v) -> f.indentation_size = Integer.parseInt(v)); settings_handlers.put("org.eclipse.jdt.core.formatter.tabulation.char", (f, v) -> f.indent_with_spaces = v.equals("space")); settings_handlers.put("org.eclipse.jdt.core.formatter.indent_empty_lines", (f, v) -> f.indent_empty_lines = v.equals("true")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_new_line_at_end_of_file_if_missing", (f, v) -> f.insert_new_line_at_end_of_file_if_missing = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.blank_lines_before_first_class_body_declaration", (f, v) -> f.blank_lines_before_first_class_body_declaration = Integer.parseInt(v)); settings_handlers.put("org.eclipse.jdt.core.formatter.blank_lines_before_package", (f, v) -> f.blank_lines_before_package = Integer.parseInt(v)); settings_handlers.put("org.eclipse.jdt.core.formatter.blank_lines_after_package", (f, v) -> f.blank_lines_after_package = Integer.parseInt(v)); settings_handlers.put("org.eclipse.jdt.core.formatter.blank_lines_before_imports", (f, v) -> f.blank_lines_before_imports = Integer.parseInt(v)); settings_handlers.put("org.eclipse.jdt.core.formatter.blank_lines_after_imports", (f, v) -> f.blank_lines_after_imports = Integer.parseInt(v)); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_comma_in_superinterfaces", (f, v) -> f.insert_space_before_comma_in_superinterfaces = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_after_comma_in_superinterfaces", (f, v) -> f.insert_space_after_comma_in_superinterfaces = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.alignment_for_superclass_in_type_declaration", (f, v) -> f.alignment_for_superclass_in_type_declaration = wrapFromVal(Integer.parseInt(v))); settings_handlers.put("org.eclipse.jdt.core.formatter.align_type_members_on_columns", (f, v) -> f.align_type_members_on_columns = v.equals("true")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_after_comma_in_enum_constant_arguments", (f, v) -> f.insert_space_after_comma_in_enum_constant_arguments = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_comma_in_enum_declarations", (f, v) -> f.insert_space_before_comma_in_enum_declarations = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_after_comma_in_enum_declarations", (f, v) -> f.insert_space_after_comma_in_enum_declarations = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_enum_constant", (f, v) -> f.insert_space_after_opening_paren_in_enum_constant = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_enum_declaration", (f, v) -> f.insert_space_before_opening_brace_in_enum_declaration = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_enum_constant", (f, v) -> f.insert_space_before_opening_paren_in_enum_constant = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_comma_in_method_invocation_arguments", (f, v) -> f.insert_space_before_comma_in_method_invocation_arguments = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.alignment_for_arguments_in_explicit_constructor_call", (f, v) -> f.alignment_for_arguments_in_explicit_constructor_call = wrapFromVal(Integer.parseInt(v))); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_comma_in_explicitconstructorcall_arguments", (f, v) -> f.insert_space_before_comma_in_explicitconstructorcall_arguments = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_after_comma_in_explicitconstructorcall_arguments", (f, v) -> f.insert_space_after_comma_in_explicitconstructorcall_arguments = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_constructor_declaration", (f, v) -> f.insert_space_before_opening_brace_in_constructor_declaration = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_constructor_declaration", (f, v) -> f.insert_space_before_opening_paren_in_constructor_declaration = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_constructor_declaration", (f, v) -> f.insert_space_after_opening_paren_in_constructor_declaration = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_comma_in_constructor_declaration_parameters", (f, v) -> f.insert_space_before_comma_in_constructor_declaration_parameters = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_after_comma_in_constructor_declaration_parameters", (f, v) -> f.insert_space_after_comma_in_constructor_declaration_parameters = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.alignment_for_parameters_in_constructor_declaration", (f, v) -> f.alignment_for_parameters_in_constructor_declaration = wrapFromVal(Integer.parseInt(v))); settings_handlers.put("org.eclipse.jdt.core.formatter.alignment_for_union_type_in_multicatch", (f, v) -> f.alignment_for_union_type_in_multicatch = wrapFromVal(Integer.parseInt(v))); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_method_declaration", (f, v) -> f.insert_space_between_empty_parens_in_method_declaration = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_new_line_in_empty_method_body", (f, v) -> f.insert_new_line_in_empty_method_body = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_comma_in_method_declaration_throws", (f, v) -> f.insert_space_before_comma_in_method_declaration_throws = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_after_comma_in_method_declaration_throws", (f, v) -> f.insert_space_after_comma_in_method_declaration_throws = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.brace_position_for_method_declaration", (f, v) -> f.brace_position_for_method_declaration = braceFromVal(v)); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_comma_in_method_declaration_parameters", (f, v) -> f.insert_space_before_comma_in_method_declaration_parameters = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_after_comma_in_method_declaration_parameters", (f, v) -> f.insert_space_after_comma_in_method_declaration_parameters = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.alignment_for_throws_clause_in_constructor_declaration", (f, v) -> f.alignment_for_throws_clause_in_constructor_declaration = wrapFromVal(Integer.parseInt(v))); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_method_declaration", (f, v) -> f.insert_space_before_opening_paren_in_method_declaration = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.alignment_for_superinterfaces_in_type_declaration", (f, v) -> f.alignment_for_superinterfaces_in_type_declaration = wrapFromVal(Integer.parseInt(v))); // settings_handlers.put("org.eclipse.jdt.core.formatter.alignment_for_arguments_in_enum_constant", (f, v) -> f.alignment_for_arguments_in_enum_constant = wrapFromVal(Integer.parseInt(v))); settings_handlers.put("org.eclipse.jdt.core.formatter.blank_lines_between_import_groups", (f, v) -> f.blank_lines_between_import_groups = Integer.parseInt(v)); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_comma_in_enum_constant_arguments", (f, v) -> f.insert_space_before_comma_in_enum_constant_arguments = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.brace_position_for_constructor_declaration", (f, v) -> f.brace_position_for_constructor_declaration = braceFromVal(v)); settings_handlers.put("org.eclipse.jdt.core.formatter.indent_body_declarations_compare_to_enum_declaration_header", (f, v) -> f.indent_body_declarations_compare_to_enum_declaration_header = v.equals("true")); settings_handlers.put("org.eclipse.jdt.core.formatter.alignment_for_superinterfaces_in_enum_declaration", (f, v) -> f.alignment_for_superinterfaces_in_enum_declaration = wrapFromVal(Integer.parseInt(v))); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_after_comma_in_constructor_declaration_throws", (f, v) -> f.insert_space_after_comma_in_constructor_declaration_throws = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.alignment_for_parameters_in_method_declaration", (f, v) -> f.alignment_for_parameters_in_method_declaration = wrapFromVal(Integer.parseInt(v))); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_enum_constant", (f, v) -> f.insert_space_before_closing_paren_in_enum_constant = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.alignment_for_enum_constants", (f, v) -> f.alignment_for_enum_constants = wrapFromVal(Integer.parseInt(v))); settings_handlers.put("org.eclipse.jdt.core.formatter.brace_position_for_type_declaration", (f, v) -> f.brace_position_for_type_declaration = braceFromVal(v)); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_method_declaration", (f, v) -> f.insert_space_before_opening_brace_in_method_declaration = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_method_declaration", (f, v) -> f.insert_space_before_closing_paren_in_method_declaration = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.comment.new_lines_at_block_boundaries", (f, v) -> f.new_lines_at_block_boundaries = v.equals("true")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_after_unary_operator", (f, v) -> f.insert_space_after_unary_operator = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_binary_operator", (f, v) -> f.insert_space_before_binary_operator = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_after_binary_operator", (f, v) -> f.insert_space_after_binary_operator = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_between_brackets_in_array_type_reference", (f, v) -> f.insert_space_between_brackets_in_array_type_reference = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_after_opening_brace_in_array_initializer", (f, v) -> f.insert_space_after_opening_brace_in_array_initializer = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_new_line_before_closing_brace_in_array_initializer", (f, v) -> f.insert_new_line_before_closing_brace_in_array_initializer = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.alignment_for_expressions_in_array_initializer", (f, v) -> f.alignment_for_expressions_in_array_initializer = wrapFromVal(Integer.parseInt(v))); settings_handlers.put("org.eclipse.jdt.core.formatter.brace_position_for_array_initializer", (f, v) -> f.brace_position_for_array_initializer = braceFromVal(v)); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_after_opening_bracket_in_array_allocation_expression", (f, v) -> f.insert_space_after_opening_bracket_in_array_allocation_expression = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_closing_bracket_in_array_allocation_expression", (f, v) -> f.insert_space_before_closing_bracket_in_array_allocation_expression = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_comma_in_array_initializer", (f, v) -> f.insert_space_before_comma_in_array_initializer = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_opening_bracket_in_array_type_reference", (f, v) -> f.insert_space_before_opening_bracket_in_array_type_reference = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_new_line_after_opening_brace_in_array_initializer", (f, v) -> f.insert_new_line_after_opening_brace_in_array_initializer = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_opening_bracket_in_array_reference", (f, v) -> f.insert_space_before_opening_bracket_in_array_reference = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_switch", (f, v) -> f.insert_space_after_opening_paren_in_switch = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_switch", (f, v) -> f.insert_space_before_closing_paren_in_switch = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_colon_in_case", (f, v) -> f.insert_space_before_colon_in_case = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.indent_breaks_compare_to_cases", (f, v) -> f.indent_breaks_compare_to_cases = v.equals("true")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_after_colon_in_case", (f, v) -> f.insert_space_after_colon_in_case = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.brace_position_for_switch", (f, v) -> f.brace_position_for_switch = braceFromVal(v)); settings_handlers.put("org.eclipse.jdt.core.formatter.indent_switchstatements_compare_to_switch", (f, v) -> f.indent_switchstatements_compare_to_switch = v.equals("true")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_switch", (f, v) -> f.insert_space_before_opening_brace_in_switch = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_for", (f, v) -> f.insert_space_before_opening_paren_in_for = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_for", (f, v) -> f.insert_space_after_opening_paren_in_for = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_after_comma_in_for_increments", (f, v) -> f.insert_space_after_comma_in_for_increments = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_colon_in_for", (f, v) -> f.insert_space_before_colon_in_for = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_while", (f, v) -> f.insert_space_before_opening_paren_in_while = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_while", (f, v) -> f.insert_space_after_opening_paren_in_while = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_new_line_before_else_in_if_statement", (f, v) -> f.insert_new_line_before_else_in_if_statement = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_if", (f, v) -> f.insert_space_before_opening_paren_in_if = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_if", (f, v) -> f.insert_space_before_closing_paren_in_if = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.compact_else_if", (f, v) -> f.compact_else_if = v.equals("true")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_new_line_before_catch_in_try_statement", (f, v) -> f.insert_new_line_before_catch_in_try_statement = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_new_line_before_finally_in_try_statement", (f, v) -> f.insert_new_line_before_finally_in_try_statement = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_try", (f, v) -> f.insert_space_before_closing_paren_in_try = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_try", (f, v) -> f.insert_space_after_opening_paren_in_try = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.alignment_for_conditional_expression", (f, v) -> f.alignment_for_conditional_expression = wrapFromVal(Integer.parseInt(v))); settings_handlers.put("org.eclipse.jdt.core.formatter.alignment_for_arguments_in_method_invocation", (f, v) -> f.alignment_for_arguments_in_method_invocation = wrapFromVal(Integer.parseInt(v))); settings_handlers.put("org.eclipse.jdt.core.formatter.alignment_for_selector_in_method_invocation", (f, v) -> f.alignment_for_selector_in_method_invocation = wrapFromVal(Integer.parseInt(v))); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_after_question_in_conditional", (f, v) -> f.insert_space_after_question_in_conditional = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_after_question_in_wildcard", (f, v) -> f.insert_space_after_question_in_wildcard = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_after_lambda_arrow", (f, v) -> f.insert_space_after_lambda_arrow = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_parenthesized_expression", (f, v) -> f.insert_space_after_opening_paren_in_parenthesized_expression = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.alignment_for_assignment", (f, v) -> f.blank_before_alignment_for_assignmentjavadoc_tags = wrapFromVal(Integer.parseInt(v))); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_after_semicolon_in_try_resources", (f, v) -> f.insert_space_after_semicolon_in_try_resources = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.indent_statements_compare_to_body", (f, v) -> f.indent_statements_compare_to_body = v.equals("true")); settings_handlers.put("org.eclipse.jdt.core.formatter.wrap_outer_expressions_when_nested", (f, v) -> f.wrap_outer_expressions_when_nested = v.equals("true")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_cast", (f, v) -> f.insert_space_before_closing_paren_in_cast = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.format_guardian_clause_on_one_line", (f, v) -> f.format_guardian_clause_on_one_line = v.equals("true")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_after_colon_in_labeled_statement", (f, v) -> f.insert_space_after_colon_in_labeled_statement = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_parenthesized_expression_in_return", (f, v) -> f.insert_space_before_parenthesized_expression_in_return = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_parenthesized_expression_in_throw", (f, v) -> f.insert_space_before_parenthesized_expression_in_throw = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_ellipsis", (f, v) -> f.insert_space_before_ellipsis = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.brace_position_for_block", (f, v) -> f.brace_position_for_block = braceFromVal(v)); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_comma_in_for_inits", (f, v) -> f.insert_space_before_comma_in_for_inits = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.wrap_before_or_operator_multicatch", (f, v) -> f.wrap_before_or_operator_multicatch = v.equals("true")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_closing_bracket_in_array_reference", (f, v) -> f.insert_space_before_closing_bracket_in_array_reference = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_comma_in_allocation_expression", (f, v) -> f.insert_space_before_comma_in_allocation_expression = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_after_closing_brace_in_block", (f, v) -> f.insert_space_after_closing_brace_in_block = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_comma_in_constructor_declaration_throws", (f, v) -> f.insert_space_before_comma_in_constructor_declaration_throws = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_if", (f, v) -> f.insert_space_after_opening_paren_in_if = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_after_assignment_operator", (f, v) -> f.insert_space_after_assignment_operator = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_assignment_operator", (f, v) -> f.insert_space_before_assignment_operator = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_synchronized", (f, v) -> f.insert_space_after_opening_paren_in_synchronized = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_after_closing_paren_in_cast", (f, v) -> f.insert_space_after_closing_paren_in_cast = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.brace_position_for_block_in_case", (f, v) -> f.brace_position_for_block_in_case = braceFromVal(v)); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_catch", (f, v) -> f.insert_space_after_opening_paren_in_catch = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_method_invocation", (f, v) -> f.insert_space_before_opening_paren_in_method_invocation = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_after_opening_bracket_in_array_reference", (f, v) -> f.insert_space_after_opening_bracket_in_array_reference = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.alignment_for_arguments_in_qualified_allocation_expression", (f, v) -> f.alignment_for_arguments_in_qualified_allocation_expression = wrapFromVal(Integer.parseInt(v))); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_after_and_in_type_parameter", (f, v) -> f.insert_space_after_and_in_type_parameter = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_between_empty_brackets_in_array_allocation_expression", (f, v) -> f.insert_space_between_empty_brackets_in_array_allocation_expression = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.keep_empty_array_initializer_on_one_line", (f, v) -> f.keep_empty_array_initializer_on_one_line = v.equals("true")); settings_handlers.put("org.eclipse.jdt.core.formatter.continuation_indentation_for_array_initializer", (f, v) -> f.continuation_indentation_for_array_initializer = Integer.parseInt(v)); settings_handlers.put("org.eclipse.jdt.core.formatter.alignment_for_arguments_in_allocation_expression", (f, v) -> f.alignment_for_arguments_in_allocation_expression = wrapFromVal(Integer.parseInt(v))); settings_handlers.put("org.eclipse.jdt.core.formatter.brace_position_for_lambda_body", (f, v) -> f.brace_position_for_lambda_body = braceFromVal(v)); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_cast", (f, v) -> f.insert_space_after_opening_paren_in_cast = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_unary_operator", (f, v) -> f.insert_space_before_unary_operator = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.keep_imple_if_on_one_line", (f, v) -> f.keep_simple_if_on_one_line = v.equals("true")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_colon_in_labeled_statement", (f, v) -> f.insert_space_before_colon_in_labeled_statement = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_after_colon_in_for", (f, v) -> f.insert_space_after_colon_in_for = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.alignment_for_binary_expression", (f, v) -> f.alignment_for_binary_expression = wrapFromVal(Integer.parseInt(v))); settings_handlers.put("org.eclipse.jdt.core.formatter.brace_position_for_enum_declaration", (f, v) -> f.brace_position_for_enum_declaration = braceFromVal(v)); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_while", (f, v) -> f.insert_space_before_closing_paren_in_while = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_try", (f, v) -> f.insert_space_before_opening_paren_in_try = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.put_empty_statement_on_new_line", (f, v) -> f.put_empty_statement_on_new_line = v.equals("true")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_new_line_after_label", (f, v) -> f.insert_new_line_after_label = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_method_invocation", (f, v) -> f.insert_space_between_empty_parens_in_method_invocation = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_new_line_before_while_in_do_statement", (f, v) -> f.insert_new_line_before_while_in_do_statement = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_semicolon", (f, v) -> f.insert_space_before_semicolon = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.number_of_blank_lines_at_beginning_of_method_body", (f, v) -> f.number_of_blank_lines_at_beginning_of_method_body = Integer.parseInt(v)); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_colon_in_conditional", (f, v) -> f.insert_space_before_colon_in_conditional = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.indent_body_declarations_compare_to_type_header", (f, v) -> f.indent_body_declarations_compare_to_type_header = v.equals("true")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_annotation_type_member_declaration", (f, v) -> f.insert_space_before_opening_paren_in_annotation_type_member_declaration = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.wrap_before_binary_operator", (f, v) -> f.wrap_before_binary_operator = v.equals("true")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_synchronized", (f, v) -> f.insert_space_before_closing_paren_in_synchronized = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.indent_statements_compare_to_block", (f, v) -> f.indent_statements_compare_to_block = v.equals("true")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_question_in_conditional", (f, v) -> f.insert_space_before_question_in_conditional = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_before_comma_in_multiple_field_declarations", (f, v) -> f.insert_space_before_comma_in_multiple_field_declarations = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.alignment_for_compact_if", (f, v) -> f.alignment_for_compact_if = wrapFromVal(Integer.parseInt(v))); settings_handlers.put("org.eclipse.jdt.core.formatter.insert_space_after_comma_in_for_inits", (f, v) -> f.insert_space_after_comma_in_for_inits = v.equals("insert")); settings_handlers.put("org.eclipse.jdt.core.formatter.indent_switchstatements_compare_to_cases", (f, v) -> f.indent_switchstatements_compare_to_cases = v.equals("true"));
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
true
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/format/FormatLoader.java
src/main/java/org/spongepowered/despector/emitter/format/FormatLoader.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.format; import java.io.IOException; import java.nio.file.Path; /** * A loader for emitter formats. */ public interface FormatLoader { /** * Gets the loader for the given formatter type. */ public static FormatLoader getLoader(String name) { if ("eclipse".equalsIgnoreCase(name)) { return EclipseFormatLoader.instance; } throw new IllegalArgumentException("Unknown formatter type: " + name); } void load(EmitterFormat format, Path formatter, Path import_order) throws IOException; }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/emitter/format/EmitterFormat.java
src/main/java/org/spongepowered/despector/emitter/format/EmitterFormat.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.emitter.format; import com.google.common.collect.Lists; import org.spongepowered.despector.config.ConfigBase.FormatterConfig; import java.lang.reflect.Field; import java.util.List; /** * Configuration for the formatting of a source emitter. */ public class EmitterFormat { // ======================================================================== // general // ======================================================================== // max length of a line public int line_split = 999; // size of a tab in spaces public int indentation_size = 4; // number of times indented when a line is continued public int continuation_indentation = 2; // use spaces over tabs public boolean indent_with_spaces = true; // whether empty lines should be indented public boolean indent_empty_lines = false; // whether to insert a newline at end of file public boolean insert_new_line_at_end_of_file_if_missing = true; // ======================================================================== // imports // ======================================================================== public List<String> import_order = Lists.newArrayList(); public int blank_lines_before_package = 0; public int blank_lines_after_package = 1; public int blank_lines_before_imports = 1; public int blank_lines_after_imports = 1; public int blank_lines_between_import_groups = 1; // ======================================================================== // parameterized types // ======================================================================== // ======================================================================== // class // ======================================================================== public boolean insert_space_before_comma_in_superinterfaces = false; public boolean insert_space_after_comma_in_superinterfaces = true; public int blank_lines_before_first_class_body_declaration = 1; public BracePosition brace_position_for_type_declaration = BracePosition.SAME_LINE; public WrappingStyle alignment_for_superclass_in_type_declaration = WrappingStyle.DO_NOT_WRAP; public WrappingStyle alignment_for_superinterfaces_in_type_declaration = WrappingStyle.WRAP_WHEN_NEEDED; // ======================================================================== // enum // ======================================================================== public boolean insert_space_before_opening_brace_in_enum_declaration = true; public boolean insert_space_before_comma_in_enum_constant_arguments = false; public boolean insert_space_after_comma_in_enum_constant_arguments = true; public boolean insert_space_before_comma_in_enum_declarations = false; public boolean insert_space_after_comma_in_enum_declarations = true; public boolean insert_space_before_opening_paren_in_enum_constant = false; public boolean insert_space_after_opening_paren_in_enum_constant = false; public boolean insert_space_before_closing_paren_in_enum_constant = false; public boolean indent_body_declarations_compare_to_enum_declaration_header = true; public BracePosition brace_position_for_enum_declaration = BracePosition.SAME_LINE; // public WrappingStyle alignment_for_arguments_in_enum_constant = WrappingStyle.WRAP_WHEN_NEEDED; public WrappingStyle alignment_for_superinterfaces_in_enum_declaration = WrappingStyle.WRAP_WHEN_NEEDED; public WrappingStyle alignment_for_enum_constants = WrappingStyle.WRAP_ALL; // ======================================================================== // annotations // ======================================================================== public boolean insert_space_before_closing_paren_in_annotation = false; public boolean insert_new_line_after_annotation_on_type = true; public boolean insert_new_line_after_annotation_on_parameter = false; public boolean insert_new_line_after_annotation_on_package = true; public boolean insert_new_line_after_annotation_on_field = false; public boolean insert_new_line_after_annotation_on_local_variable = true; public boolean insert_new_line_after_annotation_on_method = true; // ======================================================================== // field decl // ======================================================================== public boolean align_type_members_on_columns = false; // ======================================================================== // method decl // ======================================================================== public boolean insert_space_between_empty_parens_in_method_declaration = false; public boolean insert_space_before_comma_in_method_invocation_arguments = false; public boolean insert_space_after_comma_in_method_invocation_arguments = true; public boolean insert_new_line_in_empty_method_body = false; public boolean insert_space_before_comma_in_method_declaration_throws = false; public boolean insert_space_after_comma_in_method_declaration_throws = true; public boolean insert_space_before_comma_in_method_declaration_parameters = false; public boolean insert_space_after_comma_in_method_declaration_parameters = true; public boolean insert_space_before_opening_paren_in_method_declaration = false; public boolean insert_space_before_closing_paren_in_method_declaration = false; public boolean insert_space_before_opening_brace_in_method_declaration = true; public BracePosition brace_position_for_method_declaration = BracePosition.SAME_LINE; public WrappingStyle alignment_for_parameters_in_method_declaration = WrappingStyle.WRAP_WHEN_NEEDED; // ======================================================================== // ctor // ======================================================================== public boolean insert_space_before_comma_in_explicitconstructorcall_arguments = false; public boolean insert_space_after_comma_in_explicitconstructorcall_arguments = true; public boolean insert_space_before_opening_paren_in_constructor_declaration = false; public boolean insert_space_after_opening_paren_in_constructor_declaration = false; public boolean insert_space_before_comma_in_constructor_declaration_parameters = false; public boolean insert_space_after_comma_in_constructor_declaration_parameters = true; public boolean insert_space_before_opening_brace_in_constructor_declaration = true; public boolean insert_space_after_comma_in_constructor_declaration_throws = true; public BracePosition brace_position_for_constructor_declaration = BracePosition.SAME_LINE; // explicit ctors are things like super(a,b); or this(foo,bar); public WrappingStyle alignment_for_arguments_in_explicit_constructor_call = WrappingStyle.WRAP_WHEN_NEEDED; public WrappingStyle alignment_for_parameters_in_constructor_declaration = WrappingStyle.WRAP_WHEN_NEEDED; public WrappingStyle alignment_for_throws_clause_in_constructor_declaration = WrappingStyle.WRAP_WHEN_NEEDED; // ======================================================================== // try // ======================================================================== public boolean insert_new_line_before_catch_in_try_statement = false; public WrappingStyle alignment_for_union_type_in_multicatch = WrappingStyle.WRAP_WHEN_NEEDED; // ======================================================================== // loops // ======================================================================== public boolean insert_space_before_opening_paren_in_for = true; public boolean insert_space_after_opening_paren_in_for = false; public boolean insert_space_before_colon_in_for = true; public boolean insert_space_after_colon_in_for = true; public boolean insert_space_before_opening_paren_in_while = true; public boolean insert_space_after_opening_paren_in_while = false; public boolean insert_space_before_closing_paren_in_while = false; // ======================================================================== // ifs // ======================================================================== public boolean insert_space_before_opening_paren_in_if = true; public boolean insert_space_after_opening_paren_in_if = false; public boolean insert_space_before_closing_paren_in_if = false; public boolean insert_new_line_before_else_in_if_statement = false; // ======================================================================== // switch // ======================================================================== public boolean insert_space_after_opening_paren_in_switch = false; public boolean insert_space_before_closing_paren_in_switch = false; public boolean insert_space_before_colon_in_case = false; public boolean insert_space_after_colon_in_case = false; public boolean insert_space_before_opening_brace_in_switch = true; public boolean indent_breaks_compare_to_cases = true; public boolean indent_switchstatements_compare_to_switch = false; public boolean indent_switchstatements_compare_to_cases = true; public BracePosition brace_position_for_switch = BracePosition.SAME_LINE; // ======================================================================== // annotations // ======================================================================== // ======================================================================== // comments // ======================================================================== // ======================================================================== // arrays // ======================================================================== public boolean insert_space_between_brackets_in_array_type_reference = false; public boolean insert_space_after_opening_brace_in_array_initializer = false; public boolean insert_new_line_before_closing_brace_in_array_initializer = false; public WrappingStyle alignment_for_expressions_in_array_initializer = WrappingStyle.WRAP_WHEN_NEEDED; public BracePosition brace_position_for_array_initializer = BracePosition.SAME_LINE; public boolean insert_space_after_opening_bracket_in_array_allocation_expression = false; public boolean insert_space_before_closing_bracket_in_array_allocation_expression = false; public boolean insert_space_before_comma_in_array_initializer = false; public boolean insert_space_after_comma_in_array_initializer = true; public boolean insert_space_before_opening_bracket_in_array_type_reference = false; public boolean insert_new_line_after_opening_brace_in_array_initializer = false; public boolean insert_space_before_opening_bracket_in_array_reference = false; public boolean insert_space_before_closing_bracket_in_array_reference = false; // ======================================================================== // misc // ======================================================================== public boolean new_lines_at_block_boundaries = true; public boolean insert_space_after_unary_operator = false; public boolean insert_space_before_binary_operator = true; public boolean insert_space_after_binary_operator = true; public boolean insert_space_after_lambda_arrow = true; // TODO need to finish sorting these... public boolean insert_space_after_comma_in_for_increments = true; public boolean compact_else_if = false; public boolean insert_new_line_before_finally_in_try_statement = false; public boolean insert_space_before_closing_paren_in_try = false; public boolean insert_space_after_opening_paren_in_try = false; public WrappingStyle alignment_for_conditional_expression = WrappingStyle.WRAP_WHEN_NEEDED; public WrappingStyle alignment_for_arguments_in_method_invocation = WrappingStyle.WRAP_WHEN_NEEDED; public WrappingStyle alignment_for_selector_in_method_invocation = WrappingStyle.WRAP_WHEN_NEEDED; public boolean insert_space_after_question_in_conditional = true; public boolean insert_space_after_question_in_wildcard = false; public boolean insert_space_after_opening_paren_in_parenthesized_expression = false; public WrappingStyle blank_before_alignment_for_assignmentjavadoc_tags = WrappingStyle.WRAP_WHEN_NEEDED; public boolean insert_space_after_semicolon_in_try_resources = true; public boolean indent_statements_compare_to_body = true; public boolean wrap_outer_expressions_when_nested = true; public boolean insert_space_before_closing_paren_in_cast = false; public boolean format_guardian_clause_on_one_line = false; public boolean insert_space_after_colon_in_labeled_statement = true; public boolean insert_space_before_parenthesized_expression_in_return = true; public boolean insert_space_before_parenthesized_expression_in_throw = true; public boolean insert_space_before_ellipsis = false; public BracePosition brace_position_for_block = BracePosition.SAME_LINE; public boolean insert_space_before_comma_in_for_inits = false; public boolean wrap_before_or_operator_multicatch = true; public boolean insert_space_before_comma_in_allocation_expression = false; public boolean insert_space_after_closing_brace_in_block = true; public boolean insert_space_before_comma_in_constructor_declaration_throws = false; public boolean insert_space_after_assignment_operator = true; public boolean insert_space_before_assignment_operator = true; public boolean insert_space_after_opening_paren_in_synchronized = false; public boolean insert_space_after_closing_paren_in_cast = true; public BracePosition brace_position_for_block_in_case = BracePosition.SAME_LINE; public boolean insert_space_after_opening_paren_in_catch = false; public boolean insert_space_before_opening_paren_in_method_invocation = false; public boolean insert_space_after_opening_bracket_in_array_reference = false; public WrappingStyle alignment_for_arguments_in_qualified_allocation_expression = WrappingStyle.WRAP_WHEN_NEEDED; public boolean insert_space_after_and_in_type_parameter = true; public boolean insert_space_between_empty_brackets_in_array_allocation_expression = false; public boolean keep_empty_array_initializer_on_one_line = false; public int continuation_indentation_for_array_initializer = 2; public WrappingStyle alignment_for_arguments_in_allocation_expression = WrappingStyle.WRAP_WHEN_NEEDED; public BracePosition brace_position_for_lambda_body = BracePosition.SAME_LINE; public boolean insert_space_after_opening_paren_in_cast = false; public boolean insert_space_before_unary_operator = false; public boolean keep_simple_if_on_one_line = false; public boolean insert_space_before_colon_in_labeled_statement = false; public WrappingStyle alignment_for_binary_expression = WrappingStyle.WRAP_WHEN_NEEDED; public boolean insert_space_before_opening_paren_in_try = true; public boolean put_empty_statement_on_new_line = true; public boolean insert_new_line_after_label = false; public boolean insert_space_between_empty_parens_in_method_invocation = false; public boolean insert_new_line_before_while_in_do_statement = false; public boolean insert_space_before_semicolon = false; public int number_of_blank_lines_at_beginning_of_method_body = 0; public boolean insert_space_before_colon_in_conditional = true; public boolean indent_body_declarations_compare_to_type_header = true; public boolean insert_space_before_opening_paren_in_annotation_type_member_declaration = false; public boolean wrap_before_binary_operator = true; public boolean insert_space_before_closing_paren_in_synchronized = false; public boolean indent_statements_compare_to_block = true; public boolean insert_space_before_question_in_conditional = true; public boolean insert_space_before_comma_in_multiple_field_declarations = false; public WrappingStyle alignment_for_compact_if = WrappingStyle.WRAP_WHEN_NEEDED; public boolean insert_space_after_comma_in_for_inits = true; public boolean insert_space_before_colon_in_default = false; public boolean insert_space_before_and_in_type_parameter = true; public boolean insert_space_between_empty_parens_in_constructor_declaration = false; public boolean insert_space_after_colon_in_assert = true; public WrappingStyle alignment_for_throws_clause_in_method_declaration = WrappingStyle.WRAP_WHEN_NEEDED; public boolean insert_space_before_opening_bracket_in_array_allocation_expression = false; public boolean insert_new_line_in_empty_anonymous_type_declaration = true; public boolean insert_space_after_colon_in_conditional = true; public boolean insert_space_before_closing_paren_in_for = false; public boolean insert_space_before_postfix_operator = false; public boolean insert_space_before_opening_paren_in_synchronized = true; public boolean insert_space_after_comma_in_allocation_expression = true; public boolean insert_space_before_closing_brace_in_array_initializer = false; public WrappingStyle alignment_for_resources_in_try = WrappingStyle.WRAP_WHEN_NEEDED; public boolean insert_space_before_lambda_arrow = true; public boolean insert_new_line_in_empty_block = true; public boolean insert_space_before_closing_paren_in_parenthesized_expression = false; public boolean insert_space_before_opening_paren_in_parenthesized_expression = false; public boolean insert_space_before_closing_paren_in_catch = false; public boolean insert_space_before_comma_in_multiple_local_declarations = false; public boolean insert_space_before_opening_paren_in_switch = true; public boolean insert_space_before_comma_in_for_increments = false; public boolean insert_space_after_opening_paren_in_method_invocation = false; public boolean insert_space_before_colon_in_assert = true; public boolean insert_space_before_opening_brace_in_array_initializer = true; public boolean insert_space_between_empty_braces_in_array_initializer = false; public boolean insert_space_after_opening_paren_in_method_declaration = false; public boolean insert_space_before_semicolon_in_for = false; public boolean insert_space_before_opening_paren_in_catch = true; public boolean insert_space_before_opening_angle_bracket_in_parameterized_type_reference = false; public boolean insert_space_after_comma_in_multiple_field_declarations = true; public boolean insert_space_after_comma_in_multiple_local_declarations = true; public boolean indent_body_declarations_compare_to_enum_constant_header = true; public boolean insert_space_after_semicolon_in_for = true; public boolean insert_space_before_semicolon_in_try_resources = false; public boolean keep_then_statement_on_same_line = false; public BracePosition brace_position_for_anonymous_type_declaration = BracePosition.SAME_LINE; public boolean insert_space_before_opening_brace_in_anonymous_type_declaration = true; public boolean insert_space_before_question_in_wildcard = false; public boolean insert_space_before_opening_brace_in_type_declaration = true; public boolean insert_space_before_closing_angle_bracket_in_parameterized_type_reference = false; public boolean insert_space_before_comma_in_parameterized_type_reference = false; public boolean insert_space_after_opening_angle_bracket_in_parameterized_type_reference = false; public boolean insert_space_after_comma_in_parameterized_type_reference = true; public boolean insert_space_before_opening_angle_bracket_in_type_arguments = false; public boolean insert_space_before_comma_in_type_parameters = false; public boolean insert_space_after_comma_in_type_parameters = true; public boolean insert_space_before_opening_angle_bracket_in_type_parameters = false; public boolean insert_space_after_closing_angle_bracket_in_type_parameters = true; public boolean insert_space_after_opening_angle_bracket_in_type_parameters = false; public boolean insert_space_before_closing_angle_bracket_in_type_parameters = false; public boolean insert_space_after_comma_in_type_arguments = true; public boolean insert_space_before_closing_angle_bracket_in_type_arguments = false; public boolean insert_space_after_closing_angle_bracket_in_type_arguments = false; public boolean insert_space_before_comma_in_type_arguments = false; public boolean insert_space_after_opening_angle_bracket_in_type_arguments = false; public boolean insert_space_after_comma_in_annotation = true; public boolean insert_new_line_in_empty_annotation_declaration = true; public boolean insert_space_after_opening_paren_in_annotation = false; public boolean insert_space_between_empty_parens_in_annotation_type_member_declaration = false; public boolean insert_space_before_opening_brace_in_annotation_type_declaration = true; public boolean insert_space_after_at_in_annotation = false; public BracePosition brace_position_for_annotation_type_declaration = BracePosition.SAME_LINE; public boolean insert_space_before_comma_in_annotation = false; public boolean insert_space_after_at_in_annotation_type_declaration = false; public boolean insert_space_before_opening_paren_in_annotation = false; public boolean insert_space_before_at_in_annotation_type_declaration = true; public boolean insert_new_line_after_type_annotation = false; public WrappingStyle alignment_for_arguments_in_annotation = WrappingStyle.DO_NOT_WRAP; public boolean indent_body_declarations_compare_to_annotation_declaration_header = true; public boolean clear_blank_lines_in_block_comment = true; public boolean insert_new_line_before_root_tags = true; public boolean insert_new_line_for_parameter = false; public boolean indent_parameter_description = false; public boolean indent_root_tags = true; public int comment_line_length = 80; public boolean new_lines_at_javadoc_boundaries = true; /** * Loads the formatter settings from the given formatter config. */ public void loadFrom(FormatterConfig conf) { for (Field fld : FormatterConfig.class.getFields()) { if (fld.getType().getName().startsWith("org.spongepowered.despector.")) { Object insn; try { insn = fld.get(conf); } catch (IllegalArgumentException | IllegalAccessException e) { continue; } for (Field f : fld.getType().getFields()) { Field c = null; try { c = EmitterFormat.class.getField(f.getName()); } catch (NoSuchFieldException | SecurityException e) { continue; } if (c != null) { try { c.set(this, f.get(insn)); } catch (IllegalArgumentException | IllegalAccessException e) { e.printStackTrace(); } } } continue; } Field c = null; try { c = EmitterFormat.class.getField(fld.getName()); } catch (NoSuchFieldException | SecurityException e) { continue; } if (c != null) { try { c.set(this, fld.get(conf)); } catch (IllegalArgumentException | IllegalAccessException e) { e.printStackTrace(); } } } } /** * An enumeration of various wrapping styles. */ public static enum WrappingStyle { DO_NOT_WRAP, WRAP_WHEN_NEEDED, WRAP_FIRST_OR_NEEDED, WRAP_ALL, WRAP_ALL_AND_INDENT, WRAP_ALL_EXCEPT_FIRST; } /** * An enumeration of various brace positions. */ public static enum BracePosition { SAME_LINE, NEXT_LINE, NEXT_LINE_SHIFTED, NEXT_LINE_ON_WRAP; } private static final EmitterFormat default_format = new EmitterFormat(); public static EmitterFormat defaults() { return default_format; } static { default_format.import_order.add("/#"); default_format.import_order.add(""); default_format.import_order.add("java"); default_format.import_order.add("javax"); } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/decompiler/JarWalker.java
src/main/java/org/spongepowered/despector/decompiler/JarWalker.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.decompiler; import org.spongepowered.despector.ast.SourceSet; import java.io.BufferedInputStream; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.jar.JarInputStream; import java.util.zip.ZipEntry; /** * Walks a jar file to produce an ast. Steps such as associating overriding * methods and finding string constants are also during this traversal. */ public class JarWalker { private final Path jar; /** * Creates a new jar walker. */ public JarWalker(Path jar) { this.jar = jar; } /** * Produces a new obfuscated source set for this version. */ public void walk(SourceSet sources, Decompiler decomp) { scanJar(this.jar, sources, decomp); } private void scanJar(Path path, SourceSet src, Decompiler decomp) { try (JarInputStream jar = new JarInputStream(new BufferedInputStream(Files.newInputStream(path)))) { ZipEntry entry = jar.getNextEntry(); if (entry == null) { return; } do { if (entry.isDirectory()) { continue; } final String name = entry.getName(); if (!name.endsWith(".class")) { continue; } scanClassFile(jar, src, decomp); } while ((entry = jar.getNextEntry()) != null); } catch (IOException e) { e.printStackTrace(); return; } } private void scanClassFile(JarInputStream input, SourceSet src, Decompiler decomp) throws IOException { decomp.decompile(input, src); } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/decompiler/Decompilers.java
src/main/java/org/spongepowered/despector/decompiler/Decompilers.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.decompiler; import org.spongepowered.despector.Language; import org.spongepowered.despector.decompiler.kotlin.method.graph.create.ElvisGraphProducerStep; import org.spongepowered.despector.decompiler.kotlin.method.graph.operate.KotlinTernaryPrePassOperation; import org.spongepowered.despector.decompiler.kotlin.method.postprocess.KotlinLocalsMutabilityPostProcess; import org.spongepowered.despector.decompiler.kotlin.method.special.KotlinLocalsProcessor; import org.spongepowered.despector.decompiler.method.MethodDecompiler; import org.spongepowered.despector.decompiler.method.graph.create.JumpGraphProducerStep; import org.spongepowered.despector.decompiler.method.graph.create.SwitchGraphProducerStep; import org.spongepowered.despector.decompiler.method.graph.create.TryCatchGraphProducerStep; import org.spongepowered.despector.decompiler.method.graph.operate.BlockTargetOperation; import org.spongepowered.despector.decompiler.method.graph.operate.BreakPrePassOperation; import org.spongepowered.despector.decompiler.method.graph.operate.JumpSeparateOperation; import org.spongepowered.despector.decompiler.method.graph.operate.TernaryPrePassOperation; import org.spongepowered.despector.decompiler.method.graph.process.InternalBlockProcessor; import org.spongepowered.despector.decompiler.method.graph.process.SubRegionBlockProcessor; import org.spongepowered.despector.decompiler.method.graph.process.SwitchBlockProcessor; import org.spongepowered.despector.decompiler.method.graph.process.TryCatchBlockProcessor; import org.spongepowered.despector.decompiler.method.graph.region.ChildRegionProcessor; import org.spongepowered.despector.decompiler.method.graph.region.DoWhileRegionProcessor; import org.spongepowered.despector.decompiler.method.graph.region.IfBlockRegionProcessor; import org.spongepowered.despector.decompiler.method.graph.region.WhileRegionProcessor; import org.spongepowered.despector.decompiler.method.postprocess.ForEachPostProcessor; import org.spongepowered.despector.decompiler.method.postprocess.ForFromWhilePostProcessor; import org.spongepowered.despector.decompiler.method.postprocess.IfCombiningPostProcessor; import org.spongepowered.despector.decompiler.method.special.LocalsProcessor; import java.util.EnumMap; /** * Standard decompilers. */ public final class Decompilers { public static final BaseDecompiler JAVA = new BaseDecompiler(Language.JAVA); public static final BaseDecompiler KOTLIN = new BaseDecompiler(Language.KOTLIN); public static final BaseDecompiler WILD = new BaseDecompiler(Language.ANY); public static final MethodDecompiler JAVA_METHOD = new MethodDecompiler(); public static final MethodDecompiler KOTLIN_METHOD = new MethodDecompiler(); private static final EnumMap<Language, Decompiler> DECOMPILERS = new EnumMap<>(Language.class); static { JAVA_METHOD.addGraphProducer(new JumpGraphProducerStep()); JAVA_METHOD.addGraphProducer(new SwitchGraphProducerStep()); JAVA_METHOD.addGraphProducer(new TryCatchGraphProducerStep()); JAVA_METHOD.addCleanupOperation(new JumpSeparateOperation()); JAVA_METHOD.addCleanupOperation(new BlockTargetOperation()); JAVA_METHOD.addCleanupOperation(new TernaryPrePassOperation()); JAVA_METHOD.addCleanupOperation(new BreakPrePassOperation()); JAVA_METHOD.addProcessor(new TryCatchBlockProcessor()); JAVA_METHOD.addProcessor(new InternalBlockProcessor()); JAVA_METHOD.addProcessor(new SwitchBlockProcessor()); JAVA_METHOD.addProcessor(new SubRegionBlockProcessor()); JAVA_METHOD.addRegionProcessor(new ChildRegionProcessor()); JAVA_METHOD.addRegionProcessor(new DoWhileRegionProcessor()); JAVA_METHOD.addRegionProcessor(new WhileRegionProcessor()); JAVA_METHOD.addRegionProcessor(new IfBlockRegionProcessor()); JAVA_METHOD.addPostProcessor(new IfCombiningPostProcessor()); JAVA_METHOD.addPostProcessor(new ForFromWhilePostProcessor()); JAVA_METHOD.addPostProcessor(new ForEachPostProcessor()); KOTLIN_METHOD.addGraphProducer(new JumpGraphProducerStep()); KOTLIN_METHOD.addGraphProducer(new SwitchGraphProducerStep()); KOTLIN_METHOD.addGraphProducer(new TryCatchGraphProducerStep()); KOTLIN_METHOD.addGraphProducer(new ElvisGraphProducerStep()); KOTLIN_METHOD.addCleanupOperation(new JumpSeparateOperation()); KOTLIN_METHOD.addCleanupOperation(new BlockTargetOperation()); KOTLIN_METHOD.addCleanupOperation(new BreakPrePassOperation()); KOTLIN_METHOD.addCleanupOperation(new KotlinTernaryPrePassOperation()); KOTLIN_METHOD.addProcessor(new TryCatchBlockProcessor()); KOTLIN_METHOD.addProcessor(new InternalBlockProcessor()); KOTLIN_METHOD.addProcessor(new SwitchBlockProcessor()); KOTLIN_METHOD.addProcessor(new SubRegionBlockProcessor()); KOTLIN_METHOD.addRegionProcessor(new ChildRegionProcessor()); KOTLIN_METHOD.addRegionProcessor(new DoWhileRegionProcessor()); KOTLIN_METHOD.addRegionProcessor(new WhileRegionProcessor()); KOTLIN_METHOD.addRegionProcessor(new IfBlockRegionProcessor()); KOTLIN_METHOD.addPostProcessor(new IfCombiningPostProcessor()); KOTLIN_METHOD.addPostProcessor(new ForEachPostProcessor()); KOTLIN_METHOD.addPostProcessor(new KotlinLocalsMutabilityPostProcess()); KOTLIN_METHOD.setSpecialProcessor(LocalsProcessor.class, new KotlinLocalsProcessor()); DECOMPILERS.put(Language.JAVA, JAVA); DECOMPILERS.put(Language.KOTLIN, KOTLIN); DECOMPILERS.put(Language.ANY, WILD); } /** * Gets the decompiler for the given language. */ public static Decompiler get(Language lang) { return DECOMPILERS.get(lang); } private Decompilers() { } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/decompiler/package-info.java
src/main/java/org/spongepowered/despector/decompiler/package-info.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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. */ @org.spongepowered.despector.util.NonnullByDefault package org.spongepowered.despector.decompiler;
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/decompiler/Decompiler.java
src/main/java/org/spongepowered/despector/decompiler/Decompiler.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.decompiler; import org.spongepowered.despector.ast.SourceSet; import org.spongepowered.despector.ast.type.TypeEntry; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.file.Path; /** * A decompiler. */ public interface Decompiler { /** * Gets if the given file name is valid for this decompiler. */ boolean isValid(String name); /** * Decompiles the class file at the given path. */ default TypeEntry decompile(Path cls_path, SourceSet source) throws IOException { return decompile(new FileInputStream(cls_path.toFile()), source); } /** * Decompiles the class file at the given file. */ default TypeEntry decompile(File cls_path, SourceSet source) throws IOException { return decompile(new FileInputStream(cls_path), source); } /** * Decompiles the class file in the given input stream. */ TypeEntry decompile(InputStream cls_path, SourceSet source) throws IOException; }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/decompiler/BaseDecompiler.java
src/main/java/org/spongepowered/despector/decompiler/BaseDecompiler.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.decompiler; import org.spongepowered.despector.Language; import org.spongepowered.despector.ast.AccessModifier; import org.spongepowered.despector.ast.Annotation; import org.spongepowered.despector.ast.Annotation.EnumConstant; import org.spongepowered.despector.ast.AnnotationType; import org.spongepowered.despector.ast.Locals; import org.spongepowered.despector.ast.Locals.Local; import org.spongepowered.despector.ast.SourceSet; import org.spongepowered.despector.ast.generic.ClassSignature; import org.spongepowered.despector.ast.generic.ClassTypeSignature; import org.spongepowered.despector.ast.generic.GenericClassTypeSignature; import org.spongepowered.despector.ast.generic.MethodSignature; import org.spongepowered.despector.ast.generic.TypeSignature; import org.spongepowered.despector.ast.type.AnnotationEntry; import org.spongepowered.despector.ast.type.ClassEntry; import org.spongepowered.despector.ast.type.EnumEntry; import org.spongepowered.despector.ast.type.FieldEntry; import org.spongepowered.despector.ast.type.InterfaceEntry; import org.spongepowered.despector.ast.type.MethodEntry; import org.spongepowered.despector.ast.type.TypeEntry; import org.spongepowered.despector.config.LibraryConfiguration; import org.spongepowered.despector.decompiler.error.SourceFormatException; import org.spongepowered.despector.decompiler.loader.BytecodeTranslator; import org.spongepowered.despector.decompiler.loader.ClassConstantPool; import org.spongepowered.despector.decompiler.loader.ClassConstantPool.Entry; import org.spongepowered.despector.decompiler.loader.ClassConstantPool.MethodHandleEntry; import org.spongepowered.despector.decompiler.method.PartialMethod.TryCatchRegion; import org.spongepowered.despector.parallel.MethodDecompileTask; import org.spongepowered.despector.parallel.Scheduler; import org.spongepowered.despector.parallel.Timing; import org.spongepowered.despector.util.SignatureParser; import org.spongepowered.despector.util.TypeHelper; import java.io.DataInputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * A language decompiler. */ public class BaseDecompiler implements Decompiler { public static final boolean DUMP_IR_ON_LOAD = Boolean.getBoolean("despector.debug.dump_ir"); public static final int ACC_PUBLIC = 0x0001; public static final int ACC_PRIVATE = 0x0002; public static final int ACC_PROTECTED = 0x0004; public static final int ACC_STATIC = 0x0008; public static final int ACC_FINAL = 0x0010; public static final int ACC_SUPER = 0x0020; public static final int ACC_SYNCHRONIZED = 0x0020; public static final int ACC_VOLATILE = 0x0040; public static final int ACC_BRIDGE = 0x0040; public static final int ACC_TRANSIENT = 0x0080; public static final int ACC_VARARGS = 0x0080; public static final int ACC_NATIVE = 0x0100; public static final int ACC_INTERFACE = 0x0200; public static final int ACC_ABSTRACT = 0x0400; public static final int ACC_STRICT = 0x0800; public static final int ACC_SYNTHETIC = 0x1000; public static final int ACC_ANNOTATION = 0x2000; public static final int ACC_ENUM = 0x4000; private final BytecodeTranslator bytecode = new BytecodeTranslator(); private final Language lang; private Scheduler<MethodDecompileTask> scheduler; public BaseDecompiler(Language lang) { this.lang = lang; if (LibraryConfiguration.parallel) { this.scheduler = new Scheduler<>(Runtime.getRuntime().availableProcessors()); } } @Override public boolean isValid(String name) { return name.endsWith(".class"); } @Override public TypeEntry decompile(InputStream input, SourceSet set) throws IOException { DataInputStream data = (input instanceof DataInputStream) ? (DataInputStream) input : new DataInputStream(input); long decompile_start = System.nanoTime(); int magic = data.readInt(); if (magic != 0xCAFEBABE) { throw new SourceFormatException("Not a java class file"); } /* short minor = */ data.readShort(); /* short major = */ data.readShort(); // TODO check versions and adapt loading to support a range of versions ClassConstantPool pool = new ClassConstantPool(); pool.load(data); int access_flags = data.readUnsignedShort(); String name = pool.getClass(data.readUnsignedShort()).name; if (!LibraryConfiguration.quiet) { System.out.println("Decompiling class " + name); } int super_index = data.readUnsignedShort(); String supername = super_index != 0 ? "L" + pool.getClass(super_index).name + ";" : "Ljava/lang/Object;"; int interfaces_count = data.readUnsignedShort(); List<String> interfaces = new ArrayList<>(interfaces_count); for (int i = 0; i < interfaces_count; i++) { interfaces.add(pool.getClass(data.readUnsignedShort()).name); } Language actual_lang = Language.JAVA; if (LibraryConfiguration.force_lang && this.lang != Language.ANY) { actual_lang = this.lang; } TypeEntry entry = null; if ((access_flags & ACC_ANNOTATION) != 0) { entry = new AnnotationEntry(set, actual_lang, name); } else if ((access_flags & ACC_INTERFACE) != 0) { entry = new InterfaceEntry(set, actual_lang, name); } else if ((access_flags & ACC_ENUM) != 0) { entry = new EnumEntry(set, actual_lang, name); } else { entry = new ClassEntry(set, actual_lang, name); ((ClassEntry) entry).setSuperclass(supername); } entry.setAccessModifier(AccessModifier.fromModifiers(access_flags)); entry.setFinal((access_flags & ACC_FINAL) != 0); entry.setSynthetic((access_flags & ACC_SYNTHETIC) != 0); entry.setAbstract((access_flags & ACC_ABSTRACT) != 0); entry.getInterfaces().addAll(interfaces); int field_count = data.readUnsignedShort(); for (int i = 0; i < field_count; i++) { int field_access = data.readUnsignedShort(); String field_name = pool.getUtf8(data.readUnsignedShort()); if ((field_access & ACC_ENUM) != 0) { ((EnumEntry) entry).addEnumConstant(field_name); } String field_desc = pool.getUtf8(data.readUnsignedShort()); FieldEntry field = new FieldEntry(set); field.setAccessModifier(AccessModifier.fromModifiers(field_access)); field.setFinal((field_access & ACC_FINAL) != 0); field.setName(field_name); field.setOwner(name); field.setStatic((field_access & ACC_STATIC) != 0); field.setSynthetic((field_access & ACC_SYNTHETIC) != 0); field.setVolatile((field_access & ACC_VOLATILE) != 0); field.setTransient((field_access & ACC_TRANSIENT) != 0); field.setName(field_name); field.setType(ClassTypeSignature.of(field_desc)); entry.addField(field); int attribute_count = data.readUnsignedShort(); for (int a = 0; a < attribute_count; a++) { String attribute_name = pool.getUtf8(data.readUnsignedShort()); int length = data.readInt(); if ("ConstantValue".equals(attribute_name)) { /* int constant_value_index = */ data.readUnsignedShort(); } else if ("Synthetic".equals(attribute_name)) { field.setSynthetic(true); } else if ("Signature".equals(attribute_name)) { field.setType(SignatureParser.parseFieldTypeSignature(pool.getUtf8(data.readUnsignedShort()))); } else if ("Deprecated".equals(attribute_name)) { field.setDeprecated(true); } else if ("RuntimeVisibleAnnotations".equals(attribute_name)) { int annotation_count = data.readUnsignedShort(); for (int j = 0; j < annotation_count; j++) { Annotation anno = readAnnotation(data, pool, set); field.addAnnotation(anno); anno.getType().setRuntimeVisible(true); } } else if ("RuntimeInvisibleAnnotations".equals(attribute_name)) { int annotation_count = data.readUnsignedShort(); for (int j = 0; j < annotation_count; j++) { Annotation anno = readAnnotation(data, pool, set); field.addAnnotation(anno); anno.getType().setRuntimeVisible(false); } } else { System.err.println("Skipping unknown field attribute: " + attribute_name); data.skipBytes(length); } } } List<UnfinishedMethod> unfinished_methods = new ArrayList<>(); int method_count = data.readUnsignedShort(); for (int i = 0; i < method_count; i++) { int method_access = data.readUnsignedShort(); String method_name = pool.getUtf8(data.readUnsignedShort()); String method_desc = pool.getUtf8(data.readUnsignedShort()); List<TypeSignature> param_types = new ArrayList<>(); for (String t : TypeHelper.splitSig(method_desc)) { param_types.add(ClassTypeSignature.of(t)); } MethodEntry method = new MethodEntry(set); method.setName(method_name); method.setDescription(method_desc); method.setOwner(name); method.setAbstract((method_access & ACC_ABSTRACT) != 0); method.setAccessModifier(AccessModifier.fromModifiers(method_access)); method.setFinal((method_access & ACC_FINAL) != 0); method.setStatic((method_access & ACC_STATIC) != 0); method.setSynthetic((method_access & ACC_SYNTHETIC) != 0); method.setBridge((method_access & ACC_BRIDGE) != 0); method.setSynchronized((method_access & ACC_SYNCHRONIZED) != 0); method.setNative((method_access & ACC_NATIVE) != 0); method.setVarargs((method_access & ACC_VARARGS) != 0); method.setStrictFp((method_access & ACC_STRICT) != 0); entry.addMethod(method); Locals locals = new Locals(method); method.setLocals(locals); List<String> checked_exceptions = null; UnfinishedMethod unfinished = new UnfinishedMethod(); unfinished_methods.add(unfinished); unfinished.mth = method; String method_sig = null; int attribute_count = data.readUnsignedShort(); for (int a = 0; a < attribute_count; a++) { String attribute_name = pool.getUtf8(data.readUnsignedShort()); int length = data.readInt(); if ("Code".equals(attribute_name)) { /* int max_stack = */ data.readUnsignedShort(); /* int max_locals = */ data.readUnsignedShort(); int code_length = data.readInt(); byte[] code = new byte[code_length]; int offs = 0; while (code_length > 0) { int len = data.read(code, offs, code_length); code_length -= len; offs += len; } List<TryCatchRegion> catch_regions = new ArrayList<>(); int exception_table_length = data.readUnsignedShort(); for (int j = 0; j < exception_table_length; j++) { int start_pc = data.readUnsignedShort(); int end_pc = data.readUnsignedShort(); int catch_pc = data.readUnsignedShort(); int ex_index = data.readUnsignedShort(); String ex = ex_index != 0 ? pool.getClass(ex_index).name : ""; catch_regions.add(new TryCatchRegion(start_pc, end_pc, catch_pc, ex)); } unfinished.code = code; unfinished.catch_regions = catch_regions; int code_attribute_count = data.readUnsignedShort(); for (int ca = 0; ca < code_attribute_count; ca++) { String code_attribute_name = pool.getUtf8(data.readUnsignedShort()); int clength = data.readInt(); if ("LocalVariableTable".equals(code_attribute_name)) { int lvt_length = data.readUnsignedShort(); for (int j = 0; j < lvt_length; j++) { int start_pc = data.readUnsignedShort(); int local_length = data.readUnsignedShort(); String local_name = pool.getUtf8(data.readUnsignedShort()); String local_desc = pool.getUtf8(data.readUnsignedShort()); int index = data.readUnsignedShort(); Local loc = locals.getLocal(index); loc.addLVT(start_pc, local_length, local_name, local_desc); } } else if ("LineNumberTable".equals(code_attribute_name)) { data.skipBytes(clength); } else if ("LocalVariableTypeTable".equals(code_attribute_name)) { int lvt_length = data.readUnsignedShort(); for (int j = 0; j < lvt_length; j++) { int start_pc = data.readUnsignedShort(); /* int local_length = */ data.readUnsignedShort(); /* String local_name = */ pool.getUtf8(data.readUnsignedShort()); String local_signature = pool.getUtf8(data.readUnsignedShort()); int index = data.readUnsignedShort(); Local loc = locals.getLocal(index); loc.getLVT(start_pc).setSignature(local_signature); } } else if ("StackMapTable".equals(code_attribute_name)) { data.skipBytes(clength); } else { System.err.println("Skipping unknown code attribute: " + code_attribute_name); data.skipBytes(clength); } } } else if ("Exceptions".equals(attribute_name)) { checked_exceptions = new ArrayList<>(); int exception_count = data.readUnsignedShort(); for (int j = 0; j < exception_count; j++) { checked_exceptions.add(pool.getClass(data.readUnsignedShort()).name); } } else if ("Synthetic".equals(attribute_name)) { method.setSynthetic(true); } else if ("Signature".equals(attribute_name)) { method_sig = pool.getUtf8(data.readUnsignedShort()); } else if ("Deprecated".equals(attribute_name)) { method.setDeprecated(true); } else if ("RuntimeVisibleAnnotations".equals(attribute_name)) { int annotation_count = data.readUnsignedShort(); for (int j = 0; j < annotation_count; j++) { Annotation anno = readAnnotation(data, pool, set); method.addAnnotation(anno); anno.getType().setRuntimeVisible(true); } } else if ("RuntimeInvisibleAnnotations".equals(attribute_name)) { int annotation_count = data.readUnsignedShort(); for (int j = 0; j < annotation_count; j++) { Annotation anno = readAnnotation(data, pool, set); method.addAnnotation(anno); anno.getType().setRuntimeVisible(false); } } else if ("RuntimeVisibleParameterAnnotations".equals(attribute_name)) { if (unfinished.parameter_annotations == null) { unfinished.parameter_annotations = new HashMap<>(); } int num_params = data.readUnsignedByte(); int offs = method.isStatic() ? 0 : 1; for (int k = offs; k < num_params + offs; k++) { List<Annotation> annos = unfinished.parameter_annotations.get(k); if (annos == null) { annos = new ArrayList<>(); unfinished.parameter_annotations.put(k, annos); } int annotation_count = data.readUnsignedShort(); for (int j = 0; j < annotation_count; j++) { Annotation anno = readAnnotation(data, pool, set); annos.add(anno); anno.getType().setRuntimeVisible(true); } } } else if ("RuntimeInvisibleParameterAnnotations".equals(attribute_name)) { if (unfinished.parameter_annotations == null) { unfinished.parameter_annotations = new HashMap<>(); } int num_params = data.readUnsignedByte(); int offs = method.isStatic() ? 0 : 1; for (int k = offs; k < num_params + offs; k++) { List<Annotation> annos = unfinished.parameter_annotations.get(k); if (annos == null) { annos = new ArrayList<>(); unfinished.parameter_annotations.put(k, annos); } int annotation_count = data.readUnsignedShort(); for (int j = 0; j < annotation_count; j++) { Annotation anno = readAnnotation(data, pool, set); annos.add(anno); anno.getType().setRuntimeVisible(false); } } } else if ("AnnotationDefault".equals(attribute_name)) { Object val = readElementValue(data, pool, set); method.setAnnotationValue(val); } else { System.err.println("Skipping unknown method attribute: " + attribute_name); data.skipBytes(length); } } if (method_sig != null) { method.setMethodSignature(SignatureParser.parseMethod(method_sig)); } else { MethodSignature sig = SignatureParser.parseMethod(method_desc); method.setMethodSignature(sig); if (checked_exceptions != null && !checked_exceptions.isEmpty()) { for (String ex : checked_exceptions) { sig.getThrowsSignature().add(ClassTypeSignature.of("L" + ex + ";")); } } } } List<BootstrapMethod> bootstrap_methods = new ArrayList<>(); int class_attribute_count = data.readUnsignedShort(); for (int i = 0; i < class_attribute_count; i++) { String attribute_name = pool.getUtf8(data.readUnsignedShort()); int length = data.readInt(); if ("InnerClasses".equals(attribute_name)) { int number_of_classes = data.readUnsignedShort(); for (int j = 0; j < number_of_classes; j++) { String inner_cls = pool.getClass(data.readUnsignedShort()).name; int outer_index = data.readUnsignedShort(); String outer_cls = outer_index == 0 ? null : pool.getClass(outer_index).name; int name_index = data.readUnsignedShort(); String inner_name = name_index == 0 ? null : pool.getUtf8(name_index); int acc = data.readUnsignedShort(); entry.addInnerClass(inner_cls, inner_name, outer_cls, acc); } } else if ("EnclosingMethod".equals(attribute_name)) { data.skipBytes(length); } else if ("Synthetic".equals(attribute_name)) { entry.setSynthetic(true); } else if ("Signature".equals(attribute_name)) { entry.setSignature(SignatureParser.parse(pool.getUtf8(data.readUnsignedShort()))); } else if ("SourceFile".equals(attribute_name)) { data.skipBytes(length); } else if ("SourceDebugExtension".equals(attribute_name)) { data.skipBytes(length); } else if ("Deprecated".equals(attribute_name)) { entry.setDeprecated(true); } else if ("RuntimeVisibleAnnotations".equals(attribute_name)) { int annotation_count = data.readUnsignedShort(); for (int j = 0; j < annotation_count; j++) { Annotation anno = readAnnotation(data, pool, set); if (this.lang == Language.ANY && anno.getType().getName().startsWith("kotlin")) { actual_lang = Language.KOTLIN; } entry.addAnnotation(anno); anno.getType().setRuntimeVisible(true); } } else if ("RuntimeInvisibleAnnotations".equals(attribute_name)) { int annotation_count = data.readUnsignedShort(); for (int j = 0; j < annotation_count; j++) { Annotation anno = readAnnotation(data, pool, set); entry.addAnnotation(anno); anno.getType().setRuntimeVisible(false); } } else if ("BootstrapMethods".equals(attribute_name)) { int bsm_count = data.readUnsignedShort(); for (int j = 0; j < bsm_count; j++) { BootstrapMethod bsm = new BootstrapMethod(); bootstrap_methods.add(bsm); bsm.handle = pool.getMethodHandle(data.readUnsignedShort()); int arg_count = data.readUnsignedShort(); bsm.arguments = new Entry[arg_count]; for (int k = 0; k < arg_count; k++) { bsm.arguments[k] = pool.getEntry(data.readUnsignedShort()); } } } else { System.err.println("Skipping unknown class attribute: " + attribute_name); data.skipBytes(length); } } if (entry.getSignature() == null) { ClassSignature sig = new ClassSignature(); sig.setSuperclassSignature(new GenericClassTypeSignature(supername)); for (String intr : entry.getInterfaces()) { sig.getInterfaceSignatures().add(new GenericClassTypeSignature("L" + intr + ";")); } entry.setSignature(sig); } long classloading_time = System.nanoTime() - decompile_start; Timing.time_loading_classes += classloading_time; if (!LibraryConfiguration.force_lang) { entry.setLanguage(actual_lang); } MethodDecompileTask task = new MethodDecompileTask(entry, pool, unfinished_methods, this.bytecode, bootstrap_methods); if (LibraryConfiguration.parallel) { this.scheduler.add(task); } else { long method_decompile_start = System.nanoTime(); task.run(); set.add(entry); long method_decompile_time = System.nanoTime() - method_decompile_start; Timing.time_decompiling_methods += method_decompile_time; } long decompile_time = System.nanoTime() - decompile_start; Timing.time_decompiling += decompile_time; return entry; } public void flushTasks() { if (LibraryConfiguration.parallel) { long start = System.nanoTime(); this.scheduler.execute(); for (MethodDecompileTask task : this.scheduler.getTasks()) { task.getEntry().getSource().add(task.getEntry()); } long method_decompile_time = System.nanoTime() - start; Timing.time_decompiling_methods += method_decompile_time; this.scheduler.reset(); } } private Annotation readAnnotation(DataInputStream data, ClassConstantPool pool, SourceSet set) throws IOException { String anno_type_name = pool.getUtf8(data.readUnsignedShort()); AnnotationType anno_type = set.getAnnotationType(TypeHelper.descToType(anno_type_name)); Annotation anno = new Annotation(anno_type); int value_paris = data.readUnsignedShort(); for (int k = 0; k < value_paris; k++) { String element_name = pool.getUtf8(data.readUnsignedShort()); anno.setValue(element_name, readElementValue(data, pool, set)); } return anno; } private Object readElementValue(DataInputStream data, ClassConstantPool pool, SourceSet set) throws IOException { char element_type_tag = (char) data.readUnsignedByte(); if (element_type_tag == 's') { String value = pool.getUtf8(data.readUnsignedShort()); return value; } else if (element_type_tag == 'B') { int value = pool.getInt(data.readUnsignedShort()); return Byte.valueOf((byte) value); } else if (element_type_tag == 'S') { int value = pool.getInt(data.readUnsignedShort()); return Short.valueOf((short) value); } else if (element_type_tag == 'C') { int value = pool.getInt(data.readUnsignedShort()); return Character.valueOf((char) value); } else if (element_type_tag == 'I') { int value = pool.getInt(data.readUnsignedShort()); return Integer.valueOf(value); } else if (element_type_tag == 'F') { float value = pool.getFloat(data.readUnsignedShort()); return Float.valueOf(value); } else if (element_type_tag == 'J') { long value = pool.getLong(data.readUnsignedShort()); return Long.valueOf(value); } else if (element_type_tag == 'D') { double value = pool.getDouble(data.readUnsignedShort()); return Double.valueOf(value); } else if (element_type_tag == 'Z') { int value = pool.getInt(data.readUnsignedShort()); return Boolean.valueOf(value != 0); } else if (element_type_tag == 'c') { String value = pool.getUtf8(data.readUnsignedShort()); return ClassTypeSignature.of(value); } else if (element_type_tag == '@') { Annotation value = readAnnotation(data, pool, set); return value; } else if (element_type_tag == 'e') { String enum_type = pool.getUtf8(data.readUnsignedShort()); String enum_cst = pool.getUtf8(data.readUnsignedShort()); EnumConstant value = new EnumConstant(enum_type, enum_cst); return value; } else if (element_type_tag == '[') { List<Object> value = new ArrayList<>(); int num_values = data.readUnsignedShort(); for (int i = 0; i < num_values; i++) { value.add(readElementValue(data, pool, set)); } return value; } throw new IllegalStateException(); } public static class BootstrapMethod { public MethodHandleEntry handle; public Entry[] arguments; } public static class UnfinishedMethod { public MethodEntry mth; public byte[] code; public List<TryCatchRegion> catch_regions; public Map<Integer, List<Annotation>> parameter_annotations; UnfinishedMethod() { } } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/decompiler/DirectoryWalker.java
src/main/java/org/spongepowered/despector/decompiler/DirectoryWalker.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.decompiler; import org.spongepowered.despector.ast.SourceSet; import java.io.File; import java.io.IOException; import java.nio.file.Path; /** * A directory walker which walks a directory and visits all child files and * directories. */ public class DirectoryWalker { private final Path directory; public DirectoryWalker(Path dir) { this.directory = dir; } /** * Walks this directory and visits all class files in it or any child * directory and loads them into the given {@link SourceSet}. */ public void walk(SourceSet src, Decompiler decomp) throws IOException { File dir = this.directory.toFile(); visit(dir, src, decomp); } private void visit(File file, SourceSet src, Decompiler decomp) throws IOException { if (file.isDirectory()) { for (File f : file.listFiles()) { visit(f, src, decomp); } } else { if (decomp.isValid(file.getName())) { decomp.decompile(file, src); } else if (file.getName().endsWith(".jar")) { JarWalker walker = new JarWalker(file.toPath()); walker.walk(src, decomp); } } } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/decompiler/loader/ClassConstantPool.java
src/main/java/org/spongepowered/despector/decompiler/loader/ClassConstantPool.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.decompiler.loader; import com.google.common.base.Charsets; import org.spongepowered.despector.decompiler.error.SourceFormatException; import java.io.DataInputStream; import java.io.IOException; public class ClassConstantPool { private static final boolean DUMP_CONSTANT_POOL = Boolean.getBoolean("despect.debug.jvm.dump_constant_pool"); private Entry[] values; public ClassConstantPool() { } public void load(DataInputStream data) throws IOException { int entry_count = data.readUnsignedShort(); this.values = new Entry[entry_count - 1]; for (int i = 0; i < entry_count - 1; i++) { int tag = data.readUnsignedByte(); EntryType type = EntryType.values()[tag]; switch (type) { case UTF8: { Utf8Entry u = new Utf8Entry(); int len = data.readUnsignedShort(); byte[] bytes = new byte[len]; int offs = 0; while (len > 0) { int read = data.read(bytes, offs, len); len -= read; offs += read; } u.value = new String(bytes, Charsets.UTF_8); this.values[i] = u; if (DUMP_CONSTANT_POOL) { System.out.println(i + ": Utf8 " + u.value); } break; } case INTEGER: { IntEntry c = new IntEntry(); c.value = data.readInt(); this.values[i] = c; if (DUMP_CONSTANT_POOL) { System.out.println(i + ": Int " + c.value); } break; } case FLOAT: { FloatEntry c = new FloatEntry(); c.value = data.readFloat(); this.values[i] = c; if (DUMP_CONSTANT_POOL) { System.out.println(i + ": Float " + c.value); } break; } case LONG: { LongEntry c = new LongEntry(); long l = ((long) data.readInt() << 32); l |= data.readInt(); c.value = l; this.values[i] = c; if (DUMP_CONSTANT_POOL) { System.out.println(i + ": Long " + c.value); } break; } case DOUBLE: { DoubleEntry c = new DoubleEntry(); long l = ((long) data.readInt() << 32); l |= data.readInt(); c.value = Double.longBitsToDouble(l); this.values[i] = c; if (DUMP_CONSTANT_POOL) { System.out.println(i + ": Double " + c.value); } break; } case CLASS: { ClassEntry e = new ClassEntry(); e.name_index = data.readUnsignedShort(); this.values[i] = e; if (DUMP_CONSTANT_POOL) { System.out.println(i + ": Class " + e.name_index); } break; } case STRING: { StringEntry e = new StringEntry(); e.value_index = data.readUnsignedShort(); this.values[i] = e; if (DUMP_CONSTANT_POOL) { System.out.println(i + ": String " + e.value_index); } break; } case FIELD_REF: { FieldRefEntry f = new FieldRefEntry(); f.class_index = data.readUnsignedShort(); f.name_and_type_index = data.readUnsignedShort(); this.values[i] = f; if (DUMP_CONSTANT_POOL) { System.out.println(i + ": FieldRef " + f.class_index + " " + f.name_and_type_index); } break; } case METHOD_REF: { MethodRefEntry f = new MethodRefEntry(); f.class_index = data.readUnsignedShort(); f.name_and_type_index = data.readUnsignedShort(); this.values[i] = f; if (DUMP_CONSTANT_POOL) { System.out.println(i + ": MethodRef " + f.class_index + " " + f.name_and_type_index); } break; } case INTERFACE_METHOD_REF: { MethodRefEntry f = new MethodRefEntry(); f.class_index = data.readUnsignedShort(); f.name_and_type_index = data.readUnsignedShort(); this.values[i] = f; if (DUMP_CONSTANT_POOL) { System.out.println(i + ": InterfaceMethodRef " + f.class_index + " " + f.name_and_type_index); } break; } case NAME_AND_TYPE: { NameAndTypeEntry n = new NameAndTypeEntry(); n.name_index = data.readUnsignedShort(); n.type_index = data.readUnsignedShort(); this.values[i] = n; if (DUMP_CONSTANT_POOL) { System.out.println(i + ": NameAndType " + n.name_index + " " + n.type_index); } break; } case METHOD_HANDLE: { MethodHandleEntry h = new MethodHandleEntry(); h.kind = data.readByte(); h.reference_index = data.readUnsignedShort(); this.values[i] = h; if (DUMP_CONSTANT_POOL) { System.out.println(i + ": MethodHandle " + h.kind + " " + h.reference_index); } break; } case METHOD_TYPE: { MethodTypeEntry t = new MethodTypeEntry(); t.desc_index = data.readUnsignedShort(); this.values[i] = t; if (DUMP_CONSTANT_POOL) { System.out.println(i + ": MethodType " + t.desc_index); } break; } case INVOKE_DYNAMIC: { InvokeDynamicEntry d = new InvokeDynamicEntry(); d.bootstrap_index = data.readUnsignedShort(); d.name_and_type_index = data.readUnsignedShort(); this.values[i] = d; if (DUMP_CONSTANT_POOL) { System.out.println(i + ": InvokeDynamic " + d.bootstrap_index + " " + d.name_and_type_index); } break; } default: throw new SourceFormatException("Illegal tag in constant pool"); } this.values[i].type = type; if (type == EntryType.LONG || type == EntryType.DOUBLE) { i++; } } // bake references for (int i = 0; i < entry_count - 1; i++) { Entry e = this.values[i]; if (e == null) { continue; } switch (e.type) { case UTF8: case INTEGER: case FLOAT: break; case LONG: case DOUBLE: i++; break; case CLASS: { ClassEntry c = (ClassEntry) e; c.name = getUtf8(c.name_index); break; } case STRING: { StringEntry c = (StringEntry) e; c.value = getUtf8(c.value_index); break; } case FIELD_REF: { FieldRefEntry f = (FieldRefEntry) e; f.cls = getUtf8(getClass(f.class_index).name_index); f.name = getUtf8(getNameAndType(f.name_and_type_index).name_index); f.type_name = getUtf8(getNameAndType(f.name_and_type_index).type_index); break; } case METHOD_REF: { MethodRefEntry f = (MethodRefEntry) e; f.cls = getUtf8(getClass(f.class_index).name_index); f.name = getUtf8(getNameAndType(f.name_and_type_index).name_index); f.type_name = getUtf8(getNameAndType(f.name_and_type_index).type_index); break; } case INTERFACE_METHOD_REF: { MethodRefEntry f = (MethodRefEntry) e; f.cls = getUtf8(getClass(f.class_index).name_index); f.name = getUtf8(getNameAndType(f.name_and_type_index).name_index); f.type_name = getUtf8(getNameAndType(f.name_and_type_index).type_index); break; } case NAME_AND_TYPE: { NameAndTypeEntry n = (NameAndTypeEntry) e; n.name = getUtf8(n.name_index); n.type_name = getUtf8(n.type_index); break; } case METHOD_HANDLE: break; case METHOD_TYPE: { MethodTypeEntry t = (MethodTypeEntry) e; t.desc = getUtf8(t.desc_index); break; } case INVOKE_DYNAMIC: { InvokeDynamicEntry f = (InvokeDynamicEntry) e; f.name = getUtf8(getNameAndType(f.name_and_type_index).name_index); f.type_name = getUtf8(getNameAndType(f.name_and_type_index).type_index); break; } default: throw new SourceFormatException("Illegal tag in constant pool"); } } } public Entry getEntry(int index) { return this.values[index - 1]; } public String getUtf8(int index) { return ((Utf8Entry) this.values[index - 1]).value; } public int getInt(int index) { return ((IntEntry) this.values[index - 1]).value; } public float getFloat(int index) { return ((FloatEntry) this.values[index - 1]).value; } public long getLong(int index) { return ((LongEntry) this.values[index - 1]).value; } public double getDouble(int index) { return ((DoubleEntry) this.values[index - 1]).value; } public ClassEntry getClass(int index) { return (ClassEntry) this.values[index - 1]; } public NameAndTypeEntry getNameAndType(int index) { return (NameAndTypeEntry) this.values[index - 1]; } public FieldRefEntry getFieldRef(int index) { return (FieldRefEntry) this.values[index - 1]; } public MethodRefEntry getMethodRef(int index) { return (MethodRefEntry) this.values[index - 1]; } public MethodRefEntry getInterfaceMethodRef(int index) { return (MethodRefEntry) this.values[index - 1]; } public MethodHandleEntry getMethodHandle(int index) { return (MethodHandleEntry) this.values[index - 1]; } public InvokeDynamicEntry getInvokeDynamic(int index) { return (InvokeDynamicEntry) this.values[index - 1]; } public static abstract class Entry { public EntryType type; } public static class Utf8Entry extends Entry { public String value; } public static class IntEntry extends Entry { public int value; } public static class FloatEntry extends Entry { public float value; } public static class LongEntry extends Entry { public long value; } public static class DoubleEntry extends Entry { public double value; } public static class StringEntry extends Entry { public int value_index; public String value; } public static class ClassEntry extends Entry { public int name_index; public String name; } public static class NameAndTypeEntry extends Entry { public int name_index; public int type_index; public String name; public String type_name; } public static class FieldRefEntry extends Entry { public int class_index; public int name_and_type_index; public String cls; public String name; public String type_name; } public static class MethodRefEntry extends Entry { public int class_index; public int name_and_type_index; public String cls; public String name; public String type_name; } public static class MethodHandleEntry extends Entry { public byte kind; public int reference_index; } public static class MethodTypeEntry extends Entry { public int desc_index; public String desc; } public static class InvokeDynamicEntry extends Entry { public int bootstrap_index; public int name_and_type_index; public String name; public String type_name; } public static enum EntryType { _0, UTF8, _2, INTEGER, FLOAT, LONG, DOUBLE, CLASS, STRING, FIELD_REF, METHOD_REF, INTERFACE_METHOD_REF, NAME_AND_TYPE, _13, _14, METHOD_HANDLE, METHOD_TYPE, _17, INVOKE_DYNAMIC, } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/decompiler/loader/BytecodeTranslator.java
src/main/java/org/spongepowered/despector/decompiler/loader/BytecodeTranslator.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.decompiler.loader; import org.spongepowered.despector.ast.Locals; import org.spongepowered.despector.ast.generic.ClassTypeSignature; import org.spongepowered.despector.ast.generic.MethodSignature; import org.spongepowered.despector.ast.stmt.invoke.InstanceMethodInvoke; import org.spongepowered.despector.decompiler.BaseDecompiler.BootstrapMethod; import org.spongepowered.despector.decompiler.error.SourceFormatException; import org.spongepowered.despector.decompiler.ir.DoubleInsn; import org.spongepowered.despector.decompiler.ir.FieldInsn; import org.spongepowered.despector.decompiler.ir.FloatInsn; import org.spongepowered.despector.decompiler.ir.Insn; import org.spongepowered.despector.decompiler.ir.InsnBlock; import org.spongepowered.despector.decompiler.ir.IntInsn; import org.spongepowered.despector.decompiler.ir.InvokeDynamicInsn; import org.spongepowered.despector.decompiler.ir.InvokeInsn; import org.spongepowered.despector.decompiler.ir.JumpInsn; import org.spongepowered.despector.decompiler.ir.LdcInsn; import org.spongepowered.despector.decompiler.ir.LongInsn; import org.spongepowered.despector.decompiler.ir.OpInsn; import org.spongepowered.despector.decompiler.ir.SwitchInsn; import org.spongepowered.despector.decompiler.ir.TypeInsn; import org.spongepowered.despector.decompiler.ir.TypeIntInsn; import org.spongepowered.despector.decompiler.ir.VarIntInsn; import org.spongepowered.despector.decompiler.loader.ClassConstantPool.ClassEntry; import org.spongepowered.despector.decompiler.loader.ClassConstantPool.DoubleEntry; import org.spongepowered.despector.decompiler.loader.ClassConstantPool.Entry; import org.spongepowered.despector.decompiler.loader.ClassConstantPool.FieldRefEntry; import org.spongepowered.despector.decompiler.loader.ClassConstantPool.FloatEntry; import org.spongepowered.despector.decompiler.loader.ClassConstantPool.IntEntry; import org.spongepowered.despector.decompiler.loader.ClassConstantPool.InvokeDynamicEntry; import org.spongepowered.despector.decompiler.loader.ClassConstantPool.LongEntry; import org.spongepowered.despector.decompiler.loader.ClassConstantPool.MethodHandleEntry; import org.spongepowered.despector.decompiler.loader.ClassConstantPool.MethodRefEntry; import org.spongepowered.despector.decompiler.loader.ClassConstantPool.StringEntry; import org.spongepowered.despector.decompiler.method.PartialMethod.TryCatchRegion; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class BytecodeTranslator { public BytecodeTranslator() { } public InsnBlock createIR(MethodSignature methodSignature, byte[] code, Locals locals, List<TryCatchRegion> catch_regions, ClassConstantPool pool, List<BootstrapMethod> bootstrap_methods) { InsnBlock block = new InsnBlock(); List<Integer> insn_starts = new ArrayList<>(); for (int i = 0; i < code.length;) { int opcode_index = i; insn_starts.add(opcode_index); int next = code[i++] & 0xFF; switch (next) { case 0: // NOP block.append(new OpInsn(Insn.NOOP)); break; case 1: // ACONST_NULL block.append(new LdcInsn(Insn.PUSH, null)); break; case 2: // ICONST_M1 block.append(new IntInsn(Insn.ICONST, -1)); break; case 3: // ICONST_0 block.append(new IntInsn(Insn.ICONST, 0)); break; case 4: // ICONST_1 block.append(new IntInsn(Insn.ICONST, 1)); break; case 5: // ICONST_2 block.append(new IntInsn(Insn.ICONST, 2)); break; case 6: // ICONST_3 block.append(new IntInsn(Insn.ICONST, 3)); break; case 7: // ICONST_4 block.append(new IntInsn(Insn.ICONST, 4)); break; case 8: // ICONST_5 block.append(new IntInsn(Insn.ICONST, 5)); break; case 9: // LCONST_0 block.append(new LongInsn(Insn.LCONST, 0)); break; case 10: // LCONST_1 block.append(new LongInsn(Insn.LCONST, 1)); break; case 11: // FCONST_0 block.append(new FloatInsn(Insn.FCONST, 0)); break; case 12: // FCONST_1 block.append(new FloatInsn(Insn.FCONST, 1)); break; case 13: // FCONST_2 block.append(new FloatInsn(Insn.FCONST, 2)); break; case 14: // DCONST_0 block.append(new DoubleInsn(Insn.DCONST, 0)); break; case 15: // DCONST_1 block.append(new DoubleInsn(Insn.DCONST, 1)); break; case 16: {// BIPUSH int val = code[i++]; block.append(new IntInsn(Insn.ICONST, val)); break; } case 17: {// SIPUSH short val = (short) (((code[i++] & 0xFF) << 8) | (code[i++] & 0xFF)); block.append(new IntInsn(Insn.ICONST, val)); break; } case 18: {// LDC int index = code[i++] & 0xFF; Entry entry = pool.getEntry(index); if (entry instanceof IntEntry) { block.append(new IntInsn(Insn.ICONST, ((IntEntry) entry).value)); } else if (entry instanceof FloatEntry) { block.append(new FloatInsn(Insn.FCONST, ((FloatEntry) entry).value)); } else if (entry instanceof StringEntry) { block.append(new LdcInsn(Insn.PUSH, ((StringEntry) entry).value)); } else if (entry instanceof ClassEntry) { String type = ((ClassEntry) entry).name; if (!type.startsWith("[")) { type = "L" + type + ";"; } block.append(new LdcInsn(Insn.PUSH, ClassTypeSignature.of(type))); } else { throw new IllegalStateException("Unsupported constant pool entry type in LDC node " + entry.getClass().getSimpleName()); } break; } case 19: {// LDC_W int index = ((code[i++] & 0xFF) << 8) | (code[i++] & 0xFF); Entry entry = pool.getEntry(index); if (entry instanceof IntEntry) { block.append(new IntInsn(Insn.ICONST, ((IntEntry) entry).value)); } else if (entry instanceof FloatEntry) { block.append(new FloatInsn(Insn.FCONST, ((FloatEntry) entry).value)); } else if (entry instanceof StringEntry) { block.append(new LdcInsn(Insn.PUSH, ((StringEntry) entry).value)); } else if (entry instanceof ClassEntry) { block.append(new LdcInsn(Insn.PUSH, ClassTypeSignature.of("L" + ((ClassEntry) entry).name + ";"))); } else { throw new IllegalStateException("Unsupported constant pool entry type in LDC node " + entry.getClass().getSimpleName()); } break; } case 20: {// LDC2_W int index = ((code[i++] & 0xFF) << 8) | (code[i++] & 0xFF); Entry entry = pool.getEntry(index); if (entry instanceof LongEntry) { block.append(new LongInsn(Insn.LCONST, ((LongEntry) entry).value)); } else if (entry instanceof DoubleEntry) { block.append(new DoubleInsn(Insn.DCONST, ((DoubleEntry) entry).value)); } else { throw new IllegalStateException("Unsupported constant pool entry type in LDC node " + entry.getClass().getSimpleName()); } break; } case 21: // ILOAD case 22: // LLOAD case 23: // FLOAD case 24: // DLOAD case 25: { // ALOAD int val = code[i++] & 0xFF; block.append(new IntInsn(Insn.LOCAL_LOAD, val)); break; } case 26: // ILOAD_0 case 30: // LLOAD_0 case 34: // FLOAD_0 case 38: // DLOAD_0 case 42: // ALOAD_0 block.append(new IntInsn(Insn.LOCAL_LOAD, 0)); break; case 27: // ILOAD_1 case 31: // LLOAD_1 case 35: // FLOAD_1 case 39: // DLOAD_1 case 43: // ALOAD_1 block.append(new IntInsn(Insn.LOCAL_LOAD, 1)); break; case 28: // ILOAD_2 case 32: // LLOAD_2 case 36: // FLOAD_2 case 40: // DLOAD_2 case 44: // ALOAD_2 block.append(new IntInsn(Insn.LOCAL_LOAD, 2)); break; case 29: // ILOAD_3 case 33: // LLOAD_3 case 37: // FLOAD_3 case 41: // DLOAD_3 case 45: // ALOAD_3 block.append(new IntInsn(Insn.LOCAL_LOAD, 3)); break; case 46: // IALOAD case 47: // LALOAD case 48: // FALOAD case 49: // DALOAD case 50: // AALOAD case 51: // BALOAD case 52: // CALOAD case 53: // SALOAD block.append(new OpInsn(Insn.ARRAY_LOAD)); break; case 54: { // ISTORE int local = code[i++] & 0xFF; block.append(new IntInsn(Insn.LOCAL_STORE, local)); break; } case 55: // LSTORE case 56: // FSTORE case 57: // DSTORE case 58: { // ASTORE int val = code[i++] & 0xFF; block.append(new IntInsn(Insn.LOCAL_STORE, val)); break; } case 59: // ISTORE_0 case 63: // LSTORE_0 case 67: // FSTORE_0 case 71: // DSTORE_0 case 75: // ASTORE_0 block.append(new IntInsn(Insn.LOCAL_STORE, 0)); break; case 60: // ISTORE_1 case 64: // LSTORE_1 case 68: // FSTORE_1 case 72: // DSTORE_1 case 76: // ASTORE_1 block.append(new IntInsn(Insn.LOCAL_STORE, 1)); break; case 61: // ISTORE_2 case 65: // LSTORE_2 case 69: // FSTORE_2 case 73: // DSTORE_2 case 77: // ASTORE_2 block.append(new IntInsn(Insn.LOCAL_STORE, 2)); break; case 62: // ISTORE_3 case 66: // LSTORE_3 case 70: // FSTORE_3 case 74: // DSTORE_3 case 78: // ASTORE_3 block.append(new IntInsn(Insn.LOCAL_STORE, 3)); break; case 79: // IASTORE case 80: // LASTORE case 81: // FASTORE case 82: // DASTORE case 83: // AASTORE case 84: // BASTORE case 85: // CASTORE case 86: // SASTORE block.append(new OpInsn(Insn.ARRAY_STORE)); break; case 87: // POP block.append(new OpInsn(Insn.POP)); break; case 88: // POP2 block.append(new OpInsn(Insn.POP)); insn_starts.add(opcode_index); block.append(new OpInsn(Insn.POP)); break; case 89: // DUP block.append(new OpInsn(Insn.DUP)); break; case 90: // DUP_X1 block.append(new OpInsn(Insn.DUP_X1)); break; case 91: // DUP_X2 block.append(new OpInsn(Insn.DUP_X2)); break; case 92: // DUP2 block.append(new OpInsn(Insn.DUP2)); break; case 93: // DUP2_X1 block.append(new OpInsn(Insn.DUP2_X1)); break; case 94: // DUP2_X2 block.append(new OpInsn(Insn.DUP2_X2)); break; case 95: // SWAP block.append(new OpInsn(Insn.SWAP)); break; case 96: // IADD case 97: // LADD case 98: // FADD case 99: // DADD block.append(new OpInsn(Insn.ADD)); break; case 100: // ISUB case 101: // LSUB case 102: // FSUB case 103: // DSUB block.append(new OpInsn(Insn.SUB)); break; case 104: // IMUL case 105: // LMUL case 106: // FMUL case 107: // DMUL block.append(new OpInsn(Insn.MUL)); break; case 108: // IDIV case 109: // LDIV case 110: // FDIV case 111: // DDIV block.append(new OpInsn(Insn.DIV)); break; case 112: // IREM case 113: // LREM case 114: // FREM case 115: // DREM block.append(new OpInsn(Insn.REM)); break; case 116: // INEG case 117: // LNEG case 118: // FNEG case 119: // DNEG block.append(new OpInsn(Insn.NEG)); break; case 120: // ISHL case 121: // LSHL block.append(new OpInsn(Insn.SHL)); break; case 122: // ISHR case 123: // LSHR block.append(new OpInsn(Insn.SHR)); break; case 124: // IUSHR case 125: // LUSHR block.append(new OpInsn(Insn.USHR)); break; case 126: // IAND case 127: // LAND block.append(new OpInsn(Insn.AND)); break; case 128: // IOR case 129: // LOR block.append(new OpInsn(Insn.OR)); break; case 130: // IXOR case 131: // LXOR block.append(new OpInsn(Insn.XOR)); break; case 132: {// IINC int local = code[i++] & 0xFF; int incr = code[i++]; block.append(new VarIntInsn(Insn.IINC, local, incr)); break; } case 136: // L2I case 139: // F2I case 142: // D2I block.append(new TypeInsn(Insn.CAST, "I")); break; case 133: // I2L case 140: // F2L case 143: // D2L block.append(new TypeInsn(Insn.CAST, "J")); break; case 134: // I2F case 137: // L2F case 144: // D2F block.append(new TypeInsn(Insn.CAST, "F")); break; case 135: // I2D case 138: // L2D case 141: // F2D block.append(new TypeInsn(Insn.CAST, "D")); break; case 145: // I2B block.append(new TypeInsn(Insn.CAST, "B")); break; case 146: // I2C block.append(new TypeInsn(Insn.CAST, "C")); break; case 147: // I2S block.append(new TypeInsn(Insn.CAST, "S")); break; case 148: // LCMP case 149: // FCMPL case 150: // FCMPG case 151: // DCMPL case 152: // DCMPG block.append(new OpInsn(Insn.CMP)); break; case 153: {// IFEQ short index = (short) (((code[i++] & 0xFF) << 8) | (code[i++] & 0xFF)); block.append(new JumpInsn(Insn.IFEQ, opcode_index + index)); break; } case 154: {// IFNE short index = (short) (((code[i++] & 0xFF) << 8) | (code[i++] & 0xFF)); block.append(new JumpInsn(Insn.IFNE, opcode_index + index)); break; } case 155: {// IFLT block.append(new IntInsn(Insn.ICONST, 0)); insn_starts.add(opcode_index); short index = (short) (((code[i++] & 0xFF) << 8) | (code[i++] & 0xFF)); block.append(new JumpInsn(Insn.IF_CMPLT, opcode_index + index)); break; } case 156: {// IFGE block.append(new IntInsn(Insn.ICONST, 0)); insn_starts.add(opcode_index); short index = (short) (((code[i++] & 0xFF) << 8) | (code[i++] & 0xFF)); block.append(new JumpInsn(Insn.IF_CMPGE, opcode_index + index)); break; } case 157: {// IFGT block.append(new IntInsn(Insn.ICONST, 0)); insn_starts.add(opcode_index); short index = (short) (((code[i++] & 0xFF) << 8) | (code[i++] & 0xFF)); block.append(new JumpInsn(Insn.IF_CMPGT, opcode_index + index)); break; } case 158: {// IFLE block.append(new IntInsn(Insn.ICONST, 0)); insn_starts.add(opcode_index); short index = (short) (((code[i++] & 0xFF) << 8) | (code[i++] & 0xFF)); block.append(new JumpInsn(Insn.IF_CMPLE, opcode_index + index)); break; } case 159: {// IF_ICMPEQ short index = (short) (((code[i++] & 0xFF) << 8) | (code[i++] & 0xFF)); block.append(new JumpInsn(Insn.IF_CMPEQ, opcode_index + index)); break; } case 160: {// IF_ICMPNE short index = (short) (((code[i++] & 0xFF) << 8) | (code[i++] & 0xFF)); block.append(new JumpInsn(Insn.IF_CMPNE, opcode_index + index)); break; } case 161: {// IF_ICMPLT short index = (short) (((code[i++] & 0xFF) << 8) | (code[i++] & 0xFF)); block.append(new JumpInsn(Insn.IF_CMPLT, opcode_index + index)); break; } case 162: {// IF_ICMPGE short index = (short) (((code[i++] & 0xFF) << 8) | (code[i++] & 0xFF)); block.append(new JumpInsn(Insn.IF_CMPGE, opcode_index + index)); break; } case 163: {// IF_ICMPGT short index = (short) (((code[i++] & 0xFF) << 8) | (code[i++] & 0xFF)); block.append(new JumpInsn(Insn.IF_CMPGT, opcode_index + index)); break; } case 164: {// IF_ICMPLE short index = (short) (((code[i++] & 0xFF) << 8) | (code[i++] & 0xFF)); block.append(new JumpInsn(Insn.IF_CMPLE, opcode_index + index)); break; } case 165: {// IF_ACMPEQ short index = (short) (((code[i++] & 0xFF) << 8) | (code[i++] & 0xFF)); block.append(new JumpInsn(Insn.IF_CMPEQ, opcode_index + index)); break; } case 166: {// IF_ACMPNE short index = (short) (((code[i++] & 0xFF) << 8) | (code[i++] & 0xFF)); block.append(new JumpInsn(Insn.IF_CMPNE, opcode_index + index)); break; } case 167: {// GOTO short index = (short) (((code[i++] & 0xFF) << 8) | (code[i++] & 0xFF)); block.append(new JumpInsn(Insn.GOTO, opcode_index + index)); break; } case 168: // JSR case 169: // RET throw new SourceFormatException("Unsupported java opcode: " + next); case 170: {// TABLESWITCH while (i % 4 != 0) { i++; } int def = opcode_index + readInt(code, i); i += 4; int low = readInt(code, i); i += 4; int high = readInt(code, i); i += 4; Map<Integer, Integer> targets = new HashMap<>(); for (int j = 0; j < high - low + 1; j++) { targets.put(low + j, opcode_index + readInt(code, i)); i += 4; } block.append(new SwitchInsn(Insn.SWITCH, targets, def)); break; } case 171: {// LOOKUPSWITCH while (i % 4 != 0) { i++; } int def = opcode_index + readInt(code, i); i += 4; int npairs = readInt(code, i); i += 4; Map<Integer, Integer> targets = new HashMap<>(); for (int j = 0; j < npairs; j++) { int key = readInt(code, i); i += 4; targets.put(key, opcode_index + readInt(code, i)); i += 4; } block.append(new SwitchInsn(Insn.SWITCH, targets, def)); break; } case 172: // IRETURN case 173: // LRETURN case 174: // FRETURN case 175: // DRETURN case 176: // ARETURN block.append(new OpInsn(Insn.ARETURN)); break; case 177: // RETURN block.append(new OpInsn(Insn.RETURN)); break; case 178: { // GETSTATIC int index = ((code[i++] & 0xFF) << 8) | (code[i++] & 0xFF); FieldRefEntry ref = pool.getFieldRef(index); block.append(new FieldInsn(Insn.GETSTATIC, ref.cls, ref.name, ref.type_name)); break; } case 179: { // PUTSTATIC int index = ((code[i++] & 0xFF) << 8) | (code[i++] & 0xFF); FieldRefEntry ref = pool.getFieldRef(index); block.append(new FieldInsn(Insn.PUTSTATIC, ref.cls, ref.name, ref.type_name)); break; } case 180: { // GETFIELD int index = ((code[i++] & 0xFF) << 8) | (code[i++] & 0xFF); FieldRefEntry ref = pool.getFieldRef(index); block.append(new FieldInsn(Insn.GETFIELD, ref.cls, ref.name, ref.type_name)); break; } case 181: { // PUTFIELD int index = ((code[i++] & 0xFF) << 8) | (code[i++] & 0xFF); FieldRefEntry ref = pool.getFieldRef(index); block.append(new FieldInsn(Insn.PUTFIELD, ref.cls, ref.name, ref.type_name)); break; } case 182: // INVOKEVIRTUAL case 183: { // INVOKESPECIAL int index = ((code[i++] & 0xFF) << 8) | (code[i++] & 0xFF); MethodRefEntry ref = pool.getMethodRef(index); InstanceMethodInvoke.Type t = next == 182 ? InstanceMethodInvoke.Type.VIRTUAL : InstanceMethodInvoke.Type.SPECIAL; block.append(new InvokeInsn(Insn.INVOKE, t, ref.cls, ref.name, ref.type_name)); break; } case 184: { // INVOKESTATIC int index = ((code[i++] & 0xFF) << 8) | (code[i++] & 0xFF); MethodRefEntry ref = pool.getMethodRef(index); block.append(new InvokeInsn(Insn.INVOKESTATIC, InstanceMethodInvoke.Type.STATIC, ref.cls, ref.name, ref.type_name)); break; } case 185: {// INVOKEINTERFACE int index = ((code[i++] & 0xFF) << 8) | (code[i++] & 0xFF); // skip count and constant 0 (historical) i += 2; MethodRefEntry ref = pool.getInterfaceMethodRef(index); block.append(new InvokeInsn(Insn.INVOKE, InstanceMethodInvoke.Type.INTERFACE, ref.cls, ref.name, ref.type_name)); break; } case 186: {// INVOKEDYNAMIC int index = ((code[i++] & 0xFF) << 8) | (code[i++] & 0xFF); // skip constant 0 (historical) i += 2; InvokeDynamicEntry handle = pool.getInvokeDynamic(index); BootstrapMethod bsm = bootstrap_methods.get(handle.bootstrap_index); MethodRefEntry bsmArg = pool.getMethodRef(((MethodHandleEntry) bsm.arguments[1]).reference_index); block.append(new InvokeDynamicInsn(Insn.INVOKEDYNAMIC, "L" + bsmArg.cls + ";", bsmArg.name, bsmArg.type_name, handle.name, handle.type_name, bsmArg.type == ClassConstantPool.EntryType.INTERFACE_METHOD_REF)); break; } case 187: {// NEW int index = ((code[i++] & 0xFF) << 8) | (code[i++] & 0xFF); ClassEntry ref = pool.getClass(index); block.append(new TypeInsn(Insn.NEW, "L" + ref.name + ";")); break; } case 188: {// NEWARRAY String type = null; byte atype = code[i++]; switch (atype) { case 4: // T_BOOLEAN type = "Z"; break; case 5: // T_CHAR type = "C"; break; case 6: // T_FLOAT type = "F"; break; case 7: // T_DOUBLE type = "D"; break; case 8: // T_BYTE type = "B"; break; case 9: // T_SHORT type = "S"; break; case 10: // T_INT type = "I"; break; case 11: // T_LONG type = "J"; break; default: throw new SourceFormatException("Unsupported NEWARRAY type value: " + atype); } block.append(new TypeInsn(Insn.NEWARRAY, type)); break; } case 189: {// ANEWARRAY int index = ((code[i++] & 0xFF) << 8) | (code[i++] & 0xFF); ClassEntry ref = pool.getClass(index); String desc = ref.name; if (!desc.startsWith("[") && (desc.length() > 1 || "BSIJFDCZ".indexOf(desc.charAt(0)) == -1)) { desc = "L" + desc + ";"; } block.append(new TypeInsn(Insn.NEWARRAY, desc)); break; } case 190: // ARRAYLENGTH block.append(new FieldInsn(Insn.GETFIELD, "", "length", "I")); break; case 191: // ATHROW block.append(new OpInsn(Insn.THROW)); break; case 192: {// CHECKCAST int index = ((code[i++] & 0xFF) << 8) | (code[i++] & 0xFF); ClassEntry ref = pool.getClass(index); String desc = ref.name; if (!desc.startsWith("[")) { desc = "L" + desc + ";"; } block.append(new TypeInsn(Insn.CAST, desc)); break; } case 193: {// INSTANCEOF int index = ((code[i++] & 0xFF) << 8) | (code[i++] & 0xFF); ClassEntry ref = pool.getClass(index); block.append(new TypeInsn(Insn.INSTANCEOF, "L" + ref.name + ";")); break; } case 194: // MONITORENTER case 195: // MONITOREXIT case 196: // WIDE throw new SourceFormatException("Unsupported java opcode: " + next); case 197: {// MULTINEWARRAY int index = ((code[i++] & 0xFF) << 8) | (code[i++] & 0xFF); ClassEntry ref = pool.getClass(index); int dims = code[i++] & 0xFF; block.append(new TypeIntInsn(Insn.MULTINEWARRAY, ref.name, dims)); break; } case 198: {// IFNULL block.append(new LdcInsn(Insn.PUSH, null)); insn_starts.add(opcode_index); short index = (short) (((code[i++] & 0xFF) << 8) | (code[i++] & 0xFF)); block.append(new JumpInsn(Insn.IF_CMPEQ, opcode_index + index)); break; } case 199: {// IFNONNULL block.append(new LdcInsn(Insn.PUSH, null)); insn_starts.add(opcode_index); short index = (short) (((code[i++] & 0xFF) << 8) | (code[i++] & 0xFF)); block.append(new JumpInsn(Insn.IF_CMPNE, opcode_index + index)); break; } case 200: // GOTO_W case 201: // JSR_W throw new SourceFormatException("Unsupported java opcode: " + next); default: throw new SourceFormatException("Unknown java opcode: " + next); } } for (Insn insn : block) { if (insn instanceof JumpInsn) { JumpInsn jump = (JumpInsn) insn; jump.setTarget(insn_starts.indexOf(jump.getTarget())); } else if (insn instanceof SwitchInsn) { SwitchInsn sw = (SwitchInsn) insn; sw.setDefault(insn_starts.indexOf(sw.getDefault())); Map<Integer, Integer> new_targets = new HashMap<>(); for (Map.Entry<Integer, Integer> e : sw.getTargets().entrySet()) { new_targets.put(e.getKey(), insn_starts.indexOf(e.getValue())); } sw.getTargets().clear(); sw.getTargets().putAll(new_targets); } } for (TryCatchRegion region : catch_regions) { int start_pc = insn_starts.indexOf(region.getStart()); int end_pc = insn_starts.indexOf(region.getEnd()); int catch_pc = insn_starts.indexOf(region.getCatch()); block.getCatchRegions().add(new TryCatchRegion(start_pc, end_pc, catch_pc, region.getException())); } locals.bakeInstances(methodSignature, insn_starts); return block; } private int readInt(byte[] code, int i) { int byte1 = code[i++] & 0xFF; int byte2 = code[i++] & 0xFF; int byte3 = code[i++] & 0xFF; int byte4 = code[i++] & 0xFF; return (byte1 << 24) | (byte2 << 16) | (byte3 << 8) | byte4; } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false
Despector/Despector
https://github.com/Despector/Despector/blob/e60cfaaf7a0688bc83f8b00c392521d0b162afba/src/main/java/org/spongepowered/despector/decompiler/ir/Insn.java
src/main/java/org/spongepowered/despector/decompiler/ir/Insn.java
/* * The MIT License (MIT) * * Copyright (c) Despector <https://despector.voxelgenesis.com> * 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.spongepowered.despector.decompiler.ir; public abstract class Insn { public static final int NOOP = 0; public static final int PUSH = 1; public static final int ICONST = 2; public static final int LCONST = 3; public static final int FCONST = 4; public static final int DCONST = 5; public static final int LOCAL_LOAD = 10; public static final int LOCAL_STORE = 11; public static final int ARRAY_LOAD = 12; public static final int ARRAY_STORE = 13; public static final int GETFIELD = 14; public static final int PUTFIELD = 15; public static final int GETSTATIC = 16; public static final int PUTSTATIC = 17; public static final int INVOKE = 20; public static final int INVOKESTATIC = 21; public static final int INVOKEDYNAMIC = 22; public static final int NEW = 23; public static final int NEWARRAY = 24; public static final int THROW = 25; public static final int RETURN = 26; public static final int ARETURN = 27; public static final int CAST = 28; public static final int INSTANCEOF = 29; public static final int POP = 30; public static final int DUP = 31; public static final int DUP_X1 = 32; public static final int DUP_X2 = 33; public static final int DUP2 = 34; public static final int DUP2_X1 = 35; public static final int DUP2_X2 = 36; public static final int SWAP = 37; public static final int ADD = 40; public static final int SUB = 41; public static final int MUL = 42; public static final int DIV = 43; public static final int REM = 44; public static final int NEG = 45; public static final int SHL = 46; public static final int SHR = 47; public static final int USHR = 48; public static final int AND = 49; public static final int OR = 50; public static final int XOR = 51; public static final int IINC = 60; public static final int CMP = 61; public static final int MULTINEWARRAY = 62; public static final int IFEQ = 70; public static final int IFNE = 71; public static final int IF_CMPLT = 72; public static final int IF_CMPGT = 73; public static final int IF_CMPGE = 74; public static final int IF_CMPLE = 75; public static final int IF_CMPEQ = 76; public static final int IF_CMPNE = 77; public static final int GOTO = 78; public static final int SWITCH = 79; protected int opcode; public Insn(int op) { this.opcode = op; } public int getOpcode() { return this.opcode; } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false