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/decompiler/ir/JumpInsn.java
src/main/java/org/spongepowered/despector/decompiler/ir/JumpInsn.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; import org.spongepowered.despector.util.DebugUtil; public class JumpInsn extends Insn { private int target; public JumpInsn(int op, int operand) { super(op); this.target = operand; } public int getTarget() { return this.target; } public void setTarget(int target) { this.target = target; } @Override public String toString() { StringBuilder str = new StringBuilder(); str.append(DebugUtil.opcodeToString(this.opcode)); str.append(" ").append(this.target); return str.toString(); } }
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/DoubleInsn.java
src/main/java/org/spongepowered/despector/decompiler/ir/DoubleInsn.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; import org.spongepowered.despector.util.DebugUtil; public class DoubleInsn extends Insn { private double operand; public DoubleInsn(int op, double operand) { super(op); this.operand = operand; } public double getValue() { return this.operand; } @Override public String toString() { StringBuilder str = new StringBuilder(); str.append(DebugUtil.opcodeToString(this.opcode)); str.append(" ").append(this.operand); return str.toString(); } }
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/FloatInsn.java
src/main/java/org/spongepowered/despector/decompiler/ir/FloatInsn.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; import org.spongepowered.despector.util.DebugUtil; public class FloatInsn extends Insn { private float operand; public FloatInsn(int op, float operand) { super(op); this.operand = operand; } public float getValue() { return this.operand; } @Override public String toString() { StringBuilder str = new StringBuilder(); str.append(DebugUtil.opcodeToString(this.opcode)); str.append(" ").append(this.operand); return str.toString(); } }
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/TypeInsn.java
src/main/java/org/spongepowered/despector/decompiler/ir/TypeInsn.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; import org.spongepowered.despector.util.DebugUtil; public class TypeInsn extends Insn { private String type; public TypeInsn(int op, String type) { super(op); this.type = type; } public String getType() { return this.type; } @Override public String toString() { StringBuilder str = new StringBuilder(); str.append(DebugUtil.opcodeToString(this.opcode)); str.append(" ").append(this.type); return str.toString(); } }
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/LongInsn.java
src/main/java/org/spongepowered/despector/decompiler/ir/LongInsn.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; import org.spongepowered.despector.util.DebugUtil; public class LongInsn extends Insn { private long operand; public LongInsn(int op, long operand) { super(op); this.operand = operand; } public long getValue() { return this.operand; } @Override public String toString() { StringBuilder str = new StringBuilder(); str.append(DebugUtil.opcodeToString(this.opcode)); str.append(" ").append(this.operand); return str.toString(); } }
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/InsnBlock.java
src/main/java/org/spongepowered/despector/decompiler/ir/InsnBlock.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; import org.spongepowered.despector.decompiler.method.PartialMethod.TryCatchRegion; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class InsnBlock implements Iterable<Insn> { private final List<Insn> instructions; private int[] op_indices; private List<TryCatchRegion> catch_regions = new ArrayList<>(); public InsnBlock() { this.instructions = new ArrayList<>(); } public int size() { return this.instructions.size(); } public Insn get(int i) { return this.instructions.get(i); } public void append(Insn insn) { this.instructions.add(insn); } public List<Insn> getInstructions() { return this.instructions; } public int[] getOpcodeIndices() { return this.op_indices; } public void setOpcodeIndices(int[] op) { this.op_indices = op; } public List<TryCatchRegion> getCatchRegions() { return this.catch_regions; } @Override public String toString() { StringBuilder str = new StringBuilder(); for (Insn insn : this.instructions) { str.append(" ").append(insn).append("\n"); } return str.toString(); } @Override public Iterator<Insn> iterator() { return new Itr(); } public class Itr implements Iterator<Insn> { private int index; @Override public boolean hasNext() { return InsnBlock.this.getInstructions().size() > this.index; } @Override public Insn next() { return InsnBlock.this.getInstructions().get(this.index++); } } }
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/FieldInsn.java
src/main/java/org/spongepowered/despector/decompiler/ir/FieldInsn.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; import org.spongepowered.despector.util.DebugUtil; public class FieldInsn extends Insn { private String owner; private String name; private String desc; public FieldInsn(int op, String owner, String name, String desc) { super(op); this.owner = owner; this.name = name; this.desc = desc; } public String getOwner() { return this.owner; } public String getName() { return this.name; } public String getDescription() { return this.desc; } @Override public String toString() { StringBuilder str = new StringBuilder(); str.append(DebugUtil.opcodeToString(this.opcode)); str.append(" ").append(this.owner); str.append(" ").append(this.name); str.append(" ").append(this.desc); return str.toString(); } }
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/LdcInsn.java
src/main/java/org/spongepowered/despector/decompiler/ir/LdcInsn.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; import org.spongepowered.despector.util.DebugUtil; public class LdcInsn extends Insn { private Object operand; public LdcInsn(int op, Object operand) { super(op); this.operand = operand; } public Object getConstant() { return this.operand; } @Override public String toString() { StringBuilder str = new StringBuilder(); str.append(DebugUtil.opcodeToString(this.opcode)); str.append(" ").append(this.operand); return str.toString(); } }
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/TypeIntInsn.java
src/main/java/org/spongepowered/despector/decompiler/ir/TypeIntInsn.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; import org.spongepowered.despector.util.DebugUtil; public class TypeIntInsn extends Insn { private String type; private int val; public TypeIntInsn(int op, String type, int val) { super(op); this.type = type; this.val = val; } public String getType() { return this.type; } public int getValue() { return this.val; } @Override public String toString() { StringBuilder str = new StringBuilder(); str.append(DebugUtil.opcodeToString(this.opcode)); str.append(" ").append(this.type); str.append(" ").append(this.val); return str.toString(); } }
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/InvokeDynamicInsn.java
src/main/java/org/spongepowered/despector/decompiler/ir/InvokeDynamicInsn.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; import com.google.common.base.MoreObjects; public class InvokeDynamicInsn extends Insn { private String lambda_owner; private String lambda_name; private String lambda_desc; private String name; private String type; private boolean is_interface; public InvokeDynamicInsn(int op, String lambda_owner, String lambda_name, String lambda_desc, String name, String type, boolean intr) { super(op); this.lambda_owner = lambda_owner; this.lambda_name = lambda_name; this.lambda_desc = lambda_desc; this.name = name; this.type = type; this.is_interface = intr; } public String getLambdaOwner() { return this.lambda_owner; } public String getLambdaName() { return this.lambda_name; } public String getLambdaDescription() { return this.lambda_desc; } public String getName() { return this.name; } public String getType() { return this.type; } public boolean isInterface() { return this.is_interface; } @Override public String toString() { return MoreObjects.toStringHelper(this) .add("lambda_owner", this.lambda_owner) .add("lambda_name", this.lambda_name) .add("lambda_desc", this.lambda_desc) .add("name", this.name) .add("type", this.type) .toString(); } }
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/OpInsn.java
src/main/java/org/spongepowered/despector/decompiler/ir/OpInsn.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; import org.spongepowered.despector.util.DebugUtil; public class OpInsn extends Insn { public OpInsn(int op) { super(op); } @Override public String toString() { StringBuilder str = new StringBuilder(); str.append(DebugUtil.opcodeToString(this.opcode)); return str.toString(); } }
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/IntInsn.java
src/main/java/org/spongepowered/despector/decompiler/ir/IntInsn.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; import org.spongepowered.despector.util.DebugUtil; public class IntInsn extends Insn { private int operand; public IntInsn(int op, int operand) { super(op); this.operand = operand; } public int getValue() { return this.operand; } @Override public String toString() { StringBuilder str = new StringBuilder(); str.append(DebugUtil.opcodeToString(this.opcode)); str.append(" ").append(this.operand); return str.toString(); } }
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/VarIntInsn.java
src/main/java/org/spongepowered/despector/decompiler/ir/VarIntInsn.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; import org.spongepowered.despector.util.DebugUtil; public class VarIntInsn extends Insn { private int local; private int operand; public VarIntInsn(int op, int local, int operand) { super(op); this.local = local; this.operand = operand; } public int getLocal() { return this.local; } public int getValue() { return this.operand; } @Override public String toString() { StringBuilder str = new StringBuilder(); str.append(DebugUtil.opcodeToString(this.opcode)); str.append(" ").append(this.local); str.append(" ").append(this.operand); return str.toString(); } }
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/InvokeInsn.java
src/main/java/org/spongepowered/despector/decompiler/ir/InvokeInsn.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; import static com.google.common.base.Preconditions.checkNotNull; import org.spongepowered.despector.ast.stmt.invoke.InstanceMethodInvoke; import org.spongepowered.despector.util.DebugUtil; public class InvokeInsn extends Insn { private InstanceMethodInvoke.Type type; private String owner; private String name; private String desc; public InvokeInsn(int op, InstanceMethodInvoke.Type type, String owner, String name, String desc) { super(op); this.type = checkNotNull(type, "type"); this.owner = checkNotNull(owner, "owner"); this.name = checkNotNull(name, "name"); this.desc = checkNotNull(desc, "desc"); } public InstanceMethodInvoke.Type getType() { return this.type; } public String getOwner() { return this.owner; } public String getName() { return this.name; } public String getDescription() { return this.desc; } @Override public String toString() { StringBuilder str = new StringBuilder(); str.append(DebugUtil.opcodeToString(this.opcode)); str.append(" ").append(this.owner); str.append(" ").append(this.name); str.append(" ").append(this.desc); return str.toString(); } }
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/SwitchInsn.java
src/main/java/org/spongepowered/despector/decompiler/ir/SwitchInsn.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; import org.spongepowered.despector.util.DebugUtil; import java.util.Map; public class SwitchInsn extends Insn { private final Map<Integer, Integer> targets; private int dflt; public SwitchInsn(int op, Map<Integer, Integer> targets, int dflt) { super(op); this.targets = targets; this.dflt = dflt; } public Map<Integer, Integer> getTargets() { return this.targets; } public int getDefault() { return this.dflt; } public void setDefault(int target) { this.dflt = target; } @Override public String toString() { StringBuilder str = new StringBuilder(); str.append(DebugUtil.opcodeToString(this.opcode)); return str.toString(); } }
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/error/SourceFormatException.java
src/main/java/org/spongepowered/despector/decompiler/error/SourceFormatException.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.error; public class SourceFormatException extends RuntimeException { private static final long serialVersionUID = 1L; public SourceFormatException() { super(); } public SourceFormatException(String msg) { super(msg); } public SourceFormatException(Throwable cause) { super(cause); } public SourceFormatException(String msg, Throwable cause) { super(msg, cause); } }
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/kotlin/package-info.java
src/main/java/org/spongepowered/despector/decompiler/kotlin/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.kotlin;
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/kotlin/method/package-info.java
src/main/java/org/spongepowered/despector/decompiler/kotlin/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.decompiler.kotlin.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/decompiler/kotlin/method/postprocess/package-info.java
src/main/java/org/spongepowered/despector/decompiler/kotlin/method/postprocess/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.kotlin.method.postprocess;
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/kotlin/method/postprocess/KotlinLocalsMutabilityPostProcess.java
src/main/java/org/spongepowered/despector/decompiler/kotlin/method/postprocess/KotlinLocalsMutabilityPostProcess.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.kotlin.method.postprocess; import org.spongepowered.despector.ast.Locals.LocalInstance; import org.spongepowered.despector.ast.stmt.StatementBlock; import org.spongepowered.despector.ast.stmt.StatementVisitor; import org.spongepowered.despector.ast.stmt.assign.ArrayAssignment; import org.spongepowered.despector.ast.stmt.assign.InstanceFieldAssignment; import org.spongepowered.despector.ast.stmt.assign.LocalAssignment; import org.spongepowered.despector.ast.stmt.assign.StaticFieldAssignment; import org.spongepowered.despector.ast.stmt.branch.Break; 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.If.Elif; import org.spongepowered.despector.ast.stmt.branch.If.Else; 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.branch.TryCatch.CatchBlock; import org.spongepowered.despector.ast.stmt.branch.While; import org.spongepowered.despector.ast.stmt.invoke.InvokeStatement; import org.spongepowered.despector.ast.stmt.misc.Comment; import org.spongepowered.despector.ast.stmt.misc.Increment; import org.spongepowered.despector.ast.stmt.misc.Return; import org.spongepowered.despector.ast.stmt.misc.Throw; import org.spongepowered.despector.decompiler.method.postprocess.StatementPostProcessor; import java.util.HashSet; import java.util.Set; /** * A post processing statement for determining which locals are mutated after * assignment. */ public class KotlinLocalsMutabilityPostProcess implements StatementPostProcessor { @Override public void postprocess(StatementBlock block) { LocalMutabilityVisitor visitor = new LocalMutabilityVisitor(); block.accept(visitor); } /** * An instruction visitor for determining which locals are mutated. */ private static class LocalMutabilityVisitor implements StatementVisitor { private final Set<LocalInstance> defined = new HashSet<>(); public LocalMutabilityVisitor() { } private void check(LocalInstance local) { if (this.defined.contains(local)) { local.setEffectivelyFinal(false); return; } this.defined.add(local); local.setEffectivelyFinal(true); } @Override public void visitLocalAssignment(LocalAssignment assign) { LocalInstance local = assign.getLocal(); check(local); } @Override public void visitIncrement(Increment incr) { LocalInstance local = incr.getLocal(); check(local); } @Override public void visitArrayAssignment(ArrayAssignment arrayAssign) { } @Override public void visitBreak(Break breakStatement) { } @Override public void visitCatchBlock(CatchBlock catchBlock) { } @Override public void visitComment(Comment comment) { } @Override public void visitDoWhile(DoWhile doWhileLoop) { } @Override public void visitElif(Elif elseBlock) { } @Override public void visitElse(Else elseBlock) { } @Override public void visitFor(For forLoop) { } @Override public void visitForEach(ForEach forLoop) { } @Override public void visitIf(If ifBlock) { } @Override public void visitInstanceFieldAssignment(InstanceFieldAssignment stmt) { } @Override public void visitInvoke(InvokeStatement stmt) { } @Override public void visitReturn(Return returnValue) { } @Override public void visitStaticFieldAssignment(StaticFieldAssignment staticFieldAssign) { } @Override public void visitSwitch(Switch tableSwitch) { } @Override public void visitSwitchCase(Case case1) { } @Override public void visitThrow(Throw throwException) { } @Override public void visitTryCatch(TryCatch tryBlock) { } @Override public void visitWhile(While whileLoop) { } } }
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/kotlin/method/special/KotlinLocalsProcessor.java
src/main/java/org/spongepowered/despector/decompiler/kotlin/method/special/KotlinLocalsProcessor.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.kotlin.method.special; import org.spongepowered.despector.ast.Locals; 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.method.PartialMethod; import org.spongepowered.despector.decompiler.method.special.LocalsProcessor; /** * A locals processor for locals in kotlin that attempts to simplify some of the * extra locals that kotlin creates. */ public class KotlinLocalsProcessor implements LocalsProcessor { @Override public void process(PartialMethod mth, Locals locals) { // InsnBlock insn = mth.getOpcodes(); // IntInsn last = null; // // for (int i = 0; i < insn.size(); i++) { // Insn next = insn.get(i); // if (next.getOpcode() == Insn.LOCAL_LOAD) { // last = (IntInsn) next; // } else if (next.getOpcode() == Insn.LOCAL_STORE && last != null) { // IntInsn store = (IntInsn) next; // int life = nextMod(insn, i + 1, store.getValue()); // if (life == -1) { // life = insn.size(); // } // int last_read = lastRead(insn, i + 1, life, store.getValue()); // // if (last_read == -1) { // // the local is never read before it is next written to // insn.remove(last); // insn.remove(next); // ops.remove(last); // ops.remove(next); // i -= 2; // last = null; // continue; // } // int invalid = nextMod(ops, i + 1, last.var); // if (invalid == -1) { // invalid = ops.size(); // } // // if (invalid > last_read) { // invalid = Math.min(invalid, life); // // for (int j = i + 1; j < invalid; j++) { // AbstractInsnNode n = ops.get(j); // if (n.getOpcode() >= ILOAD && n.getOpcode() <= ALOAD) { // VarInsnNode var = (VarInsnNode) n; // if (var.var == store.var) { // var.var = last.var; // } // } // } // // insn.remove(last); // insn.remove(next); // ops.remove(last); // ops.remove(next); // i -= 2; // } // last = null; // } else { // last = null; // } // } // // Map<Label, List<Integer>> label_targets = new HashMap<>(); // for (int i = 0; i < ops.size(); i++) { // AbstractInsnNode next = ops.get(i); // if (next instanceof JumpInsnNode) { // JumpInsnNode jump = (JumpInsnNode) next; // List<Integer> target = label_targets.get(jump.label.getLabel()); // if (target == null) { // target = new ArrayList<>(); // label_targets.put(jump.label.getLabel(), target); // } // target.add(i); // } // } // // outer: for (int i = 0; i < ops.size(); i++) { // AbstractInsnNode next = ops.get(i); // if (next.getOpcode() >= ISTORE && next.getOpcode() <= ASTORE) { // VarInsnNode store = (VarInsnNode) next; // int life = nextMod(ops, i + 1, store.var); // if (life == -1) { // life = ops.size(); // } // int reads = countReads(ops, i + 1, life, store.var, label_targets); // if (reads == 1 && locals.getLocal(store.var).getLVT().isEmpty()) { // List<AbstractInsnNode> val = new ArrayList<>(); // int required = -1; // for (int o = i - 1; o >= 0; o--) { // AbstractInsnNode prev = ops.get(o); // if (prev instanceof JumpInsnNode || prev instanceof LabelNode || prev instanceof FrameNode // || prev instanceof LineNumberNode) { // continue outer; // } // val.add(0, prev); // required += AstUtil.getStackDelta(prev); // if (required == 0) { // break; // } // } // if (val.size() > 1 && reads != 1) { // continue; // } // for (AbstractInsnNode v : val) { // insn.remove(v); // ops.remove(v); // i--; // } // insn.remove(next); // for (int o = i + 1; o < life; o++) { // AbstractInsnNode n = ops.get(o); // if (n.getOpcode() >= ILOAD && n.getOpcode() <= ALOAD) { // VarInsnNode var = (VarInsnNode) n; // if (var.var == store.var) { // for (AbstractInsnNode v : val) { // insn.insertBefore(var, v.clone(null)); // ops.add(o++, v); // } // insn.remove(var); // ops.remove(var); // o--; // life--; // } // } // } // } // } // } } /** * Gets the index of the last read of the given local. */ public static int lastRead(InsnBlock ops, int start, int end, int local) { for (int i = end - 1; i >= start; i--) { Insn next = ops.get(i); if (next.getOpcode() == Insn.LOCAL_LOAD) { IntInsn node = (IntInsn) next; if (node.getValue() == local) { return i; } } } return -1; } /** * Gets the index of the next modification of the given local. */ public static int nextMod(InsnBlock ops, int start, int local) { for (int i = start; i < ops.size(); i++) { Insn next = ops.get(i); if (next.getOpcode() == Insn.LOCAL_STORE) { IntInsn node = (IntInsn) next; if (node.getValue() == local) { return i; } } } return -1; } }
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/kotlin/method/special/package-info.java
src/main/java/org/spongepowered/despector/decompiler/kotlin/method/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.decompiler.kotlin.method.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/decompiler/kotlin/method/graph/package-info.java
src/main/java/org/spongepowered/despector/decompiler/kotlin/method/graph/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.kotlin.method.graph;
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/kotlin/method/graph/operate/package-info.java
src/main/java/org/spongepowered/despector/decompiler/kotlin/method/graph/operate/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.kotlin.method.graph.operate;
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/kotlin/method/graph/operate/KotlinTernaryPrePassOperation.java
src/main/java/org/spongepowered/despector/decompiler/kotlin/method/graph/operate/KotlinTernaryPrePassOperation.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.kotlin.method.graph.operate; import static com.google.common.base.Preconditions.checkState; import org.spongepowered.despector.ast.Locals; import org.spongepowered.despector.ast.insn.condition.Condition; import org.spongepowered.despector.decompiler.kotlin.method.graph.data.WhenBlockSection; import org.spongepowered.despector.decompiler.kotlin.method.graph.data.WhenBlockSection.WhenCondition; import org.spongepowered.despector.decompiler.method.ConditionBuilder; import org.spongepowered.despector.decompiler.method.PartialMethod; import org.spongepowered.despector.decompiler.method.graph.GraphOperation; import org.spongepowered.despector.decompiler.method.graph.data.TryCatchMarkerType; import org.spongepowered.despector.decompiler.method.graph.data.block.InlineBlockSection; import org.spongepowered.despector.decompiler.method.graph.data.block.TernaryBlockSection; import org.spongepowered.despector.decompiler.method.graph.data.opcode.ConditionalOpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.GotoOpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.OpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.ProcessedOpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.TryCatchMarkerOpcodeBlock; import org.spongepowered.despector.util.AstUtil; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; /** * A graph operation that pre-processes ternaries before the rest of the graph * processing. */ public class KotlinTernaryPrePassOperation implements GraphOperation { @Override public void process(PartialMethod partial) { List<OpcodeBlock> blocks = partial.getGraph(); for (int i = 0; i < blocks.size(); i++) { OpcodeBlock block = blocks.get(i); if (block instanceof TryCatchMarkerOpcodeBlock) { TryCatchMarkerOpcodeBlock tc = (TryCatchMarkerOpcodeBlock) block; if (tc.getType() == TryCatchMarkerType.CATCH) { blocks.get(i + 1).omitFromTernaryCheck(true); } } else if (block instanceof ProcessedOpcodeBlock && ((ProcessedOpcodeBlock) block).getPrecompiledSection() instanceof TernaryBlockSection) { blocks.get(i + 1).omitFromTernaryCheck(true); } } for (int i = 0; i < blocks.size(); i++) { OpcodeBlock block = blocks.get(i); if (!block.isOmittedFromTernaryCheck() && AstUtil.hasStartingRequirement(block.getOpcodes())) { i -= compileTernary(blocks, i, partial.getLocals()); } } } private static int compileTernary(List<OpcodeBlock> blocks, int end, Locals locals) { if (end < 4) { return 0; } // This is similar to the java ternary pre-pass operation except that // it is generalized to support an arbitrary number of else-if cases in // order to create a when statement if there is more than one OpcodeBlock consumer = blocks.get(end); int start = end - 1; List<OpcodeBlock> true_blocks = new ArrayList<>(); OpcodeBlock tr = blocks.get(start--); // if a ternary is at the end of a try-catch region then the end marker // will be placed after the value of the ternary but before the consumer // so we check for it and skip it if it is there if (tr instanceof TryCatchMarkerOpcodeBlock) { tr = blocks.get(start--); } true_blocks.add(0, tr); OpcodeBlock go = blocks.get(start--); while (!(go instanceof GotoOpcodeBlock) || go.getTarget() != consumer) { // scan backwards until we reach a goto block targetting our // consumer which will be the end of the last case true_blocks.add(0, go); go = blocks.get(start--); } checkState(!true_blocks.isEmpty()); List<WhenBlock> conditions = new ArrayList<>(); OpcodeBlock first = null; OpcodeBlock first_true = true_blocks.get(0); while (go instanceof GotoOpcodeBlock) { // for so long as we have more cases the next block will be a goto List<OpcodeBlock> false_blocks = new ArrayList<>(); OpcodeBlock fl = blocks.get(start--); // collect the body of the next section so long as its not // conditional or a goto and its not targeting the start of the next // section. while ((!(fl instanceof GotoOpcodeBlock) && !(fl instanceof ConditionalOpcodeBlock)) || fl.getTarget() != first_true) { false_blocks.add(0, fl); fl = blocks.get(start--); } checkState(!false_blocks.isEmpty()); OpcodeBlock first_false = false_blocks.get(0); Set<OpcodeBlock> seen = new HashSet<>(); seen.add(first_true); seen.add(first_false); seen.add(fl); seen.add(go); List<ConditionalOpcodeBlock> condition_blocks = new ArrayList<>(); condition_blocks.add((ConditionalOpcodeBlock) fl); // we go backwards through the conditional blocks until we we reach // something that is not a condition or something that does not // target a block we are expecting. for (; start >= 0; start--) { OpcodeBlock next = blocks.get(start); if (next instanceof GotoOpcodeBlock || !(next instanceof ConditionalOpcodeBlock) || !seen.contains(next.getTarget())) { break; } seen.add(next); condition_blocks.add(0, (ConditionalOpcodeBlock) next); } Condition cond = ConditionBuilder.makeCondition(condition_blocks, locals, first_false, first_true); WhenBlock next_when = new WhenBlock(cond); next_when.getBody().addAll(false_blocks); first = condition_blocks.get(0); first_true = first; conditions.add(0, next_when); if (start < 0) { break; } go = blocks.get(start--); } OpcodeBlock replacement = null; if (conditions.size() > 1) { // If we have more than one case then its a when statement WhenBlockSection when = new WhenBlockSection(); // TODO support nesting? for (OpcodeBlock t : true_blocks) { if (t instanceof ProcessedOpcodeBlock) { when.getElseBody().add(((ProcessedOpcodeBlock) t).getPrecompiledSection()); } else { checkState(!(t instanceof GotoOpcodeBlock) && !(t instanceof ConditionalOpcodeBlock)); when.getElseBody().add(new InlineBlockSection(t)); } } for (WhenBlock when_block : conditions) { WhenCondition when_cond = new WhenCondition(when_block.getCondition()); for (OpcodeBlock t : when_block.getBody()) { if (t instanceof ProcessedOpcodeBlock) { when_cond.getBody().add(((ProcessedOpcodeBlock) t).getPrecompiledSection()); } else { checkState(!(t instanceof GotoOpcodeBlock) && !(t instanceof ConditionalOpcodeBlock)); when_cond.getBody().add(new InlineBlockSection(t)); } } when.getConditions().add(when_cond); } replacement = new ProcessedOpcodeBlock(first.getStart(), first.getEnd(), when); } else { // with only a single case we have a ternary TernaryBlockSection ternary = new TernaryBlockSection(conditions.get(0).getCondition()); if (true_blocks.size() > 1) { true_blocks.add(consumer); compileTernary(true_blocks, true_blocks.size() - 1, locals); true_blocks.remove(consumer); } for (OpcodeBlock t : true_blocks) { if (t instanceof ProcessedOpcodeBlock) { ternary.getFalseBody().add(((ProcessedOpcodeBlock) t).getPrecompiledSection()); } else { checkState(!(t instanceof GotoOpcodeBlock) && !(t instanceof ConditionalOpcodeBlock)); ternary.getFalseBody().add(new InlineBlockSection(t)); } } List<OpcodeBlock> false_blocks = conditions.get(0).getBody(); if (false_blocks.size() > 1) { false_blocks.add(consumer); compileTernary(false_blocks, false_blocks.size() - 1, locals); false_blocks.remove(consumer); } for (OpcodeBlock t : false_blocks) { if (t instanceof ProcessedOpcodeBlock) { ternary.getTrueBody().add(((ProcessedOpcodeBlock) t).getPrecompiledSection()); } else { checkState(!(t instanceof GotoOpcodeBlock) && !(t instanceof ConditionalOpcodeBlock)); ternary.getTrueBody().add(new InlineBlockSection(t)); } } replacement = new ProcessedOpcodeBlock(first.getStart(), first.getEnd(), ternary); } replacement.setTarget(consumer); start = blocks.indexOf(first); int removed = 0; if (consumer instanceof ConditionalOpcodeBlock) { ((ConditionalOpcodeBlock) consumer).setPrefix(replacement); GraphOperation.remap(blocks, first, consumer); start--; } else { blocks.set(start, replacement); GraphOperation.remap(blocks, first, replacement); } for (int i = end - 1; i >= start + 1; i--) { if (blocks.get(i) instanceof TryCatchMarkerOpcodeBlock) { removed--; continue; } blocks.remove(i); removed++; } return removed; } /** * A temporary construct for holding the cases of a when/ternary in * construction. */ public static class WhenBlock { private Condition condition; private final List<OpcodeBlock> body = new ArrayList<>(); public WhenBlock(Condition cond) { this.condition = cond; } public Condition getCondition() { return this.condition; } public void setCondition(Condition cond) { this.condition = cond; } public List<OpcodeBlock> getBody() { return this.body; } } }
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/kotlin/method/graph/data/package-info.java
src/main/java/org/spongepowered/despector/decompiler/kotlin/method/graph/data/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.kotlin.method.graph.data;
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/kotlin/method/graph/data/WhenBlockSection.java
src/main/java/org/spongepowered/despector/decompiler/kotlin/method/graph/data/WhenBlockSection.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.kotlin.method.graph.data; import static com.google.common.base.Preconditions.checkState; import org.spongepowered.despector.ast.Locals; import org.spongepowered.despector.ast.insn.Instruction; import org.spongepowered.despector.ast.insn.condition.Condition; import org.spongepowered.despector.ast.kotlin.When; import org.spongepowered.despector.ast.kotlin.When.Case; 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.assign.LocalAssignment; import org.spongepowered.despector.ast.stmt.invoke.InvokeStatement; import org.spongepowered.despector.decompiler.method.graph.data.block.BlockSection; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Deque; import java.util.List; /** * a block section containing a processed kotlin when statement. */ public class WhenBlockSection extends BlockSection { private final List<WhenCondition> conditions = new ArrayList<>(); private final List<BlockSection> else_body = new ArrayList<>(); public WhenBlockSection() { } public List<WhenCondition> getConditions() { return this.conditions; } /** * Gets the {@link BlockSection}s forming the false case of this ternary. */ public List<BlockSection> getElseBody() { return this.else_body; } @Override public void appendTo(StatementBlock block, Locals locals, Deque<Instruction> stack) { Statement last = block.popStatement(); checkState(last instanceof LocalAssignment); When when = new When(((LocalAssignment) last).getLocal(), ((LocalAssignment) last).getValue()); boolean has_last = false; for (WhenCondition cond : this.conditions) { StatementBlock case_body = new StatementBlock(Type.SWITCH); Deque<Instruction> case_stack = new ArrayDeque<>(); for (BlockSection section : cond.getBody()) { section.appendTo(case_body, locals, case_stack); } Case cs = new Case(cond.getCondition(), case_body, case_stack.peek()); has_last = cs.getLast() != null; when.getCases().add(cs); } StatementBlock case_body = new StatementBlock(Type.SWITCH); Deque<Instruction> case_stack = new ArrayDeque<>(); for (BlockSection section : this.else_body) { section.appendTo(case_body, locals, case_stack); } when.setElseBody(case_body, case_stack.peek()); if (has_last) { stack.push(when); } else { block.append(new InvokeStatement(when)); } } /** * A condition of a when statement. */ public static class WhenCondition { private Condition condition; private final List<BlockSection> body = new ArrayList<>(); public WhenCondition(Condition cond) { this.condition = cond; } public Condition getCondition() { return this.condition; } public void setCondition(Condition cond) { this.condition = cond; } public List<BlockSection> getBody() { return this.body; } } }
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/kotlin/method/graph/data/ElvisBlockSection.java
src/main/java/org/spongepowered/despector/decompiler/kotlin/method/graph/data/ElvisBlockSection.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.kotlin.method.graph.data; import static com.google.common.base.Preconditions.checkState; import org.spongepowered.despector.ast.Locals; import org.spongepowered.despector.ast.insn.Instruction; import org.spongepowered.despector.ast.kotlin.Elvis; import org.spongepowered.despector.ast.stmt.StatementBlock; import org.spongepowered.despector.decompiler.method.StatementBuilder; import org.spongepowered.despector.decompiler.method.graph.data.block.BlockSection; import org.spongepowered.despector.decompiler.method.graph.data.opcode.OpcodeBlock; import java.util.ArrayDeque; import java.util.Deque; /** * A block section represents a kotlin elvis (?:) statement. */ public class ElvisBlockSection extends BlockSection { private final OpcodeBlock block; public ElvisBlockSection(OpcodeBlock block) { this.block = block; } /** * Gets the body of the else clause. */ public OpcodeBlock getBlock() { return this.block; } @Override public void appendTo(StatementBlock block, Locals locals, Deque<Instruction> stack) { // The blocksection before this one should have been our processed elvis // and will have left the value to be null checked on the stack StatementBlock dummy = new StatementBlock(StatementBlock.Type.IF); Deque<Instruction> dummy_stack = new ArrayDeque<>(); StatementBuilder.appendBlock(this.block, dummy, locals, dummy_stack); checkState(dummy_stack.size() == 1); Elvis elvis = new Elvis(stack.pop(), dummy_stack.pop()); stack.push(elvis); } }
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/kotlin/method/graph/create/package-info.java
src/main/java/org/spongepowered/despector/decompiler/kotlin/method/graph/create/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.kotlin.method.graph.create;
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/kotlin/method/graph/create/ElvisGraphProducerStep.java
src/main/java/org/spongepowered/despector/decompiler/kotlin/method/graph/create/ElvisGraphProducerStep.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.kotlin.method.graph.create; import static com.google.common.base.Preconditions.checkState; import org.spongepowered.despector.decompiler.ir.Insn; import org.spongepowered.despector.decompiler.ir.InsnBlock; import org.spongepowered.despector.decompiler.ir.JumpInsn; import org.spongepowered.despector.decompiler.kotlin.method.graph.data.ElvisBlockSection; import org.spongepowered.despector.decompiler.method.PartialMethod; import org.spongepowered.despector.decompiler.method.graph.GraphOperation; import org.spongepowered.despector.decompiler.method.graph.GraphProducerStep; import org.spongepowered.despector.decompiler.method.graph.data.opcode.BodyOpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.OpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.ProcessedOpcodeBlock; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; /** * A graph operation that pre-processes ternaries before the rest of the graph * processing. */ public class ElvisGraphProducerStep implements GraphProducerStep { private Map<Integer, ElvisBlockSection> sections = new HashMap<>(); @Override public void collectBreakpoints(PartialMethod partial, Set<Integer> break_points) { this.sections.clear(); InsnBlock ops = partial.getOpcodes(); if (ops.size() == 0) { return; } int last = ops.get(0).getOpcode(); for (int i = 1; i < ops.size(); i++) { // an elvis statement when compiled will look something like: // // Example: var a = b ?: "" // // ALOAD 0 the arg of the elvis // DUP // IFNULL L1 might be a numerical comparison as well // GOTO L2 // L1 // POP // LDC "" the default value // L2 // ASTORE 1 the consumer of the value // // Sometimes with the condition inverted and the else case placed // before the GOTO int next = ops.get(i).getOpcode(); if (last == Insn.DUP && ((next >= Insn.IFEQ && next <= Insn.IF_CMPNE))) { // Hello elvis int o = i; int start = i - 1; Insn search = ops.get(o++); int target = ((JumpInsn) search).getTarget(); JumpInsn ggoto = null; // loop forwards and look for the label we're targeting with the // check, and pick up the last goto on the way // // There will occasionally be a cast or something in here, not // sure if I need to pay attention to it or not for the purposes // of decompiling. while (true) { search = ops.get(o++); if (o - 1 == target) { break; } else if (search.getOpcode() == Insn.GOTO) { ggoto = (JumpInsn) search; } } checkState(ggoto != null); // The target will be the end of the else body target = ggoto.getTarget(); List<Insn> else_body = new ArrayList<>(); boolean first_pop = false; while (true) { search = ops.get(o++); if (o - 1 == target) { break; } // we ignore the first pop which is removing the checked // value from the stack if (search.getOpcode() == Insn.POP && !first_pop) { first_pop = true; } else { else_body.add(search); } } OpcodeBlock holder = new BodyOpcodeBlock(0, 0); holder.getOpcodes().addAll(else_body); // remove any break points that were placed inside the elvis for (int l = start; l < o; l++) { break_points.remove(l); } // add break points at the start and end of the elvis // // the one just before will create a body block that ends with // the checked value being left on the stack break_points.add(o - 1); break_points.add(start - 1); // TODO we need to parse any ternaries that might be contained // in the else body of the elvis statement. ElvisBlockSection elvis = new ElvisBlockSection(holder); // store the elvis to be created properly later this.sections.put(Integer.valueOf(o - 1), elvis); } last = next; } } @Override public void formEdges(PartialMethod partial, List<Integer> sorted_break_points, List<OpcodeBlock> block_list) { for (int i = 0; i < block_list.size(); i++) { OpcodeBlock block = block_list.get(i); ElvisBlockSection elvis = this.sections.get(block.getStart()); // Now we loop through the break points and find any that we // compiled an elvis statement for to create the block section. if (elvis != null) { ProcessedOpcodeBlock replacement = new ProcessedOpcodeBlock(block.getStart(), block.getEnd(), elvis); replacement.setTarget(block.getTarget()); block_list.set(block_list.indexOf(block), replacement); // omit the next block from the ternary check as it will look // like a ternary since its missing the value which is the // result of the elvis statement block_list.get(block_list.indexOf(block) + 1).omitFromTernaryCheck(true); GraphOperation.remap(block_list, block, replacement); } } this.sections.clear(); } }
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/method/StatementBuilder.java
src/main/java/org/spongepowered/despector/decompiler/method/StatementBuilder.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.method; import org.spongepowered.despector.ast.AstVisitor; import org.spongepowered.despector.ast.Locals; import org.spongepowered.despector.ast.Locals.Local; import org.spongepowered.despector.ast.Locals.LocalInstance; 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.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.op.NegativeOperator; import org.spongepowered.despector.ast.insn.op.Operator; import org.spongepowered.despector.ast.insn.op.OperatorType; import org.spongepowered.despector.ast.insn.var.ArrayAccess; 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.ast.stmt.StatementBlock; import org.spongepowered.despector.ast.stmt.assign.ArrayAssignment; import org.spongepowered.despector.ast.stmt.assign.FieldAssignment; import org.spongepowered.despector.ast.stmt.assign.InstanceFieldAssignment; import org.spongepowered.despector.ast.stmt.assign.LocalAssignment; import org.spongepowered.despector.ast.stmt.assign.StaticFieldAssignment; 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.InvokeStatement; import org.spongepowered.despector.ast.stmt.invoke.New; import org.spongepowered.despector.ast.stmt.invoke.StaticMethodInvoke; import org.spongepowered.despector.ast.stmt.misc.Increment; import org.spongepowered.despector.ast.stmt.misc.Return; import org.spongepowered.despector.ast.stmt.misc.Throw; 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.IntInsn; import org.spongepowered.despector.decompiler.ir.InvokeDynamicInsn; import org.spongepowered.despector.decompiler.ir.InvokeInsn; import org.spongepowered.despector.decompiler.ir.LdcInsn; import org.spongepowered.despector.decompiler.ir.LongInsn; 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.method.graph.data.opcode.OpcodeBlock; import org.spongepowered.despector.util.TypeHelper; import org.spongepowered.despector.util.serialization.MessagePacker; import java.io.IOException; import java.util.Deque; /** * A utility for forming non-control flow opcodes into statements. */ public final class StatementBuilder { /** * Appends the given opcode block to the statement block. */ public static void appendBlock(OpcodeBlock op, StatementBlock block, Locals locals, Deque<Instruction> stack) { // Decompiles a set of opcodes into statements. for (int index = 0; index < op.getOpcodes().size(); index++) { int label_index = op.getStart() + index; Insn next = op.getOpcodes().get(index); switch (next.getOpcode()) { case Insn.NOOP: break; case Insn.ICONST: stack.push(new IntConstant(((IntInsn) next).getValue())); break; case Insn.LCONST: stack.push(new LongConstant(((LongInsn) next).getValue())); break; case Insn.FCONST: stack.push(new FloatConstant(((FloatInsn) next).getValue())); break; case Insn.DCONST: stack.push(new DoubleConstant(((DoubleInsn) next).getValue())); break; case Insn.PUSH: { LdcInsn ldc = (LdcInsn) next; if (ldc.getConstant() == null) { stack.push(NullConstant.NULL); } else if (ldc.getConstant() instanceof String) { stack.push(new StringConstant((String) ldc.getConstant())); } else if (ldc.getConstant() instanceof ClassTypeSignature) { stack.push(new TypeConstant((ClassTypeSignature) ldc.getConstant())); } else { throw new IllegalStateException("Unsupported ldc constant: " + ldc.getConstant().getClass().getName()); } break; } case Insn.LOCAL_LOAD: { IntInsn var = (IntInsn) next; Local local = locals.getLocal(var.getValue()); stack.push(new LocalAccess(local.getInstance(label_index))); break; } case Insn.LOCAL_STORE: { IntInsn var = (IntInsn) next; Instruction val = stack.pop(); Local local = locals.getLocal(var.getValue()); LocalInstance instance = local.getInstance(label_index); if (!local.isParameter() && local.getParameterInstance() != null) { instance.setType(val.inferType()); } block.append(new LocalAssignment(instance, val)); break; } case Insn.ARRAY_LOAD: { Instruction index_arg = stack.pop(); Instruction var = stack.pop(); stack.push(new ArrayAccess(var, index_arg)); break; } case Insn.ARRAY_STORE: { Instruction val = stack.pop(); Instruction index_arg = stack.pop(); Instruction var = stack.pop(); if (var instanceof LocalAccess) { LocalInstance local = ((LocalAccess) var).getLocal(); if (local.getType() == null) { local.setType(TypeSignature.arrayOf(val.inferType())); } } if (var instanceof NewArray) { NewArray array = (NewArray) var; if (array.getInitializer() == null) { array.setInitialValues(new Instruction[((IntConstant) array.getSize()).getConstant()]); } array.getInitializer()[((IntConstant) index_arg).getConstant()] = val; break; } block.append(new ArrayAssignment(var, index_arg, val)); break; } case Insn.POP: { Instruction arg = stack.pop(); if (arg instanceof InstanceMethodInvoke || arg instanceof StaticMethodInvoke) { block.append(new InvokeStatement(arg)); } break; } case Insn.DUP: { stack.push(stack.peek()); break; } case Insn.DUP_X1: { Instruction val = stack.pop(); Instruction val2 = stack.pop(); stack.push(val); stack.push(val2); stack.push(val); break; } case Insn.DUP_X2: { Instruction val = stack.pop(); Instruction val2 = stack.pop(); Instruction val3 = stack.pop(); stack.push(val); stack.push(val3); stack.push(val2); stack.push(val); break; } case Insn.DUP2: { Instruction val = stack.pop(); Instruction val2 = stack.peek(); stack.push(val); stack.push(val2); stack.push(val); break; } case Insn.DUP2_X1: { Instruction val = stack.pop(); Instruction val2 = stack.pop(); Instruction val3 = stack.pop(); stack.push(val2); stack.push(val); stack.push(val3); stack.push(val2); stack.push(val); break; } case Insn.DUP2_X2: { Instruction val = stack.pop(); Instruction val2 = stack.pop(); Instruction val3 = stack.pop(); Instruction val4 = stack.pop(); stack.push(val2); stack.push(val); stack.push(val4); stack.push(val3); stack.push(val2); stack.push(val); break; } case Insn.SWAP: { Instruction val = stack.pop(); Instruction val2 = stack.pop(); stack.push(val); stack.push(val2); break; } case Insn.ADD: { Instruction right = stack.pop(); Instruction left = stack.pop(); stack.push(new Operator(OperatorType.ADD, left, right)); break; } case Insn.SUB: { Instruction right = stack.pop(); Instruction left = stack.pop(); stack.push(new Operator(OperatorType.SUBTRACT, left, right)); break; } case Insn.MUL: { Instruction right = stack.pop(); Instruction left = stack.pop(); stack.push(new Operator(OperatorType.MULTIPLY, left, right)); break; } case Insn.DIV: { Instruction right = stack.pop(); Instruction left = stack.pop(); stack.push(new Operator(OperatorType.DIVIDE, left, right)); break; } case Insn.REM: { Instruction right = stack.pop(); Instruction left = stack.pop(); stack.push(new Operator(OperatorType.REMAINDER, left, right)); break; } case Insn.NEG: { Instruction right = stack.pop(); stack.push(new NegativeOperator(right)); break; } case Insn.SHL: { Instruction right = stack.pop(); Instruction left = stack.pop(); stack.push(new Operator(OperatorType.SHIFT_LEFT, left, right)); break; } case Insn.SHR: { Instruction right = stack.pop(); Instruction left = stack.pop(); stack.push(new Operator(OperatorType.SHIFT_RIGHT, left, right)); break; } case Insn.USHR: { Instruction right = stack.pop(); Instruction left = stack.pop(); stack.push(new Operator(OperatorType.UNSIGNED_SHIFT_RIGHT, left, right)); break; } case Insn.AND: { Instruction right = stack.pop(); Instruction left = stack.pop(); stack.push(new Operator(OperatorType.AND, left, right)); break; } case Insn.OR: { Instruction right = stack.pop(); Instruction left = stack.pop(); stack.push(new Operator(OperatorType.OR, left, right)); break; } case Insn.XOR: { Instruction right = stack.pop(); Instruction left = stack.pop(); stack.push(new Operator(OperatorType.XOR, left, right)); break; } case Insn.IINC: { VarIntInsn inc = (VarIntInsn) next; Local local = locals.getLocal(inc.getLocal()); Increment insn = new Increment(local.getInstance(label_index), inc.getValue()); block.append(insn); break; } case Insn.CMP: { Instruction right = stack.pop(); Instruction left = stack.pop(); stack.push(new NumberCompare(left, right)); break; } case Insn.ARETURN: block.append(new Return(stack.pop())); break; case Insn.RETURN: block.append(new Return()); break; case Insn.GETSTATIC: { FieldInsn field = (FieldInsn) next; String owner = field.getOwner(); if (!owner.startsWith("[")) { owner = "L" + owner + ";"; } FieldAccess arg = new StaticFieldAccess(field.getName(), ClassTypeSignature.of(field.getDescription()), owner); stack.push(arg); break; } case Insn.PUTSTATIC: { FieldInsn field = (FieldInsn) next; Instruction val = stack.pop(); String owner = field.getOwner(); if (!owner.startsWith("[")) { owner = "L" + owner + ";"; } FieldAssignment assign = new StaticFieldAssignment(field.getName(), ClassTypeSignature.of(field.getDescription()), owner, val); block.append(assign); break; } case Insn.GETFIELD: { FieldInsn field = (FieldInsn) next; String owner = field.getOwner(); if (!owner.startsWith("[")) { owner = "L" + owner + ";"; } FieldAccess arg = new InstanceFieldAccess(field.getName(), ClassTypeSignature.of(field.getDescription()), owner, stack.pop()); stack.push(arg); break; } case Insn.PUTFIELD: { FieldInsn field = (FieldInsn) next; Instruction val = stack.pop(); Instruction owner = stack.pop(); String owner_t = field.getOwner(); if (!owner_t.startsWith("[")) { owner_t = "L" + owner_t + ";"; } FieldAssignment assign = new InstanceFieldAssignment(field.getName(), ClassTypeSignature.of(field.getDescription()), owner_t, owner, val); block.append(assign); break; } case Insn.INVOKE: { InvokeInsn method = (InvokeInsn) next; if (method.getName().equals("<init>")) { Instruction[] args = new Instruction[TypeHelper.paramCount(method.getDescription())]; for (int i = args.length - 1; i >= 0; i--) { args[i] = stack.pop(); } if (stack.peek() instanceof UninitializedNew) { UninitializedNew new_arg = (UninitializedNew) stack.pop(); if (stack.peek() instanceof UninitializedNew) { UninitializedNew new_arg2 = (UninitializedNew) stack.pop(); if (new_arg2 == new_arg) { stack.push(new New(new_arg.type, method.getDescription(), args)); break; } stack.push(new_arg2); } New insn = new New(new_arg.type, method.getDescription(), args); block.append(new InvokeStatement(insn)); break; } else if (stack.peek() instanceof LocalAccess) { LocalAccess callee = (LocalAccess) stack.pop(); String owner = method.getOwner(); if (!owner.startsWith("[")) { owner = "L" + owner + ";"; } InstanceMethodInvoke arg = new InstanceMethodInvoke(method.getType(), method.getName(), method.getDescription(), owner, args, callee); block.append(new InvokeStatement(arg)); break; } throw new IllegalStateException("Callee of call to <init> was " + stack.pop()); } String ret = TypeHelper.getRet(method.getDescription()); Instruction[] args = new Instruction[TypeHelper.paramCount(method.getDescription())]; for (int i = args.length - 1; i >= 0; i--) { args[i] = stack.pop(); } Instruction callee = stack.pop(); String owner = method.getOwner(); if (!owner.startsWith("[")) { owner = "L" + owner + ";"; } InstanceMethodInvoke arg = new InstanceMethodInvoke(method.getType(), method.getName(), method.getDescription(), owner, args, callee); if (ret.equals("V")) { block.append(new InvokeStatement(arg)); } else { stack.push(arg); } break; } case Insn.INVOKESTATIC: { InvokeInsn method = (InvokeInsn) next; String ret = TypeHelper.getRet(method.getDescription()); Instruction[] args = new Instruction[TypeHelper.paramCount(method.getDescription())]; for (int i = args.length - 1; i >= 0; i--) { args[i] = stack.pop(); } String owner = method.getOwner(); if (!owner.startsWith("[")) { owner = "L" + owner + ";"; } StaticMethodInvoke arg = new StaticMethodInvoke(method.getName(), method.getDescription(), owner, args); if (ret.equals("V")) { block.append(new InvokeStatement(arg)); } else { stack.push(arg); } break; } case Insn.INVOKEDYNAMIC: { InvokeDynamicInsn invoke = (InvokeDynamicInsn) next; TypeSignature type = ClassTypeSignature.of(TypeHelper.getRet(invoke.getType())); if (invoke.isInterface()) { MethodReference handle = new MethodReference(stack.pop(), invoke.getLambdaOwner(), invoke.getLambdaName(), invoke.getLambdaDescription(), type, invoke.getName()); stack.push(handle); } else { // need to pop off the captured locals which will be on the // stack, we don't actually care about them for decompiling int type_args = TypeHelper.paramCount(invoke.getType()); for (int i = 0; i < type_args; i++) { stack.pop(); } Lambda handle = new Lambda(invoke.getLambdaOwner(), invoke.getLambdaName(), invoke.getLambdaDescription(), type, invoke.getName()); stack.push(handle); } break; } case Insn.NEW: { TypeSignature type = ClassTypeSignature.of(((TypeInsn) next).getType()); stack.push(new UninitializedNew(type)); break; } case Insn.NEWARRAY: { Instruction size = stack.pop(); TypeInsn array = (TypeInsn) next; stack.push(new NewArray(ClassTypeSignature.of(array.getType()), size, null)); break; } case Insn.MULTINEWARRAY: { TypeIntInsn array = (TypeIntInsn) next; Instruction[] size = new Instruction[array.getValue()]; for (int i = array.getValue() - 1; i >= 0; i--) { size[i] = stack.pop(); } stack.push(new MultiNewArray(ClassTypeSignature.of(array.getType()), size)); break; } case Insn.THROW: block.append(new Throw(stack.pop())); break; case Insn.CAST: { TypeInsn cast = (TypeInsn) next; String desc = cast.getType(); stack.push(new Cast(ClassTypeSignature.of(desc), stack.pop())); break; } case Insn.INSTANCEOF: { TypeInsn insn = (TypeInsn) next; Instruction val = stack.pop(); String type = insn.getType(); stack.push(new InstanceOf(val, ClassTypeSignature.of(type))); break; } case Insn.IFEQ: case Insn.IFNE: case Insn.IF_CMPEQ: case Insn.IF_CMPNE: case Insn.IF_CMPLT: case Insn.IF_CMPGE: case Insn.IF_CMPGT: case Insn.IF_CMPLE: case Insn.GOTO: // All jumps are handled by the implicit structure of the // graph break; default: System.err.println("Unsupported opcode: " + next.getOpcode()); throw new IllegalStateException(); } } } private StatementBuilder() { } private static class UninitializedNew implements Instruction { public TypeSignature type; public UninitializedNew(TypeSignature t) { this.type = t; } @Override public TypeSignature inferType() { throw new IllegalStateException(); } @Override public void accept(AstVisitor visitor) { throw new IllegalStateException(); } @Override public void writeTo(MessagePacker pack) throws IOException { throw new IllegalStateException(); } } }
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/method/package-info.java
src/main/java/org/spongepowered/despector/decompiler/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.decompiler.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/decompiler/method/MethodDecompiler.java
src/main/java/org/spongepowered/despector/decompiler/method/MethodDecompiler.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.method; import static com.google.common.base.Preconditions.checkNotNull; import org.spongepowered.despector.ast.insn.Instruction; import org.spongepowered.despector.ast.insn.var.LocalAccess; import org.spongepowered.despector.ast.stmt.StatementBlock; import org.spongepowered.despector.ast.type.MethodEntry; import org.spongepowered.despector.config.LibraryConfiguration; import org.spongepowered.despector.decompiler.ir.InsnBlock; import org.spongepowered.despector.decompiler.ir.JumpInsn; import org.spongepowered.despector.decompiler.ir.SwitchInsn; import org.spongepowered.despector.decompiler.method.graph.GraphOperation; import org.spongepowered.despector.decompiler.method.graph.GraphProcessor; import org.spongepowered.despector.decompiler.method.graph.GraphProducerStep; import org.spongepowered.despector.decompiler.method.graph.RegionProcessor; import org.spongepowered.despector.decompiler.method.graph.data.block.BlockSection; import org.spongepowered.despector.decompiler.method.graph.data.opcode.BodyOpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.OpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.SwitchOpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.TryCatchMarkerOpcodeBlock; import org.spongepowered.despector.decompiler.method.postprocess.StatementPostProcessor; import org.spongepowered.despector.decompiler.method.special.SpecialMethodProcessor; import org.spongepowered.despector.decompiler.method.special.UninitializedNewVisitor; import java.io.StringWriter; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Comparator; import java.util.Deque; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import javax.annotation.Nullable; /** * A decompiler for method bodies. */ public class MethodDecompiler { public static final String targeted_breakpoint = ""; private final List<GraphProducerStep> graph_producers = new ArrayList<>(); private final List<GraphOperation> cleanup_operations = new ArrayList<>(); private final List<GraphProcessor> processors = new ArrayList<>(); private final List<RegionProcessor> region_processors = new ArrayList<>(); private final List<StatementPostProcessor> post_processors = new ArrayList<>(); private final Map<Class<?>, SpecialMethodProcessor> special_processors = new HashMap<>(); /** * Adds the given {@link GraphProducerStep} to the end of the graph * producers. */ public void addGraphProducer(GraphProducerStep step) { this.graph_producers.add(checkNotNull(step, "step")); } /** * Adds the given {@link GraphOperation} to the end of the cleanup steps. */ public void addCleanupOperation(GraphOperation op) { this.cleanup_operations.add(checkNotNull(op, "op")); } /** * Adds the given {@link GraphProcessor} to the end of the graph processors. */ public void addProcessor(GraphProcessor proc) { this.processors.add(checkNotNull(proc, "proc")); } /** * Adds the given {@link RegionProcessor} to the end of the region * processors. */ public void addRegionProcessor(RegionProcessor proc) { this.region_processors.add(checkNotNull(proc, "proc")); } /** * Adds the given {@link StatementPostProcessor} to the end of the post * processors. */ public void addPostProcessor(StatementPostProcessor post) { this.post_processors.add(checkNotNull(post, "post")); } /** * Adds the given {@link SpecialMethodProcessor} to the special processors. */ public <T extends SpecialMethodProcessor> void setSpecialProcessor(Class<T> type, T processor) { this.special_processors.put(checkNotNull(type, "type"), checkNotNull(processor, "processor")); } /** * Gets the registered {@link SpecialMethodProcessor} corresponding to the * given type, or null if not found. */ @SuppressWarnings("unchecked") @Nullable public <T extends SpecialMethodProcessor> T getSpecialProcessor(Class<T> type) { return (T) this.special_processors.get(checkNotNull(type, "type")); } /** * Decompiles the given asm method to a statement block. */ public StatementBlock decompile(MethodEntry entry) { if (entry.getIR() == null || entry.getIR().size() == 0) { return null; } // Setup the partial method PartialMethod partial = new PartialMethod(this, entry); // Convert the instructions linked list to an array list for easier // processing StatementBlock block = new StatementBlock(StatementBlock.Type.METHOD); partial.setBlock(block); // Creates the initial form of the control flow graph List<OpcodeBlock> graph = makeGraph(partial); partial.setGraph(graph); for (int i = 0; i < graph.size() - 1; i++) { OpcodeBlock b = graph.get(i); if (b.getTarget() == null && !(b instanceof SwitchOpcodeBlock) && !(b instanceof TryCatchMarkerOpcodeBlock)) { System.err.println("Block with null target: " + b.getStart()); for (OpcodeBlock bl : graph) { System.out.print(bl.toString()); } throw new IllegalStateException(); } } if (partial.getEntry().getName().equals(targeted_breakpoint)) { System.out.println(); } if (LibraryConfiguration.emit_block_debug) { StringWriter w = new StringWriter(); for (OpcodeBlock g : graph) { w.append(g.toString()); } entry.block_debug[0] = w.toString(); } // process the graph to perform in-graph operations prior to flattening // it to a list of block sections for (GraphOperation op : this.cleanup_operations) { op.process(partial); } if (LibraryConfiguration.emit_block_debug) { StringWriter w = new StringWriter(); for (OpcodeBlock g : partial.getGraph()) { w.append(g.toString()); } entry.block_debug[1] = w.toString(); } if (partial.getEntry().getName().equals(targeted_breakpoint)) { for (OpcodeBlock g : graph) { System.out.println(g.toString()); } System.out.println(); } // Performs a sequence of transformations to convert the graph into a // simple array of partially decompiled block sections. List<BlockSection> flat_graph = new ArrayList<>(); flattenGraph(partial, graph, graph.size(), flat_graph); if (LibraryConfiguration.emit_block_debug) { StringWriter w = new StringWriter(); for (BlockSection g : flat_graph) { w.append(g.toString()); } entry.block_debug[2] = w.toString(); } // Append all block sections to the output in order. This finalizes all // decompilation of statements not already decompiled. Deque<Instruction> stack = new ArrayDeque<>(); // some compilers create what resembles a kotlin elvis statement in the // switch synthetic methods and it breaks our pure java decompiler // // TODO We should add a processor that can handle this even in java to // produce almost correct code rather than erroring int start = 0; if (entry.getName().startsWith("$SWITCH_TABLE$")) { start = 2; stack.push(new LocalAccess(entry.getLocals().getLocal(0).getInstance(0))); } for (int i = start; i < flat_graph.size(); i++) { BlockSection op = flat_graph.get(i); op.appendTo(block, entry.getLocals(), stack); } for (StatementPostProcessor post : this.post_processors) { try { post.postprocess(block); } catch (Exception e) { if (!LibraryConfiguration.quiet) { System.err.println("Failed to apply post processor: " + post.getClass().getSimpleName()); e.printStackTrace(); } } } block.accept(new UninitializedNewVisitor()); return block; } private List<OpcodeBlock> makeGraph(PartialMethod partial) { InsnBlock instructions = partial.getOpcodes(); Set<Integer> break_points = new HashSet<>(); // queries all graph producers to determine where the instructions // should be broken up to form the graph for (GraphProducerStep step : this.graph_producers) { step.collectBreakpoints(partial, break_points); } // Sort the break points List<Integer> sorted_break_points = new ArrayList<>(break_points); sorted_break_points.sort(Comparator.naturalOrder()); List<OpcodeBlock> block_list = new ArrayList<>(); // turn all blocks to the basic body opcode block, the various // processors will then replace these with the specialized opcode blocks int last_brk = 0; for (int brk : sorted_break_points) { // accumulate the opcodes beween the next breakpoint and the last // breakpoint. OpcodeBlock block = new BodyOpcodeBlock(last_brk, brk); block_list.add(block); for (int i = last_brk; i <= brk; i++) { block.getOpcodes().add(instructions.get(i)); } last_brk = brk + 1; } if (last_brk < instructions.size()) { OpcodeBlock block = new BodyOpcodeBlock(last_brk, instructions.size() - 1); block_list.add(block); for (int i = last_brk; i < instructions.size(); i++) { block.getOpcodes().add(instructions.get(i)); } } for (int i = 0; i < block_list.size() - 1; i++) { OpcodeBlock next = block_list.get(i); if (!(next.getLast() instanceof JumpInsn) && !(next.getLast() instanceof SwitchInsn)) { next.setTarget(block_list.get(i + 1)); } } // form the edges of the graph for (GraphProducerStep step : this.graph_producers) { step.formEdges(partial, sorted_break_points, block_list); } return block_list; } /** * Processes the given region into one or more block sections. */ public void flattenGraph(PartialMethod partial, List<OpcodeBlock> blocks, int stop_point, List<BlockSection> result) { int stop_offs = blocks.size() - stop_point; if (stop_offs < 0) { return; } outer: for (int i = 0; i < blocks.size() - stop_offs; i++) { OpcodeBlock region_start = blocks.get(i); for (GraphProcessor processor : this.processors) { int next = processor.process(partial, blocks, region_start, result); if (next != -1) { i = next; continue outer; } } } } /** * Processes the given region with the registered region processors. */ public BlockSection processRegion(PartialMethod partial, List<OpcodeBlock> region, OpcodeBlock ret, int body_start) { for (RegionProcessor proc : this.region_processors) { BlockSection block = proc.process(partial, region, ret, body_start); if (block != null) { return block; } } return 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/decompiler/method/ConditionBuilder.java
src/main/java/org/spongepowered/despector/decompiler/method/ConditionBuilder.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.method; import static org.spongepowered.despector.util.ConditionUtil.inverse; import org.spongepowered.despector.ast.Locals; import org.spongepowered.despector.ast.insn.Instruction; import org.spongepowered.despector.ast.insn.condition.AndCondition; import org.spongepowered.despector.ast.insn.condition.BooleanCondition; import org.spongepowered.despector.ast.insn.condition.CompareCondition; import org.spongepowered.despector.ast.insn.condition.CompareCondition.CompareOperator; import org.spongepowered.despector.ast.insn.condition.Condition; import org.spongepowered.despector.ast.insn.condition.OrCondition; import org.spongepowered.despector.ast.insn.misc.NumberCompare; import org.spongepowered.despector.ast.stmt.StatementBlock; import org.spongepowered.despector.decompiler.ir.Insn; import org.spongepowered.despector.decompiler.method.graph.data.opcode.ConditionalOpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.OpcodeBlock; import org.spongepowered.despector.util.ConditionUtil; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Deque; import java.util.List; /** * A utility for forming a condition a set of conditional jumps. */ public final class ConditionBuilder { /** * Creates a simple condition from the given {@link ConditionalOpcodeBlock}. */ public static Condition makeSimpleCondition(ConditionalOpcodeBlock block, Locals locals) { // This forms the condition representing the conditional jump of the // given block StatementBlock dummy = new StatementBlock(StatementBlock.Type.IF); Deque<Instruction> dummy_stack = new ArrayDeque<>(); if (block.getPrefix() != null) { block.getPrefix().toBlockSection().appendTo(dummy, locals, dummy_stack); } StatementBuilder.appendBlock(block, dummy, locals, dummy_stack); switch (block.getLast().getOpcode()) { case Insn.IFEQ: { if (dummy_stack.size() != 1) { throw new IllegalStateException(); } Instruction val = dummy_stack.pop(); if (val instanceof NumberCompare) { NumberCompare cmp = (NumberCompare) val; return new CompareCondition(cmp.getLeftOperand(), cmp.getRightOperand(), CompareOperator.EQUAL); } return new BooleanCondition(val, true); } case Insn.IFNE: { if (dummy_stack.size() != 1) { throw new IllegalStateException(); } Instruction val = dummy_stack.pop(); if (val instanceof NumberCompare) { NumberCompare cmp = (NumberCompare) val; return new CompareCondition(cmp.getLeftOperand(), cmp.getRightOperand(), CompareOperator.NOT_EQUAL); } return new BooleanCondition(val, false); } case Insn.IF_CMPEQ: case Insn.IF_CMPNE: case Insn.IF_CMPLT: case Insn.IF_CMPLE: case Insn.IF_CMPGT: case Insn.IF_CMPGE: { if (dummy_stack.size() != 2) { throw new IllegalStateException(); } Instruction b = dummy_stack.pop(); Instruction a = dummy_stack.pop(); return new CompareCondition(a, b, CompareCondition.fromOpcode(block.getLast().getOpcode())); } default: throw new IllegalStateException("Unsupported conditional jump opcode " + block.getLast().getOpcode()); } } private static void dfs(ConditionGraphNode next, Deque<Condition> stack) { // performs a depth-first-search to populate each node in the graph's // partial conditions if (!stack.isEmpty()) { // Add the condition up to this point to the partial conditions of // this node. This represents a path from the root to this node and // the condition of that path is the and of all simple conditions of // the nodes along the path if (stack.size() == 1) { next.addPartialCondition(stack.peek()); } else { Condition partial = new AndCondition(stack); next.addPartialCondition(partial); } } if (next.getSimpleCondition() == null) { return; } // Push the simple condition of this node to the stack and recurse into // the target branch stack.addLast(next.getSimpleCondition()); dfs(next.getTarget(), stack); stack.pollLast(); // Same thing for the else_target except we push the inverse of this // node's condition stack.addLast(inverse(next.getSimpleCondition())); dfs(next.getElseTarget(), stack); stack.pollLast(); } /** * Converts the given set of {@link OpcodeBlock}s to a condition. */ public static Condition makeCondition(List<ConditionalOpcodeBlock> blocks, Locals locals, OpcodeBlock body, OpcodeBlock ret) { List<ConditionGraphNode> nodes = new ArrayList<>(blocks.size()); ConditionGraphNode body_node = new ConditionGraphNode(null); ConditionGraphNode ret_node = new ConditionGraphNode(null); // Forms a condition from a set of conditional jumps. This is done by // performing a depth first search of the nodes which form the // condition. Each path through the graph from root to the start of the // body is found and combined with OR to form a very much expanded // version of the condition for this block. This is then simplified // before being returned. for (int i = 0; i < blocks.size(); i++) { OpcodeBlock next = blocks.get(i); // make the nodes and compute the simple condition for each node. nodes.add(new ConditionGraphNode(makeSimpleCondition((ConditionalOpcodeBlock) next, locals))); } for (int i = 0; i < blocks.size(); i++) { ConditionalOpcodeBlock next = blocks.get(i); ConditionGraphNode node = nodes.get(i); // connect the nodes if (next.getTarget() == body) { node.setTarget(body_node); } else if (next.getTarget() == ret) { node.setTarget(ret_node); } else { int target = blocks.indexOf(next.getTarget()); if (target == -1) { throw new IllegalStateException("Condition target was unknown block " + next.getTarget().getStart()); } node.setTarget(nodes.get(target)); } if (next.getElseTarget() == body) { node.setElseTarget(body_node); } else if (next.getElseTarget() == ret) { node.setElseTarget(ret_node); } else { int target = blocks.indexOf(next.getElseTarget()); if (target == -1) { throw new IllegalStateException("Condition else target was unknown block " + next.getElseTarget().getStart()); } node.setElseTarget(nodes.get(target)); } } ConditionGraphNode start = nodes.get(0); // perform the dfs Deque<Condition> stack = new ArrayDeque<>(); dfs(start, stack); OrCondition condition = new OrCondition(body_node.getPartialConditions()); return ConditionUtil.simplifyCondition(condition); } private ConditionBuilder() { } /** * A node for converting a control flow graph to a condition. */ private static class ConditionGraphNode { private final Condition condition; private final List<Condition> partial_conditions = new ArrayList<>(); private ConditionGraphNode target; private ConditionGraphNode else_target; public ConditionGraphNode(Condition c) { this.condition = c; } public Condition getSimpleCondition() { return this.condition; } public ConditionGraphNode getTarget() { return this.target; } public void setTarget(ConditionGraphNode node) { this.target = node; } public ConditionGraphNode getElseTarget() { return this.else_target; } public void setElseTarget(ConditionGraphNode node) { this.else_target = node; } public List<Condition> getPartialConditions() { return this.partial_conditions; } public void addPartialCondition(Condition cond) { this.partial_conditions.add(cond); } } }
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/method/PartialMethod.java
src/main/java/org/spongepowered/despector/decompiler/method/PartialMethod.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.method; import org.spongepowered.despector.ast.Locals; import org.spongepowered.despector.ast.stmt.StatementBlock; import org.spongepowered.despector.ast.type.MethodEntry; import org.spongepowered.despector.decompiler.ir.InsnBlock; import org.spongepowered.despector.decompiler.method.graph.data.block.BlockSection; import org.spongepowered.despector.decompiler.method.graph.data.opcode.OpcodeBlock; import java.util.ArrayList; import java.util.List; /** * A structure of intermediate data of a method which is in the process of being * decompiled. */ public class PartialMethod { private final MethodDecompiler decompiler; private final MethodEntry method; private StatementBlock block; private List<OpcodeBlock> graph; private List<BlockSection> final_blocks = new ArrayList<>(); public PartialMethod(MethodDecompiler decompiler, MethodEntry method) { this.decompiler = decompiler; this.method = method; } /** * Gets the associated method decompiler. */ public MethodDecompiler getDecompiler() { return this.decompiler; } /** * Gets the {@link MethodEntry} which is being populated. */ public MethodEntry getEntry() { return this.method; } /** * Gets the method locals. */ public Locals getLocals() { return this.method.getLocals(); } /** * Gets the method's opcodes. */ public InsnBlock getOpcodes() { return this.method.getIR(); } /** * Gets the {@link StatementBlock} that will be filled with the final * statements. */ public StatementBlock getBlock() { return this.block; } /** * Sets the {@link StatementBlock}. */ public void setBlock(StatementBlock block) { this.block = block; } /** * Gets the opcode blocks forming the graph of the method's control flow. */ public List<OpcodeBlock> getGraph() { return this.graph; } /** * Sets the opcode blocks forming the graph of the method's control flow. */ public void setGraph(List<OpcodeBlock> graph) { this.graph = graph; } /** * Gets the final block sections. */ public List<BlockSection> getFinalBlocks() { return this.final_blocks; } public static class TryCatchRegion { private int start_pc; private int end_pc; private int catch_pc; private String ex; public TryCatchRegion(int start_pc, int end_pc, int catch_pc, String ex) { this.start_pc = start_pc; this.end_pc = end_pc; this.catch_pc = catch_pc; this.ex = ex; } public int getStart() { return this.start_pc; } public int getEnd() { return this.end_pc; } public int getCatch() { return this.catch_pc; } public String getException() { return this.ex; } } }
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/method/postprocess/package-info.java
src/main/java/org/spongepowered/despector/decompiler/method/postprocess/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.method.postprocess;
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/method/postprocess/StatementPostProcessor.java
src/main/java/org/spongepowered/despector/decompiler/method/postprocess/StatementPostProcessor.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.method.postprocess; import org.spongepowered.despector.ast.stmt.StatementBlock; /** * A post processor for performing final operations to a statement block just * before it is finalized and set onto the method. */ public interface StatementPostProcessor { /** * Process the given statement block. */ void postprocess(StatementBlock block); }
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/method/postprocess/ForEachPostProcessor.java
src/main/java/org/spongepowered/despector/decompiler/method/postprocess/ForEachPostProcessor.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.method.postprocess; 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.Statement; import org.spongepowered.despector.ast.stmt.StatementBlock; import org.spongepowered.despector.ast.stmt.assign.LocalAssignment; 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.If.Elif; 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.branch.TryCatch.CatchBlock; import org.spongepowered.despector.ast.stmt.branch.While; import org.spongepowered.despector.ast.stmt.invoke.InstanceMethodInvoke; 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; import org.spongepowered.despector.util.AstUtil; import java.util.ArrayList; import java.util.List; /** * A post processor that converts for loops to for-each loops where it is * determined likely that the original structure was a for-each loop. * * <p>This handles both iterator based for each loops and loops over simple * arrays.</p> */ public class ForEachPostProcessor implements StatementPostProcessor { private static final StatementMatcher<For> LIST_ITERATOR = StatementMatcher.forLoop() .init(MatchContext.storeLocal("list_iterator", StatementMatcher.localAssign() .value(InstructionMatcher.instanceMethodInvoke() .name("iterator") .build()) .build())) .condition(ConditionMatcher.bool() .value(InstructionMatcher.instanceMethodInvoke() .name("hasNext") .callee(InstructionMatcher.localAccess() .fromContext("list_iterator") .build()) .build()) .build()) .incr(StatementMatcher.NONE) .body(0, StatementMatcher.localAssign() .value(InstructionMatcher.instanceMethodInvoke() .name("next") .callee(InstructionMatcher.localAccess() .fromContext("list_iterator") .build()) .autoUnwrap() .build()) .autoUnwrap() .build()) .build(); private static final StatementMatcher<?> ARRAY_ITERATOR_ASSIGN = MatchContext.storeLocal("array", StatementMatcher.localAssign().build()); private static final StatementMatcher<?> ARRAY_ITERATOR_SIZE = MatchContext.storeLocal("array_size", StatementMatcher.localAssign() .type(ClassTypeSignature.INT) .value(InstructionMatcher.instanceFieldAccess() .name("length") .owner(InstructionMatcher.localAccess() .fromContext("array") .build()) .build()) .build()); private static final StatementMatcher<?> ARRAY_ITERATOR = StatementMatcher.forLoop() .init(MatchContext.storeLocal("index", StatementMatcher.localAssign() .value(InstructionMatcher.intConstant() .value(0) .build()) .build())) .incr(StatementMatcher.increment() .build()) .body(0, StatementMatcher.localAssign() .value(InstructionMatcher.arrayAccess() .array(InstructionMatcher.localAccess() .fromContext("array") .build()) .index(InstructionMatcher.localAccess() .fromContext("index") .build()) .build()) .build()) .build(); @Override public void postprocess(StatementBlock block) { List<Statement> to_remove = new ArrayList<>(); for (Statement stmt : block.getStatements()) { if (stmt instanceof If) { If iif = (If) stmt; postprocess(iif.getBody()); for (Elif elif : iif.getElifBlocks()) { postprocess(elif.getBody()); } if (iif.getElseBlock() != null) { postprocess(iif.getElseBlock().getBody()); } } else if (stmt instanceof While) { While wwhile = (While) stmt; postprocess(wwhile.getBody()); } else if (stmt instanceof DoWhile) { DoWhile dowhile = (DoWhile) stmt; postprocess(dowhile.getBody()); } else if (stmt instanceof For) { if (!checkIterator(block, (For) stmt)) { checkArray(block, (For) stmt, to_remove); } postprocess(((For) stmt).getBody()); } else if (stmt instanceof Switch) { Switch sswitch = (Switch) stmt; for (Case cs : sswitch.getCases()) { postprocess(cs.getBody()); } } else if (stmt instanceof TryCatch) { TryCatch trycatch = (TryCatch) stmt; postprocess(trycatch.getTryBlock()); for (CatchBlock ccatch : trycatch.getCatchBlocks()) { postprocess(ccatch.getBlock()); } } } for (Statement stmt : to_remove) { block.getStatements().remove(stmt); } } /** * Checks if the given for loop is a collection interator and replaces it * with a for-each loop. */ public boolean checkIterator(StatementBlock block, For ffor) { if (!LIST_ITERATOR.matches(MatchContext.create(), ffor)) { return false; } LocalInstance local = ((LocalAssignment) ffor.getInit()).getLocal(); for (int o = 1; o < ffor.getBody().getStatementCount(); o++) { Statement stmt = ffor.getBody().getStatement(o); if (AstUtil.references(stmt, local)) { return false; } } LocalInstance next_assign = ((LocalAssignment) ffor.getBody().getStatement(0)).getLocal(); Instruction list = ((InstanceMethodInvoke) ((LocalAssignment) ffor.getInit()).getValue()).getCallee(); ffor.getBody().getStatements().remove(0); ForEach foreach = new ForEach(list, next_assign, ffor.getBody()); block.getStatements().set(block.getStatements().indexOf(ffor), foreach); return true; } /** * Checks if the given for loop is an array interator and replaces it with a * for-each loop. */ public boolean checkArray(StatementBlock block, For ffor, List<Statement> to_remove) { int i = block.getStatements().indexOf(ffor); if (i < 2) { return false; } if (ffor.getBody().getStatementCount() < 1) { return false; } MatchContext ctx = MatchContext.create(); if (!ARRAY_ITERATOR_ASSIGN.matches(ctx, block.getStatement(i - 2))) { return false; } LocalAssignment array_assign = (LocalAssignment) block.getStatement(i - 2); LocalInstance array = array_assign.getLocal(); if (array.getType() == null || !array.getType().isArray()) { return false; } if (!ARRAY_ITERATOR_SIZE.matches(ctx, block.getStatement(i - 1))) { return false; } if (!ARRAY_ITERATOR.matches(ctx, ffor)) { return false; } for (int o = 1; o < ffor.getBody().getStatementCount(); o++) { Statement stmt = ffor.getBody().getStatement(o); if (AstUtil.references(stmt, ((LocalAssignment) ffor.getInit()).getLocal()) || AstUtil.references(stmt, ((LocalAssignment) block.getStatement(i - 1)).getLocal())) { return false; } } to_remove.add(block.getStatement(i - 2)); to_remove.add(block.getStatement(i - 1)); LocalInstance local = ((LocalAssignment) ffor.getBody().getStatement(0)).getLocal(); ffor.getBody().getStatements().remove(0); ForEach foreach = new ForEach(array_assign.getValue(), local, ffor.getBody()); block.getStatements().set(block.getStatements().indexOf(ffor), foreach); 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/decompiler/method/postprocess/IfCombiningPostProcessor.java
src/main/java/org/spongepowered/despector/decompiler/method/postprocess/IfCombiningPostProcessor.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.method.postprocess; import org.spongepowered.despector.ast.insn.condition.AndCondition; 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.If; import org.spongepowered.despector.ast.stmt.branch.If.Elif; 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.branch.TryCatch.CatchBlock; import org.spongepowered.despector.ast.stmt.branch.While; /** * A post processor which cleans up nested if statements that can be simplified. */ public class IfCombiningPostProcessor implements StatementPostProcessor { @Override public void postprocess(StatementBlock block) { for (Statement stmt : block.getStatements()) { if (stmt instanceof If) { check((If) stmt); } else if (stmt instanceof While) { While wwhile = (While) stmt; postprocess(wwhile.getBody()); } else if (stmt instanceof DoWhile) { DoWhile dowhile = (DoWhile) stmt; postprocess(dowhile.getBody()); } else if (stmt instanceof For) { For ffor = (For) stmt; postprocess(ffor.getBody()); } else if (stmt instanceof Switch) { Switch sswitch = (Switch) stmt; for (Case cs : sswitch.getCases()) { postprocess(cs.getBody()); } } else if (stmt instanceof TryCatch) { TryCatch trycatch = (TryCatch) stmt; postprocess(trycatch.getTryBlock()); for (CatchBlock ccatch : trycatch.getCatchBlocks()) { postprocess(ccatch.getBlock()); } } } } /** * Checks if the given if statement can be simplified. */ public void check(If ifblock) { if (ifblock.getElifBlocks().isEmpty() && ifblock.getElseBlock() == null) { StatementBlock block = ifblock.getBody(); if (block.getStatementCount() == 1 && block.getStatement(0) instanceof If) { If inner = (If) block.getStatement(0); if (inner.getElifBlocks().isEmpty() && inner.getElseBlock() == null) { ifblock.setCondition(new AndCondition(ifblock.getCondition(), inner.getCondition())); block.getStatements().clear(); for (Statement stmt : inner.getBody().getStatements()) { block.append(stmt); } check(ifblock); } } } postprocess(ifblock.getBody()); if (!ifblock.getElifBlocks().isEmpty()) { for (int i = 0; i < ifblock.getElifBlocks().size() - 1; i++) { postprocess(ifblock.getElifBlocks().get(i).getBody()); } Elif last_elif = ifblock.getElifBlocks().get(ifblock.getElifBlocks().size() - 1); StatementBlock block = last_elif.getBody(); if (ifblock.getElseBlock() == null) { if (block.getStatementCount() == 1 && block.getStatement(0) instanceof If) { If inner = (If) block.getStatement(0); if (inner.getElifBlocks().isEmpty() && inner.getElseBlock() == null) { last_elif.setCondition(new AndCondition(last_elif.getCondition(), inner.getCondition())); block.getStatements().clear(); for (Statement stmt : inner.getBody().getStatements()) { block.append(stmt); } } } } postprocess(block); } if (ifblock.getElseBlock() != null) { StatementBlock block = ifblock.getElseBlock().getBody(); if (block.getStatementCount() == 1 && block.getStatement(0) instanceof If) { If inner = (If) block.getStatement(0); ifblock.new Elif(inner.getCondition(), inner.getBody()); postprocess(inner.getBody()); for (int i = 0; i < inner.getElifBlocks().size(); i++) { Elif elif = inner.getElifBlocks().get(i); postprocess(elif.getBody()); ifblock.new Elif(elif.getCondition(), elif.getBody()); } block.getStatements().clear(); if (inner.getElseBlock() != null) { block.getStatements().addAll(inner.getElseBlock().getBody().getStatements()); } else { ifblock.setElseBlock(null); } } else { postprocess(block); } } } }
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/method/postprocess/ForFromWhilePostProcessor.java
src/main/java/org/spongepowered/despector/decompiler/method/postprocess/ForFromWhilePostProcessor.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.method.postprocess; import org.spongepowered.despector.ast.Locals.LocalInstance; import org.spongepowered.despector.ast.stmt.Statement; import org.spongepowered.despector.ast.stmt.StatementBlock; import org.spongepowered.despector.ast.stmt.assign.LocalAssignment; import org.spongepowered.despector.ast.stmt.branch.Break; import org.spongepowered.despector.ast.stmt.branch.DoWhile; import org.spongepowered.despector.ast.stmt.branch.For; import org.spongepowered.despector.ast.stmt.branch.If; import org.spongepowered.despector.ast.stmt.branch.If.Elif; 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.branch.TryCatch.CatchBlock; import org.spongepowered.despector.ast.stmt.branch.While; import org.spongepowered.despector.ast.stmt.misc.Increment; import org.spongepowered.despector.transform.matcher.ConditionMatcher; import org.spongepowered.despector.transform.matcher.MatchContext; import org.spongepowered.despector.transform.matcher.StatementMatcher; import org.spongepowered.despector.util.AstUtil; import java.util.ArrayList; import java.util.List; /** * A post processor that converts while loops to for loops where it is * determined likely that the original structure was a for loop. */ public class ForFromWhilePostProcessor implements StatementPostProcessor { private static final StatementMatcher<?> STORE = MatchContext.storeLocal("loop_val", StatementMatcher.localAssign() .build()); private static final StatementMatcher<?> LOOP = StatementMatcher.whileLoop() .condition(ConditionMatcher.references("loop_val")) .build(); @Override public void postprocess(StatementBlock block) { List<Statement> to_remove = new ArrayList<>(); for (Statement stmt : block.getStatements()) { if (stmt instanceof If) { If iif = (If) stmt; postprocess(iif.getBody()); for (Elif elif : iif.getElifBlocks()) { postprocess(elif.getBody()); } if (iif.getElseBlock() != null) { postprocess(iif.getElseBlock().getBody()); } } else if (stmt instanceof While) { While wwhile = (While) stmt; checkWhile(block, wwhile, to_remove); postprocess(wwhile.getBody()); } else if (stmt instanceof DoWhile) { DoWhile dowhile = (DoWhile) stmt; postprocess(dowhile.getBody()); } else if (stmt instanceof For) { For ffor = (For) stmt; postprocess(ffor.getBody()); } else if (stmt instanceof Switch) { Switch sswitch = (Switch) stmt; for (Case cs : sswitch.getCases()) { postprocess(cs.getBody()); } } else if (stmt instanceof TryCatch) { TryCatch trycatch = (TryCatch) stmt; postprocess(trycatch.getTryBlock()); for (CatchBlock ccatch : trycatch.getCatchBlocks()) { postprocess(ccatch.getBlock()); } } } for (Statement stmt : to_remove) { block.getStatements().remove(stmt); } } private void checkWhile(StatementBlock block, While wwhile, List<Statement> to_remove) { int i = block.getStatements().indexOf(wwhile); if (i == 0) { return; } Statement last = block.getStatement(i - 1); MatchContext ctx = MatchContext.create(); if (!STORE.matches(ctx, last)) { return; } if (!LOOP.matches(ctx, wwhile)) { return; } LocalInstance loop_val = ctx.getLocal("loop_val"); for (int o = i + 1; o < block.getStatementCount(); o++) { Statement n = block.getStatement(o); if (n instanceof LocalAssignment) { if (((LocalAssignment) n).getLocal() == loop_val) { break; } } if (AstUtil.references(block.getStatement(o), loop_val)) { return; } } StatementBlock body = wwhile.getBody(); Increment increment = null; if (!body.getStatements().isEmpty()) { Statement body_last = body.getStatements().get(body.getStatements().size() - 1); if (body_last instanceof Increment && ((Increment) body_last).getLocal() == loop_val) { increment = (Increment) body_last; } } if (increment != null) { body.getStatements().remove(increment); } For ffor = new For(last, wwhile.getCondition(), increment, body); for (Break bbreak : wwhile.getBreaks()) { bbreak.setLoop(ffor); } to_remove.add(last); block.getStatements().set(i, ffor); } }
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/method/special/package-info.java
src/main/java/org/spongepowered/despector/decompiler/method/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.decompiler.method.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/decompiler/method/special/LocalsProcessor.java
src/main/java/org/spongepowered/despector/decompiler/method/special/LocalsProcessor.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.method.special; import org.spongepowered.despector.ast.Locals; import org.spongepowered.despector.decompiler.method.PartialMethod; /** * A special processor for pre-processing a method's locals. */ public interface LocalsProcessor extends SpecialMethodProcessor { /** * Process the given method's locals. */ void process(PartialMethod mth, Locals locals); }
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/method/special/UninitializedNewVisitor.java
src/main/java/org/spongepowered/despector/decompiler/method/special/UninitializedNewVisitor.java
package org.spongepowered.despector.decompiler.method.special; import org.spongepowered.despector.ast.Locals.LocalInstance; 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.InstanceMethodInvoke; import org.spongepowered.despector.ast.stmt.invoke.Lambda; import org.spongepowered.despector.ast.stmt.invoke.MethodReference; import org.spongepowered.despector.ast.stmt.invoke.New; import org.spongepowered.despector.ast.stmt.invoke.StaticMethodInvoke; public class UninitializedNewVisitor implements InstructionVisitor { @Override public void visitArrayAccess(ArrayAccess insn) { } @Override public void visitCast(Cast 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 visitLocalInstance(LocalInstance local) { } @Override public void visitLongConstant(LongConstant insn) { } @Override public void visitMultiNewArray(MultiNewArray insn) { } @Override public void visitNegativeOperator(NegativeOperator insn) { } @Override public void visitNew(New 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 visitTypeConstant(TypeConstant 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/decompiler/method/special/SpecialMethodProcessor.java
src/main/java/org/spongepowered/despector/decompiler/method/special/SpecialMethodProcessor.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.method.special; /** * A marker interface for special processors. */ public interface SpecialMethodProcessor { }
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/method/graph/GraphProcessor.java
src/main/java/org/spongepowered/despector/decompiler/method/graph/GraphProcessor.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.method.graph; import org.spongepowered.despector.decompiler.method.PartialMethod; import org.spongepowered.despector.decompiler.method.graph.data.block.BlockSection; import org.spongepowered.despector.decompiler.method.graph.data.opcode.OpcodeBlock; import java.util.List; /** * A processor that processes a region of the graph into one or more block * sections. */ public interface GraphProcessor { /** * Processes the given region starting at the start block and outputs zero * or more blocks to the {@link BlockSection} list. * * @return -1 if no part of the region was processed, else return the index * in the region of the last processed block */ int process(PartialMethod partial, List<OpcodeBlock> blocks, OpcodeBlock region_start, List<BlockSection> final_blocks); }
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/method/graph/GraphOperation.java
src/main/java/org/spongepowered/despector/decompiler/method/graph/GraphOperation.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.method.graph; import org.spongepowered.despector.decompiler.method.PartialMethod; import org.spongepowered.despector.decompiler.method.graph.data.opcode.ConditionalOpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.OpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.SwitchOpcodeBlock; import java.util.List; import java.util.Map; /** * An operation on the opcode graph that modifies the graph in place. */ public interface GraphOperation { /** * Processes the opcode graph of the given partial method. */ void process(PartialMethod partial); /** * Remaps all references to the given from block to the new block. */ static void remap(List<OpcodeBlock> blocks, OpcodeBlock from, OpcodeBlock to) { for (OpcodeBlock other : blocks) { if (other instanceof ConditionalOpcodeBlock) { ConditionalOpcodeBlock cond = (ConditionalOpcodeBlock) other; if (cond.getElseTarget() == from) { cond.setElseTarget(to); } } if (other.getTarget() == from) { other.setTarget(to); } if (other.getTargettedBy().contains(from)) { other.getTargettedBy().remove(from); other.getTargettedBy().add(to); } if (other instanceof SwitchOpcodeBlock) { SwitchOpcodeBlock sswitch = (SwitchOpcodeBlock) other; for (Map.Entry<Integer, OpcodeBlock> e : sswitch.getAdditionalTargets().entrySet()) { if (e.getValue() == from) { sswitch.getAdditionalTargets().put(e.getKey(), to); } } } } } }
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/method/graph/package-info.java
src/main/java/org/spongepowered/despector/decompiler/method/graph/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.method.graph;
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/method/graph/RegionProcessor.java
src/main/java/org/spongepowered/despector/decompiler/method/graph/RegionProcessor.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.method.graph; import org.spongepowered.despector.decompiler.method.PartialMethod; import org.spongepowered.despector.decompiler.method.graph.data.block.BlockSection; import org.spongepowered.despector.decompiler.method.graph.data.opcode.BreakMarkerOpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.ConditionalOpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.GotoOpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.OpcodeBlock; import java.util.ArrayList; import java.util.List; /** * Processes a region of the graph into a single {@link BlockSection}. */ public interface RegionProcessor { /** * Processes the given region. */ BlockSection process(PartialMethod partial, List<OpcodeBlock> region, OpcodeBlock ret, int body_start); /** * Gets the end of the region starting at the start index, or -1 if it does * not form a sub region. */ static int getRegionEnd(List<OpcodeBlock> blk, OpcodeBlock ret, int start) { List<OpcodeBlock> blocks = new ArrayList<>(blk); if (ret != null) { blocks.add(ret); } OpcodeBlock region_start = blocks.get(start); // if the target is behind the start then we break as this is likely the // condition of a do-while // the check is less or equal because when a header is split from a // condition its break point is set to the same as the condition, in a // simple do-while the condition targets the body which was originally // split from the condition and therefore has the same break_point so we // do this check to catch that. There are no conditions where two // conditions may have the same break point that could break this (ie. // the case where a condition was targeting a condition after it that // has the same break_point is impossible). int end = start + 1; if (region_start instanceof ConditionalOpcodeBlock) { ConditionalOpcodeBlock cond = (ConditionalOpcodeBlock) region_start; int end_a = blocks.indexOf(cond.getTarget()); if (cond.getTarget().getStart() <= region_start.getStart()) { boolean found = false; if (cond.getTarget() instanceof ConditionalOpcodeBlock) { ConditionalOpcodeBlock target = (ConditionalOpcodeBlock) cond.getTarget(); for (OpcodeBlock op : target.getTargettedBy()) { if (op instanceof GotoOpcodeBlock && op.getStart() > cond.getStart()) { end_a = blocks.indexOf(op); found = true; break; } } } if (!found) { return -1; } } if (end_a > start) { if (cond.getTarget() instanceof ConditionalOpcodeBlock) { ConditionalOpcodeBlock cond_target = (ConditionalOpcodeBlock) cond.getTarget(); if (cond_target.getTarget().getStart() < cond_target.getStart() && cond_target.getTarget().getStart() > region_start.getStart()) { end_a = blocks.indexOf(cond_target.getTarget()); } } } if (end_a < start && cond.getTarget() instanceof ConditionalOpcodeBlock) { ConditionalOpcodeBlock target = (ConditionalOpcodeBlock) cond.getTarget(); for (OpcodeBlock op : target.getTargettedBy()) { if (op instanceof GotoOpcodeBlock && op.getStart() > cond.getStart()) { end_a = blocks.indexOf(op); break; } } } int end_b = blocks.indexOf(cond.getElseTarget()); // Use the target of the start node as a starting point for our // search end = Math.max(end_a, end_b); } else { end = blocks.indexOf(region_start.getTarget()); } boolean is_goto = region_start instanceof GotoOpcodeBlock; return getRegionEnd(blocks, start, end, is_goto, ret); } /** * Gets the end of the region starting at the start index, or -1 if it does * not form a sub region. */ static int getRegionEnd(List<OpcodeBlock> blocks, int start, int end, boolean is_goto, OpcodeBlock ret) { // This is a rather brute force search for the next node after the start // node which post-dominates the preceding nodes. int end_extension = 0; int end_a = -1; int end_b = -1; check: while (true) { for (int o = 0; o < start; o++) { OpcodeBlock next = blocks.get(o); end_a = blocks.indexOf(next.getTarget()); if (next instanceof ConditionalOpcodeBlock) { ConditionalOpcodeBlock cond = (ConditionalOpcodeBlock) next; end_b = blocks.indexOf(cond.getTarget()); } else { end_b = -1; } if ((end_a > start && end_a < end) || (end_b > start && end_b < end)) { // If any block before the start points into the region then // our start node wasn't actually the start of a subregion. return -1; } } for (int o = start + 1; o < end; o++) { OpcodeBlock next = blocks.get(o); end_a = blocks.indexOf(next.getTarget()); if (next instanceof ConditionalOpcodeBlock) { if (end_a < o && next.getTarget() instanceof ConditionalOpcodeBlock) { ConditionalOpcodeBlock target = (ConditionalOpcodeBlock) next.getTarget(); for (OpcodeBlock op : target.getTargettedBy()) { if (op instanceof GotoOpcodeBlock && op.getStart() > next.getStart()) { end_a = blocks.indexOf(op); break; } } } ConditionalOpcodeBlock cond = (ConditionalOpcodeBlock) next; end_b = blocks.indexOf(cond.getTarget()); } else { end_b = -1; } int new_end = Math.max(end_a, end_b); if (new_end > end) { if (next instanceof GotoOpcodeBlock) { OpcodeBlock target = ((GotoOpcodeBlock) next).getTarget(); OpcodeBlock alt = next; int alt_end = o; for (OpcodeBlock block : target.getTargettedBy()) { if (block instanceof GotoOpcodeBlock) { int block_index = blocks.indexOf(block); if (block_index > start && block_index < end && block_index > alt_end) { alt_end = block_index; alt = block; } } } if (alt != next) { end = Math.max(end, alt_end); ((GotoOpcodeBlock) next).setTarget(alt); continue; } } else { OpcodeBlock target = next.getTarget(); OpcodeBlock alt = next; int alt_end = o; for (OpcodeBlock block : target.getTargettedBy()) { if (block instanceof GotoOpcodeBlock) { int block_index = blocks.indexOf(block); if (block_index > start && block_index < end && block_index > alt_end) { alt_end = block_index; alt = block; } } else if (block instanceof BreakMarkerOpcodeBlock) { int block_index = blocks.indexOf(block); if (block_index > start && block_index < end && block_index > alt_end) { alt_end = block_index; alt = block; } } } if (alt != next) { end = Math.max(end, alt_end); next.setTarget(alt); continue; } } // We've found a block inside the current region that points // to a block past the current end of the region. Resize the // region to include it and restart the search. end = new_end; continue check; } } if (is_goto) { OpcodeBlock next = blocks.get(end); int pos_ext = end_extension; while (next instanceof ConditionalOpcodeBlock) { ConditionalOpcodeBlock cond = (ConditionalOpcodeBlock) next; end_a = blocks.indexOf(cond.getTarget()); end_b = blocks.indexOf(cond.getElseTarget()); if(end_a == -1 && end_b == -1) { break; } if ((end_a > start && end_a < end) || (end_b > start && end_b < end)) { end_extension = ++pos_ext; next = blocks.get(end + end_extension); continue; } pos_ext++; next = blocks.get(end + pos_ext); } } for (int o = end + end_extension; o < blocks.size(); o++) { OpcodeBlock next = blocks.get(o); end_a = blocks.indexOf(next.getTarget()); if (next instanceof ConditionalOpcodeBlock) { ConditionalOpcodeBlock cond = (ConditionalOpcodeBlock) next; end_b = blocks.indexOf(cond.getTarget()); } else { end_b = -1; } if ((end_a > start && end_a < end) || (end_b > start && end_b < end)) { return -1; } } break; } if (end >= blocks.size()) { return -1; } return end + end_extension; } }
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/method/graph/GraphProducerStep.java
src/main/java/org/spongepowered/despector/decompiler/method/graph/GraphProducerStep.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.method.graph; import org.spongepowered.despector.decompiler.method.PartialMethod; import org.spongepowered.despector.decompiler.method.graph.data.opcode.OpcodeBlock; import java.util.List; import java.util.Set; /** * A producer for dividing up the opcodes into blocks and joining them together * into a graph. */ public interface GraphProducerStep { /** * Adds the indices of any opcodes that the opcode list should be split * after to the break_points set. */ void collectBreakpoints(PartialMethod partial, Set<Integer> break_points); /** * Forms edges between blocks in the graph. */ void formEdges(PartialMethod partial, List<Integer> sorted_break_points, List<OpcodeBlock> block_list); static OpcodeBlock find(List<OpcodeBlock> blocks, int op) { for (OpcodeBlock block : blocks) { if (block.getStart() <= op && block.getEnd() >= op) { return block; } } throw new IllegalStateException(); } }
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/method/graph/process/SwitchBlockProcessor.java
src/main/java/org/spongepowered/despector/decompiler/method/graph/process/SwitchBlockProcessor.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.method.graph.process; import org.spongepowered.despector.config.ConfigManager; import org.spongepowered.despector.decompiler.ir.Insn; import org.spongepowered.despector.decompiler.ir.JumpInsn; import org.spongepowered.despector.decompiler.ir.SwitchInsn; import org.spongepowered.despector.decompiler.method.PartialMethod; import org.spongepowered.despector.decompiler.method.graph.GraphOperation; import org.spongepowered.despector.decompiler.method.graph.GraphProcessor; import org.spongepowered.despector.decompiler.method.graph.data.block.BlockSection; import org.spongepowered.despector.decompiler.method.graph.data.block.CommentBlockSection; import org.spongepowered.despector.decompiler.method.graph.data.block.SwitchBlockSection; import org.spongepowered.despector.decompiler.method.graph.data.block.SwitchBlockSection.SwitchCaseBlockSection; import org.spongepowered.despector.decompiler.method.graph.data.opcode.BodyOpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.ConditionalOpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.GotoOpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.OpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.SwitchOpcodeBlock; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * A graph processor to process switch statements. */ public class SwitchBlockProcessor implements GraphProcessor { @Override public int process(PartialMethod partial, List<OpcodeBlock> blocks, OpcodeBlock region_start, List<BlockSection> final_blocks) { if (region_start instanceof SwitchOpcodeBlock) { SwitchOpcodeBlock sblock = (SwitchOpcodeBlock) region_start; SwitchInsn ts = (SwitchInsn) sblock.getLast(); // create block section for this switch SwitchBlockSection sswitch = new SwitchBlockSection(region_start); final_blocks.add(sswitch); Map<Integer, SwitchCaseBlockSection> cases = new HashMap<>(); OpcodeBlock end = null; int end_label = -1; OpcodeBlock fartherst = null; int farthest_break = 0; boolean all_return = true; for (Map.Entry<Integer, Integer> l : ts.getTargets().entrySet()) { SwitchCaseBlockSection cs = cases.get(l.getValue()); if (cs != null) { // if multiple targets for the same block we'll already have // a case made for this target cs.getTargets().add(l.getKey()); continue; } cs = sswitch.new SwitchCaseBlockSection(); sswitch.addCase(cs); cases.put(l.getValue(), cs); cs.getTargets().add(l.getKey()); List<OpcodeBlock> case_region = new ArrayList<>(); OpcodeBlock block = sblock.getAdditionalTargets().get(l.getKey()); case_region.add(block); int start = blocks.indexOf(block) + 1; if (start < blocks.size()) { block = blocks.get(start); while (!sblock.getAdditionalTargets().containsValue(block) && block != end) { // while we don't run into another case ass blocks to // this case // and we don't run into the end (which we'll find later // based on // the targets of the break statements). case_region.add(block); start++; if (start >= blocks.size()) { break; } block = blocks.get(start); } } OpcodeBlock last = case_region.get(case_region.size() - 1); if (last.getStart() > farthest_break) { // update the farthest block found fartherst = last; farthest_break = last.getStart(); } if (last instanceof BodyOpcodeBlock) { int op = last.getLast().getOpcode(); if (op != Insn.RETURN && op != Insn.ARETURN) { // not the case that all cases return all_return = false; } } else { all_return = false; } if (last instanceof GotoOpcodeBlock) { // break statements become gotos, so if we find a goto // at the end of the case we use its target to end the last // case end = last.getTarget(); GotoOpcodeBlock goto_block = (GotoOpcodeBlock) last; end_label = ((JumpInsn) last.getLast()).getTarget(); case_region.remove(last); cs.setBreaks(true); OpcodeBlock dummy_end = new BodyOpcodeBlock(goto_block.getStart(), goto_block.getEnd()); dummy_end.setTarget(goto_block); case_region.add(dummy_end); GraphOperation.remap(case_region, goto_block, dummy_end); for (OpcodeBlock o : case_region) { if (o instanceof ConditionalOpcodeBlock) { ConditionalOpcodeBlock c = (ConditionalOpcodeBlock) o; if (c.getTarget() == goto_block.getTarget() || c.getTarget().getStart() > dummy_end.getEnd()) { c.setTarget(dummy_end); } } } } else if (all_return) { boolean need_dummy = false; OpcodeBlock case_end = case_region.get(case_region.size() - 1); for (OpcodeBlock o : case_region) { if (o instanceof ConditionalOpcodeBlock) { ConditionalOpcodeBlock c = (ConditionalOpcodeBlock) o; if (c.getTarget().getStart() > case_end.getEnd()) { need_dummy = true; } } } if (need_dummy) { OpcodeBlock dummy_end = new BodyOpcodeBlock(case_end.getEnd(), case_end.getEnd()); case_region.add(dummy_end); for (OpcodeBlock o : case_region) { if (o instanceof ConditionalOpcodeBlock) { ConditionalOpcodeBlock c = (ConditionalOpcodeBlock) o; if (c.getTarget().getStart() > case_end.getEnd()) { c.setTarget(dummy_end); } } } } } try { // recursively flatten the case area partial.getDecompiler().flattenGraph(partial, case_region, case_region.size(), cs.getBody()); } catch (Throwable e) { if (ConfigManager.getConfig().print_opcodes_on_error) { List<String> comment = new ArrayList<>(); for (OpcodeBlock op : case_region) { comment.add(op.getDebugHeader()); for (Insn insn : op.getOpcodes()) { comment.add(insn.toString()); } } cs.getBody().add(new CommentBlockSection(comment)); } else { throw e; } } } SwitchCaseBlockSection cs = cases.get(ts.getDefault()); if (cs != null) { // set the case pointed to as default as the default block cs.setDefault(true); } else if (!all_return && end_label != ts.getDefault()) { // no block was pointed to as default, and they didn't all // return // (if they did all return then we have no way of telling where // the default case ends, and it doesn't matter that we emit it // anyway so we just ignore it and let it sit after the switch) // otherwise we build a new case for everything between the end // of our last case and the end block as the default block. cs = sswitch.new SwitchCaseBlockSection(); cases.put(ts.getDefault(), cs); sswitch.addCase(cs); List<OpcodeBlock> case_region = new ArrayList<>(); OpcodeBlock block = sblock.getAdditionalTargets().get(-1); case_region.add(block); int start = blocks.indexOf(block) + 1; if (start < blocks.size()) { block = blocks.get(start); while (!sblock.getAdditionalTargets().containsValue(block) && block != end) { case_region.add(block); start++; if (start >= blocks.size()) { break; } block = blocks.get(start); } } OpcodeBlock last = case_region.get(case_region.size() - 1); if (last.getStart() > farthest_break) { fartherst = last; farthest_break = last.getStart(); } cs.setDefault(true); try { partial.getDecompiler().flattenGraph(partial, case_region, case_region.size(), cs.getBody()); } catch (Exception e) { // TODO: should make a util function for this, it appears in // a lot of places if (ConfigManager.getConfig().print_opcodes_on_error) { List<String> comment = new ArrayList<>(); for (OpcodeBlock op : case_region) { comment.add(op.getDebugHeader()); for (Insn insn : op.getOpcodes()) { comment.add(insn.toString()); } } cs.getBody().add(new CommentBlockSection(comment)); } else { throw e; } } } if (end == null) { return blocks.indexOf(fartherst); } if (!blocks.contains(end)) { return blocks.size(); } // end points to the block after the last block which is part of // this switch so we subtract 1 return blocks.indexOf(end) - 1; } return -1; } }
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/method/graph/process/package-info.java
src/main/java/org/spongepowered/despector/decompiler/method/graph/process/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.method.graph.process;
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/method/graph/process/TryCatchBlockProcessor.java
src/main/java/org/spongepowered/despector/decompiler/method/graph/process/TryCatchBlockProcessor.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.method.graph.process; import static com.google.common.base.Preconditions.checkState; import org.spongepowered.despector.ast.Locals; import org.spongepowered.despector.config.ConfigManager; import org.spongepowered.despector.decompiler.ir.Insn; import org.spongepowered.despector.decompiler.ir.IntInsn; import org.spongepowered.despector.decompiler.method.PartialMethod; import org.spongepowered.despector.decompiler.method.graph.GraphProcessor; import org.spongepowered.despector.decompiler.method.graph.data.TryCatchMarkerType; import org.spongepowered.despector.decompiler.method.graph.data.block.BlockSection; import org.spongepowered.despector.decompiler.method.graph.data.block.CommentBlockSection; import org.spongepowered.despector.decompiler.method.graph.data.block.TryCatchBlockSection; import org.spongepowered.despector.decompiler.method.graph.data.block.TryCatchBlockSection.CatchBlockSection; import org.spongepowered.despector.decompiler.method.graph.data.opcode.GotoOpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.OpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.TryCatchMarkerOpcodeBlock; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; /** * A graph processor to process try-catch blocks. */ public class TryCatchBlockProcessor implements GraphProcessor { @Override public int process(PartialMethod partial, List<OpcodeBlock> blocks, OpcodeBlock region_start, List<BlockSection> final_blocks) { if (region_start instanceof TryCatchMarkerOpcodeBlock) { TryCatchMarkerOpcodeBlock marker = (TryCatchMarkerOpcodeBlock) region_start; checkState(marker.getType() == TryCatchMarkerType.START); List<OpcodeBlock> body = new ArrayList<>(); List<TryCatchMarkerOpcodeBlock> all_ends = new ArrayList<>(); for (int l = blocks.indexOf(marker.getEndMarker()); l < blocks.size(); l++) { OpcodeBlock next_end = blocks.get(l); if (!(next_end instanceof TryCatchMarkerOpcodeBlock)) { break; } TryCatchMarkerOpcodeBlock next_marker = (TryCatchMarkerOpcodeBlock) next_end; if (next_marker.getType() == TryCatchMarkerType.CATCH) { break; } checkState(next_marker.getType() == TryCatchMarkerType.END); all_ends.add(next_marker); } TryCatchMarkerOpcodeBlock last_start = all_ends.get(all_ends.size() - 1).getStartMarker(); TryCatchMarkerOpcodeBlock first_end = all_ends.get(0); for (int end = blocks.indexOf(last_start) + 1; end < blocks.indexOf(first_end); end++) { OpcodeBlock next = blocks.get(end); body.add(next); } int end = blocks.indexOf(all_ends.get(all_ends.size() - 1)) + 1; OpcodeBlock next = blocks.get(end); OpcodeBlock end_of_catch = null; int last_block = -1; if (next instanceof GotoOpcodeBlock) { end_of_catch = next.getTarget(); last_block = blocks.indexOf(end_of_catch); if (last_block > 1) { OpcodeBlock prev = blocks.get(last_block - 1); if (prev instanceof TryCatchMarkerOpcodeBlock && ((TryCatchMarkerOpcodeBlock) prev).getType() == TryCatchMarkerType.START) { last_block -= 2; } } } else { end--; } TryCatchBlockSection try_section = new TryCatchBlockSection(); try { partial.getDecompiler().flattenGraph(partial, body, body.size(), try_section.getBody()); } catch (Exception e) { if (ConfigManager.getConfig().print_opcodes_on_error) { List<String> comment = new ArrayList<>(); for (OpcodeBlock op : body) { comment.add(op.getDebugHeader()); for (Insn insn : op.getOpcodes()) { comment.add(insn.toString()); } } try_section.getBody().add(new CommentBlockSection(comment)); } else { throw e; } } while (!all_ends.isEmpty()) { end++; next = blocks.get(end); if (next instanceof TryCatchMarkerOpcodeBlock) { TryCatchMarkerOpcodeBlock next_marker = (TryCatchMarkerOpcodeBlock) next; checkState(next_marker.getType() == TryCatchMarkerType.CATCH); List<String> extra_exceptions = new ArrayList<>(); for (; end < blocks.size();) { OpcodeBlock cnext = blocks.get(end++); if (cnext instanceof TryCatchMarkerOpcodeBlock) { TryCatchMarkerOpcodeBlock cnext_marker = (TryCatchMarkerOpcodeBlock) cnext; boolean found = false; for (Iterator<TryCatchMarkerOpcodeBlock> it = all_ends.iterator(); it.hasNext();) { TryCatchMarkerOpcodeBlock t = it.next(); if (cnext_marker.getAsmNode() == t.getAsmNode()) { found = true; it.remove(); break; } } checkState(found); extra_exceptions.add(cnext_marker.getAsmNode().getException()); } else { end--; break; } } Collections.reverse(extra_exceptions); int label_index = -1; int local_num = -1; OpcodeBlock catch_start = blocks.get(end++); int k = 0; for (Iterator<Insn> it = catch_start.getOpcodes().iterator(); it.hasNext();) { Insn op = it.next(); if (op.getOpcode() == Insn.LOCAL_STORE) { local_num = ((IntInsn) op).getValue(); label_index = catch_start.getStart() - (catch_start.getOpcodes().size() - k); it.remove(); break; } else if (op.getOpcode() == Insn.POP) { it.remove(); break; } k++; } Locals.LocalInstance local = label_index == -1 ? null : partial.getLocals().getLocal(local_num).getInstance(label_index); List<OpcodeBlock> catch_body = new ArrayList<>(); catch_body.add(catch_start); int stop_index = -1; if (end_of_catch != null && last_block != -1) { for (int j = end; j < blocks.size(); j++) { OpcodeBlock cnext = blocks.get(j); if (cnext instanceof TryCatchMarkerOpcodeBlock) { break; } catch_body.add(cnext); if (cnext instanceof GotoOpcodeBlock && cnext.getTarget() == end_of_catch) { break; } else if (cnext == end_of_catch) { all_ends.clear(); break; } } stop_index = catch_body.size() - 1; } else { // TODO: if we have no lvt I'll need to do some // backup check of checking where the catch var is // last used and stopping there for (int j = end; j < blocks.size(); j++) { OpcodeBlock cnext = blocks.get(j); if (cnext.getEnd() > local.getEnd()) { break; } catch_body.add(cnext); } last_block = blocks.indexOf(catch_body.get(catch_body.size() - 1)); stop_index = catch_body.size(); } CatchBlockSection cblock = new CatchBlockSection(extra_exceptions, local); try { partial.getDecompiler().flattenGraph(partial, catch_body, stop_index, cblock.getBody()); } catch (Exception e) { if (ConfigManager.getConfig().print_opcodes_on_error) { List<String> comment = new ArrayList<>(); for (OpcodeBlock op : catch_body) { comment.add(op.getDebugHeader()); for (Insn insn : op.getOpcodes()) { comment.add(insn.toString()); } } cblock.getBody().add(new CommentBlockSection(comment)); } else { throw e; } } try_section.getCatchBlocks().add(cblock); } } final_blocks.add(try_section); return last_block; } return -1; } }
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/method/graph/process/InternalBlockProcessor.java
src/main/java/org/spongepowered/despector/decompiler/method/graph/process/InternalBlockProcessor.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.method.graph.process; import org.spongepowered.despector.decompiler.method.PartialMethod; import org.spongepowered.despector.decompiler.method.graph.GraphProcessor; import org.spongepowered.despector.decompiler.method.graph.data.block.BlockSection; import org.spongepowered.despector.decompiler.method.graph.data.opcode.OpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.ProcessedOpcodeBlock; import java.util.List; /** * A graph processor that extracts a precompiled section from a block. */ public class InternalBlockProcessor implements GraphProcessor { @Override public int process(PartialMethod partial, List<OpcodeBlock> blocks, OpcodeBlock region_start, List<BlockSection> final_blocks) { if (region_start instanceof ProcessedOpcodeBlock) { final_blocks.add(((ProcessedOpcodeBlock) region_start).getPrecompiledSection()); return blocks.indexOf(region_start); } return -1; } }
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/method/graph/process/SubRegionBlockProcessor.java
src/main/java/org/spongepowered/despector/decompiler/method/graph/process/SubRegionBlockProcessor.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.method.graph.process; import org.spongepowered.despector.config.ConfigManager; import org.spongepowered.despector.config.LibraryConfiguration; import org.spongepowered.despector.decompiler.ir.Insn; import org.spongepowered.despector.decompiler.method.PartialMethod; import org.spongepowered.despector.decompiler.method.graph.GraphProcessor; import org.spongepowered.despector.decompiler.method.graph.RegionProcessor; import org.spongepowered.despector.decompiler.method.graph.data.block.BlockSection; import org.spongepowered.despector.decompiler.method.graph.data.block.CommentBlockSection; import org.spongepowered.despector.decompiler.method.graph.data.block.InlineBlockSection; import org.spongepowered.despector.decompiler.method.graph.data.opcode.BodyOpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.ConditionalOpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.GotoOpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.OpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.ProcessedOpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.TryCatchMarkerOpcodeBlock; import org.spongepowered.despector.util.AstUtil; import java.util.ArrayList; import java.util.List; /** * A graph processor that checks the region for the next sub region and * processes it. */ public class SubRegionBlockProcessor implements GraphProcessor { @Override public int process(PartialMethod partial, List<OpcodeBlock> blocks, OpcodeBlock region_start, List<BlockSection> final_blocks) { // here we're splitting a graph into regions by points that both // dominate and post-dominate the rest of the graph (eg. points that all // control points pass through). // we can then process these regions and transform them into a single // block representing the control flow statement of the region. Each // region will always contain exactly one control flow statement (not // counting nesting). // We iterate the blocks in order to ensure that we always fine the // earliest block of any control flow statement which makes sure that we // don't miss any parts of the control flow statement. int i = blocks.indexOf(region_start); int end = -1; boolean targeted_in_future = false; if (!(region_start instanceof ConditionalOpcodeBlock) && !(region_start instanceof GotoOpcodeBlock)) { for (OpcodeBlock t : region_start.getTargettedBy()) { int index = blocks.indexOf(t); if (index > i) { targeted_in_future = true; if (index > end) { end = index; } } } // if the block is targeted by a block farther in the code then // this block is the first block in a do_while loop if (!targeted_in_future) { // If the next block isn't conditional then we simply append // it // to the output. final_blocks.add(new InlineBlockSection(region_start)); return i; } } if (end == -1) { end = RegionProcessor.getRegionEnd(blocks, null, i); } else if (end != blocks.size() - 1) { end++; } if (end == -1) { // Any conditional block should always form the start of a // region at this level. throw new IllegalStateException("Conditional jump not part of control flow statement??"); } OpcodeBlock last = blocks.get(end); if (blocks.get(end - 1) instanceof TryCatchMarkerOpcodeBlock) { end--; } List<OpcodeBlock> region = new ArrayList<>(); for (int o = i; o < end; o++) { region.add(blocks.get(o)); } boolean dummy_end_used = false; if (end == blocks.size() - 1 && region_start.getTarget().getStart() == last.getEnd() + 1) { region.add(blocks.get(end)); OpcodeBlock dummy = new BodyOpcodeBlock(last.getEnd() + 1, last.getEnd() + 1); last = dummy; region.add(dummy); dummy_end_used = true; } OpcodeBlock first = region.get(0); if (first instanceof ConditionalOpcodeBlock && AstUtil.hasStartingRequirement(first.getOpcodes()) && ((ConditionalOpcodeBlock) first).getPrefix() == null) { OpcodeBlock prev = blocks.get(i - 1); if (prev instanceof ProcessedOpcodeBlock) { ((ConditionalOpcodeBlock) first).setPrefix(prev); } } // process the region down to a single block try { final_blocks.add(partial.getDecompiler().processRegion(partial, region, last, targeted_in_future ? 0 : 1)); } catch (Exception e) { if (ConfigManager.getConfig().print_opcodes_on_error) { List<String> comment = new ArrayList<>(); for (OpcodeBlock op : region) { comment.add(op.getDebugHeader()); for (Insn insn : op.getOpcodes()) { comment.add(insn.toString()); } } OpcodeBlock op = blocks.get(end); if (targeted_in_future && op instanceof GotoOpcodeBlock) { comment.add(op.getDebugHeader()); for (Insn insn : op.getOpcodes()) { comment.add(insn.toString()); } } final_blocks.add(new CommentBlockSection(comment)); if (!LibraryConfiguration.quiet) { System.err.println("Error decompiling subregion of " + partial.getEntry()); e.printStackTrace(); } } else { throw e; } } if (targeted_in_future && blocks.get(end) instanceof GotoOpcodeBlock) { return end; } if(dummy_end_used) { return end; } return end - 1; } }
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/method/graph/region/ChildRegionProcessor.java
src/main/java/org/spongepowered/despector/decompiler/method/graph/region/ChildRegionProcessor.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.method.graph.region; import static com.google.common.base.Preconditions.checkState; import org.spongepowered.despector.decompiler.method.MethodDecompiler; import org.spongepowered.despector.decompiler.method.PartialMethod; import org.spongepowered.despector.decompiler.method.graph.GraphOperation; import org.spongepowered.despector.decompiler.method.graph.RegionProcessor; import org.spongepowered.despector.decompiler.method.graph.data.block.BlockSection; import org.spongepowered.despector.decompiler.method.graph.data.block.BreakBlockSection; import org.spongepowered.despector.decompiler.method.graph.data.opcode.BreakMarkerOpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.ConditionalOpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.GotoOpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.OpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.ProcessedOpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.SwitchOpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.TryCatchMarkerOpcodeBlock; import java.util.ArrayList; import java.util.List; /** * A region processor which checks for sub regions and recursively processes * them. */ public class ChildRegionProcessor implements RegionProcessor { @Override public BlockSection process(PartialMethod partial, List<OpcodeBlock> region, OpcodeBlock ret, int body_start) { if (partial.getEntry().getName().equals(MethodDecompiler.targeted_breakpoint)) { System.out.println("Searching for child region in range " + region.get(body_start).getStart() + " to " + ret.getStart()); } // The first step is to find any points within the region that are sub // regions (eg. both dominate and post-domintate the rest of the graph // excluding the end point of the current region). These sub-regions are // then processes ahead of time into their own control flow statements // which are then nested inside of this region. int subregion_search_end = 1; boolean is_first_condition = true; OpcodeBlock sstart = region.get(0); if (sstart instanceof SwitchOpcodeBlock) { // handled by switch processor return null; } if (sstart instanceof GotoOpcodeBlock) { subregion_search_end = region.size() - region.indexOf(sstart.getTarget()); is_first_condition = false; } for (int i = body_start; i < region.size() - subregion_search_end; i++) { OpcodeBlock next = region.get(i); if (next instanceof SwitchOpcodeBlock) { int last = i; SwitchOpcodeBlock sswitch = (SwitchOpcodeBlock) next; for (OpcodeBlock b : sswitch.getAdditionalTargets().values()) { int o = region.indexOf(b); if (o == -1) { continue; } if (o > last) { last = o; } if (region.get(o - 1) instanceof GotoOpcodeBlock) { OpcodeBlock end = region.get(o - 1).getTarget(); int j = region.indexOf(end); if (j > last) { last = j; } } } // form subregion from switch bounds List<OpcodeBlock> subregion = new ArrayList<>(); for (int o = i; o <= last; o++) { OpcodeBlock n = region.get(o); subregion.add(n); } OpcodeBlock sub_ret = last >= region.size() ? ret : region.get(last); List<BlockSection> secs = new ArrayList<>(); partial.getDecompiler().flattenGraph(partial, subregion, subregion.size() - 1, secs); checkState(secs.size() == 1); // the first block is set to the condensed subregion block and // the rest if the blocks in the subregion are removed. ProcessedOpcodeBlock replacement = new ProcessedOpcodeBlock(region.get(i).getStart(), region.get(last).getEnd(), secs.get(0)); replacement.setTarget(sub_ret); GraphOperation.remap(region, region.get(i), replacement); region.set(i, replacement); for (int o = last - 1; o > i; o--) { region.remove(o); } continue; } else if(next instanceof TryCatchMarkerOpcodeBlock) { TryCatchMarkerOpcodeBlock s = (TryCatchMarkerOpcodeBlock) next; int end = region.indexOf(s.getEndMarker()); if (end != -1) { OpcodeBlock after = region.get(end + 1); if(after instanceof GotoOpcodeBlock) { OpcodeBlock after_target = ((GotoOpcodeBlock) after).getTarget(); end = region.indexOf(after_target); if (end == -1 && after_target == ret) { end = region.size(); } List<OpcodeBlock> subregion = new ArrayList<>(); for (int o = i; o < end; o++) { OpcodeBlock n = region.get(o); subregion.add(n); } OpcodeBlock sub_ret = end >= region.size() ? ret : region.get(end); List<BlockSection> secs = new ArrayList<>(); partial.getDecompiler().flattenGraph(partial, subregion, subregion.size() - 1, secs); checkState(secs.size() == 1); // the first block is set to the condensed subregion block and // the rest if the blocks in the subregion are removed. ProcessedOpcodeBlock replacement = new ProcessedOpcodeBlock(region.get(i).getStart(), region.get(end - 1).getEnd(), secs.get(0)); replacement.setTarget(sub_ret); GraphOperation.remap(region, region.get(i), replacement); GraphOperation.remap(region, region.get(i+1), replacement); region.set(i, replacement); for (int o = end - 1; o > i; o--) { region.remove(o); } continue; } } } else if (!(next instanceof ConditionalOpcodeBlock) && !(next instanceof GotoOpcodeBlock)) { is_first_condition = false; continue; } // The end block is already not included in `region` so we can // simply try and get the region end of any block in this region and // if there is an end defined then we know that it forms a sub // region. int end = -1; if (next.getTarget() == ret) { end = RegionProcessor.getRegionEnd(region, ret, i); if (end == region.size()) { OpcodeBlock last = region.get(region.size() - 1); boolean is_break = false; if (last instanceof GotoOpcodeBlock) { GotoOpcodeBlock last_goto = (GotoOpcodeBlock) last; if (last_goto.getTarget() == sstart) { // while loop and this is a break; is_break = true; } else if (last_goto.getTarget() == next) { is_first_condition = false; } } else if (last instanceof ConditionalOpcodeBlock) { ConditionalOpcodeBlock cond = (ConditionalOpcodeBlock) last; if (cond.getTarget() == sstart) { is_break = true; } boolean is_condition_part = true; for (int j = i + 1; j < region.size() - 1; j++) { if (!(region.get(i) instanceof ConditionalOpcodeBlock)) { is_condition_part = false; break; } } if (is_condition_part) { continue; } } if (is_break) { BreakBlockSection sec = null; OpcodeBlock prev = region.get(i - 1); if (prev instanceof ProcessedOpcodeBlock) { BlockSection compiled = ((ProcessedOpcodeBlock) prev).getPrecompiledSection(); if (compiled instanceof BreakBlockSection) { sec = (BreakBlockSection) compiled; } } else if (is_first_condition) { continue; } if (sec == null) { sec = new BreakBlockSection(new BreakMarkerOpcodeBlock(next.getStart(), next.getStart(), BreakMarkerOpcodeBlock.MarkerType.BREAK), BreakMarkerOpcodeBlock.MarkerType.BREAK); sec.getInlinedConditions().add((ConditionalOpcodeBlock) next); OpcodeBlock replace = new ProcessedOpcodeBlock(next.getStart(), next.getStart(), sec); region.set(i, replace); GraphOperation.remap(region, next, replace); } else { sec.getInlinedConditions().add((ConditionalOpcodeBlock) next); region.remove(i); i--; } continue; } } else if (end == -1 && next instanceof ConditionalOpcodeBlock) { OpcodeBlock pos = null; for (OpcodeBlock targeting : ret.getTargettedBy()) { if (targeting instanceof GotoOpcodeBlock) { GotoOpcodeBlock ggoto = (GotoOpcodeBlock) targeting; if (ggoto.getEnd() < sstart.getTarget().getStart() && ggoto.getStart() > next.getEnd() && (pos == null || ggoto.getStart() < pos.getStart())) { pos = ggoto; } } else if (targeting instanceof BreakMarkerOpcodeBlock) { BreakMarkerOpcodeBlock bbreak = (BreakMarkerOpcodeBlock) targeting; if (bbreak.getEnd() < sstart.getTarget().getStart() && bbreak.getStart() > next.getEnd() && (pos == null || bbreak.getStart() < pos.getStart())) { pos = bbreak; } } } if (pos != null) { next.setTarget(pos); end = region.indexOf(pos); } } } else if (next.getTarget() == sstart) { // this is a continue statement boolean part_of_end = true; for (int o = i + 1; o < region.size() - subregion_search_end; o++) { OpcodeBlock n = region.get(o); if (n instanceof ConditionalOpcodeBlock) { if (n.getTarget() == sstart || n.getTarget() == ret) { continue; } } part_of_end = false; break; } if (part_of_end) { return null; } BreakBlockSection sec = null; if (region.get(i - 1) instanceof ProcessedOpcodeBlock) { BlockSection compiled = ((ProcessedOpcodeBlock) region.get(i - 1)).getPrecompiledSection(); if (compiled instanceof BreakBlockSection) { sec = (BreakBlockSection) compiled; } } if (sec == null) { sec = new BreakBlockSection(new BreakMarkerOpcodeBlock(next.getStart(), next.getEnd(), BreakMarkerOpcodeBlock.MarkerType.CONTINUE), BreakMarkerOpcodeBlock.MarkerType.CONTINUE); sec.getInlinedConditions().add((ConditionalOpcodeBlock) next); OpcodeBlock replace = new ProcessedOpcodeBlock(next.getStart(), next.getEnd(), sec); region.set(i, replace); GraphOperation.remap(region, next, replace); } else { sec.getInlinedConditions().add((ConditionalOpcodeBlock) next); region.remove(i); i--; } OpcodeBlock last = region.get(region.size() - 1); if (!(last instanceof GotoOpcodeBlock) && sstart instanceof ConditionalOpcodeBlock) { GotoOpcodeBlock fake_loop = new GotoOpcodeBlock(last.getStart(), last.getEnd()); fake_loop.setTarget(sstart); region.add(fake_loop); } continue; } else { end = RegionProcessor.getRegionEnd(region, ret, i); } if (end != -1) { if (partial.getEntry().getName().equals(MethodDecompiler.targeted_breakpoint)) { System.out.println("Child region found from " + next.getStart() + " to " + region.get(end - 1).getStart()); } List<OpcodeBlock> subregion = new ArrayList<>(); for (int o = i; o < end; o++) { OpcodeBlock n = region.get(o); subregion.add(n); } OpcodeBlock sub_ret = end >= region.size() ? ret : region.get(end); BlockSection s = partial.getDecompiler().processRegion(partial, subregion, sub_ret, 1); // the first block is set to the condensed subregion block and // the rest if the blocks in the subregion are removed. OpcodeBlock region_start = region.get(i); ProcessedOpcodeBlock replacement = new ProcessedOpcodeBlock(region_start.getStart(), region_start.getEnd(), s); replacement.setTarget(sub_ret); GraphOperation.remap(region, region_start, replacement); region.set(i, replacement); for (int o = end - 1; o > i; o--) { region.remove(o); } } } return 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/decompiler/method/graph/region/IfBlockRegionProcessor.java
src/main/java/org/spongepowered/despector/decompiler/method/graph/region/IfBlockRegionProcessor.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.method.graph.region; import org.spongepowered.despector.ast.insn.condition.Condition; import org.spongepowered.despector.decompiler.method.ConditionBuilder; import org.spongepowered.despector.decompiler.method.PartialMethod; import org.spongepowered.despector.decompiler.method.graph.RegionProcessor; import org.spongepowered.despector.decompiler.method.graph.data.block.BlockSection; import org.spongepowered.despector.decompiler.method.graph.data.block.IfBlockSection; import org.spongepowered.despector.decompiler.method.graph.data.block.IfBlockSection.ElifBlockSection; import org.spongepowered.despector.decompiler.method.graph.data.block.WhileBlockSection; import org.spongepowered.despector.decompiler.method.graph.data.opcode.ConditionalOpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.GotoOpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.OpcodeBlock; import java.util.ArrayList; import java.util.List; /** * A region processor that tries to form an if-elseif-else block from the * region. This is designed to be called last when processing regions and will * always find an if statement or else error. */ public class IfBlockRegionProcessor implements RegionProcessor { @Override public BlockSection process(PartialMethod partial, List<OpcodeBlock> region, OpcodeBlock ret, int body_start) { OpcodeBlock start = region.get(0); // split the region into the condition and the body body_start = 1; List<ConditionalOpcodeBlock> condition_blocks = new ArrayList<>(); OpcodeBlock next = region.get(body_start); condition_blocks.add((ConditionalOpcodeBlock) start); while (next instanceof ConditionalOpcodeBlock) { condition_blocks.add((ConditionalOpcodeBlock) next); body_start++; next = region.get(body_start); } OpcodeBlock body = region.get(body_start); OpcodeBlock cond_ret = ret; for (OpcodeBlock c : condition_blocks) { if (c.getTarget() != body && !condition_blocks.contains(c.getTarget())) { cond_ret = c.getTarget(); break; } } for (OpcodeBlock c : condition_blocks) { if (c instanceof ConditionalOpcodeBlock) { ConditionalOpcodeBlock cond = (ConditionalOpcodeBlock) c; if (cond.getTarget().equals(body) && cond.getTarget() != body) { cond.setTarget(body); } if (cond.getElseTarget().equals(body) && cond.getElseTarget() != body) { cond.setElseTarget(body); } if (cond.getTarget().equals(cond_ret) && cond.getTarget() != cond_ret) { cond.setTarget(cond_ret); } if (cond.getElseTarget().equals(cond_ret) && cond.getElseTarget() != cond_ret) { cond.setElseTarget(cond_ret); } } } // form the condition from the header Condition cond = ConditionBuilder.makeCondition(condition_blocks, partial.getLocals(), body, cond_ret); int else_start = region.size(); if (cond_ret != ret && region.contains(cond_ret)) { else_start = region.indexOf(cond_ret); } OpcodeBlock body_end = region.get(else_start - 1); if (body_end instanceof GotoOpcodeBlock && body_end.getTarget() == start) { // we've got ourselves an inverse while/for loop WhileBlockSection section = new WhileBlockSection(cond); for (int i = body_start; i < else_start - 1; i++) { next = region.get(i); section.appendBody(next.toBlockSection()); } return section; } if (body_end instanceof GotoOpcodeBlock) { else_start--; } IfBlockSection section = new IfBlockSection(cond); // Append the body for (int i = body_start; i < else_start; i++) { next = region.get(i); if (next instanceof ConditionalOpcodeBlock) { List<OpcodeBlock> subregion = new ArrayList<>(); for (int o = i; o < else_start; o++) { OpcodeBlock n = region.get(o); subregion.add(n); } OpcodeBlock sub_ret = else_start >= region.size() ? ret : region.get(else_start); BlockSection s = partial.getDecompiler().processRegion(partial, subregion, sub_ret, 1); section.appendBody(s); break; } section.appendBody(next.toBlockSection()); } while (region.contains(cond_ret)) { if (cond_ret instanceof ConditionalOpcodeBlock) { List<ConditionalOpcodeBlock> elif_condition = new ArrayList<>(); next = region.get(body_start); elif_condition.add((ConditionalOpcodeBlock) cond_ret); body_start = region.indexOf(cond_ret) + 1; while (next instanceof ConditionalOpcodeBlock) { elif_condition.add((ConditionalOpcodeBlock) next); body_start++; next = region.get(body_start); } OpcodeBlock elif_body = region.get(body_start); cond_ret = ret; for (OpcodeBlock c : elif_condition) { if (c.getTarget() != elif_body && !elif_condition.contains(c.getTarget())) { cond_ret = c.getTarget(); break; } } Condition elif_cond = ConditionBuilder.makeCondition(elif_condition, partial.getLocals(), elif_body, cond_ret); ElifBlockSection elif = section.new ElifBlockSection(elif_cond); int elif_end = region.size(); if (cond_ret != ret) { elif_end = region.indexOf(cond_ret); } if (region.get(elif_end - 1) instanceof GotoOpcodeBlock) { elif_end--; } // Append the body for (int i = body_start; i < elif_end; i++) { next = region.get(i); elif.append(next.toBlockSection()); } } else { else_start = region.indexOf(cond_ret); for (int i = else_start; i < region.size(); i++) { next = region.get(i); section.appendElseBody(next.toBlockSection()); } break; } } return section; } }
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/method/graph/region/package-info.java
src/main/java/org/spongepowered/despector/decompiler/method/graph/region/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.method.graph.region;
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/method/graph/region/WhileRegionProcessor.java
src/main/java/org/spongepowered/despector/decompiler/method/graph/region/WhileRegionProcessor.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.method.graph.region; import org.spongepowered.despector.ast.insn.condition.Condition; import org.spongepowered.despector.decompiler.method.ConditionBuilder; import org.spongepowered.despector.decompiler.method.PartialMethod; import org.spongepowered.despector.decompiler.method.graph.RegionProcessor; import org.spongepowered.despector.decompiler.method.graph.data.block.BlockSection; import org.spongepowered.despector.decompiler.method.graph.data.block.BreakBlockSection; import org.spongepowered.despector.decompiler.method.graph.data.block.BreakableBlockSection; import org.spongepowered.despector.decompiler.method.graph.data.block.DoWhileBlockSection; import org.spongepowered.despector.decompiler.method.graph.data.block.IfBlockSection; import org.spongepowered.despector.decompiler.method.graph.data.block.IfBlockSection.ElifBlockSection; import org.spongepowered.despector.decompiler.method.graph.data.block.SwitchBlockSection; import org.spongepowered.despector.decompiler.method.graph.data.block.SwitchBlockSection.SwitchCaseBlockSection; import org.spongepowered.despector.decompiler.method.graph.data.block.TryCatchBlockSection; import org.spongepowered.despector.decompiler.method.graph.data.block.TryCatchBlockSection.CatchBlockSection; import org.spongepowered.despector.decompiler.method.graph.data.block.WhileBlockSection; import org.spongepowered.despector.decompiler.method.graph.data.opcode.BreakMarkerOpcodeBlock.MarkerType; import org.spongepowered.despector.decompiler.method.graph.data.opcode.ConditionalOpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.GotoOpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.OpcodeBlock; import java.util.ArrayList; import java.util.List; /** * A region processor that checks if the region forms a while loop. */ public class WhileRegionProcessor implements RegionProcessor { @Override public BlockSection process(PartialMethod partial, List<OpcodeBlock> region, OpcodeBlock ret, int body_start) { OpcodeBlock start = region.get(0); if (start instanceof GotoOpcodeBlock) { List<ConditionalOpcodeBlock> condition_blocks = new ArrayList<>(); OpcodeBlock next = start.getTarget(); int pos = region.indexOf(next); int cond_start = pos; while (next instanceof ConditionalOpcodeBlock) { condition_blocks.add((ConditionalOpcodeBlock) next); pos++; if (pos >= region.size()) { break; } next = region.get(pos); } OpcodeBlock body = region.get(1); Condition cond = ConditionBuilder.makeCondition(condition_blocks, partial.getLocals(), body, ret); WhileBlockSection section = new WhileBlockSection(cond); for (int i = 1; i < cond_start; i++) { next = region.get(i); BlockSection block = next.toBlockSection(); replaceBreaks(block, start.getTarget(), ret, section, false); section.appendBody(block); } return section; } return null; } /** * Replaces break marker blocks within the region. */ public static void replaceBreaks(BlockSection section, OpcodeBlock continue_point, OpcodeBlock break_point, BreakableBlockSection loop, boolean nested) { if (section instanceof BreakBlockSection) { BreakBlockSection bbreak = (BreakBlockSection) section; OpcodeBlock target = bbreak.getMarker().getOldTarget(); if (bbreak.getType() == MarkerType.BREAK && target == break_point) { loop.getBreaks().add(bbreak); bbreak.setLoop((BlockSection) loop); bbreak.setNested(nested); } else if (bbreak.getType() == MarkerType.CONTINUE && target == continue_point) { loop.getBreaks().add(bbreak); bbreak.setLoop((BlockSection) loop); bbreak.setNested(nested); } } else if (section instanceof WhileBlockSection) { WhileBlockSection wwhile = (WhileBlockSection) section; for (BlockSection block : wwhile.getBody()) { replaceBreaks(block, continue_point, break_point, loop, true); } } else if (section instanceof IfBlockSection) { IfBlockSection iif = (IfBlockSection) section; for (BlockSection block : iif.getBody()) { replaceBreaks(block, continue_point, break_point, loop, nested); } for (BlockSection block : iif.getElseBody()) { replaceBreaks(block, continue_point, break_point, loop, nested); } for (ElifBlockSection elif : iif.getElifBlocks()) { for (BlockSection block : elif.getBody()) { replaceBreaks(block, continue_point, break_point, loop, nested); } } } else if (section instanceof DoWhileBlockSection) { DoWhileBlockSection dowhile = (DoWhileBlockSection) section; for (BlockSection block : dowhile.getBody()) { replaceBreaks(block, continue_point, break_point, loop, true); } } else if (section instanceof SwitchBlockSection) { SwitchBlockSection sswitch = (SwitchBlockSection) section; for (SwitchCaseBlockSection cs : sswitch.getCases()) { for (BlockSection block : cs.getBody()) { replaceBreaks(block, continue_point, break_point, loop, true); } } } else if (section instanceof TryCatchBlockSection) { TryCatchBlockSection trycatch = (TryCatchBlockSection) section; for (BlockSection block : trycatch.getBody()) { replaceBreaks(block, continue_point, break_point, loop, nested); } for (CatchBlockSection cb : trycatch.getCatchBlocks()) { for (BlockSection block : cb.getBody()) { replaceBreaks(block, continue_point, break_point, loop, nested); } } } } }
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/method/graph/region/DoWhileRegionProcessor.java
src/main/java/org/spongepowered/despector/decompiler/method/graph/region/DoWhileRegionProcessor.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.method.graph.region; import static com.google.common.base.Preconditions.checkState; import org.spongepowered.despector.ast.insn.condition.BooleanCondition; import org.spongepowered.despector.ast.insn.condition.Condition; import org.spongepowered.despector.ast.insn.cst.IntConstant; import org.spongepowered.despector.decompiler.method.ConditionBuilder; import org.spongepowered.despector.decompiler.method.PartialMethod; import org.spongepowered.despector.decompiler.method.graph.RegionProcessor; import org.spongepowered.despector.decompiler.method.graph.data.block.BlockSection; import org.spongepowered.despector.decompiler.method.graph.data.block.DoWhileBlockSection; import org.spongepowered.despector.decompiler.method.graph.data.block.WhileBlockSection; import org.spongepowered.despector.decompiler.method.graph.data.opcode.ConditionalOpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.GotoOpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.OpcodeBlock; import java.util.ArrayList; import java.util.List; /** * A region processor that checks if the region forms a do-while loop. */ public class DoWhileRegionProcessor implements RegionProcessor { @Override public BlockSection process(PartialMethod partial, List<OpcodeBlock> region, OpcodeBlock ret, int body_start) { OpcodeBlock start = region.get(0); if (body_start == 0) { List<ConditionalOpcodeBlock> condition_blocks = new ArrayList<>(); int cond_start = region.size() - 1; OpcodeBlock next = region.get(cond_start); while (next instanceof ConditionalOpcodeBlock) { condition_blocks.add(0, (ConditionalOpcodeBlock) next); cond_start--; next = region.get(cond_start); } Condition cond = null; if (condition_blocks.isEmpty()) { checkState(ret instanceof GotoOpcodeBlock); WhileBlockSection section = new WhileBlockSection(new BooleanCondition(new IntConstant(1), false)); for (int i = 0; i < region.size(); i++) { next = region.get(i); section.appendBody(next.toBlockSection()); } return section; } cond_start++; cond = ConditionBuilder.makeCondition(condition_blocks, partial.getLocals(), start, ret); boolean targetted_by_previous_jump = condition_blocks.get(0).getTargettedBy().stream() .filter((b) -> b.getStart() < start.getStart()).findAny().isPresent(); if (targetted_by_previous_jump) { WhileBlockSection section = new WhileBlockSection(cond); for (int i = 0; i < cond_start; i++) { next = region.get(i); section.appendBody(next.toBlockSection()); } return section; } DoWhileBlockSection section = new DoWhileBlockSection(cond); for (int i = 0; i < cond_start; i++) { next = region.get(i); section.append(next.toBlockSection()); } return section; } return 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/decompiler/method/graph/operate/package-info.java
src/main/java/org/spongepowered/despector/decompiler/method/graph/operate/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.method.graph.operate;
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/method/graph/operate/JumpSeparateOperation.java
src/main/java/org/spongepowered/despector/decompiler/method/graph/operate/JumpSeparateOperation.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.method.graph.operate; import org.spongepowered.despector.decompiler.ir.Insn; import org.spongepowered.despector.decompiler.method.PartialMethod; import org.spongepowered.despector.decompiler.method.graph.GraphOperation; import org.spongepowered.despector.decompiler.method.graph.data.opcode.BodyOpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.ConditionalOpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.GotoOpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.OpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.SwitchOpcodeBlock; import org.spongepowered.despector.util.AstUtil; import java.util.ArrayList; import java.util.List; /** * a graph operation that splits the opcodes that are before a jump but not part * of the actual jump statement away from the jump. */ public class JumpSeparateOperation implements GraphOperation { @Override public void process(PartialMethod partial) { // Split the opcodes that form the condition away from the preceding // statements. List<OpcodeBlock> blocks = partial.getGraph(); List<OpcodeBlock> fblocks = new ArrayList<>(); for (OpcodeBlock block : blocks) { if (block instanceof GotoOpcodeBlock) { OpcodeBlock header = new BodyOpcodeBlock(block.getStart(), block.getEnd() - 1); if(block.getOpcodes().size() == 1) { fblocks.add(block); continue; } for(int i = 0; i < block.getOpcodes().size() - 1; i++) { header.getOpcodes().add(block.getOpcodes().get(i)); } Insn ggoto = block.getOpcodes().get(block.getOpcodes().size() - 1); block.getOpcodes().clear(); block.getOpcodes().add(ggoto); block.setBounds(block.getEnd(), block.getEnd()); // Have to ensure that we remap any blocks that were // targeting this block to target the header. GraphOperation.remap(blocks, block, header); GraphOperation.remap(fblocks, block, header); header.setTarget(block); fblocks.add(header); fblocks.add(block); } else if (block instanceof ConditionalOpcodeBlock || block instanceof SwitchOpcodeBlock) { int cond_start = AstUtil.findStartLastStatement(block.getOpcodes()); if (cond_start > 0) { OpcodeBlock header = new BodyOpcodeBlock(block.getStart(), block.getStart() + cond_start - 1); block.setBounds(block.getStart() + cond_start, block.getEnd()); for (int i = 0; i < cond_start; i++) { header.getOpcodes().add(block.getOpcodes().get(i)); } for (int i = cond_start - 1; i >= 0; i--) { block.getOpcodes().remove(i); } // Have to ensure that we remap any blocks that were // targeting this block to target the header. GraphOperation.remap(blocks, block, header); GraphOperation.remap(fblocks, block, header); header.setTarget(block); fblocks.add(header); fblocks.add(block); } else { fblocks.add(block); } } else { fblocks.add(block); } } blocks.clear(); blocks.addAll(fblocks); } }
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/method/graph/operate/TernaryPrePassOperation.java
src/main/java/org/spongepowered/despector/decompiler/method/graph/operate/TernaryPrePassOperation.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.method.graph.operate; import static com.google.common.base.Preconditions.checkState; import org.spongepowered.despector.ast.Locals; import org.spongepowered.despector.ast.insn.condition.Condition; import org.spongepowered.despector.decompiler.method.ConditionBuilder; import org.spongepowered.despector.decompiler.method.PartialMethod; import org.spongepowered.despector.decompiler.method.PartialMethod.TryCatchRegion; import org.spongepowered.despector.decompiler.method.graph.GraphOperation; import org.spongepowered.despector.decompiler.method.graph.data.TryCatchMarkerType; import org.spongepowered.despector.decompiler.method.graph.data.block.InlineBlockSection; import org.spongepowered.despector.decompiler.method.graph.data.block.TernaryBlockSection; import org.spongepowered.despector.decompiler.method.graph.data.opcode.ConditionalOpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.GotoOpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.OpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.ProcessedOpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.TryCatchMarkerOpcodeBlock; import org.spongepowered.despector.util.AstUtil; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; /** * A graph operation that pre-processes ternaries before the rest of the graph * processing. */ public class TernaryPrePassOperation implements GraphOperation { @Override public void process(PartialMethod partial) { List<OpcodeBlock> blocks = partial.getGraph(); for (int i = 0; i < blocks.size(); i++) { OpcodeBlock block = blocks.get(i); if (block instanceof TryCatchMarkerOpcodeBlock) { TryCatchMarkerOpcodeBlock tc = (TryCatchMarkerOpcodeBlock) block; if (tc.getType() == TryCatchMarkerType.CATCH) { blocks.get(i + 1).omitFromTernaryCheck(true); } } else if (block instanceof ProcessedOpcodeBlock && ((ProcessedOpcodeBlock) block).getPrecompiledSection() instanceof TernaryBlockSection) { blocks.get(i + 1).omitFromTernaryCheck(true); } } for (int i = 0; i < blocks.size(); i++) { OpcodeBlock block = blocks.get(i); if (!block.isOmittedFromTernaryCheck() && AstUtil.hasStartingRequirement(block.getOpcodes())) { i -= compileTernary(blocks, i, partial.getLocals()); } } } private static int compileTernary(List<OpcodeBlock> blocks, int end, Locals locals) { if (end < 4) { return 0; } OpcodeBlock consumer = blocks.get(end); int start = end - 1; List<OpcodeBlock> true_blocks = new ArrayList<>(); OpcodeBlock tr = blocks.get(start--); TryCatchRegion tr_end = null; if (tr instanceof TryCatchMarkerOpcodeBlock) { tr_end = ((TryCatchMarkerOpcodeBlock) tr).getAsmNode(); tr = blocks.get(start--); } true_blocks.add(0, tr); OpcodeBlock go = blocks.get(start--); while (!(go instanceof GotoOpcodeBlock) || go.getTarget() != consumer) { if (go instanceof TryCatchMarkerOpcodeBlock) { TryCatchRegion go_tr = ((TryCatchMarkerOpcodeBlock) go).getAsmNode(); if (go_tr == tr_end) { return 0; } } true_blocks.add(0, go); if (start < 0) { return 0; } go = blocks.get(start--); } checkState(!true_blocks.isEmpty()); List<OpcodeBlock> false_blocks = new ArrayList<>(); OpcodeBlock fl = blocks.get(start--); OpcodeBlock first_true = true_blocks.get(0); while ((!(go instanceof GotoOpcodeBlock) && !(go instanceof ConditionalOpcodeBlock)) || fl.getTarget() != first_true) { false_blocks.add(0, fl); if (start < 0) { return 0; } fl = blocks.get(start--); } checkState(!false_blocks.isEmpty()); OpcodeBlock first_false = false_blocks.get(0); Set<OpcodeBlock> seen = new HashSet<>(); seen.add(first_true); seen.add(first_false); seen.add(fl); seen.add(go); List<ConditionalOpcodeBlock> condition_blocks = new ArrayList<>(); condition_blocks.add((ConditionalOpcodeBlock) fl); boolean has_more = false; for (; start >= 0; start--) { OpcodeBlock next = blocks.get(start); if (next instanceof GotoOpcodeBlock && next.getTarget() == consumer) { has_more = true; } if (!(next instanceof GotoOpcodeBlock) && !(next instanceof ConditionalOpcodeBlock)) { break; } if (!seen.contains(next.getTarget())) { break; } seen.add(next); condition_blocks.add(0, (ConditionalOpcodeBlock) next); } Condition cond = ConditionBuilder.makeCondition(condition_blocks, locals, first_false, first_true); TernaryBlockSection ternary = new TernaryBlockSection(cond); while (true_blocks.size() > 1) { true_blocks.add(consumer); int n = compileTernary(true_blocks, true_blocks.size() - 1, locals); true_blocks.remove(consumer); if (n == 0) { break; } } for (OpcodeBlock t : true_blocks) { if (t instanceof ProcessedOpcodeBlock) { ternary.getFalseBody().add(((ProcessedOpcodeBlock) t).getPrecompiledSection()); } else { checkState(!(t instanceof GotoOpcodeBlock) && !(t instanceof ConditionalOpcodeBlock)); ternary.getFalseBody().add(new InlineBlockSection(t)); } } while (false_blocks.size() > 1) { false_blocks.add(consumer); int n = compileTernary(false_blocks, false_blocks.size() - 1, locals); false_blocks.remove(consumer); if (n == 0) { break; } } for (OpcodeBlock t : false_blocks) { if (t instanceof ProcessedOpcodeBlock) { ternary.getTrueBody().add(((ProcessedOpcodeBlock) t).getPrecompiledSection()); } else { checkState(!(t instanceof GotoOpcodeBlock) && !(t instanceof ConditionalOpcodeBlock)); ternary.getTrueBody().add(new InlineBlockSection(t)); } } OpcodeBlock first = condition_blocks.get(0); OpcodeBlock replacement = new ProcessedOpcodeBlock(first.getStart(), first.getEnd(), ternary); replacement.setTarget(consumer); start = blocks.indexOf(first); int removed = has_more ? 1 : 0; blocks.set(start, replacement); GraphOperation.remap(blocks, first, replacement); for (int i = end - 1; i >= start + 1; i--) { if (blocks.get(i) instanceof TryCatchMarkerOpcodeBlock) { removed--; continue; } blocks.remove(i); removed++; } return removed; } }
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/method/graph/operate/BreakPrePassOperation.java
src/main/java/org/spongepowered/despector/decompiler/method/graph/operate/BreakPrePassOperation.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.method.graph.operate; import org.spongepowered.despector.decompiler.method.PartialMethod; import org.spongepowered.despector.decompiler.method.graph.GraphOperation; import org.spongepowered.despector.decompiler.method.graph.data.opcode.BreakMarkerOpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.BreakMarkerOpcodeBlock.MarkerType; import org.spongepowered.despector.decompiler.method.graph.data.opcode.ConditionalOpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.GotoOpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.OpcodeBlock; import java.util.ArrayList; import java.util.List; /** * A pre pass operation that identifies jumps that form control flow breaks. */ public class BreakPrePassOperation implements GraphOperation { @Override public void process(PartialMethod partial) { List<OpcodeBlock> blocks = partial.getGraph(); List<GotoOpcodeBlock> candidates = new ArrayList<>(); for (OpcodeBlock block : blocks) { if (block instanceof GotoOpcodeBlock) { candidates.add((GotoOpcodeBlock) block); } } List<Loop> loops = new ArrayList<>(); search: for (GotoOpcodeBlock ggoto : candidates) { OpcodeBlock target = ggoto.getTarget(); int target_index = blocks.indexOf(target); if (target.getStart() < ggoto.getStart()) { // a back edge is either a while loop or a continue statement List<GotoOpcodeBlock> others = new ArrayList<>(); for (OpcodeBlock block : target.getTargettedBy()) { if (block instanceof GotoOpcodeBlock) { others.add((GotoOpcodeBlock) block); } } int goto_index = blocks.indexOf(ggoto); for (GotoOpcodeBlock other : others) { int other_index = blocks.indexOf(other); if (other_index > goto_index) { // if there is another goto targetting our target from // farther in the opcodes then this goto is a continue continue search; } } Loop loop = new Loop(); loop.start = Math.min(goto_index, target_index); loop.end = Math.max(goto_index, target_index); loop.ggoto = ggoto; loop.condition = target; loops.add(loop); continue; } // if this goto is pointing forwards but its not targetting a // condition then its either a break or a else/try-catch block, // we'll figure out which later if (target instanceof ConditionalOpcodeBlock) { // otherwise if this goto points forwards then we need to check // if the condition its targetting is forming the back edge boolean found = false; for (int i = target_index; i < blocks.size(); i++) { OpcodeBlock next = blocks.get(i); if (!(next instanceof ConditionalOpcodeBlock)) { continue search; } ConditionalOpcodeBlock cond = (ConditionalOpcodeBlock) next; if (blocks.indexOf(cond.getTarget()) < target_index) { // we have a back edge found = true; break; } } if (!found) { continue; } List<GotoOpcodeBlock> others = new ArrayList<>(); for (OpcodeBlock block : target.getTargettedBy()) { if (block instanceof GotoOpcodeBlock) { others.add((GotoOpcodeBlock) block); } } int goto_index = blocks.indexOf(ggoto); for (GotoOpcodeBlock other : others) { int other_index = blocks.indexOf(other); if (other_index < goto_index) { // if there is another goto targetting our target from // farther in the opcodes then this goto is a continue continue search; } } Loop loop = new Loop(); loop.start = Math.min(goto_index, target_index); loop.end = Math.max(goto_index, target_index); loop.ggoto = ggoto; loop.condition = target; loops.add(loop); } } dowhile: for (OpcodeBlock block : blocks) { if (!(block instanceof ConditionalOpcodeBlock)) { continue; } ConditionalOpcodeBlock cond = (ConditionalOpcodeBlock) block; if (cond.getTarget().getStart() < cond.getStart()) { int i = blocks.indexOf(cond); OpcodeBlock prev = blocks.get(i); ConditionalOpcodeBlock first = cond; while (prev instanceof ConditionalOpcodeBlock) { OpcodeBlock target = prev.getTarget(); if (target.getStart() < prev.getStart()) { break; } first = (ConditionalOpcodeBlock) prev; if (i == 0) { break; } prev = blocks.get(--i); } for (Loop loop : loops) { if (loop.condition == first) { continue dowhile; } } Loop loop = new Loop(); loop.start = blocks.indexOf(cond.getTarget()); loop.end = blocks.indexOf(first); loop.ggoto = null; loop.condition = first; loops.add(loop); } } outer: for (GotoOpcodeBlock ggoto : candidates) { int goto_index = blocks.indexOf(ggoto); for (Loop loop : loops) { if (loop.ggoto == ggoto) { continue outer; } } MarkerType type = null; Loop found = null; int outermost = blocks.size(); for (Loop loop : loops) { if (goto_index > loop.start && goto_index < loop.end) { if (ggoto.getTarget() == loop.condition) { // this is a continue type = MarkerType.CONTINUE; found = loop; break; } else if (blocks.indexOf(ggoto.getTarget()) > loop.end) { // this is a break if (loop.start < outermost) { outermost = loop.start; type = MarkerType.BREAK; found = loop; } } } } if (type != null) { BreakMarkerOpcodeBlock replacement = new BreakMarkerOpcodeBlock(ggoto.getStart(), ggoto.getEnd(), type); replacement.setTarget(ggoto.getTarget()); replacement.getOpcodes().addAll(ggoto.getOpcodes()); replacement.setMarked(found.condition); blocks.set(blocks.indexOf(ggoto), replacement); GraphOperation.remap(blocks, ggoto, replacement); } } } /** * A representation of a loop. */ private static class Loop { public int start; public int end; public GotoOpcodeBlock ggoto; public OpcodeBlock condition; public Loop() { } } }
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/method/graph/operate/BlockTargetOperation.java
src/main/java/org/spongepowered/despector/decompiler/method/graph/operate/BlockTargetOperation.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.method.graph.operate; import org.spongepowered.despector.decompiler.method.PartialMethod; import org.spongepowered.despector.decompiler.method.graph.GraphOperation; import org.spongepowered.despector.decompiler.method.graph.data.opcode.ConditionalOpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.OpcodeBlock; import java.util.List; /** * A graph operation that populates the {@link OpcodeBlock#getTargettedBy()} * collection. */ public class BlockTargetOperation implements GraphOperation { @Override public void process(PartialMethod partial) { List<OpcodeBlock> blocks = partial.getGraph(); for (int i = 0; i < blocks.size(); i++) { OpcodeBlock block = blocks.get(i); if (block instanceof ConditionalOpcodeBlock) { ((ConditionalOpcodeBlock) block).getElseTarget().targettedBy(block); } if (block.hasTarget() && blocks.indexOf(block.getTarget()) != i + 1) { block.getTarget().targettedBy(block); } } } }
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/method/graph/data/package-info.java
src/main/java/org/spongepowered/despector/decompiler/method/graph/data/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.method.graph.data;
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/method/graph/data/TryCatchMarkerType.java
src/main/java/org/spongepowered/despector/decompiler/method/graph/data/TryCatchMarkerType.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.method.graph.data; /** * An enumeration of the different types of markers used to deliminate try-catch * blocks. */ public enum TryCatchMarkerType { START, END, CATCH }
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/method/graph/data/block/BlockSection.java
src/main/java/org/spongepowered/despector/decompiler/method/graph/data/block/BlockSection.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.method.graph.data.block; import org.spongepowered.despector.ast.Locals; import org.spongepowered.despector.ast.insn.Instruction; import org.spongepowered.despector.ast.stmt.StatementBlock; import java.util.Deque; /** * Represents a single section in the final output. */ public abstract class BlockSection { /** * Appends this block section to the final output. * * @param block The statement block to append to * @param stack The current instruction stack */ public abstract void appendTo(StatementBlock block, Locals locals, Deque<Instruction> stack); }
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/method/graph/data/block/BreakableBlockSection.java
src/main/java/org/spongepowered/despector/decompiler/method/graph/data/block/BreakableBlockSection.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.method.graph.data.block; import java.util.List; /** * A block section representing a breakable control flow element. */ public interface BreakableBlockSection { /** * Gets the control flow breaks targeting this section. */ List<BreakBlockSection> getBreaks(); }
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/method/graph/data/block/package-info.java
src/main/java/org/spongepowered/despector/decompiler/method/graph/data/block/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.method.graph.data.block;
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/method/graph/data/block/InlineBlockSection.java
src/main/java/org/spongepowered/despector/decompiler/method/graph/data/block/InlineBlockSection.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.method.graph.data.block; import org.spongepowered.despector.ast.Locals; import org.spongepowered.despector.ast.insn.Instruction; import org.spongepowered.despector.ast.stmt.StatementBlock; import org.spongepowered.despector.decompiler.method.StatementBuilder; import org.spongepowered.despector.decompiler.method.graph.data.opcode.OpcodeBlock; import java.util.Deque; /** * A block section that contains a single {@link OpcodeBlock}. */ public class InlineBlockSection extends BlockSection { private final OpcodeBlock block; public InlineBlockSection(OpcodeBlock block) { this.block = block; } /** * Gets the {@link OpcodeBlock} that is represented by this block section. */ public OpcodeBlock getBlock() { return this.block; } @Override public void appendTo(StatementBlock block, Locals locals, Deque<Instruction> stack) { StatementBuilder.appendBlock(this.block, block, locals, stack); } }
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/method/graph/data/block/SwitchBlockSection.java
src/main/java/org/spongepowered/despector/decompiler/method/graph/data/block/SwitchBlockSection.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.method.graph.data.block; import static com.google.common.base.Preconditions.checkState; import org.spongepowered.despector.ast.Locals; import org.spongepowered.despector.ast.insn.Instruction; import org.spongepowered.despector.ast.stmt.StatementBlock; import org.spongepowered.despector.ast.stmt.branch.Switch; import org.spongepowered.despector.decompiler.ir.Insn; import org.spongepowered.despector.decompiler.method.StatementBuilder; import org.spongepowered.despector.decompiler.method.graph.data.opcode.OpcodeBlock; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Deque; import java.util.List; /** * A block section representing a switch statement. */ public class SwitchBlockSection extends BlockSection { private final OpcodeBlock switchblock; private final List<SwitchCaseBlockSection> cases = new ArrayList<>(); public SwitchBlockSection(OpcodeBlock s) { this.switchblock = s; } /** * Gets the opcode block ending with the initial switch opcode. */ public OpcodeBlock getSwitchBlock() { return this.switchblock; } /** * Gets the cases of this switch block. */ public List<SwitchCaseBlockSection> getCases() { return this.cases; } /** * Adds a case to this switch block. */ public void addCase(SwitchCaseBlockSection cs) { this.cases.add(cs); } @Override public void appendTo(StatementBlock block, Locals locals, Deque<Instruction> stack) { Insn last = this.switchblock.getLast(); this.switchblock.getOpcodes().remove(last); StatementBuilder.appendBlock(this.switchblock, block, locals, stack); this.switchblock.getOpcodes().add(last); Switch sswitch = new Switch(stack.pop()); for (SwitchCaseBlockSection cs : this.cases) { StatementBlock body = new StatementBlock(StatementBlock.Type.SWITCH); Deque<Instruction> body_stack = new ArrayDeque<>(); for (BlockSection body_section : cs.getBody()) { body_section.appendTo(body, locals, body_stack); } checkState(body_stack.isEmpty()); sswitch.new Case(body, cs.doesBreak(), cs.isDefault(), cs.getTargets()); } block.append(sswitch); } /** * Represents a switch case block. */ public class SwitchCaseBlockSection { private final List<BlockSection> body = new ArrayList<>(); private final List<Integer> targets = new ArrayList<>(); private boolean breaks; private boolean is_default; public SwitchCaseBlockSection() { } /** * Gets the {@link BlockSection}s forming the body. */ public List<BlockSection> getBody() { return this.body; } /** * Gets the targets of this case. */ public List<Integer> getTargets() { return this.targets; } /** * Gets if this case ends with a break or flows into the next case. */ public boolean doesBreak() { return this.breaks; } /** * Sets if this case ends with a break or flows into the next case. */ public void setBreaks(boolean state) { this.breaks = state; } /** * Gets if this case is the default case. */ public boolean isDefault() { return this.is_default; } /** * Sets if this case is the default case. */ public void setDefault(boolean state) { this.is_default = state; } } }
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/method/graph/data/block/DoWhileBlockSection.java
src/main/java/org/spongepowered/despector/decompiler/method/graph/data/block/DoWhileBlockSection.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.method.graph.data.block; import static com.google.common.base.Preconditions.checkState; import org.spongepowered.despector.ast.Locals; import org.spongepowered.despector.ast.insn.Instruction; import org.spongepowered.despector.ast.insn.condition.Condition; import org.spongepowered.despector.ast.stmt.StatementBlock; import org.spongepowered.despector.ast.stmt.branch.DoWhile; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Deque; import java.util.List; /** * A block section representing a do-while loop. */ public class DoWhileBlockSection extends BlockSection implements BreakableBlockSection { private final Condition condition; private final List<BlockSection> body = new ArrayList<>(); private final List<BreakBlockSection> breaks = new ArrayList<>(); public DoWhileBlockSection(Condition cond) { this.condition = cond; } /** * Gets the condition of the loop. */ public Condition getCondition() { return this.condition; } /** * Gets the {@link BlockSection}s that form the body of the loop. */ public List<BlockSection> getBody() { return this.body; } /** * Appends the given {@link BlockSection} to the body of the loop. */ public void append(BlockSection section) { this.body.add(section); } @Override public List<BreakBlockSection> getBreaks() { return this.breaks; } @Override public void appendTo(StatementBlock block, Locals locals, Deque<Instruction> stack) { StatementBlock body = new StatementBlock(StatementBlock.Type.WHILE); Deque<Instruction> body_stack = new ArrayDeque<>(); for (BlockSection body_section : this.body) { body_section.appendTo(body, locals, body_stack); } checkState(body_stack.isEmpty()); DoWhile dowhile = new DoWhile(this.condition, body); block.append(dowhile); } }
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/method/graph/data/block/TernaryBlockSection.java
src/main/java/org/spongepowered/despector/decompiler/method/graph/data/block/TernaryBlockSection.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.method.graph.data.block; import static com.google.common.base.Preconditions.checkState; import org.spongepowered.despector.ast.Locals; import org.spongepowered.despector.ast.insn.Instruction; import org.spongepowered.despector.ast.insn.condition.Condition; import org.spongepowered.despector.ast.insn.misc.Ternary; import org.spongepowered.despector.ast.stmt.StatementBlock; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Deque; import java.util.List; /** * A block section containing a processed ternary. */ public class TernaryBlockSection extends BlockSection { private final List<BlockSection> true_body = new ArrayList<>(); private final List<BlockSection> false_body = new ArrayList<>(); private final Condition condition; public TernaryBlockSection(Condition cond) { this.condition = cond; } /** * Gets the {@link BlockSection}s forming the true case of this ternary. */ public List<BlockSection> getTrueBody() { return this.true_body; } /** * Gets the {@link BlockSection}s forming the false case of this ternary. */ public List<BlockSection> getFalseBody() { return this.false_body; } /** * Gets the condition of this ternary. */ public Condition getCondition() { return this.condition; } @Override public void appendTo(StatementBlock block, Locals locals, Deque<Instruction> stack) { StatementBlock dummy = new StatementBlock(StatementBlock.Type.IF); Deque<Instruction> dummy_stack = new ArrayDeque<>(); for (BlockSection sec : this.true_body) { sec.appendTo(dummy, locals, dummy_stack); } checkState(dummy_stack.size() == 1); Instruction true_val = dummy_stack.pop(); for (BlockSection sec : this.false_body) { sec.appendTo(dummy, locals, dummy_stack); } checkState(dummy_stack.size() == 1); Instruction false_val = dummy_stack.pop(); Ternary ternary = new Ternary(this.condition, true_val, false_val); stack.push(ternary); } }
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/method/graph/data/block/BreakBlockSection.java
src/main/java/org/spongepowered/despector/decompiler/method/graph/data/block/BreakBlockSection.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.method.graph.data.block; import org.spongepowered.despector.ast.Locals; import org.spongepowered.despector.ast.insn.Instruction; import org.spongepowered.despector.ast.insn.condition.Condition; import org.spongepowered.despector.ast.stmt.StatementBlock; import org.spongepowered.despector.ast.stmt.branch.Break; import org.spongepowered.despector.ast.stmt.branch.Break.Breakable; import org.spongepowered.despector.ast.stmt.branch.Break.Type; import org.spongepowered.despector.ast.stmt.branch.If; import org.spongepowered.despector.decompiler.method.ConditionBuilder; import org.spongepowered.despector.decompiler.method.graph.data.opcode.BreakMarkerOpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.BreakMarkerOpcodeBlock.MarkerType; import org.spongepowered.despector.decompiler.method.graph.data.opcode.ConditionalOpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.OpcodeBlock; import java.util.ArrayList; import java.util.Deque; import java.util.List; /** * A block section that contains a single {@link OpcodeBlock}. */ public class BreakBlockSection extends BlockSection { private BlockSection loop; private BreakMarkerOpcodeBlock marker; private MarkerType type; private boolean nested; private Breakable br; private Break final_break; private final List<ConditionalOpcodeBlock> inlined = new ArrayList<>(); public BreakBlockSection(BreakMarkerOpcodeBlock marker, MarkerType type) { this.marker = marker; this.type = type; } /** * Gets the {@link OpcodeBlock} that broken by this break. */ public BlockSection getLoop() { return this.loop; } /** * Sets the {@link OpcodeBlock} that broken by this break. */ public void setLoop(BlockSection section) { this.loop = section; } /** * Gets the marker opcode block this section was formed from. */ public BreakMarkerOpcodeBlock getMarker() { return this.marker; } /** * Gets the break marker type. */ public MarkerType getType() { return this.type; } /** * Gets if this break is nested within an inner loop and will require a * label to break the outer loop. */ public boolean isNested() { return this.nested; } /** * Sets if this break is nested within an inner loop and will require a * label to break the outer loop. */ public void setNested(boolean state) { this.nested = state; } /** * Gets the breakable loop that this break is breaking. */ public Breakable getBreakable() { return this.br; } /** * Sets the breakable loop that this break is breaking. */ public void setBreakable(Breakable br) { this.br = br; if (this.final_break != null) { this.final_break.setLoop(br); } } /** * Gets any inlined conditional blocks for this control flow break. * * <p>Sometimes a control flow break is formed from a set of conditions * rather than a goto if the goto would be the only thing in the body of an * if statement.</p> */ public List<ConditionalOpcodeBlock> getInlinedConditions() { return this.inlined; } @Override public void appendTo(StatementBlock block, Locals locals, Deque<Instruction> stack) { this.final_break = new Break(this.br, this.type == MarkerType.BREAK ? Type.BREAK : Type.CONTINUE, this.nested); if (!this.inlined.isEmpty()) { ConditionalOpcodeBlock last = this.inlined.get(this.inlined.size() - 1); Condition cond = ConditionBuilder.makeCondition(this.inlined, locals, last.getTarget(), last.getElseTarget()); StatementBlock inner = new StatementBlock(StatementBlock.Type.IF); inner.append(this.final_break); If ifblock = new If(cond, inner); block.append(ifblock); return; } block.append(this.final_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/decompiler/method/graph/data/block/IfBlockSection.java
src/main/java/org/spongepowered/despector/decompiler/method/graph/data/block/IfBlockSection.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.method.graph.data.block; import static com.google.common.base.Preconditions.checkState; import org.spongepowered.despector.ast.Locals; import org.spongepowered.despector.ast.insn.Instruction; import org.spongepowered.despector.ast.insn.condition.Condition; 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 java.util.ArrayDeque; import java.util.ArrayList; import java.util.Deque; import java.util.List; /** * A block section representing an if statement, optionally with a number of * else-if blocks and an else block. */ public class IfBlockSection extends BlockSection { private final Condition condition; private final List<BlockSection> body = new ArrayList<>(); private final List<BlockSection> else_ = new ArrayList<>(); private final List<ElifBlockSection> elif = new ArrayList<>(); public IfBlockSection(Condition cond) { this.condition = cond; } /** * Gets the condition of the initial if statement. */ public Condition getCondition() { return this.condition; } /** * Gets the {@link BlockSection}s that form the body of the initial if * statement. */ public List<BlockSection> getBody() { return this.body; } /** * Appends the given {@link BlockSection} to the body of the initial if * statement. */ public void appendBody(BlockSection section) { this.body.add(section); } /** * Gets the {@link BlockSection}s that form the else block of this if * statement. */ public List<BlockSection> getElseBody() { return this.else_; } /** * Appends the given {@link BlockSection} to the else body of this if * statement. */ public void appendElseBody(BlockSection section) { this.else_.add(section); } /** * Gets any else-if blocks attached to this if statement. */ public List<ElifBlockSection> getElifBlocks() { return this.elif; } /** * Adds an else-if block to this if statement. */ public void addElif(ElifBlockSection elif) { this.elif.add(elif); } @Override public void appendTo(StatementBlock block, Locals locals, Deque<Instruction> stack) { StatementBlock body = new StatementBlock(StatementBlock.Type.IF); Deque<Instruction> body_stack = new ArrayDeque<>(); for (BlockSection body_section : this.body) { body_section.appendTo(body, locals, body_stack); } if (!body_stack.isEmpty()) { throw new IllegalStateException(); } If iff = new If(this.condition, body); for (ElifBlockSection elif : this.elif) { StatementBlock elif_body = new StatementBlock(StatementBlock.Type.IF); Deque<Instruction> elif_stack = new ArrayDeque<>(); for (BlockSection body_section : elif.getBody()) { body_section.appendTo(elif_body, locals, elif_stack); } checkState(elif_stack.isEmpty()); iff.new Elif(elif.getCondition(), elif_body); } if (!this.else_.isEmpty()) { StatementBlock else_body = new StatementBlock(StatementBlock.Type.IF); Deque<Instruction> else_stack = new ArrayDeque<>(); for (BlockSection body_section : this.else_) { body_section.appendTo(else_body, locals, else_stack); } checkState(else_stack.isEmpty()); if (else_body.getStatements().size() == 1 && else_body.getStatements().get(0) instanceof If) { If internal_if = (If) else_body.getStatements().get(0); iff.new Elif(internal_if.getCondition(), internal_if.getBody()); for (Elif el : internal_if.getElifBlocks()) { iff.new Elif(el.getCondition(), el.getBody()); } if (internal_if.getElseBlock() != null) { iff.new Else(internal_if.getElseBlock().getBody()); } } else { iff.new Else(else_body); } } block.append(iff); } /** * An elif block attached to an if statement. */ public class ElifBlockSection { private final Condition condition; private final List<BlockSection> body = new ArrayList<>(); /** * Creates a new {@link ElifBlockSection}. The new block is * automatically added to the parent {@link IfBlockSection}. */ public ElifBlockSection(Condition cond) { this.condition = cond; IfBlockSection.this.addElif(this); } /** * Gets the condition of this else-if block. */ public Condition getCondition() { return this.condition; } /** * Gets the {@link BlockSection}s that form the body. */ public List<BlockSection> getBody() { return this.body; } /** * Appends the given {@link BlockSection} to the body. */ public void append(BlockSection section) { this.body.add(section); } } }
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/method/graph/data/block/CommentBlockSection.java
src/main/java/org/spongepowered/despector/decompiler/method/graph/data/block/CommentBlockSection.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.method.graph.data.block; import org.spongepowered.despector.ast.Locals; import org.spongepowered.despector.ast.insn.Instruction; import org.spongepowered.despector.ast.stmt.StatementBlock; import org.spongepowered.despector.ast.stmt.misc.Comment; import java.util.ArrayList; import java.util.Deque; import java.util.List; /** * Represents a block section containing a comment. */ public class CommentBlockSection extends BlockSection { private final List<String> text = new ArrayList<>(); public CommentBlockSection(List<String> txt) { this.text.addAll(txt); } @Override public void appendTo(StatementBlock block, Locals locals, Deque<Instruction> stack) { block.append(new Comment(this.text)); } }
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/method/graph/data/block/TryCatchBlockSection.java
src/main/java/org/spongepowered/despector/decompiler/method/graph/data/block/TryCatchBlockSection.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.method.graph.data.block; import static com.google.common.base.Preconditions.checkState; import org.spongepowered.despector.ast.Locals; import org.spongepowered.despector.ast.insn.Instruction; import org.spongepowered.despector.ast.stmt.StatementBlock; import org.spongepowered.despector.ast.stmt.branch.TryCatch; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Deque; import java.util.List; /** * A block section containing a processed try-catch block. */ public class TryCatchBlockSection extends BlockSection { private final List<BlockSection> body = new ArrayList<>(); private final List<CatchBlockSection> catches = new ArrayList<>(); public TryCatchBlockSection() { } /** * Gets the body of this try statement. */ public List<BlockSection> getBody() { return this.body; } /** * Gets the catch blocks of this try statement. */ public List<CatchBlockSection> getCatchBlocks() { return this.catches; } @Override public void appendTo(StatementBlock block, Locals locals, Deque<Instruction> stack) { StatementBlock body = new StatementBlock(StatementBlock.Type.TRY); Deque<Instruction> body_stack = new ArrayDeque<>(); for (BlockSection body_section : this.body) { body_section.appendTo(body, locals, body_stack); } checkState(body_stack.isEmpty()); TryCatch ttry = new TryCatch(body); block.append(ttry); for (CatchBlockSection c : this.catches) { StatementBlock cbody = new StatementBlock(StatementBlock.Type.WHILE); Deque<Instruction> cbody_stack = new ArrayDeque<>(); for (BlockSection body_section : c.getBody()) { body_section.appendTo(cbody, locals, cbody_stack); } checkState(cbody_stack.isEmpty()); if (c.getLocal() != null) { ttry.new CatchBlock(c.getLocal(), c.getExceptions(), cbody); } else { ttry.new CatchBlock("e", c.getExceptions(), cbody); } } } /** * A processed catch block. */ public static class CatchBlockSection { private Locals.LocalInstance exlocal; private final List<String> exception = new ArrayList<>(); private final List<BlockSection> body = new ArrayList<>(); public CatchBlockSection(List<String> ex, Locals.LocalInstance local) { this.exception.addAll(ex); this.exlocal = local; } /** * Gets the local instance of the local holding the exception instance. */ public Locals.LocalInstance getLocal() { return this.exlocal; } /** * Gets all exceptions for this catch block. */ public List<String> getExceptions() { return this.exception; } /** * Gets the body of this catch block. */ public List<BlockSection> getBody() { return this.body; } } }
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/method/graph/data/block/WhileBlockSection.java
src/main/java/org/spongepowered/despector/decompiler/method/graph/data/block/WhileBlockSection.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.method.graph.data.block; import static com.google.common.base.Preconditions.checkState; import org.spongepowered.despector.ast.Locals; import org.spongepowered.despector.ast.insn.Instruction; import org.spongepowered.despector.ast.insn.condition.Condition; import org.spongepowered.despector.ast.stmt.StatementBlock; import org.spongepowered.despector.ast.stmt.branch.While; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Deque; import java.util.List; /** * A block section representing a while block. */ public class WhileBlockSection extends BlockSection implements BreakableBlockSection { private final Condition condition; private final List<BlockSection> body = new ArrayList<>(); private final List<BreakBlockSection> breaks = new ArrayList<>(); public WhileBlockSection(Condition cond) { this.condition = cond; } /** * Gets the condition of the while block. */ public Condition getCondition() { return this.condition; } /** * Gets the block sections forming the body. */ public List<BlockSection> getBody() { return this.body; } /** * Appends the given {@link BlockSection} to the body. */ public void appendBody(BlockSection block) { this.body.add(block); } @Override public List<BreakBlockSection> getBreaks() { return this.breaks; } @Override public void appendTo(StatementBlock block, Locals locals, Deque<Instruction> stack) { StatementBlock body = new StatementBlock(StatementBlock.Type.WHILE); While wwhile = new While(this.condition, body); for (BreakBlockSection bbreak : this.breaks) { bbreak.setBreakable(wwhile); } Deque<Instruction> body_stack = new ArrayDeque<>(); for (BlockSection body_section : this.body) { body_section.appendTo(body, locals, body_stack); } checkState(body_stack.isEmpty()); block.append(wwhile); } }
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/method/graph/data/opcode/OpcodeBlock.java
src/main/java/org/spongepowered/despector/decompiler/method/graph/data/opcode/OpcodeBlock.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.method.graph.data.opcode; import org.spongepowered.despector.decompiler.ir.Insn; import org.spongepowered.despector.decompiler.method.graph.data.block.BlockSection; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; /** * Represents a section of opcodes in the original method that forms part of a * graph representing the control flow through the method. The opcode blocks are * created by sectioning the original opcodes at any point that may cause a jump * and any point targeted by a jump. */ public abstract class OpcodeBlock { protected int start_pc; protected int end_pc; protected final List<Insn> opcodes = new ArrayList<>(); protected OpcodeBlock target; protected Set<OpcodeBlock> targetted_by = new HashSet<>(); protected boolean exclude_from_ternary_check = false; public OpcodeBlock(int start, int end) { this.start_pc = start; this.end_pc = end; } /** * Gets the index of the opcode which this block was formed starting at. Can * be used as a general method of ordering blocks but beware as it is not * recalculated if block are split or joined. */ public int getStart() { return this.start_pc; } public int getEnd() { return this.end_pc; } public void setBounds(int start, int end) { this.start_pc = start; this.end_pc = end; } /** * Gets the opcodes that are part of this block. */ public List<Insn> getOpcodes() { return this.opcodes; } public Insn getLast() { return this.opcodes.get(this.opcodes.size() - 1); } /** * Gets the block targeted by this block. If this block ends with a * condition jump the target will be the block that control will be passed * to if the condition is true. */ public OpcodeBlock getTarget() { return this.target; } /** * Gets if this block has a target block. */ public boolean hasTarget() { return this.target != null; } /** * Sets the target block of this block. */ public void setTarget(OpcodeBlock block) { this.target = block; } /** * Gets a set of blocks which target this block. */ public Set<OpcodeBlock> getTargettedBy() { return this.targetted_by; } /** * Adds a block as having this block as a target. */ public void targettedBy(OpcodeBlock block) { this.targetted_by.add(block); } /** * Gets if this block is omitted from the check for ternaries. * * <p>As an example: typically statements do not overlap blocks so a check * is done if a block's initial opcodes expect values to be already on the * stack which indicates that the block is preceeded by a ternary which is * then precompiled in a pre-pass.</p> * * <p>However in the event of a try-catch statement ending in a return the * marker for the end of the try block will split the return from the values * it is returning.</p> */ public boolean isOmittedFromTernaryCheck() { return this.exclude_from_ternary_check; } /** * Sets this block as omitted from the ternary check. */ public void omitFromTernaryCheck(boolean state) { this.exclude_from_ternary_check = state; } public abstract BlockSection toBlockSection(); /** * Prints this block's information for debugging. */ public void print() { System.out.println(toString()); } /** * Gets a single line of information for emitting this block in a comment * for debugging. */ public abstract String getDebugHeader(); @Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append(getDebugHeader()).append("\n"); for (Insn insn : this.opcodes) { builder.append(" ").append(insn.toString()).append("\n"); } return builder.toString(); } @Override public boolean equals(Object o) { if (!(o instanceof OpcodeBlock)) { return false; } OpcodeBlock op = (OpcodeBlock) o; return op.getStart() == this.start_pc && op.getEnd() == this.end_pc; } }
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/method/graph/data/opcode/TryCatchMarkerOpcodeBlock.java
src/main/java/org/spongepowered/despector/decompiler/method/graph/data/opcode/TryCatchMarkerOpcodeBlock.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.method.graph.data.opcode; import org.spongepowered.despector.decompiler.method.PartialMethod.TryCatchRegion; import org.spongepowered.despector.decompiler.method.graph.data.TryCatchMarkerType; import org.spongepowered.despector.decompiler.method.graph.data.block.BlockSection; /** * An opcode block that acts as a marker to mark the start and end of a try * block and the start of a catch block as there are no opcodes to deliminate * these. */ public class TryCatchMarkerOpcodeBlock extends OpcodeBlock { private final TryCatchMarkerType marker_type; private final TryCatchRegion tc_node; private TryCatchMarkerOpcodeBlock start; private TryCatchMarkerOpcodeBlock end; public TryCatchMarkerOpcodeBlock(TryCatchMarkerType marker, TryCatchRegion tc) { super(-1, -1); this.marker_type = marker; this.tc_node = tc; if (this.marker_type == TryCatchMarkerType.START) { this.start = this; } else if (this.marker_type == TryCatchMarkerType.END) { this.end = this; } } /** * Gets the type of this marker. */ public TryCatchMarkerType getType() { return this.marker_type; } /** * Gets the {@link TryCatchRegion} that this marker is from. */ public TryCatchRegion getAsmNode() { return this.tc_node; } /** * Gets the start marker corresponding to this marker. */ public TryCatchMarkerOpcodeBlock getStartMarker() { return this.start; } /** * Sets the start marker corresponding to this marker. */ public void setStartMarker(TryCatchMarkerOpcodeBlock start) { this.start = start; } /** * Gets the end marker corresponding to this marker. */ public TryCatchMarkerOpcodeBlock getEndMarker() { return this.end; } /** * Sets the end marker corresponding to this marker. */ public void setEndMarker(TryCatchMarkerOpcodeBlock end) { this.end = end; } @Override public BlockSection toBlockSection() { throw new IllegalStateException("Unexpected try-catch marker block"); } @Override public void print() { System.out.println("TryCatch marker: " + this.marker_type.name()); System.out.println(" part of " + this.tc_node); } @Override public String getDebugHeader() { return "TryCatch: " + this.start_pc + "-" + this.end_pc + " (target: " + (this.target != null ? this.target.getStart() : -1) + ", marker: " + this.marker_type.name() + ")"; } @Override public boolean equals(Object o) { return this == o; } }
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/method/graph/data/opcode/package-info.java
src/main/java/org/spongepowered/despector/decompiler/method/graph/data/opcode/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.method.graph.data.opcode;
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/method/graph/data/opcode/BodyOpcodeBlock.java
src/main/java/org/spongepowered/despector/decompiler/method/graph/data/opcode/BodyOpcodeBlock.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.method.graph.data.opcode; import org.spongepowered.despector.decompiler.method.graph.data.block.BlockSection; import org.spongepowered.despector.decompiler.method.graph.data.block.InlineBlockSection; /** * An opcode block containing a simple sequence of opcodes with no control flow * logic. */ public class BodyOpcodeBlock extends OpcodeBlock { public BodyOpcodeBlock(int start, int end) { super(start, end); } @Override public BlockSection toBlockSection() { return new InlineBlockSection(this); } @Override public String getDebugHeader() { return "Body: " + this.start_pc + "-" + this.end_pc + " (target: " + (this.target != null ? this.target.getStart() : -1) + ")"; } }
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/method/graph/data/opcode/GotoOpcodeBlock.java
src/main/java/org/spongepowered/despector/decompiler/method/graph/data/opcode/GotoOpcodeBlock.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.method.graph.data.opcode; import org.spongepowered.despector.decompiler.method.graph.data.block.BlockSection; /** * An opcode block representing an unconditional jump. */ public class GotoOpcodeBlock extends OpcodeBlock { public GotoOpcodeBlock(int start, int end) { super(start, end); } @Override public BlockSection toBlockSection() { throw new IllegalStateException("Unexpected conditional block"); } @Override public String getDebugHeader() { return "Goto: " + this.start_pc + "-" + this.end_pc + " (target: " + (this.target != null ? this.target.getStart() : -1) + ")"; } }
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/method/graph/data/opcode/BreakMarkerOpcodeBlock.java
src/main/java/org/spongepowered/despector/decompiler/method/graph/data/opcode/BreakMarkerOpcodeBlock.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.method.graph.data.opcode; import org.spongepowered.despector.decompiler.method.graph.data.block.BlockSection; import org.spongepowered.despector.decompiler.method.graph.data.block.BreakBlockSection; /** * An opcode block that acts as a marker for a control flow break. */ public class BreakMarkerOpcodeBlock extends OpcodeBlock { private MarkerType type; private OpcodeBlock marked; public BreakMarkerOpcodeBlock(int start, int end, MarkerType type) { super(start, end); this.type = type; } /** * Gets the type of this control flow break. */ public MarkerType getType() { return this.type; } /** * Sets the type of this control flow break. */ public void setType(MarkerType type) { this.type = type; } /** * Gets the targeted block. */ public OpcodeBlock getMarked() { return this.marked; } /** * Sets the targeted block. */ public void setMarked(OpcodeBlock marked) { this.marked = marked; } @Override public OpcodeBlock getTarget() { return null; } @Override public boolean hasTarget() { return false; } /** * Gets the original target of this block as the regular * {@link #getTarget()} method is changed to return null to prevent it from * breaking the region detection. */ public OpcodeBlock getOldTarget() { return this.target; } @Override public BlockSection toBlockSection() { return new BreakBlockSection(this, this.type); } @Override public String getDebugHeader() { return "Break: " + this.start_pc + "-" + this.end_pc + " (target: " + (this.target != null ? this.target.getStart() : -1) + ", marker: " + this.type.name() + ", marked: " + (this.marked != null ? this.marked.getStart() : -1) + ")"; } /** * An enumeration of the types of control flow breaks. */ public static enum MarkerType { BREAK, CONTINUE; } }
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/method/graph/data/opcode/SwitchOpcodeBlock.java
src/main/java/org/spongepowered/despector/decompiler/method/graph/data/opcode/SwitchOpcodeBlock.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.method.graph.data.opcode; import org.spongepowered.despector.decompiler.method.graph.data.block.BlockSection; import java.util.HashMap; import java.util.Map; /** * An opcode block ending with a switch instruction. */ public class SwitchOpcodeBlock extends OpcodeBlock { private final Map<Integer, OpcodeBlock> additional_targets = new HashMap<>(); public SwitchOpcodeBlock(int start, int end) { super(start, end); } /** * Gets a map of any additional target blocks. Used by switches to represent * the blocks targetted by the various cases. */ public Map<Integer, OpcodeBlock> getAdditionalTargets() { return this.additional_targets; } @Override public BlockSection toBlockSection() { throw new IllegalStateException("Unexpected switch block"); } @Override public String getDebugHeader() { String s = "Switch: " + this.start_pc + "-" + this.end_pc + " (target: " + (this.target != null ? this.target.getStart() : -1) + ")\n"; for (Map.Entry<Integer, OpcodeBlock> a : this.additional_targets.entrySet()) { s += " " + a.getKey() + " -> " + a.getValue().getStart() + "\n"; } return s; } }
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/method/graph/data/opcode/ProcessedOpcodeBlock.java
src/main/java/org/spongepowered/despector/decompiler/method/graph/data/opcode/ProcessedOpcodeBlock.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.method.graph.data.opcode; import org.spongepowered.despector.decompiler.method.graph.data.block.BlockSection; /** * An opcode block wrapping an already processed block section. */ public class ProcessedOpcodeBlock extends OpcodeBlock { private BlockSection internal = null; public ProcessedOpcodeBlock(int start, int end, BlockSection internal) { super(start, end); this.internal = internal; } /** * Gets the precompiled {@link BlockSection} of this block if it exists. */ public BlockSection getPrecompiledSection() { return this.internal; } /** * Sets the precompiled {@link BlockSection} of this block.. */ public void setPrecompiled(BlockSection section) { this.internal = section; } @Override public BlockSection toBlockSection() { return this.internal; } @Override public String getDebugHeader() { return "Processed: " + this.start_pc + "-" + this.end_pc + " (target: " + (this.target != null ? this.target.getStart() : -1) + ", internal: " + this.internal.getClass().getSimpleName() + ")"; } }
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/method/graph/data/opcode/ConditionalOpcodeBlock.java
src/main/java/org/spongepowered/despector/decompiler/method/graph/data/opcode/ConditionalOpcodeBlock.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.method.graph.data.opcode; import org.spongepowered.despector.decompiler.method.graph.data.block.BlockSection; /** * An opcode block holding a conditional jump. */ public class ConditionalOpcodeBlock extends OpcodeBlock { private OpcodeBlock else_target; private OpcodeBlock prefix; public ConditionalOpcodeBlock(int start, int end) { super(start, end); } /** * Gets the alternate target of this block. Only present for conditional * jumps where it represents the block to which control is passed if the * condition is false. */ public OpcodeBlock getElseTarget() { return this.else_target; } /** * Has an alternate target. */ public boolean hasElseTarget() { return this.else_target != null; } /** * Sets the alternate target. */ public void setElseTarget(OpcodeBlock block) { this.else_target = block; } public OpcodeBlock getPrefix() { return this.prefix; } public void setPrefix(OpcodeBlock block) { this.prefix = block; } @Override public BlockSection toBlockSection() { throw new IllegalStateException("Unexpected conditional block"); } @Override public void print() { super.print(); System.out.println(" Target: " + (this.target != null ? this.target.getStart() : -1)); System.out.println(" Else Target: " + (this.else_target != null ? this.else_target.getStart() : -1)); } @Override public String getDebugHeader() { return "Conditional: " + this.start_pc + "-" + this.end_pc + " (target: " + (this.target != null ? this.target.getStart() : -1) + ", else_target: " + (this.else_target != null ? this.else_target.getEnd() : -1) + ")"; } }
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/method/graph/create/SwitchGraphProducerStep.java
src/main/java/org/spongepowered/despector/decompiler/method/graph/create/SwitchGraphProducerStep.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.method.graph.create; import org.spongepowered.despector.decompiler.ir.Insn; import org.spongepowered.despector.decompiler.ir.InsnBlock; import org.spongepowered.despector.decompiler.ir.SwitchInsn; import org.spongepowered.despector.decompiler.method.PartialMethod; import org.spongepowered.despector.decompiler.method.graph.GraphOperation; import org.spongepowered.despector.decompiler.method.graph.GraphProducerStep; import org.spongepowered.despector.decompiler.method.graph.data.opcode.OpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.SwitchOpcodeBlock; import java.util.List; import java.util.Map; import java.util.Set; /** * A graph producer that creates opcode blocks for switch statements. */ public class SwitchGraphProducerStep implements GraphProducerStep { @Override public void collectBreakpoints(PartialMethod partial, Set<Integer> break_points) { InsnBlock instructions = partial.getOpcodes(); for (int i = 0; i < instructions.size(); i++) { Insn next = instructions.get(i); if (next instanceof SwitchInsn) { break_points.add(i); SwitchInsn ts = (SwitchInsn) next; for (int l : ts.getTargets().values()) { break_points.add(l - 1); } break_points.add(ts.getDefault() - 1); } } } @Override public void formEdges(PartialMethod partial, List<Integer> sorted_break_points, List<OpcodeBlock> block_list) { for (int i = 0; i < block_list.size(); i++) { // Now we go through and form an edge from any block and the block // it flows (or jumps) into next. OpcodeBlock block = block_list.get(i); if (!(block.getLast() instanceof SwitchInsn)) { continue; } SwitchOpcodeBlock replacement = new SwitchOpcodeBlock(block.getStart(), block.getEnd()); replacement.getOpcodes().addAll(block.getOpcodes()); replacement.setTarget(block.getTarget()); block_list.set(i, replacement); GraphOperation.remap(block_list, block, replacement); SwitchInsn ts = (SwitchInsn) block.getLast(); for (Map.Entry<Integer, Integer> r : ts.getTargets().entrySet()) { replacement.getAdditionalTargets().put(r.getKey(), GraphProducerStep.find(block_list, r.getValue())); } replacement.getAdditionalTargets().put(-1, GraphProducerStep.find(block_list, ts.getDefault())); } } }
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/method/graph/create/package-info.java
src/main/java/org/spongepowered/despector/decompiler/method/graph/create/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.method.graph.create;
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/method/graph/create/JumpGraphProducerStep.java
src/main/java/org/spongepowered/despector/decompiler/method/graph/create/JumpGraphProducerStep.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.method.graph.create; import org.spongepowered.despector.decompiler.ir.Insn; import org.spongepowered.despector.decompiler.ir.InsnBlock; import org.spongepowered.despector.decompiler.ir.JumpInsn; import org.spongepowered.despector.decompiler.method.PartialMethod; import org.spongepowered.despector.decompiler.method.graph.GraphOperation; import org.spongepowered.despector.decompiler.method.graph.GraphProducerStep; import org.spongepowered.despector.decompiler.method.graph.data.opcode.ConditionalOpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.GotoOpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.OpcodeBlock; import java.util.List; import java.util.Set; /** * A graph producer that creates opcode blocks for condition and unconditional * jumps. */ public class JumpGraphProducerStep implements GraphProducerStep { @Override public void collectBreakpoints(PartialMethod partial, Set<Integer> break_points) { InsnBlock instructions = partial.getOpcodes(); for (int i = 0; i < instructions.size(); i++) { Insn next = instructions.get(i); if (next instanceof JumpInsn) { if(next.getOpcode() == Insn.GOTO && i > 0) { break_points.add(i - 1); } break_points.add(i); // also break before labels targetted by jump opcodes to have a // break between the body of an if block and the statements // after it int target = ((JumpInsn) next).getTarget() - 1; if (target >= 0) { break_points.add(target); } continue; } } } @Override public void formEdges(PartialMethod partial, List<Integer> sorted_break_points, List<OpcodeBlock> block_list) { for (int i = 0; i < block_list.size(); i++) { // Now we go through and form an edge from any block and the block // it flows (or jumps) into next. OpcodeBlock block = block_list.get(i); if (block.getLast() instanceof JumpInsn) { int label = ((JumpInsn) block.getLast()).getTarget(); if (block.getLast().getOpcode() == Insn.GOTO) { GotoOpcodeBlock replacement = new GotoOpcodeBlock(block.getStart(), block.getEnd()); block_list.set(i, replacement); replacement.getOpcodes().addAll(block.getOpcodes()); replacement.setTarget(GraphProducerStep.find(block_list, label)); GraphOperation.remap(block_list, block, replacement); } else { ConditionalOpcodeBlock replacement = new ConditionalOpcodeBlock(block.getStart(), block.getEnd()); OpcodeBlock next = block_list.get(block_list.indexOf(block) + 1); block_list.set(i, replacement); replacement.getOpcodes().addAll(block.getOpcodes()); replacement.setTarget(GraphProducerStep.find(block_list, label)); replacement.setElseTarget(next); GraphOperation.remap(block_list, block, replacement); } } } } }
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/method/graph/create/TryCatchGraphProducerStep.java
src/main/java/org/spongepowered/despector/decompiler/method/graph/create/TryCatchGraphProducerStep.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.method.graph.create; import org.spongepowered.despector.ast.Locals; import org.spongepowered.despector.ast.Locals.LocalInstance; import org.spongepowered.despector.decompiler.ir.Insn; import org.spongepowered.despector.decompiler.ir.InsnBlock; import org.spongepowered.despector.decompiler.method.PartialMethod; import org.spongepowered.despector.decompiler.method.PartialMethod.TryCatchRegion; import org.spongepowered.despector.decompiler.method.graph.GraphProducerStep; import org.spongepowered.despector.decompiler.method.graph.data.TryCatchMarkerType; import org.spongepowered.despector.decompiler.method.graph.data.opcode.OpcodeBlock; import org.spongepowered.despector.decompiler.method.graph.data.opcode.TryCatchMarkerOpcodeBlock; import java.util.List; import java.util.Set; /** * A graph producer that creates opcode blocks for try-catch statements. It * inserts also inserts marker blocks to denote the start and end of try blocks * and the start of catch blocks. */ public class TryCatchGraphProducerStep implements GraphProducerStep { @Override public void collectBreakpoints(PartialMethod partial, Set<Integer> break_points) { InsnBlock instructions = partial.getOpcodes(); Locals locals = partial.getLocals(); for (TryCatchRegion tc : partial.getOpcodes().getCatchRegions()) { if (tc.getStart() > 0) { break_points.add(tc.getStart() - 1); } break_points.add(tc.getEnd()); break_points.add(tc.getCatch()); LocalInstance local = null; for (int i = tc.getCatch() + 1; i < instructions.size(); i++) { local = locals.findLocal(i, "L" + tc.getException() + ";"); if (local == null) { local = locals.findLocal(i, "Ljava/lang/RuntimeException;"); if (local == null) { local = locals.findLocal(i, "Ljava/lang/Exception;"); } } if (local != null) { break; } } if (local != null && local.getEnd() < instructions.size() - 1) { break_points.add(local.getEnd()); } } } @Override public void formEdges(PartialMethod partial, List<Integer> sorted_break_points, List<OpcodeBlock> block_list) { for (int i = partial.getOpcodes().getCatchRegions().size() - 1; i >= 0; i--) { TryCatchRegion tc = partial.getOpcodes().getCatchRegions().get(i); TryCatchMarkerOpcodeBlock start_marker = new TryCatchMarkerOpcodeBlock(TryCatchMarkerType.START, tc); TryCatchMarkerOpcodeBlock end_marker = new TryCatchMarkerOpcodeBlock(TryCatchMarkerType.END, tc); TryCatchMarkerOpcodeBlock handler_marker = new TryCatchMarkerOpcodeBlock(TryCatchMarkerType.CATCH, tc); start_marker.setEndMarker(end_marker); end_marker.setStartMarker(start_marker); handler_marker.setStartMarker(start_marker); handler_marker.setEndMarker(end_marker); OpcodeBlock start; if (tc.getStart() == 0) { start = GraphProducerStep.find(block_list, 0); } else { start = GraphProducerStep.find(block_list, tc.getStart()); } OpcodeBlock end = block_list.get(block_list.indexOf(GraphProducerStep.find(block_list, tc.getEnd()))); OpcodeBlock handler = GraphProducerStep.find(block_list, tc.getCatch()); if (end.getEnd() == tc.getEnd() && end.getLast().getOpcode() == Insn.ARETURN) { end = block_list.get(block_list.indexOf(GraphProducerStep.find(block_list, tc.getEnd() + 1))); } block_list.add(block_list.indexOf(start), start_marker); block_list.add(block_list.indexOf(end), end_marker); block_list.add(block_list.indexOf(handler), handler_marker); } } }
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/transform/TypeTransformer.java
src/main/java/org/spongepowered/despector/transform/TypeTransformer.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.transform; import org.spongepowered.despector.ast.type.TypeEntry; /** * A transformer which can perform some operation against a type. */ public interface TypeTransformer { /** * Transforms the given type. */ void transform(TypeEntry 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/transform/package-info.java
src/main/java/org/spongepowered/despector/transform/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.transform;
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/transform/matcher/MatchContext.java
src/main/java/org/spongepowered/despector/transform/matcher/MatchContext.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.transform.matcher; import org.spongepowered.despector.ast.Locals.LocalInstance; import org.spongepowered.despector.ast.stmt.Statement; import org.spongepowered.despector.ast.stmt.assign.LocalAssignment; import org.spongepowered.despector.ast.stmt.branch.ForEach; import java.util.HashMap; import java.util.Map; /** * A match context used to share context between matchers. */ public class MatchContext { public static <T extends Statement> StatementMatcher<T> storeLocal(String identifier, StatementMatcher<T> inner) { return new LocalStoreMatcher<>(identifier, inner); } public static MatchContext create() { return new MatchContext(); } private Map<String, LocalInstance> locals; MatchContext() { } /** * Stores the given local as the given identifier. */ public void setLocal(String ident, LocalInstance local) { if (this.locals == null) { this.locals = new HashMap<>(); } this.locals.put(ident, local); } /** * Gets the local defined by the given identifier. */ public LocalInstance getLocal(String ident) { if (this.locals == null) { return null; } return this.locals.get(ident); } /** * A matcher which stores the local within the matched statement if a match * is successful. * * <p>Only the local assigned to in a {@link LocalAssignment} or the loop * local of a {@link ForEach} loop is supported.</p> */ public static class LocalStoreMatcher<T extends Statement> implements StatementMatcher<T> { private String identifier; private StatementMatcher<T> internal; public LocalStoreMatcher(String identifier, StatementMatcher<T> inner) { this.identifier = identifier; this.internal = inner; } public StatementMatcher<T> getInternalMatcher() { return this.internal; } @Override public T match(MatchContext ctx, Statement stmt) { T inner = this.internal.match(ctx, stmt); if (inner == null) { return null; } if (inner instanceof LocalAssignment) { ctx.setLocal(this.identifier, ((LocalAssignment) inner).getLocal()); } if (inner instanceof ForEach) { ctx.setLocal(this.identifier, ((ForEach) inner).getValueAssignment()); } return inner; } } }
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/transform/matcher/InstructionMatcher.java
src/main/java/org/spongepowered/despector/transform/matcher/InstructionMatcher.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.transform.matcher; import org.spongepowered.despector.ast.insn.Instruction; import org.spongepowered.despector.ast.insn.misc.Cast; import org.spongepowered.despector.ast.stmt.invoke.InstanceMethodInvoke; import org.spongepowered.despector.transform.matcher.instruction.ArrayAccessMatcher; import org.spongepowered.despector.transform.matcher.instruction.InstanceFieldAccessMatcher; import org.spongepowered.despector.transform.matcher.instruction.InstanceMethodInvokeMatcher; import org.spongepowered.despector.transform.matcher.instruction.IntConstantMatcher; import org.spongepowered.despector.transform.matcher.instruction.LocalAccessMatcher; import org.spongepowered.despector.transform.matcher.instruction.NullConstantMatcher; import org.spongepowered.despector.transform.matcher.instruction.StaticFieldAccessMatcher; import org.spongepowered.despector.transform.matcher.instruction.StaticInvokeMatcher; import org.spongepowered.despector.transform.matcher.instruction.StringConstantMatcher; import javax.annotation.Nullable; /** * A matcher for instructions. */ public interface InstructionMatcher<T extends Instruction> { /** * Attempts to match against the given instruction and return the matched * instruction if successful. */ @Nullable T match(MatchContext ctx, Instruction insn); /** * Attempts to match against the given instruction and return the matched * instruction if successful. */ @Nullable default T match(Instruction insn) { return match(MatchContext.create(), insn); } /** * Attempts to match against the given instruction.. */ default boolean matches(MatchContext ctx, Instruction insn) { return match(ctx, insn) != null; } /** * A matcher which matches any instruction. */ static final InstructionMatcher<?> ANY = new Any(); public static StaticInvokeMatcher.Builder staticInvoke() { return new StaticInvokeMatcher.Builder(); } public static InstanceMethodInvokeMatcher.Builder instanceMethodInvoke() { return new InstanceMethodInvokeMatcher.Builder(); } public static LocalAccessMatcher.Builder localAccess() { return new LocalAccessMatcher.Builder(); } public static InstanceFieldAccessMatcher.Builder instanceFieldAccess() { return new InstanceFieldAccessMatcher.Builder(); } public static IntConstantMatcher.Builder intConstant() { return new IntConstantMatcher.Builder(); } public static ArrayAccessMatcher.Builder arrayAccess() { return new ArrayAccessMatcher.Builder(); } public static StaticFieldAccessMatcher.Builder staticFieldAccess() { return new StaticFieldAccessMatcher.Builder(); } public static StringConstantMatcher.Builder stringConstant() { return new StringConstantMatcher.Builder(); } public static NullConstantMatcher.Builder nullConstant() { return new NullConstantMatcher.Builder(); } /** * An instruction matcher that matches any instruction. */ public static class Any implements InstructionMatcher<Instruction> { Any() { } @Override public Instruction match(MatchContext ctx, Instruction stmt) { return stmt; } } /** * Unwraps the given instruction if it is a cast or a primative unboxing * method. */ static Instruction unwrapCast(Instruction insn) { if (insn instanceof Cast) { return unwrapCast(((Cast) insn).getValue()); } if (insn instanceof InstanceMethodInvoke) { InstanceMethodInvoke invoke = (InstanceMethodInvoke) insn; String key = invoke.getOwner() + invoke.getMethodName() + invoke.getMethodDescription(); if ("Ljava/lang/Number;byteValue()B".equals(key)) { return unwrapCast(invoke.getCallee()); } if ("Ljava/lang/Number;shortValue()S".equals(key)) { return unwrapCast(invoke.getCallee()); } if ("Ljava/lang/Number;intValue()I".equals(key)) { return unwrapCast(invoke.getCallee()); } if ("Ljava/lang/Number;longValue()J".equals(key)) { return unwrapCast(invoke.getCallee()); } if ("Ljava/lang/Number;floatValue()F".equals(key)) { return unwrapCast(invoke.getCallee()); } if ("Ljava/lang/Number;doubleValue()D".equals(key)) { return unwrapCast(invoke.getCallee()); } } return insn; } }
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/transform/matcher/package-info.java
src/main/java/org/spongepowered/despector/transform/matcher/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.transform.matcher;
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/transform/matcher/StatementMatcher.java
src/main/java/org/spongepowered/despector/transform/matcher/StatementMatcher.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.transform.matcher; import org.spongepowered.despector.ast.stmt.Statement; import org.spongepowered.despector.transform.matcher.statement.ForEachMatcher; import org.spongepowered.despector.transform.matcher.statement.ForLoopMatcher; import org.spongepowered.despector.transform.matcher.statement.IfMatcher; import org.spongepowered.despector.transform.matcher.statement.IncrementMatcher; import org.spongepowered.despector.transform.matcher.statement.InstanceFieldAssignmentMatcher; import org.spongepowered.despector.transform.matcher.statement.InvokeMatcher; import org.spongepowered.despector.transform.matcher.statement.LocalAssignmentMatcher; import org.spongepowered.despector.transform.matcher.statement.ReturnValueMatcher; import org.spongepowered.despector.transform.matcher.statement.StaticFieldAssignmentMatcher; import org.spongepowered.despector.transform.matcher.statement.WhileLoopMatcher; import javax.annotation.Nullable; /** * A matcher for statements. */ public interface StatementMatcher<T extends Statement> { /** * Attempts to match against the given statement and return the matched * statement if successful. */ @Nullable T match(MatchContext ctx, Statement stmt); /** * Attempts to match against the given statement and return the matched * statement if successful. */ @Nullable default T match(Statement stmt) { return match(MatchContext.create(), stmt); } /** * Attempts to match against the given statement.. */ default boolean matches(MatchContext ctx, Statement stmt) { return match(ctx, stmt) != null; } /** * A matcher which matches any statement. */ static final StatementMatcher<?> ANY = new Any(); /** * A matcher which matches no statement. Does match true for a null * statement. */ static final StatementMatcher<?> NONE = new None(); static ForEachMatcher.Builder forEach() { return new ForEachMatcher.Builder(); } static ForLoopMatcher.Builder forLoop() { return new ForLoopMatcher.Builder(); } static LocalAssignmentMatcher.Builder localAssign() { return new LocalAssignmentMatcher.Builder(); } static IncrementMatcher.Builder increment() { return new IncrementMatcher.Builder(); } static WhileLoopMatcher.Builder whileLoop() { return new WhileLoopMatcher.Builder(); } static StaticFieldAssignmentMatcher.Builder staticFieldAssign() { return new StaticFieldAssignmentMatcher.Builder(); } static InstanceFieldAssignmentMatcher.Builder instanceFieldAssign() { return new InstanceFieldAssignmentMatcher.Builder(); } static InvokeMatcher.Builder invoke() { return new InvokeMatcher.Builder(); } static IfMatcher.Builder ifThen() { return new IfMatcher.Builder(); } static ReturnValueMatcher.Builder returnValue() { return new ReturnValueMatcher.Builder(); } /** * A statement matcher that matches any statement. */ public static class Any implements StatementMatcher<Statement> { Any() { } @Override public Statement match(MatchContext ctx, Statement stmt) { return stmt; } } /** * A statement matcher that matches no statement excetp a null statement. */ public static class None implements StatementMatcher<Statement> { None() { } @Override public Statement match(MatchContext ctx, Statement stmt) { return null; } @Override public boolean matches(MatchContext ctx, Statement stmt) { return stmt == 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/transform/matcher/ConditionMatcher.java
src/main/java/org/spongepowered/despector/transform/matcher/ConditionMatcher.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.transform.matcher; import org.spongepowered.despector.ast.Locals.LocalInstance; import org.spongepowered.despector.ast.insn.condition.Condition; import org.spongepowered.despector.transform.matcher.condition.AndConditionMatcher; import org.spongepowered.despector.transform.matcher.condition.BooleanConditionMatcher; import org.spongepowered.despector.transform.matcher.condition.CompareConditionMatcher; import org.spongepowered.despector.transform.matcher.condition.ConditionReferenceMatcher; import org.spongepowered.despector.transform.matcher.condition.InverseConditionMatcher; import org.spongepowered.despector.transform.matcher.condition.OrConditionMatcher; import javax.annotation.Nullable; /** * A matcher for conditions. */ public interface ConditionMatcher<T extends Condition> { /** * Matches against the given condition and return the matched condition if * successful. */ @Nullable T match(MatchContext ctx, Condition cond); /** * Matches against the given condition and return the matched condition if * successful. */ @Nullable default T match(Condition cond) { return match(MatchContext.create(), cond); } /** * Attempts to match the given condition. */ default boolean matches(MatchContext ctx, Condition cond) { return match(ctx, cond) != null; } /** * Attempts to match the given condition. */ default boolean matches(Condition cond) { return match(MatchContext.create(), cond) != null; } /** * A matcher which matches any condition. */ static final ConditionMatcher<?> ANY = new Any(); static BooleanConditionMatcher.Builder bool() { return new BooleanConditionMatcher.Builder(); } static InverseConditionMatcher.Builder inverse() { return new InverseConditionMatcher.Builder(); } static CompareConditionMatcher.Builder compare() { return new CompareConditionMatcher.Builder(); } static AndConditionMatcher.Builder and() { return new AndConditionMatcher.Builder(); } static OrConditionMatcher.Builder or() { return new OrConditionMatcher.Builder(); } static ConditionReferenceMatcher references(LocalInstance local) { return new ConditionReferenceMatcher(local); } static ConditionReferenceMatcher references(String ctx) { return new ConditionReferenceMatcher(ctx); } /** * A condition matcher that matches any condition. */ public static class Any implements ConditionMatcher<Condition> { Any() { } @Override public Condition match(MatchContext ctx, Condition stmt) { return stmt; } } }
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/transform/matcher/instruction/package-info.java
src/main/java/org/spongepowered/despector/transform/matcher/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.transform.matcher.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/transform/matcher/instruction/StringConstantMatcher.java
src/main/java/org/spongepowered/despector/transform/matcher/instruction/StringConstantMatcher.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.transform.matcher.instruction; import org.spongepowered.despector.ast.insn.Instruction; import org.spongepowered.despector.ast.insn.cst.StringConstant; import org.spongepowered.despector.transform.matcher.InstructionMatcher; import org.spongepowered.despector.transform.matcher.MatchContext; /** * A matcher for integer constants. */ public class StringConstantMatcher implements InstructionMatcher<StringConstant> { private String value; StringConstantMatcher(String value) { this.value = value; } @Override public StringConstant match(MatchContext ctx, Instruction insn) { if (!(insn instanceof StringConstant)) { return null; } StringConstant invoke = (StringConstant) insn; if (this.value != null && !this.value.equals(invoke.getConstant())) { return null; } return invoke; } /** * A matcher builder. */ public static class Builder { private String value; public Builder() { reset(); } public Builder value(String val) { this.value = val; return this; } /** * Resets this builder. */ public Builder reset() { this.value = null; return this; } public StringConstantMatcher build() { return new StringConstantMatcher(this.value); } } }
java
MIT
e60cfaaf7a0688bc83f8b00c392521d0b162afba
2026-01-05T02:42:07.381192Z
false