InjectionPoint.java
9.89 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
* This file is part of LiteLoader.
* Copyright (C) 2012-16 Adam Mummery-Smith
* All Rights Reserved.
*/
package com.mumfrey.liteloader.transformers.event;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.InsnList;
import com.google.common.base.Joiner;
/**
* Base class for injection point discovery classes. Each subclass describes a
* strategy for locating code injection points within a method, with the
* {@link #find} method populating a collection with insn nodes from the method
* which satisfy its strategy.
*
* <p>This base class also contains composite strategy factory methods such as
* {@code and} and {@code or} which allow strategies to be combined using
* intersection (and) or union (or) relationships to allow multiple strategies
* to be easily combined.</p>
*
* <p>You are free to create your own injection point subclasses, but take note
* that it is allowed for a single InjectionPoint instance to be used for
* multiple injections and thus implementing classes MUST NOT cache the insn
* list, event, or nodes instance passed to the {@code find} method, as each
* call to {@code find} must be considered a separate functional contract and
* the InjectionPoint's lifespan is not linked to the discovery lifespan,
* therefore it is important that the InjectionPoint implementation is fully
* STATELESS.</p>
*
* @author Adam Mummery-Smith
*/
public abstract class InjectionPoint
{
/**
* Capture locals as well as args
*/
protected boolean captureLocals;
protected boolean logLocals;
/**
* Find injection points in the supplied insn list
*
* @param desc Method descriptor, supplied to allow return types and
* arguments etc. to be determined
* @param insns Insn list to search in, the strategy MUST ONLY add nodes
* from this list to the {@code nodes} collection
* @param nodes Collection of nodes to populate. Injectors should NOT make
* any assumptions about the state of this collection and should only
* call add()
* @param event Event being injected here, supplied to allow alteration of
* behaviour for specific event configurations (eg. cancellable)
* @return true if one or more injection points were found
*/
public abstract boolean find(String desc, InsnList insns, Collection<AbstractInsnNode> nodes, Event event);
/**
* Set whether this injection point should capture local variables as well
* as method arguments.
*
* @param captureLocals
* @return this, for fluent interface
*/
public InjectionPoint setCaptureLocals(boolean captureLocals)
{
this.captureLocals = captureLocals;
return this;
}
/**
* Get whether capture locals is enabled
*/
public boolean captureLocals()
{
return this.captureLocals;
}
/**
* Since it's virtually impossible to know what locals are available at a
* given injection point by reading the source, this method causes the
* injection point to dump the locals to the debug log at injection time.
*
* @param logLocals
* @return this, for fluent interface
*/
public InjectionPoint setLogLocals(boolean logLocals)
{
this.logLocals = logLocals;
return this;
}
/**
* Get whether log locals is enabled
*/
public boolean logLocals()
{
return this.logLocals;
}
@Override
public String toString()
{
return "InjectionPoint(" + this.getClass().getSimpleName() + ")";
}
/**
* Composite injection point
*
* @author Adam Mummery-Smith
*/
abstract static class CompositeInjectionPoint extends InjectionPoint
{
protected final InjectionPoint[] components;
protected CompositeInjectionPoint(InjectionPoint... components)
{
if (components == null || components.length < 2)
{
throw new IllegalArgumentException("Must supply two or more component injection points for composite point!");
}
this.components = components;
for (InjectionPoint component : this.components)
{
this.captureLocals |= component.captureLocals;
this.logLocals |= component.logLocals;
}
}
@Override
public String toString()
{
return "CompositeInjectionPoint(" + this.getClass().getSimpleName() + ")[" + Joiner.on(',').join(this.components) + "]";
}
}
static final class Intersection extends InjectionPoint.CompositeInjectionPoint
{
public Intersection(InjectionPoint... points)
{
super(points);
}
@Override
public boolean find(String desc, InsnList insns, Collection<AbstractInsnNode> nodes, Event event)
{
boolean found = false;
@SuppressWarnings("unchecked")
ArrayList<AbstractInsnNode>[] allNodes = new ArrayList[this.components.length];
for (int i = 0; i < this.components.length; i++)
{
allNodes[i] = new ArrayList<AbstractInsnNode>();
this.components[i].find(desc, insns, allNodes[i], event);
}
ArrayList<AbstractInsnNode> alpha = allNodes[0];
for (int nodeIndex = 0; nodeIndex < alpha.size(); nodeIndex++)
{
AbstractInsnNode node = alpha.get(nodeIndex);
boolean in = true;
for (int b = 1; b < allNodes.length; b++)
{
if (!allNodes[b].contains(node))
{
break;
}
}
if (!in) continue;
nodes.add(node);
found = true;
}
return found;
}
}
static final class Union extends InjectionPoint.CompositeInjectionPoint
{
public Union(InjectionPoint... points)
{
super(points);
}
@Override
public boolean find(String desc, InsnList insns, Collection<AbstractInsnNode> nodes, Event event)
{
LinkedHashSet<AbstractInsnNode> allNodes = new LinkedHashSet<AbstractInsnNode>();
for (int i = 0; i < this.components.length; i++)
{
this.components[i].find(desc, insns, allNodes, event);
}
nodes.addAll(allNodes);
return allNodes.size() > 0;
}
}
static final class Shift extends InjectionPoint
{
private final InjectionPoint input;
private final int shift;
public Shift(InjectionPoint input, int shift)
{
if (input == null)
{
throw new IllegalArgumentException("Must supply an input injection point for SHIFT");
}
this.input = input;
this.shift = shift;
}
@Override
public InjectionPoint setCaptureLocals(boolean captureLocals)
{
return this.input.setCaptureLocals(captureLocals);
}
@Override
public boolean captureLocals()
{
return this.input.captureLocals();
}
@Override
public InjectionPoint setLogLocals(boolean logLocals)
{
return this.input.setLogLocals(logLocals);
}
@Override
public boolean logLocals()
{
return this.input.logLocals();
}
@Override
public String toString()
{
return "InjectionPoint(" + this.getClass().getSimpleName() + ")[" + this.input + "]";
}
@Override
public boolean find(String desc, InsnList insns, Collection<AbstractInsnNode> nodes, Event event)
{
List<AbstractInsnNode> list = (nodes instanceof List) ? (List<AbstractInsnNode>)nodes : new ArrayList<AbstractInsnNode>(nodes);
this.input.find(desc, insns, nodes, event);
for (int i = 0; i < list.size(); i++)
{
list.set(i, insns.get(insns.indexOf(list.get(i)) + this.shift));
}
if (nodes != list)
{
nodes.clear();
nodes.addAll(list);
}
return nodes.size() > 0;
}
}
/**
* Returns a composite injection point which returns the intersection of
* nodes from all component injection points
*
* @param operands
*/
public static InjectionPoint and(InjectionPoint... operands)
{
return new InjectionPoint.Intersection(operands);
}
/**
* Returns a composite injection point which returns the union of nodes from
* all component injection points.
*
* @param operands
*/
public static InjectionPoint or(InjectionPoint... operands)
{
return new InjectionPoint.Union(operands);
}
/**
* Returns an injection point which returns all insns immediately following
* insns from the supplied injection point.
*
* @param point
*/
public static InjectionPoint after(InjectionPoint point)
{
return new InjectionPoint.Shift(point, 1);
}
/**
* Returns an injection point which returns all insns immediately prior to
* insns from the supplied injection point.
*
* @param point
*/
public static InjectionPoint before(InjectionPoint point)
{
return new InjectionPoint.Shift(point, -1);
}
/**
* Returns an injection point which returns all insns offset by the
* specified "count" from insns from the supplied injection point.
*
* @param point
*/
public static InjectionPoint shift(InjectionPoint point, int count)
{
return new InjectionPoint.Shift(point, count);
}
}