Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2014 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 java.lang.reflect.Method;
     18 
     19 public class Main {
     20   static class A {
     21     public int foo() { return 1; }
     22   }
     23 
     24   static class B extends A {
     25     public int $opt$bar() { return super.foo(); }
     26   }
     27 
     28   static class C extends B {
     29     public int foo() { return 42; }
     30   }
     31 
     32   static class D extends C {
     33   }
     34 
     35   static void assertEquals(int expected, int value) {
     36     if (expected != value) {
     37       throw new Error("Expected " + expected + ", got " + value);
     38     }
     39   }
     40 
     41   public static void main(String[] args) throws Exception {
     42     // Workaround for b/18051191.
     43     System.out.println("Test started");
     44     assertEquals(1, new B().$opt$bar());
     45     assertEquals(1, new C().$opt$bar());
     46     assertEquals(1, new D().$opt$bar());
     47 
     48     Class<?> c = Class.forName("InvokeSuper");
     49     Method m = c.getMethod("run");
     50     assertEquals(42, ((Integer)m.invoke(c.newInstance(), new Object[0])).intValue());
     51 
     52     c = Class.forName("SubClass");
     53     assertEquals(42, ((Integer)m.invoke(c.newInstance(), new Object[0])).intValue());
     54   }
     55 }
     56