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 IsDefaultTest { 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 class ImplementsWithRegular implements RegularInterface { sayHi()32 public void sayHi() { 33 System.out.println("hello specific"); 34 } 35 } 36 printIsDefault(Class<?> klass)37 private static void printIsDefault(Class<?> klass) { 38 Method m; 39 try { 40 m = klass.getMethod("sayHi"); 41 } catch (Throwable t) { 42 System.out.println(t); 43 return; 44 } 45 46 boolean isDefault = m.isDefault(); 47 System.out.println(klass.getName() + " is default = " + (isDefault ? "yes" : "no")); 48 } 49 test()50 public static void test() { 51 System.out.println("=============================="); 52 System.out.println("Are These Methods Default:"); 53 System.out.println("=============================="); 54 55 printIsDefault(DefaultInterface.class); 56 printIsDefault(RegularInterface.class); 57 printIsDefault(ImplementsWithDefault.class); 58 printIsDefault(ImplementsWithRegular.class); 59 } 60 } 61