1 /*
2  * Copyright (C) 2016 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 DefaultDeclared {
20   interface DefaultInterface {
sayHi()21     default void sayHi() {
22       System.out.println("hi default");
23     }
24   }
25 
26   interface RegularInterface {
sayHi()27     void sayHi();
28   }
29 
30   class ImplementsWithDefault implements DefaultInterface {}
31 
32   class ImplementsWithDeclared implements DefaultInterface {
sayHi()33     public void sayHi() {
34       System.out.println("hello specific from default");
35     }
36   }
37 
38   abstract class UnimplementedWithRegular implements RegularInterface { }
39 
40   class ImplementsWithRegular implements RegularInterface {
sayHi()41     public void sayHi() {
42       System.out.println("hello specific");
43     }
44   }
45 
printGetMethod(Class<?> klass)46   private static void printGetMethod(Class<?> klass) {
47     Method m;
48     try {
49       m = klass.getDeclaredMethod("sayHi");
50       System.out.println("No error thrown for class " + klass.toString());
51     } catch (NoSuchMethodException e) {
52       System.out.println("NoSuchMethodException thrown for class " + klass.toString());
53     } catch (Throwable t) {
54       System.out.println("Unknown error thrown for class " + klass.toString());
55       t.printStackTrace(System.out);
56     }
57   }
58 
test()59   public static void test() {
60     System.out.println("==============================");
61     System.out.println("Are These Methods found by getDeclaredMethod:");
62     System.out.println("==============================");
63 
64     printGetMethod(DefaultInterface.class);
65     printGetMethod(RegularInterface.class);
66     printGetMethod(ImplementsWithDefault.class);
67     printGetMethod(ImplementsWithDeclared.class);
68     printGetMethod(ImplementsWithRegular.class);
69     printGetMethod(UnimplementedWithRegular.class);
70   }
71 }
72