1 /*
2  * Copyright (C) 2024 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 package com.android.hoststubgen.test.tinyframework;
17 
18 import android.hosttest.annotation.HostSideTestWholeClassKeep;
19 
20 import java.util.concurrent.atomic.AtomicBoolean;
21 
22 @HostSideTestWholeClassKeep
23 public class TinyFrameworkMethodCallReplace {
24     //  This method should return true.
nonStaticMethodCallReplaceTester()25     public static boolean nonStaticMethodCallReplaceTester() throws Exception {
26         final AtomicBoolean ab = new AtomicBoolean(false);
27 
28         Thread th = new Thread(() -> {
29             ab.set(Thread.currentThread().isDaemon());
30         });
31         // This Thread.start() call will be redirected to ReplaceTo.startThread()
32         // (because of the policy file directive) which will make the thread "daemon" and start it.
33         th.start();
34         th.join();
35 
36         return ab.get(); // This should be true.
37     }
38 
staticMethodCallReplaceTester()39     public static int staticMethodCallReplaceTester() {
40         // This method call will be replaced with ReplaceTo.add().
41         return originalAdd(1, 2);
42     }
43 
originalAdd(int a, int b)44     private static int originalAdd(int a, int b) {
45         return a + b - 1; // Original is broken.
46     }
47 
48     public static class ReplaceTo {
startThread(Thread thread)49         public static void startThread(Thread thread) {
50             thread.setDaemon(true);
51             thread.start();
52         }
53 
add(int a, int b)54         public static int add(int a, int b) {
55             return a + b;
56         }
57     }
58 }
59