Path: blob/master/test/hotspot/jtreg/runtime/InvocationTests/invokespecial/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 invokespecial;2526import java.lang.reflect.Method;27import java.lang.reflect.Modifier;2829public class Checker extends shared.Checker {3031public Checker(Class staticTargetClass, Class dynamicTargetClass) {32super(staticTargetClass, dynamicTargetClass);33}3435public String check (Class callerClass) {36// If objectref is null, the invokespecial instruction throws a NullPointerException.37if (dynamicTargetClass == null) {38return "java.lang.NullPointerException";39}4041// TODO: find a citation from spec for this case42Method resolvedMethod;43try {44// May throw VerifyError45resolvedMethod = getMethodInHierarchy(staticTargetClass);46} catch (Throwable e) {47return e.getClass().getName();48}4950if (resolvedMethod == null) {51return "java.lang.NoSuchMethodError";52}5354// If:55// - the resolved method is protected (4.7)56// - it is a member of a superclass of the current class57// - the method is not declared in the same run-time package (5.3) as the current class58// then:59// the class of objectref must be either the current class or a subclass of the60// current class.6162if (Modifier.isProtected(resolvedMethod.getModifiers())) {63Method methodInSuperclass = getMethodInHierarchy(resolvedMethod.getDeclaringClass().getSuperclass());6465if (methodInSuperclass != null) {66String resolvedMethodPkg = getClassPackageName(resolvedMethod.getDeclaringClass());67String methodInSuperclassPkg = getClassPackageName(methodInSuperclass.getDeclaringClass());6869if (!resolvedMethodPkg.equals(methodInSuperclassPkg)) {70//TODO: clarify this71// if (callerClass == methodInSuperclass.getDeclaringClass()) {72// return "java.lang.IllegalAccessError";73// }74}75}76}7778/*79* The resolved method is selected for invocation unless all of80* the following conditions are true:81* * TODO: The ACC_SUPER flag (see Table 4.1, "Class access and property82* modifiers") is set for the current class.83* * The class of the resolved method is a superclass of the84* current class - assumed by construction procedure85*86* * The resolved method is not an instance initialization method (3.9).87*/88if (!"<init>".equals(methodName)) {89/*90* Let C be the direct superclass of the current class:91* * If C contains a declaration for an instance method with the same92* name and descriptor as the resolved method, then this method will be93* invoked. The lookup procedure terminates.94* * Otherwise, if C has a superclass, this same lookup procedure is95* performed recursively using the direct superclass of C. The method to96* be invoked is the result of the recursive invocation of this lookup97* procedure.98* * Otherwise, an AbstractMethodError is raised.99* TODO: so far, sometimes NSME is thrown100*/101Class klass = dynamicTargetClass.getSuperclass();102103while (klass != Object.class) {104Method method = getDeclaredMethod(klass);105106if (method != null) {107/*108* If the resolved method is a class (static) method, the109* invokespecial instruction throws an IncompatibleClassChangeError.110*/111if (Modifier.isStatic(method.getModifiers())) {112return "java.lang.IncompatibleClassChangeError";113}114115// Check access rights116if ( checkAccess(method, callerClass)117// && !(118// Modifier.isProtected(method.getModifiers())119// && (120// staticTargetClass.isAssignableFrom(callerClass)121// || getClassPackageName(staticTargetClass).equals(getClassPackageName(callerClass))122// )123//124// )125)126{127return String.format("%s.%s"128, method.getDeclaringClass().getSimpleName()129, methodName130);131} else {132// IAE is thrown when located method can't be accessed from the call site133return "java.lang.IllegalAccessError";134}135}136137klass = klass.getSuperclass();138}139140return "java.lang.AbstractMethodError";141} else {142// The resolved method is an instance initialization method (3.9).143}144145// TODO: change146return "---";147}148}149150151