aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/ftbsc/lll/utils
diff options
context:
space:
mode:
author zaaarf <me@zaaarf.foo>2024-04-20 10:33:50 +0200
committer zaaarf <me@zaaarf.foo>2024-04-20 10:33:50 +0200
commit8f6f5615ce4dcfa100e73950195ae872dc8d3032 (patch)
tree77b8908767c8571ad69ff59e09e9b24dc96d9bdd /src/main/java/ftbsc/lll/utils
parentd06b6211bc0815c36d44c65312c097058901b1c5 (diff)
feat: findFirstFreeIndex method
Diffstat (limited to 'src/main/java/ftbsc/lll/utils')
-rw-r--r--src/main/java/ftbsc/lll/utils/StackUtils.java25
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);
+ }
}