BeforeStringInvoke.java
2.37 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
* This file is part of LiteLoader.
* Copyright (C) 2012-16 Adam Mummery-Smith
* All Rights Reserved.
*/
package com.mumfrey.liteloader.transformers.event.inject;
import java.util.Collection;
import org.spongepowered.asm.lib.tree.AbstractInsnNode;
import org.spongepowered.asm.lib.tree.InsnList;
import org.spongepowered.asm.lib.tree.LdcInsnNode;
import com.mumfrey.liteloader.transformers.event.MethodInfo;
import com.mumfrey.liteloader.util.log.LiteLoaderLogger;
/**
* An injection point which searches for a matching String LDC insn immediately
* prior to a qualifying invoke.
*
* @author Adam Mummery-Smith
*/
public class BeforeStringInvoke extends BeforeInvoke
{
private static final String STRING_VOID_SIG = "(Ljava/lang/String;)V";
private final String ldcValue;
private boolean foundLdc;
public BeforeStringInvoke(String ldcValue, MethodInfo method)
{
this(ldcValue, method, -1);
}
public BeforeStringInvoke(String ldcValue, MethodInfo method, int ordinal)
{
super(method, ordinal);
this.ldcValue = ldcValue;
for (int i = 0; i < this.methodSignatures.length; i++)
{
if (!STRING_VOID_SIG.equals(this.methodSignatures[i]))
{
throw new IllegalArgumentException("BeforeStringInvoke requires method with with signature " + STRING_VOID_SIG);
}
}
}
@Override
public boolean find(String desc, InsnList insns, Collection<AbstractInsnNode> nodes)
{
this.foundLdc = false;
return super.find(desc, insns, nodes);
}
@Override
protected void inspectInsn(String desc, InsnList insns, AbstractInsnNode insn)
{
if (insn instanceof LdcInsnNode)
{
LdcInsnNode node = (LdcInsnNode)insn;
if (node.cst instanceof String && this.ldcValue.equals(node.cst))
{
if (this.logging) LiteLoaderLogger.info("BeforeInvoke found a matching LDC with value %s", node.cst);
this.foundLdc = true;
return;
}
}
this.foundLdc = false;
}
@Override
protected boolean matchesInsn(InsnInfo nodeInfo, int ordinal)
{
if (this.logging) LiteLoaderLogger.debug("BeforeInvoke foundLdc \"%s\" = %s", this.ldcValue, this.foundLdc);
return this.foundLdc && super.matchesInsn(nodeInfo, ordinal);
}
}