This repository has been archived by the owner on Mar 15, 2024. It is now read-only.
/
LambdaExpressionTrees.java
executable file
·211 lines (179 loc) · 8.93 KB
/
LambdaExpressionTrees.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
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
package org.enumerable.lambda.support.expression;
import japa.parser.ast.expr.Expression;
import org.enumerable.lambda.Fn0;
import org.enumerable.lambda.Fn1;
import org.enumerable.lambda.Fn2;
import org.enumerable.lambda.Fn3;
import org.enumerable.lambda.annotation.LambdaLocal;
import org.enumerable.lambda.weaving.InMemoryCompiler;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.Type;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.LocalVariableNode;
import org.objectweb.asm.tree.MethodNode;
import org.objectweb.asm.tree.analysis.Analyzer;
import org.objectweb.asm.tree.analysis.Frame;
import org.objectweb.asm.util.ASMifierMethodVisitor;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.StringReader;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import static org.enumerable.lambda.exception.UncheckedException.uncheck;
import static org.objectweb.asm.Type.*;
public class LambdaExpressionTrees {
static int expressionId = 1;
static InMemoryCompiler compiler = new InMemoryCompiler();
public static Expression parseExpression(String expression) {
try {
Class<?> parserClass = Class.forName("japa.parser.ASTParser");
Constructor<?> ctor = parserClass.getConstructor(Reader.class);
ctor.setAccessible(true);
Object parser = ctor.newInstance(new StringReader(expression));
Method method = parserClass.getMethod("Expression");
method.setAccessible(true);
return (Expression) method.invoke(parser);
} catch (Exception e) {
throw uncheck(e);
}
}
static Expression parseExpressionFromSingleMethodClass(Class<?> aClass, String... parameters) {
return parseExpressionFromMethod(Fn0.getLambdaMethod(aClass), parameters);
}
public static Expression parseExpressionFromMethod(Method method, String... parameters) {
try {
MethodNode mn = findMethodNode(method);
LocalVariableNode[] parameterLocals = new LocalVariableNode[parameters.length];
Type[] argumentTypes = getArgumentTypes(mn.desc);
int realIndex = 1;
for (int i = 0; i < parameters.length; i++) {
parameterLocals[i] = new LocalVariableNode(parameters[i], argumentTypes[i].getDescriptor(), null,
null, null, realIndex);
realIndex += argumentTypes[i].getSize();
}
final ExpressionInterpreter interpreter = new ExpressionInterpreter(mn, parameterLocals);
Analyzer analyzer = new Analyzer(interpreter) {
protected Frame newFrame(Frame src) {
Frame frame = super.newFrame(src);
interpreter.setCurrentFrame(frame);
return frame;
}
protected void newControlFlowEdge(int insn, int successor) {
interpreter.newControlFlowEdge(insn, successor);
}
};
interpreter.analyzer = analyzer;
analyzer.analyze(getInternalName(method.getDeclaringClass()), mn);
return interpreter.expression;
} catch (Exception e) {
throw uncheck(e);
}
}
static void printASMifiedMethod(Method method) {
try {
MethodNode mn = findMethodNode(method);
ASMifierMethodVisitor asm = new ASMifierMethodVisitor();
mn.accept(asm);
PrintWriter pw = new PrintWriter(System.out);
asm.print(pw);
pw.flush();
} catch (Exception e) {
throw uncheck(e);
}
}
@SuppressWarnings("unchecked")
static MethodNode findMethodNode(Method method) throws IOException {
String className = method.getDeclaringClass().getName();
ClassReader cr;
if (InMemoryCompiler.bytesByClassName.containsKey(className))
cr = new ClassReader(InMemoryCompiler.bytesByClassName.get(className));
else
cr = new ClassReader(className);
ClassNode cn = new ClassNode();
cr.accept(cn, 0);
String descriptor = getMethodDescriptor(method);
for (MethodNode mn : (List<MethodNode>) cn.methods) {
if (method.getName().equals(mn.name) && descriptor.equals(mn.desc))
return mn;
}
throw new IllegalStateException("Cannot find method which does exist");
}
@SuppressWarnings("unchecked")
public static <R extends Expression> R toExpression(Fn0<?> fn) {
if (fn.getClass().getDeclaredFields().length > 0)
throw new IllegalArgumentException("Turning Closures into Expressions isn't supported");
List<LambdaLocal> parameters = fn.getParameters();
String[] parameterNames = new String[parameters.size()];
for (int i = 0; i < parameters.size(); i++)
parameterNames[i] = parameters.get(i).name();
return (R) parseExpressionFromSingleMethodClass(fn.getClass(), parameterNames);
}
public static <R> Fn0<R> toFn0(Class<R> returnType, Expression expression) {
try {
String className = "ExpressionFn0_" + expressionId++;
String source = "class " + className + " extends " + Fn0.class.getName() + "{ public "
+ typeToString(returnType) + " call() { return " + expression + "; }}";
return compileAndCreate(className, source);
} catch (Exception e) {
throw uncheck(e);
}
}
public static <A1, R> Fn1<A1, R> toFn1(Class<R> returnType, Class<A1> a1Type, String a1Name,
Expression expression) {
try {
String className = "ExpressionFn1_" + expressionId++;
String source = "class " + className + " extends " + Fn1.class.getName() + "{ public "
+ typeToString(returnType) + " call(" + typeToString(a1Type) + " " + a1Name + ") { return "
+ expression + "; } public " + typeToString(returnType) + " call(Object " + a1Name
+ ") { return call((" + typeToString(a1Type) + ") " + a1Name + "); } }";
return compileAndCreate(className, source);
} catch (Exception e) {
throw uncheck(e);
}
}
public static <A1, A2, R> Fn2<A1, A2, R> toFn2(Class<R> returnType, Class<A1> a1Type, String a1Name,
Class<A2> a2Type, String a2Name, Expression expression) {
try {
String className = "ExpressionFn2_" + expressionId++;
String source = "class " + className + " extends " + Fn2.class.getName() + "{ public "
+ typeToString(returnType) + " call(" + typeToString(a1Type) + " " + a1Name + ", "
+ typeToString(a2Type) + " " + a2Name + ") { return " + expression + "; } public "
+ typeToString(returnType) + " call(Object " + a1Name + ", Object " + a2Name
+ ") { return call((" + typeToString(a1Type) + ") " + a1Name + ", (" + typeToString(a2Type)
+ ") " + a2Name + "); } }";
return compileAndCreate(className, source);
} catch (Exception e) {
throw uncheck(e);
}
}
public static <A1, A2, A3, R> Fn3<A1, A2, A3, R> toFn3(Class<R> returnType, Class<A1> a1Type, String a1Name,
Class<A2> a2Type, String a2Name, Class<A2> a3Type, String a3Name, Expression expression) {
try {
String className = "ExpressionFn3_" + expressionId++;
String source = "class " + className + " extends " + Fn3.class.getName() + "{ public "
+ typeToString(returnType) + " call(" + typeToString(a1Type) + " " + a1Name + ", "
+ typeToString(a2Type) + " " + a2Name + ", " + typeToString(a3Type) + " " + a3Name
+ ") { return " + expression + "; } public " + typeToString(returnType) + " call(Object "
+ a1Name + ", Object " + a2Name + ", Object " + a3Name + ") { return call(("
+ typeToString(a1Type) + ") " + a1Name + ", (" + typeToString(a2Type) + ") " + a2Name + ", ("
+ typeToString(a3Type) + ") " + a3Name + "); } }";
return compileAndCreate(className, source);
} catch (Exception e) {
throw uncheck(e);
}
}
static String typeToString(Class<?> returnType) {
return returnType.isArray() ? returnType.getComponentType().getName() + "[]" : returnType.getName();
}
@SuppressWarnings("unchecked")
static <R extends Fn0<?>> R compileAndCreate(String className, String source) throws IOException,
InstantiationException, IllegalAccessException, InvocationTargetException {
Class<?> aClass = compiler.compile(className, source);
Constructor<?> ctor = aClass.getDeclaredConstructors()[0];
ctor.setAccessible(true);
return (R) ctor.newInstance();
}
}