diff options
author | zaaarf <me@zaaarf.foo> | 2024-04-20 10:33:50 +0200 |
---|---|---|
committer | zaaarf <me@zaaarf.foo> | 2024-04-20 10:33:50 +0200 |
commit | 8f6f5615ce4dcfa100e73950195ae872dc8d3032 (patch) | |
tree | 77b8908767c8571ad69ff59e09e9b24dc96d9bdd /src | |
parent | d06b6211bc0815c36d44c65312c097058901b1c5 (diff) |
feat: findFirstFreeIndex method
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/ftbsc/lll/utils/StackUtils.java | 25 |
1 files changed, 17 insertions, 8 deletions
diff --git a/src/main/java/ftbsc/lll/utils/StackUtils.java b/src/main/java/ftbsc/lll/utils/StackUtils.java index 225b1ee..da9abe8 100644 --- a/src/main/java/ftbsc/lll/utils/StackUtils.java +++ b/src/main/java/ftbsc/lll/utils/StackUtils.java @@ -71,16 +71,25 @@ public class StackUtils implements Opcodes { * @return the index value of the new local variable */ public static int addLocalVariable(MethodNode method, String name, String desc, LabelNode start, LabelNode end) { - final int targetIndex = - method.localVariables - .stream() - .max(Comparator.comparingInt(v -> v.index)) - .map(var -> var.desc.equals("J") || var.desc.equals("D") - ? var.index + 2 //skip two if long or double - major java moment - : var.index + 1) - .orElse(0); + final int targetIndex = findFirstFreeIndex(method); LocalVariableNode variable = new LocalVariableNode(name, desc, null, start, end, targetIndex); method.localVariables.add(variable); return targetIndex; } + + /** + * Finds the first free index in the given {@link MethodNode}. + * Used to create variables. + * @param method the method to look in + * @return the found index + */ + public static int findFirstFreeIndex(MethodNode method) { + return method.localVariables + .stream() + .max(Comparator.comparingInt(v -> v.index)) + .map(var -> var.desc.equals("J") || var.desc.equals("D") + ? var.index + 2 //skip two if long or double - major java moment + : var.index + 1) + .orElse(0); + } } |