Skip to content

Commit

Permalink
Add a test for the missing lambda body (#328)
Browse files Browse the repository at this point in the history
  • Loading branch information
IotaBread committed Jan 21, 2024
1 parent 950c78c commit b7977d6
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 0 deletions.
3 changes: 3 additions & 0 deletions test/org/jetbrains/java/decompiler/SingleClassesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,9 @@ private void registerDefault() {
register(JAVA_21_PREVIEW, "TestRawProcessor");
register(JAVA_21_PREVIEW, "TestFmtProcessor");
register(JAVA_21_PREVIEW, "TestCustomProcessor");

// TODO: lambda body is erased
register(JASM, "TestMissingLambdaBody");
}

private void registerEntireClassPath() {
Expand Down
108 changes: 108 additions & 0 deletions testData/results/pkg/TestMissingLambdaBody.dec
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package pkg;

import java.util.function.Consumer;

public class TestMissingLambdaBody {
public void init() {
this.visit(var1 -> {// 24 31
});// 69
}// 32

public void visit(Consumer var1) {
var1.accept("Foo".hashCode());// 38 39 40 41 42
var1.accept("Bar".length());// 43 44 45 46 47
}// 48

public Number accept(Number var1) {
System.out.println(var1);// 54 55 56
return var1;// 57 58
}
}

class 'pkg/TestMissingLambdaBody' {
method 'init ()V' {
0 6
7 6
8 6
9 6
a 8
}

method 'lambda$init$0 (Ljava/lang/Integer;)V' {
9 7
}

method 'visit (Ljava/util/function/Consumer;)V' {
0 11
1 11
2 11
3 11
4 11
5 11
6 11
7 11
8 11
9 11
a 11
b 11
c 11
d 11
e 12
f 12
10 12
11 12
12 12
13 12
14 12
15 12
16 12
17 12
18 12
19 12
1a 12
1b 12
1c 13
}

method 'accept (Ljava/lang/Number;)Ljava/lang/Number;' {
0 16
1 16
2 16
3 16
4 16
5 16
6 16
7 17
8 17
}
}

Lines mapping:
24 <-> 7
31 <-> 7
32 <-> 9
38 <-> 12
39 <-> 12
40 <-> 12
41 <-> 12
42 <-> 12
43 <-> 13
44 <-> 13
45 <-> 13
46 <-> 13
47 <-> 13
48 <-> 14
54 <-> 17
55 <-> 17
56 <-> 17
57 <-> 18
58 <-> 18
69 <-> 8
Not mapped:
25
26
64
65
66
67
68
75 changes: 75 additions & 0 deletions testData/src/jasm/TestMissingLambdaBody.jasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* This code can be assembled with <a href="https://wiki.openjdk.java.net/display/CodeTools/asmtools">asmtools</a>
* using <code>asmtools jasm -g *.jasm</code> command line.
*/
package pkg;

super public class TestMissingLambdaBody
version 52:0
{
// weird fix
const #1 = Method TestMissingLambdaBody.lambda$init$0:"(Ljava/lang/Integer;)V";

public Method "<init>":"()V"
stack 1 locals 1
{
aload_0;
invokespecial Method java/lang/Object."<init>":"()V";
return;

}
public Method init:"()V"
stack 2 locals 1
{
aload_0;
aload_0;
invokedynamic InvokeDynamic REF_invokeStatic:Method 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;":accept:"(Lpkg/TestMissingLambdaBody;)Ljava/util/function/Consumer;" {
MethodType "(Ljava/lang/Object;)V",
MethodHandle REF_invokeVirtual:#1,
MethodType "(Ljava/lang/Integer;)V"
};
invokevirtual Method visit:"(Ljava/util/function/Consumer;)V";
return;

}
public Method visit:"(Ljava/util/function/Consumer;)V"
stack 2 locals 2
{
aload_1;
ldc String "Foo";
invokevirtual Method java/lang/String.hashCode:"()I";
invokestatic Method java/lang/Integer.valueOf:"(I)Ljava/lang/Integer;";
invokeinterface InterfaceMethod java/util/function/Consumer.accept:"(Ljava/lang/Object;)V", 2;
aload_1;
ldc String "Bar";
invokevirtual Method java/lang/String.length:"()I";
invokestatic Method java/lang/Integer.valueOf:"(I)Ljava/lang/Integer;";
invokeinterface InterfaceMethod java/util/function/Consumer.accept:"(Ljava/lang/Object;)V", 2;
return;

}
public Method accept:"(Ljava/lang/Number;)Ljava/lang/Number;"
stack 2 locals 2
{
getstatic Field java/lang/System.out:"Ljava/io/PrintStream;";
aload_1;
invokevirtual Method java/io/PrintStream.println:"(Ljava/lang/Object;)V";
aload_1;
areturn;

}
private synthetic Method lambda$init$0:"(Ljava/lang/Integer;)V"
stack 2 locals 2
{
aload_0;
aload_1;
invokevirtual Method accept:"(Ljava/lang/Number;)Ljava/lang/Number;";
checkcast java/lang/Integer; // No idea why javac would compile this checkcast
pop;
return;

}

public static final InnerClass Lookup=class java/lang/invoke/MethodHandles$Lookup of class java/lang/invoke/MethodHandles;

} // end Class TestMissingLambdaBody

0 comments on commit b7977d6

Please sign in to comment.