Path: blob/master/test/hotspot/jtreg/runtime/InvocationTests/invokevirtual/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 invokevirtual;2526import java.lang.reflect.Method;27import java.lang.reflect.Modifier;282930public class Checker extends shared.Checker {31public Checker(Class staticTargetClass, Class dynamicTargetClass) {32super(staticTargetClass, dynamicTargetClass);33}3435public String check (Class callerClass) {36Method m;37try {38// May cause java.lang.VerifyError39m = getOverriddenMethod();40} catch (Throwable e) {41return e.getClass().getName();42}4344// Check method accessibility (it's a static property, according to JLS #6.6: Access Control)45if (m != null) {46Method staticTargetMethod = getDeclaredMethod(staticTargetClass);4748if (checkAccess(staticTargetMethod, callerClass)) {49// Can't invoke abstract method50if ( Modifier.isAbstract(m.getModifiers())) {51return "java.lang.AbstractMethodError";52}5354return String.format("%s.%s"55, m.getDeclaringClass().getSimpleName()56, methodName57);58} else {59// if method isn't accessible, IllegalAccessError is thrown60return "java.lang.IllegalAccessError";61}62} else {63// if method == null, NoSuchMethodError is thrown64return "java.lang.NoSuchMethodError";65}66}6768public Method getOverriddenMethod() {69return getOverriddenMethod(staticTargetClass, dynamicTargetClass);70}7172public Method getOverriddenMethod(Class staticTarget, Class dynamicTarget) {73// Assertion #1. C is a subclass of A74if (!staticTarget.isAssignableFrom(dynamicTarget)) {75return null;76}7778Method staticTargetMethod = getDeclaredMethod(staticTarget);79Method dynamicTargetMethod = getDeclaredMethod(dynamicTarget);8081if (staticTarget.equals(dynamicTarget)) {82return staticTargetMethod;83}8485// TODO: ? need to find out the right behavior86if (staticTargetMethod == null) {87return null;88}8990// Dynamic target doesn't have desired method, so check its superclass91if (dynamicTargetMethod == null) {92return getOverriddenMethod(staticTarget, dynamicTarget.getSuperclass());93} else {94// Private method can't override anything95if (Modifier.isPrivate(dynamicTargetMethod.getModifiers())) {96return getOverriddenMethod(staticTarget, dynamicTarget.getSuperclass());97}98}99100// TODO: abstract methods101102//Assertion #3.a: A.m2 is PUB || PROT || (PP && PKG(A) == PKG(C))103int staticTargetModifiers = staticTargetMethod.getModifiers();104{105boolean isPublic = Modifier.isPublic(staticTargetModifiers);106boolean isProtected = Modifier.isProtected(staticTargetModifiers);107boolean isPrivate = Modifier.isPrivate(staticTargetModifiers) ;108String staticTargetPkg = getClassPackageName(staticTarget);109String dynamicTargetPkg = getClassPackageName(dynamicTarget);110111if (isPublic || isProtected112|| (!isPublic && !isProtected && !isPrivate113&& staticTargetPkg.equals(dynamicTargetPkg))) {114return dynamicTargetMethod;115}116}117// OR118//Assertion #3.b: exists m3: C.m1 != B.m3, A.m2 != B.m3, B.m3 overrides A.m2, C.m1 overrides B.m3119Class ancestor = dynamicTarget.getSuperclass();120while (ancestor != staticTarget) {121Method OverriddenM2 = getOverriddenMethod(staticTarget, ancestor);122Method m3 = getDeclaredMethod(ancestor);123Method m1 = getOverriddenMethod(ancestor, dynamicTarget);124125if (m1 != null && m3 != null) {126if (m1.equals(dynamicTargetMethod) && m3.equals(OverriddenM2)) {127return dynamicTargetMethod;128}129} else {130if (m1 == null && dynamicTargetMethod == null131&& m3 == null && OverriddenM2 == null)132{133return null;134}135}136137ancestor = ancestor.getSuperclass();138}139140return getOverriddenMethod(staticTarget, dynamicTarget.getSuperclass());141}142}143144145