1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package art;
18 import art.constmethodhandle.TestInvoke;
19 import java.util.*;
20 import java.lang.reflect.*;
21 import java.lang.invoke.MethodHandle;
22 import java.lang.invoke.MethodHandles;
23 import java.lang.invoke.MethodType;
24 
25 public class Test1948 {
26   // These are initialized by a method added by test_generator.
27   // They will contain the dex bytes of TestInvoker but with the method handle changed to pointing
28   // to sayBye.
29   public static final byte[] CLASS_BYTES;
30   public static final byte[] DEX_BYTES;
31   static {
32     try {
33       // TestGenerator will add functions that get the base64 string of these functions. When we
34       // compile this the functions haven't been generated yet though so just do things this way.
35       MethodHandle getClassBase64 = MethodHandles.lookup().findStatic(
36           Test1948.class, "getClassBase64", MethodType.methodType(String.class));
37       MethodHandle getDexBase64 = MethodHandles.lookup().findStatic(
38           Test1948.class, "getDexBase64", MethodType.methodType(String.class));
39       CLASS_BYTES = Base64.getDecoder().decode((String)getClassBase64.invokeExact());
40       DEX_BYTES = Base64.getDecoder().decode((String)getDexBase64.invokeExact());
41     } catch (Throwable e) {
42       throw new Error("Failed to initialize statics: ", e);
43     }
44   }
45 
run()46   public static void run() throws Throwable {
47     // NB Because we aren't using desugar we cannot use capturing-lambda or string concat anywhere
48     // in this test! Version 9+ javac turns these into invokedynamics using bootstrap methods not
49     // currently present in android.
50     new TestInvoke().runTest(
51         new Runnable() { public void run() { System.out.println("Do nothing"); } });
52     new TestInvoke().runTest(
53         new Runnable() {
54           public void run() {
55             System.out.println("transforming calling function");
56             Redefinition.doCommonClassRedefinition(TestInvoke.class, CLASS_BYTES, DEX_BYTES);
57           }
58         });
59     new TestInvoke().runTest(
60         new Runnable() { public void run() { System.out.println("Do nothing"); } });
61   }
62 }
63