JsonInjection.java
4.41 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
package com.mumfrey.liteloader.transformers.event.json;
import java.io.Serializable;
import java.lang.reflect.Constructor;
import com.google.gson.annotations.SerializedName;
import com.mumfrey.liteloader.transformers.event.InjectionPoint;
import com.mumfrey.liteloader.transformers.event.MethodInfo;
import com.mumfrey.liteloader.transformers.event.inject.BeforeInvoke;
import com.mumfrey.liteloader.transformers.event.inject.BeforeReturn;
import com.mumfrey.liteloader.transformers.event.inject.BeforeStringInvoke;
import com.mumfrey.liteloader.transformers.event.inject.MethodHead;
/**
* A JSON injection point definition
*
* @author Adam Mummery-Smith
*/
public class JsonInjection implements Serializable
{
private static final long serialVersionUID = 1L;
/**
* Method to inject into
*/
@SerializedName("method")
private String methodName;
/**
* Type of injection point
*/
@SerializedName("type")
private JsonInjectionType type;
/**
* Shift type (optional)
*/
@SerializedName("shift")
private JsonInjectionShiftType shift;
/**
* Target method to search for when using INVOKE and INVOKESTRING
*/
@SerializedName("target")
private String target;
/**
* Ordinal to use when using INVOKE and INVOKESTRING
*/
@SerializedName("ordinal")
private int ordinal = -1;
/**
* InjectionPoint class to use for CUSTOM
*/
@SerializedName("class")
private String className;
/**
* Constructor arguments to pass wehn using CUSTOM
*/
@SerializedName("args")
private Object[] args;
private transient MethodInfo method;
private transient InjectionPoint injectionPoint;
public MethodInfo getMethod()
{
return this.method;
}
public InjectionPoint getInjectionPoint()
{
return this.injectionPoint;
}
public void parse(JsonMethods methods)
{
this.method = this.parseMethod(methods);
this.injectionPoint = this.parseInjectionPoint(methods);
}
private MethodInfo parseMethod(JsonMethods methods)
{
try
{
return methods.get(this.methodName);
}
catch (NullPointerException ex)
{
throw new InvalidEventJsonException("'method' must not be null for injection");
}
}
public InjectionPoint parseInjectionPoint(JsonMethods methods)
{
switch (this.type)
{
case INVOKE:
return this.applyShift(new BeforeInvoke(methods.get(this.getTarget()), this.ordinal));
case INVOKESTRING:
return this.applyShift(new BeforeStringInvoke(this.getArg(0).toString(), methods.get(this.getTarget()), this.ordinal));
case RETURN:
return this.applyShift(new BeforeReturn(this.ordinal));
case HEAD:
return new MethodHead();
case CUSTOM:
try
{
@SuppressWarnings("unchecked")
Class<InjectionPoint> injectionPointClass = (Class<InjectionPoint>)Class.forName(this.className);
if (this.args != null)
{
Constructor<InjectionPoint> ctor = injectionPointClass.getDeclaredConstructor(Object[].class);
return ctor.newInstance(this.args);
}
return injectionPointClass.newInstance();
}
catch (Exception ex)
{
throw new RuntimeException(ex);
}
}
throw new InvalidEventJsonException("Could not parse injection type");
}
private Object getArg(int arg)
{
if (this.args == null || this.args.length >= this.args.length || arg < 0)
return "";
return this.args[arg];
}
private String getTarget()
{
if (this.target != null && this.shift == null)
{
if (this.target.startsWith("before(") && this.target.endsWith(")"))
{
this.target = this.target.substring(7, this.target.length() - 8);
this.shift = JsonInjectionShiftType.BEFORE;
}
else if (this.target.startsWith("after(") && this.target.endsWith(")"))
{
this.target = this.target.substring(6, this.target.length() - 7);
this.shift = JsonInjectionShiftType.AFTER;
}
}
if (this.target == null)
{
throw new InvalidEventJsonException("'target' is required for injection type " + this.type.name());
}
return this.target;
}
private InjectionPoint applyShift(InjectionPoint injectionPoint)
{
if (this.shift != null)
{
switch (this.shift)
{
case AFTER:
return InjectionPoint.after(injectionPoint);
case BEFORE:
return InjectionPoint.before(injectionPoint);
}
}
return injectionPoint;
}
}