forked from jooby-project/jooby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsns.java
More file actions
62 lines (50 loc) · 1.73 KB
/
Copy pathInsns.java
File metadata and controls
62 lines (50 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby.internal.asm;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.InsnList;
import java.util.Iterator;
import java.util.Spliterator;
import java.util.function.Function;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import static java.util.Spliterator.ORDERED;
import static java.util.Spliterators.spliteratorUnknownSize;
public class Insns {
private static class InsnIterator implements Iterator<AbstractInsnNode> {
private AbstractInsnNode node;
private Function<AbstractInsnNode, AbstractInsnNode> next;
public InsnIterator(AbstractInsnNode node,
Function<AbstractInsnNode, AbstractInsnNode> next) {
this.node = node;
this.next = next;
}
@Override public boolean hasNext() {
return node != null;
}
@Override public AbstractInsnNode next() {
AbstractInsnNode it = node;
node = next.apply(it);
return it;
}
}
public static Stream<AbstractInsnNode> next(AbstractInsnNode node) {
return stream(node, AbstractInsnNode::getNext);
}
public static Stream<AbstractInsnNode> previous(AbstractInsnNode node) {
return stream(node, AbstractInsnNode::getPrevious);
}
public static Stream<AbstractInsnNode> last(InsnList node) {
return previous(node.getLast());
}
private static Stream<AbstractInsnNode> stream(AbstractInsnNode node,
Function<AbstractInsnNode, AbstractInsnNode> next) {
Spliterator<AbstractInsnNode> iterator = spliteratorUnknownSize(
new InsnIterator(node, next),
ORDERED);
return StreamSupport.stream(iterator, false);
}
}