-
Notifications
You must be signed in to change notification settings - Fork 8
/
Utils.java
57 lines (52 loc) · 1.6 KB
/
Utils.java
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
package software.coley.lljzip;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import software.coley.lljzip.util.MemorySegmentUtil;
import java.lang.foreign.MemorySegment;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Utilities for zip tests.
*
* @author Matt Coley
*/
public class Utils {
/**
* Asserts the string has been found and is the <b>ONLY</b> matching string in the class.
*
* @param code
* Class bytecode.
* @param target
* String instance to look for.
*/
public static void assertDefinesString(byte[] code, String target) {
boolean[] visited = new boolean[1];
ClassReader cr = new ClassReader(code);
cr.accept(new ClassVisitor(Opcodes.ASM9) {
@Override
public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
return new MethodVisitor(Opcodes.ASM9) {
@Override
public void visitLdcInsn(Object value) {
visited[0] = true;
assertEquals(target, value);
}
};
}
}, ClassReader.SKIP_FRAMES);
assertTrue(visited[0], "The entry did not visit any LDC constants");
}
/**
* Asserts the string has been found and is the <b>ONLY</b> matching string in the class.
*
* @param code
* Class bytecode.
* @param target
* String instance to look for.
*/
public static void assertDefinesString(MemorySegment code, String target) {
assertDefinesString(MemorySegmentUtil.toByteArray(code), target);
}
}