1 /*
2  * Copyright (C) 2023 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.HostSideTestClassLoadHook;
19 import android.hosttest.annotation.HostSideTestKeep;
20 import android.hosttest.annotation.HostSideTestRemove;
21 import android.hosttest.annotation.HostSideTestStub;
22 import android.hosttest.annotation.HostSideTestSubstitute;
23 import android.hosttest.annotation.HostSideTestThrow;
24 
25 /**
26  * Test without class-wide annotations.
27  */
28 @HostSideTestStub
29 @HostSideTestClassLoadHook(
30         "com.android.hoststubgen.test.tinyframework.TinyFrameworkClassLoadHook.onClassLoaded")
31 public class TinyFrameworkClassAnnotations {
32     @HostSideTestStub
TinyFrameworkClassAnnotations()33     public TinyFrameworkClassAnnotations() {
34     }
35 
36     @HostSideTestStub
37     public int stub = 1;
38 
39     @HostSideTestKeep
40     public int keep = 2;
41 
42     // Members will be deleted by default.
43     // Deleted fields cannot have an initial value, because otherwise .ctor will fail to set it at
44     // runtime.
45     public int remove;
46 
47     @HostSideTestStub
addOne(int value)48     public int addOne(int value) {
49         return addOneInner(value);
50     }
51 
52     @HostSideTestKeep
addOneInner(int value)53     public int addOneInner(int value) {
54         return value + 1;
55     }
56 
57     @HostSideTestRemove // Explicitly remove
toBeRemoved(String foo)58     public void toBeRemoved(String foo) {
59         throw new RuntimeException();
60     }
61 
62     @HostSideTestStub
63     @HostSideTestSubstitute(suffix = "_host")
addTwo(int value)64     public int addTwo(int value) {
65         throw new RuntimeException("not supported on host side");
66     }
67 
addTwo_host(int value)68     public int addTwo_host(int value) {
69         return value + 2;
70     }
71 
72     @HostSideTestStub
73     @HostSideTestSubstitute(suffix = "_host")
nativeAddThree(int value)74     public static native int nativeAddThree(int value);
75 
76     // This method is private, but at runtime, it'll inherit the visibility of the original method
nativeAddThree_host(int value)77     private static int nativeAddThree_host(int value) {
78         return value + 3;
79     }
80 
81     @HostSideTestThrow
unsupportedMethod()82     public String unsupportedMethod() {
83         return "This value shouldn't be seen on the host side.";
84     }
85 
86     @HostSideTestStub
visibleButUsesUnsupportedMethod()87     public String visibleButUsesUnsupportedMethod() {
88         return unsupportedMethod();
89     }
90 }
91