mirror of
https://github.com/zaaarf/lillero.git
synced 2024-11-21 23:14:51 +01:00
feat: findFirstFreeIndex method
This commit is contained in:
parent
d06b6211bc
commit
8f6f5615ce
1 changed files with 17 additions and 8 deletions
|
@ -71,16 +71,25 @@ public class StackUtils implements Opcodes {
|
||||||
* @return the index value of the new local variable
|
* @return the index value of the new local variable
|
||||||
*/
|
*/
|
||||||
public static int addLocalVariable(MethodNode method, String name, String desc, LabelNode start, LabelNode end) {
|
public static int addLocalVariable(MethodNode method, String name, String desc, LabelNode start, LabelNode end) {
|
||||||
final int targetIndex =
|
final int targetIndex = findFirstFreeIndex(method);
|
||||||
method.localVariables
|
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()
|
.stream()
|
||||||
.max(Comparator.comparingInt(v -> v.index))
|
.max(Comparator.comparingInt(v -> v.index))
|
||||||
.map(var -> var.desc.equals("J") || var.desc.equals("D")
|
.map(var -> var.desc.equals("J") || var.desc.equals("D")
|
||||||
? var.index + 2 //skip two if long or double - major java moment
|
? var.index + 2 //skip two if long or double - major java moment
|
||||||
: var.index + 1)
|
: var.index + 1)
|
||||||
.orElse(0);
|
.orElse(0);
|
||||||
LocalVariableNode variable = new LocalVariableNode(name, desc, null, start, end, targetIndex);
|
|
||||||
method.localVariables.add(variable);
|
|
||||||
return targetIndex;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue