Skip to content

Commit 8f6f561

Browse files
committed
feat: findFirstFreeIndex method
1 parent d06b621 commit 8f6f561

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

src/main/java/ftbsc/lll/utils/StackUtils.java

+17-8
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,25 @@ public static int addLocalVariable(MethodNode method, String name, String desc)
7171
* @return the index value of the new local variable
7272
*/
7373
public static int addLocalVariable(MethodNode method, String name, String desc, LabelNode start, LabelNode end) {
74-
final int targetIndex =
75-
method.localVariables
76-
.stream()
77-
.max(Comparator.comparingInt(v -> v.index))
78-
.map(var -> var.desc.equals("J") || var.desc.equals("D")
79-
? var.index + 2 //skip two if long or double - major java moment
80-
: var.index + 1)
81-
.orElse(0);
74+
final int targetIndex = findFirstFreeIndex(method);
8275
LocalVariableNode variable = new LocalVariableNode(name, desc, null, start, end, targetIndex);
8376
method.localVariables.add(variable);
8477
return targetIndex;
8578
}
79+
80+
/**
81+
* Finds the first free index in the given {@link MethodNode}.
82+
* Used to create variables.
83+
* @param method the method to look in
84+
* @return the found index
85+
*/
86+
public static int findFirstFreeIndex(MethodNode method) {
87+
return method.localVariables
88+
.stream()
89+
.max(Comparator.comparingInt(v -> v.index))
90+
.map(var -> var.desc.equals("J") || var.desc.equals("D")
91+
? var.index + 2 //skip two if long or double - major java moment
92+
: var.index + 1)
93+
.orElse(0);
94+
}
8695
}

0 commit comments

Comments
 (0)