Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/reflect/Method/defaultMethodModeling/DefaultMethodModeling.java
38889 views
/*1* Copyright (c) 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 801159026* @summary Check modeling of default methods27* @author Joseph D. Darcy28*/2930import java.util.Objects;31import java.lang.reflect.*;32import java.lang.annotation.*;3334import static java.lang.reflect.Modifier.*;3536public class DefaultMethodModeling {37public static void main(String... args) {38int failures = 0;3940Class<?>[] classes = {SuperC.class, SuperCchild.class,41SuperI.class, SuperIchild.class,42SuperIwithDefault.class, SuperIwithDefaultChild.class,43Base.class, Combo1.class, Combo2.class,44SonSuperIwithDefault.class, DaughterSuperIwithDefault.class, GrandchildSuperIwithDefault.class, D.class,45B.class, C.class, B1.class, D1.class46};4748for(Class<?> clazz : classes) {49System.err.println(clazz.toString());50for(Method m : clazz.getMethods()) {51if (m.getDeclaringClass() != java.lang.Object.class)52failures += testMethod(m);53}54}5556if (failures > 0)57throw new RuntimeException();58}5960private static int testMethod(Method m) {61ExpectedModel em = Objects.requireNonNull(m.getAnnotation(ExpectedModel.class));62boolean failed = false;6364if (m.getModifiers() != em.modifiers()) {65failed = true;66System.err.printf("Unexpected modifiers %d; expected %d%n", m.getModifiers(), em.modifiers());67}6869if (m.isDefault() != em.isDefault()) {70failed = true;71System.err.printf("Unexpected isDefualt %b; expected b%n", m.isDefault(), em.isDefault());72}7374if (!m.getDeclaringClass().equals(em.declaringClass())) {75failed = true;76System.err.printf("Unexpected isDefualt %s; expected %s%n",77m.getDeclaringClass().toString(), em.declaringClass().toString());78}7980return (!failed) ? 0 :1;81}82}8384@Retention(RetentionPolicy.RUNTIME)85@interface ExpectedModel {86boolean isDefault() default false;87int modifiers() default PUBLIC;88Class<?> declaringClass();89}9091abstract class SuperC {92@ExpectedModel(modifiers=PUBLIC|ABSTRACT, declaringClass=SuperC.class)93public abstract void foo();9495@ExpectedModel(declaringClass=SuperC.class)96public void bar() {97;98}99}100101class SuperCchild extends SuperC {102@ExpectedModel(declaringClass=SuperCchild.class)103@Override104public void foo() {;}105}106107// -=-=-=-108109interface SuperI {110@ExpectedModel(modifiers=PUBLIC|ABSTRACT, declaringClass=SuperI.class)111void foo();112113@ExpectedModel(modifiers=PUBLIC|ABSTRACT, declaringClass=SuperI.class)114void bar();115}116117class SuperIchild implements SuperI {118@ExpectedModel(declaringClass=SuperIchild.class)119public void foo() {;}120121@ExpectedModel(declaringClass=SuperIchild.class)122public void bar() {;}123}124125// -=-=-=-126127interface SuperIwithDefault {128@ExpectedModel(modifiers=PUBLIC|ABSTRACT, declaringClass=SuperIwithDefault.class)129void foo();130131@ExpectedModel(isDefault=true, declaringClass=SuperIwithDefault.class)132default void bar() {133;134}135}136137class SuperIwithDefaultChild implements SuperIwithDefault {138@ExpectedModel(declaringClass=SuperIwithDefaultChild.class)139@Override140public void foo() {;}141}142143// -=-=-=-144145abstract class Base {146@ExpectedModel(modifiers=PUBLIC|ABSTRACT, declaringClass=Base.class)147abstract public void baz();148149@ExpectedModel(declaringClass=Base.class)150public void quux() {;}151}152153abstract class Combo1 extends Base implements SuperI {154@ExpectedModel(declaringClass=Combo1.class)155public void wombat() {}156}157158abstract class Combo2 extends Base implements SuperIwithDefault {159@ExpectedModel(declaringClass=Combo2.class)160public void wombat() {}161}162163// -=-=-=-164165interface SonSuperIwithDefault extends SuperIwithDefault {166@ExpectedModel(modifiers=PUBLIC|ABSTRACT, declaringClass=SonSuperIwithDefault.class)167void baz();168169@ExpectedModel(isDefault=true, declaringClass=SonSuperIwithDefault.class)170default void bazD() {;}171}172173interface DaughterSuperIwithDefault extends SuperIwithDefault {174@ExpectedModel(modifiers=PUBLIC|ABSTRACT, declaringClass=DaughterSuperIwithDefault.class)175void quux();176177@ExpectedModel(isDefault=true, declaringClass=DaughterSuperIwithDefault.class)178default void quuxD() {;}179}180181interface GrandchildSuperIwithDefault extends SonSuperIwithDefault, DaughterSuperIwithDefault {182@ExpectedModel(modifiers=PUBLIC|ABSTRACT, declaringClass=GrandchildSuperIwithDefault.class)183void wombat();184185@ExpectedModel(isDefault=true, declaringClass=GrandchildSuperIwithDefault.class)186default void wombatD() {;}187188}189190class D implements GrandchildSuperIwithDefault {191@ExpectedModel(declaringClass=D.class)192public void wombat(){}193194@ExpectedModel(declaringClass=D.class)195public void baz(){}196197@ExpectedModel(declaringClass=D.class)198public void foo(){}199200@ExpectedModel(declaringClass=D.class)201public void quux(){}202}203204class D1 implements SonSuperIwithDefault, DaughterSuperIwithDefault {205@ExpectedModel(declaringClass=D1.class)206public void foo(){}207208@ExpectedModel(declaringClass=D1.class)209public void baz(){}210211@ExpectedModel(declaringClass=D1.class)212public void quux(){}213}214215// -=-=-=-216217// What does re-abstraction look like?218219class A implements SuperIwithDefault {220@ExpectedModel(declaringClass=A.class)221@Override222public void foo(){;}223}224225abstract class B extends A {226@ExpectedModel(modifiers=PUBLIC|ABSTRACT, declaringClass=B.class)227@Override228public abstract void bar();229}230231class C extends B implements SuperIwithDefault {232@ExpectedModel(declaringClass=C.class)233public void bar(){}234}235236abstract class A1 implements SonSuperIwithDefault {237@ExpectedModel(modifiers=PUBLIC|ABSTRACT, declaringClass=A1.class)238public abstract void baz();239240@ExpectedModel(modifiers=PUBLIC|ABSTRACT, declaringClass=A1.class)241public abstract void foo();242}243244class B1 extends A1 {245@ExpectedModel(declaringClass=B1.class)246@Override247public void foo(){;}248249@ExpectedModel(declaringClass=B1.class)250@Override251public void baz(){}252}253254255