55import net .bytebuddy .description .type .TypeDescription ;
66import net .bytebuddy .implementation .Implementation ;
77import net .bytebuddy .implementation .bytecode .StackManipulation ;
8+ import net .bytebuddy .implementation .bytecode .StackSize ;
89import net .bytebuddy .jar .asm .Handle ;
910import net .bytebuddy .jar .asm .MethodVisitor ;
1011import net .bytebuddy .jar .asm .Opcodes ;
1112import net .bytebuddy .jar .asm .Type ;
1213
13- import java .lang .invoke .CallSite ;
14- import java .lang .invoke .LambdaMetafactory ;
15- import java .lang .invoke .MethodHandles ;
16- import java .lang .invoke .MethodType ;
14+ import java .lang .invoke .*;
15+ import java .lang .reflect .Method ;
1716import java .util .List ;
1817import java .util .Map ;
1918import java .util .stream .Collectors ;
@@ -23,6 +22,15 @@ public class InvokeDynamic implements StackManipulation {
2322 private final Map <String , Variable > fnVars ;
2423 private ScriptContext scriptContext ;
2524
25+ private static Method metaFactory ;
26+ static {
27+ try {
28+ metaFactory = LambdaMetafactory .class .getDeclaredMethod ("metafactory" , MethodHandles .Lookup .class , String .class , MethodType .class , MethodType .class , MethodHandle .class , MethodType .class );
29+ } catch (NoSuchMethodException e ) {
30+ e .printStackTrace ();
31+ }
32+ }
33+
2634 public InvokeDynamic (ScriptContext scriptContext , Map <String , Variable > fnVars ) {
2735 this .scriptContext = scriptContext ;
2836 this .fnVars = fnVars ;
@@ -41,20 +49,31 @@ public Size apply(MethodVisitor methodVisitor, Implementation.Context implementa
4149 .collect (Collectors .joining ());
4250 descriptor = descriptor .replaceAll ("%s" ,scriptContext .getInsnType ().getDescriptor () + k );
4351
44- methodVisitor .visitInvokeDynamicInsn ("run" ,
52+
53+
54+ // methodVisitor.visitInvokeDynamicInsn("run",
55+ // "(Lcz/neumimto/nts/DecompileTest;IZ)Ljava/lang/Runnable;",
56+ // new Handle(Opcodes.H_INVOKESTATIC, "java/lang/invoke/LambdaMetafactory", "metafactory", "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;", false),
57+ // Type.getType("()V"),
58+ // new Handle(Opcodes.H_INVOKEVIRTUAL, "cz/neumimto/nts/DecompileTest", "lambda$a$0", "(IZ)V", false),
59+ // Type.getType("()V"));
60+ //
61+
62+
63+
64+ methodVisitor .visitInvokeDynamicInsn ("run" ,
4565 descriptor ,
46- new Handle (Opcodes .H_INVOKESTATIC ,
47- new TypeDescription .ForLoadedType (LambdaMetafactory .class ).getInternalName (),
66+ new Handle (Opcodes .H_INVOKESTATIC , new TypeDescription .ForLoadedType (LambdaMetafactory .class ).getInternalName (),
4867 "metafactory" ,
49- MethodType . methodType ( CallSite . class , MethodHandles . Lookup . class , String . class , MethodType . class , String . class , Object []. class ). toMethodDescriptorString ( ),
68+ Type . getMethodDescriptor ( metaFactory ),
5069 false ),
51- Type .VOID_TYPE ,
52- new Handle (Opcodes .H_INVOKESPECIAL ,
70+ Type .getType ( "()V" ) ,
71+ new Handle (Opcodes .H_INVOKEVIRTUAL ,
5372 scriptContext .getInsnType ().getInternalName (),
54- Scope .LAMBDA_METHOD_NAME .apply (scriptContext .getScopes ().size () - 1 ),
73+ Scope .LAMBDA_METHOD_NAME .apply (scriptContext .getScopes ().size () - 2 ),
5574 "(" +k +")V" ,
5675 false ),
57- Type .VOID_TYPE );
58- return null ;
76+ Type .getType ( "()V" ) );
77+ return new StackManipulation . Size ( 1 , 1 ) ;
5978 }
6079}
0 commit comments