blob: 0b73edecc3425bc29d121f433bc6b84f0fa04f24 [file] [log] [blame]
Søren Gjessed8547be2024-05-31 08:57:24 +02001// Copyright (c) 2024, the R8 project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4package jdk21.ir.optimize.interfaces;
5
6import static org.junit.Assert.assertEquals;
7
8import com.android.tools.r8.TestBase;
9import com.android.tools.r8.TestParameters;
10import com.android.tools.r8.TestParametersCollection;
11import com.android.tools.r8.ToolHelper;
12import com.android.tools.r8.utils.codeinspector.CodeInspector;
13import com.android.tools.r8.utils.codeinspector.InstructionSubject;
14import java.io.Serializable;
15import java.lang.reflect.InvocationTargetException;
16import java.lang.reflect.Method;
17import org.junit.Test;
18import org.junit.runner.RunWith;
19import org.junit.runners.Parameterized;
20import org.junit.runners.Parameterized.Parameter;
21import org.junit.runners.Parameterized.Parameters;
22
23// This test is also present for base test module and JDK-17 to demonstrate the different javac
24// byte code.
25@RunWith(Parameterized.class)
26public class CastWithMultipleBoundsJavacBytecodeTest extends TestBase {
27
28 @Parameter(0)
29 public TestParameters parameters;
30
31 @Parameters(name = "{0}")
32 public static TestParametersCollection data() {
33 return getTestParameters().withAllRuntimesAndApiLevels().build();
34 }
35
36 @Test
37 public void testR8JavacCode() throws Exception {
38 parameters.assumeIsOrSimulateNoneRuntime();
39 // javac from JDK-21 generates the following code for getLambda with just one checkcast to
40 // the static return type:
41 //
42 // static java.io.Serializable getLambda();
43 // descriptor: ()Ljava/io/Serializable;
44 // flags: (0x0008) ACC_STATIC
45 // Code:
46 // stack=1, locals=0, args_size=0
47 // 0: invokedynamic #7, 0 // InvokeDynamic #0:run:()Ljava/lang/Runnable;
48 // 5: checkcast #11 // class java/io/Serializable
49 // 8: areturn
50 assertEquals(
51 1,
52 new CodeInspector(ToolHelper.getClassFileForTestClass(TestClass.class))
53 .clazz(TestClass.class)
54 .uniqueMethodWithOriginalName("getLambda")
55 .streamInstructions()
56 .filter(InstructionSubject::isCheckCast)
57 .count());
58 }
59
60 @Test
61 public void testR8() throws Exception {
62 testForR8(parameters.getBackend())
63 .addInnerClassesAndStrippedOuter(getClass())
64 .addKeepMainRule(TestClass.class)
65 .setMinApi(parameters)
66 .addKeepMainRule(TestClass.class)
67 .run(parameters.getRuntime(), TestClass.class);
68 }
69
70 static class TestClass {
71 static void invokeLambda(Object o)
72 throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
73 Method runMethod = o.getClass().getMethod("run");
74 runMethod.invoke(o);
75 }
76
77 static Serializable getLambda() {
78 return (Runnable & Serializable) () -> System.out.println("base lambda");
79 }
80
81 public static void main(String[] args) throws Exception {
82 invokeLambda(getLambda());
83 }
84 }
85}