|
1 | 1 | package com.falsepattern.jfunge.interpreter.instructions; |
2 | 2 |
|
3 | | -import com.falsepattern.jfunge.interpreter.ExecutionContext; |
| 3 | +import com.falsepattern.jfunge.interpreter.LambdaHelper; |
4 | 4 | import lombok.val; |
5 | 5 |
|
6 | 6 | import java.lang.annotation.Retention; |
7 | 7 | import java.lang.annotation.RetentionPolicy; |
8 | | -import java.lang.invoke.LambdaMetafactory; |
9 | | -import java.lang.invoke.MethodHandles; |
10 | | -import java.lang.invoke.MethodType; |
11 | 8 | import java.lang.reflect.Modifier; |
12 | 9 | import java.util.Arrays; |
13 | 10 | import java.util.function.IntConsumer; |
|
16 | 13 | public interface InstructionSet { |
17 | 14 | default void load(ObjIntConsumer<Instruction> instructionSet) { |
18 | 15 | val clazz = this.getClass(); |
19 | | - Arrays.stream(clazz.getDeclaredMethods()).filter((method) -> Modifier.isStatic(method.getModifiers()) && method.isAnnotationPresent(Instr.class)).forEach((method) -> { |
20 | | - val lookup = MethodHandles.lookup(); |
21 | | - val methodType = MethodType.methodType(void.class, ExecutionContext.class); |
22 | | - try { |
23 | | - val lambda = (Instruction) LambdaMetafactory.metafactory(lookup, "process", MethodType.methodType(Instruction.class), methodType, lookup.findStatic(clazz, method.getName(), methodType), methodType).getTarget().invokeExact(); |
24 | | - val ann = method.getAnnotation(Instr.class); |
25 | | - instructionSet.accept(lambda, ann.value()); |
26 | | - } catch (Throwable e) { |
27 | | - throw new RuntimeException(e); |
28 | | - } |
29 | | - }); |
| 16 | + Arrays.stream(clazz.getDeclaredMethods()) |
| 17 | + .filter((method) -> Modifier.isStatic(method.getModifiers()) && method.isAnnotationPresent(Instr.class)) |
| 18 | + .forEach((method) -> { |
| 19 | + try { |
| 20 | + val lambda = (Instruction) LambdaHelper.newLambdaMetaFactory(Instruction.class, method).invokeExact(); |
| 21 | + val ann = method.getAnnotation(Instr.class); |
| 22 | + instructionSet.accept(lambda, ann.value()); |
| 23 | + } catch (Throwable e) { |
| 24 | + throw new RuntimeException(e); |
| 25 | + } |
| 26 | + }); |
30 | 27 | } |
31 | 28 |
|
32 | 29 | default void unload(IntConsumer instructionSet) { |
33 | 30 | val clazz = this.getClass(); |
34 | | - Arrays.stream(clazz.getDeclaredMethods()).filter((method) -> Modifier.isStatic(method.getModifiers()) && method.isAnnotationPresent(Instr.class)).forEach((method) -> { |
35 | | - val ann = method.getAnnotation(Instr.class); |
36 | | - instructionSet.accept(ann.value()); |
37 | | - }); |
| 31 | + Arrays.stream(clazz.getDeclaredMethods()) |
| 32 | + .filter((method) -> Modifier.isStatic(method.getModifiers()) && method.isAnnotationPresent(Instr.class)) |
| 33 | + .forEach((method) -> { |
| 34 | + val ann = method.getAnnotation(Instr.class); |
| 35 | + instructionSet.accept(ann.value()); |
| 36 | + }); |
38 | 37 | } |
39 | 38 |
|
40 | 39 | @Retention(RetentionPolicy.RUNTIME) |
|
0 commit comments