Path: blob/master/test/hotspot/jtreg/runtime/InvocationTests/invokeinterface/Checker.java
40948 views
/*1* Copyright (c) 2009, 2019, 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*22*/2324package invokeinterface;2526import java.lang.reflect.Method;27import java.lang.reflect.Modifier;2829public class Checker extends shared.Checker {30private Class interfaceClass;3132public Checker(Class interfaceClass, Class dynamicTargetClass) {33super(interfaceClass, dynamicTargetClass);3435if (staticTargetClass.isInterface()) {36this.interfaceClass = staticTargetClass;37} else {38throw new RuntimeException("Static target class should be an interface.");39}40}4142public String check (Class callerClass) {43// Check access rights to interface for caller44if (!checkAccess(interfaceClass, callerClass)) {45return "java.lang.IllegalAccessError";46}4748// NSME is thrown when interface doesn't declare the method49if (getDeclaredMethod(interfaceClass) == null) {50return "java.lang.NoSuchMethodError";51}5253// 9.1.5 Access to Interface Member Names54// "All interface members are implicitly public. They are55// accessible outside the package where the interface is56// declared if the interface is also declared public or57// protected, in accordance with the rules of 6.6."5859// Search for method declaration in the hierarchy60Class klass = dynamicTargetClass;6162while (klass != Object.class) {63Method method = getDeclaredMethod(klass);6465if (method != null) {66int modifiers = method.getModifiers();6768// Check whether obtained method is public and isn't abstract69if ( Modifier.isPublic(modifiers)) {70if (Modifier.isAbstract(modifiers)) {71return "java.lang.AbstractMethodError";72} else {73return String.format("%s.%s",74method.getDeclaringClass().getSimpleName(),75methodName);76}77} else {78// IAE is thrown when located method isn't PUBLIC79// or private. Private methods are skipped when80// looking for an interface method.81if (!Modifier.isPrivate(modifiers)) {82return "java.lang.IllegalAccessError";83}84}85}8687klass = klass.getSuperclass();88}8990// No method declaration is found91return "java.lang.AbstractMethodError";92}93}949596