Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/reflect/Method/IsDefaultTest.java
38828 views
/*1* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 800504226* @summary Check behavior of Method.isDefault27* @author Joseph D. Darcy28*/2930import java.lang.reflect.*;31import java.lang.annotation.*;32import java.util.*;3334public class IsDefaultTest {35public static void main(String... argv) throws Exception {36int failures = 0;37int visitationCount = 0;3839List<Class<?>> classList = new ArrayList<>();40classList.add(TestType1.class);41classList.add(TestType2.class);42classList.add(TestType3.class);43classList.add(TestType4.class);44classList.add(TestType2.nestedTestType2.class);45classList.add(TestType5.class);46classList.add(TestType5.nestedTestType5.class);47classList.add(TestType6.class);48classList.add(TestType6.nestedTestType6.class);49classList.add(TestType7.class);5051for(Class<?> clazz: classList) {52for(Method method: clazz.getDeclaredMethods()) {53ExpectedIsDefault expectedIsDefault = method.getAnnotation(ExpectedIsDefault.class);54if (expectedIsDefault != null) {55visitationCount++;56boolean expected = expectedIsDefault.value();57boolean actual = method.isDefault();5859if (actual != expected) {60failures++;61System.err.printf("ERROR: On %s expected isDefault of ''%s''; got ''%s''.\n",62method.toString(), expected, actual);63}64}65}66}6768if (visitationCount == 0) {69System.err.println("Test failed because no methods checked.");70throw new RuntimeException();71}7273if (failures > 0) {74System.err.println("Test failed.");75throw new RuntimeException();76}77}78}7980interface TestType1 {81@ExpectedIsDefault(false)82void foo();8384@ExpectedIsDefault(true)85default void bar() {}; // Default method8687@ExpectedIsDefault(true)88default void bar(int i) {}; // Default method8990@ExpectedIsDefault(true)91default void bar(String i) {}; // Default method92}9394class TestType2 {95@ExpectedIsDefault(false)96void bar() {};9798interface nestedTestType2 {99@ExpectedIsDefault(true)100default void nestedBar() {};101}102}103104class TestType3 implements TestType1 {105@ExpectedIsDefault(false)106public void foo(){}107108@ExpectedIsDefault(false)109@Override110public void bar() {};111112@ExpectedIsDefault(false)113@Override114public void bar(int i) {};115}116117@interface TestType4 {118@ExpectedIsDefault(false)119String value();120121@ExpectedIsDefault(false)122String anotherValue() default "";123}124125interface TestType5 {126@ExpectedIsDefault(false)127abstract void aFoo();128129@ExpectedIsDefault(false)130static void sFoo() {};131132@ExpectedIsDefault(true)133public default void pBar() {};134135@ExpectedIsDefault(true)136public default String sBar() {return "";};137138interface nestedTestType5{139@ExpectedIsDefault(false)140void nestedFoo();141142@ExpectedIsDefault(true)143default void nestedBar() {};144}145}146147class TestType6{148interface nestedTestType6 {149@ExpectedIsDefault(true)150default void nestedBar() {};151152@ExpectedIsDefault(false)153void nestedFoo();154}155156@ExpectedIsDefault(false)157void foo(nestedTestType6 n) {}158}159160class TestType7 implements TestType6.nestedTestType6 {161162@ExpectedIsDefault(false)163public void nestedFoo() {}164165@ExpectedIsDefault(false)166@Override167public void nestedBar() {};168}169170@Retention(RetentionPolicy.RUNTIME)171@interface ExpectedIsDefault {172boolean value();173}174175176