Path: blob/master/test/hotspot/jtreg/runtime/InvocationTests/shared/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 shared;2526import java.lang.reflect.Method;27import java.lang.reflect.Modifier;2829public abstract class Checker {30protected Class staticTargetClass;31protected Class dynamicTargetClass;32protected String methodName;3334public abstract String check (Class callSite);3536public Checker(Class staticTargetClass, Class dynamicTargetClass) {37if (!staticTargetClass.isAssignableFrom(dynamicTargetClass)) {38throw new RuntimeException("Dynamic target class should be a subclass of the static target class.");39}4041// **********************************************42// NB!!! All classes are assumed to be PUBLIC !!!43// **********************************************44Class klass = dynamicTargetClass;45while (klass != Object.class) {46if (!Modifier.isPublic(klass.getModifiers())) {47throw new AssertionError("Class "+klass.getName()+" isn't public.");48}4950klass = klass.getSuperclass();51}5253this.methodName = Utils.TARGET_METHOD_NAME;54this.staticTargetClass = staticTargetClass;55this.dynamicTargetClass = dynamicTargetClass;56}5758protected Method getMethodInHierarchy (Class klass) {59return getMethodInHierarchy(klass, methodName);60}6162protected Method getMethodInHierarchy (Class klass, String name) {63while (klass != null) {64Method method = getDeclaredMethod (klass, name);6566if ( method != null) {67// TODO: why doesn't this check work in VM?68// int modifiers = method.getModifiers();69//70// if (Modifier.isPrivate(modifiers)) {71// if (klass == initialClass) {72// return method;73// }74// } else {75// return method;76// }77return method;78}79klass = klass.getSuperclass();80}8182return null;83}8485protected Method getMethod (Class klass) {86return getMethod (klass, methodName);87}8889protected Method getDeclaredMethod (Class klass) {90return getDeclaredMethod (klass, methodName);91}9293static protected Method getMethod (Class klass, String name) {94return findMethod (klass.getMethods(), name);95}9697static protected Method getDeclaredMethod (Class klass, String name) {98return findMethod (klass.getDeclaredMethods(), name);99}100101static protected Method findMethod (Method[] methods, String name) {102for (Method method : methods) {103if (name.equals(method.getName())) {104return method;105}106}107108return null;109}110111static public String getClassPackageName(Class klass) {112String name = klass.getName();113return getClassPackageName(name);114}115116static public String getClassPackageName(String name) {117int lastDotIndex = name.lastIndexOf('.');118if (lastDotIndex > -1) {119return name.substring(0, lastDotIndex);120} else {121return "";122}123}124125public static String abbreviateResult(String result) {126// Abbreviate exception names127result = result.replaceAll("java.lang.NullPointerException", "NPE");128result = result.replaceAll("java.lang.IllegalAccessError", "IAE");129result = result.replaceAll("java.lang.IllegalAccessException", "IAExc");130result = result.replaceAll("java.lang.NoSuchMethodError", "NSME");131result = result.replaceAll("java.lang.AbstractMethodError", "AME");132result = result.replaceAll("java.lang.IncompatibleClassChangeError", "ICCE");133result = result.replaceAll("java.lang.VerifyError", "VE");134result = result.replaceAll("java.lang.ClassFormatError", "CFE");135136return result;137}138139// Check access possibility from particular call site140protected boolean checkAccess(Class klass, Class callerClass) {141int modifiers = klass.getModifiers();142143return checkAccess(modifiers, klass, callerClass);144}145146protected boolean checkAccess(Method m, Class callerClass) {147int modifiers = m.getModifiers();148Class declaringClass = m.getDeclaringClass();149150return checkAccess(modifiers, declaringClass, callerClass);151}152153protected boolean checkAccess(int modifiers, Class klass, Class callerClass) {154if ( Modifier.isPublic(modifiers) ) {155return true;156} else if ( Modifier.isProtected(modifiers) ) {157if (klass.isAssignableFrom(callerClass)) {158return true;159} else if (getClassPackageName(klass).equals(getClassPackageName(callerClass))) {160return true;161}162} else if ( Modifier.isPrivate(modifiers)) {163if (klass == callerClass) {164return true;165}166} else if (getClassPackageName(klass).equals(getClassPackageName(callerClass))) {167return true;168} else {169// if method isn't accessible, IllegalAccessException is thrown170return false;171}172173return false;174}175}176177178