Home | History | Annotate | Download | only in src
      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 {
     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             testDontGvnLoadClassWithAccessChecks(new Object());
     42         } catch (IllegalAccessError e) {
     43             System.out.println("Got expected error instanceof (keep LoadClass with access check)");
     44         }
     45     }
     46 
     47     /// CHECK-START: boolean Main.testInstanceOf() register (after)
     48     /// CHECK: InstanceOf
     49     public static boolean testInstanceOf() {
     50         return ic instanceof InaccessibleClass;
     51     }
     52 
     53     /// CHECK-START: boolean Main.testInstanceOfNull() register (after)
     54     /// CHECK: InstanceOf
     55     public static boolean testInstanceOfNull() {
     56         return null instanceof InaccessibleClass;
     57     }
     58 
     59     // TODO: write a test for for CheckCast with not null constant (after RTP can parse arguments).
     60 
     61     /// CHECK-START: other.InaccessibleClass Main.testCheckCastNull() register (after)
     62     /// CHECK: CheckCast
     63     public static InaccessibleClass testCheckCastNull() {
     64         return (InaccessibleClass) null;
     65     }
     66 
     67     /// CHECK-START: boolean Main.testDontGvnLoadClassWithAccessChecks(java.lang.Object) inliner (before)
     68     /// CHECK: InvokeStaticOrDirect
     69 
     70     /// CHECK-START: boolean Main.testDontGvnLoadClassWithAccessChecks(java.lang.Object) inliner (after)
     71     /// CHECK-NOT: InvokeStaticOrDirect
     72 
     73     /// CHECK-START: boolean Main.testDontGvnLoadClassWithAccessChecks(java.lang.Object) GVN (after)
     74     /// CHECK: LoadClass needs_access_check:false
     75     /// CHECK: LoadClass needs_access_check:true
     76     public static boolean testDontGvnLoadClassWithAccessChecks(Object o) {
     77         InaccessibleClassProxy.test(o);
     78         return ic instanceof InaccessibleClass;
     79     }
     80 
     81     public static InaccessibleClass ic;
     82 }
     83