1 /*
2  * Copyright (C) 2015 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 import other.InaccessibleClass;
18 import other.InaccessibleClassProxy;
19 
20 public class Main {
main(String[] args)21     public static void main(String[] args) {
22         try {
23             testInstanceOf();
24         } catch (IllegalAccessError e) {
25             System.out.println("Got expected error instanceof");
26         }
27 
28          try {
29             testInstanceOfNull();
30         } catch (IllegalAccessError e) {
31             System.out.println("Got expected error instanceof null");
32         }
33 
34         try {
35             testCheckCastNull();
36         } catch (IllegalAccessError e) {
37             System.out.println("Got expected error checkcast null");
38         }
39 
40         try {
41             $noinline$testCheckCast(new Object());
42         } catch (IllegalAccessError e) {
43             System.out.println("Got expected error checkcast object");
44         }
45 
46         try {
47             testDontGvnLoadClassWithAccessChecks(new Object());
48         } catch (IllegalAccessError e) {
49             System.out.println("Got expected error instanceof (keep LoadClass with access check)");
50         }
51 
52         InaccessibleClassProxy.testGetReferrersClass();
53         InaccessibleClassProxy.testGetReferrersClassViaAnotherClass();
54 
55         // Execute again now that classes have been initialized, and entrypoints may have been
56         // updated.
57         InaccessibleClassProxy.testGetReferrersClass();
58         InaccessibleClassProxy.testGetReferrersClassViaAnotherClass();
59     }
60 
61     /// CHECK-START: boolean Main.testInstanceOf() register (after)
62     /// CHECK: LoadClass class_name:other.InaccessibleClass
testInstanceOf()63     public static boolean testInstanceOf() {
64         return ic instanceof InaccessibleClass;
65     }
66 
67     /// CHECK-START: boolean Main.testInstanceOfNull() register (after)
68     /// CHECK: LoadClass class_name:other.InaccessibleClass
testInstanceOfNull()69     public static boolean testInstanceOfNull() {
70         return null instanceof InaccessibleClass;
71     }
72 
73     // TODO: write a test for for CheckCast with not null constant (after RTP can parse arguments).
74 
75     /// CHECK-START: other.InaccessibleClass Main.testCheckCastNull() register (after)
76     /// CHECK: LoadClass class_name:other.InaccessibleClass
testCheckCastNull()77     public static InaccessibleClass testCheckCastNull() {
78         return (InaccessibleClass) null;
79     }
80 
81     /// CHECK-START: other.InaccessibleClass Main.$noinline$testCheckCast(java.lang.Object) register (after)
82     /// CHECK: LoadClass class_name:other.InaccessibleClass
$noinline$testCheckCast(Object o)83     public static InaccessibleClass $noinline$testCheckCast(Object o) {
84         return (InaccessibleClass) o;
85     }
86 
87     /// CHECK-START: boolean Main.testDontGvnLoadClassWithAccessChecks(java.lang.Object) inliner (before)
88     /// CHECK: InvokeStaticOrDirect
89 
90     /// CHECK-START: boolean Main.testDontGvnLoadClassWithAccessChecks(java.lang.Object) inliner (after)
91     /// CHECK-NOT: InvokeStaticOrDirect
92 
93     /// CHECK-START: boolean Main.testDontGvnLoadClassWithAccessChecks(java.lang.Object) GVN (after)
94     /// CHECK: LoadClass needs_access_check:false
95     /// CHECK: LoadClass needs_access_check:true
testDontGvnLoadClassWithAccessChecks(Object o)96     public static boolean testDontGvnLoadClassWithAccessChecks(Object o) {
97         InaccessibleClassProxy.test(o);
98         return ic instanceof InaccessibleClass;
99     }
100 
101     public static InaccessibleClass ic;
102 }
103