Path: blob/master/jcl/src/java.base/share/classes/java/lang/VMAccess.java
12513 views
/*[INCLUDE-IF Sidecar16]*/1/*******************************************************************************2* Copyright (c) 2012, 2021 IBM Corp. and others3*4* This program and the accompanying materials are made available under5* the terms of the Eclipse Public License 2.0 which accompanies this6* distribution and is available at https://www.eclipse.org/legal/epl-2.0/7* or the Apache License, Version 2.0 which accompanies this distribution and8* is available at https://www.apache.org/licenses/LICENSE-2.0.9*10* This Source Code may also be made available under the following11* Secondary Licenses when the conditions for such availability set12* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU13* General Public License, version 2 with the GNU Classpath14* Exception [1] and GNU General Public License, version 2 with the15* OpenJDK Assembly Exception [2].16*17* [1] https://www.gnu.org/software/classpath/license.html18* [2] http://openjdk.java.net/legal/assembly-exception.html19*20* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception21*******************************************************************************/2223package java.lang;2425import java.util.Objects;26import java.util.Properties;2728/*[IF Sidecar19-SE]*/29import jdk.internal.reflect.ConstantPool;30/*[ELSE]*/31import sun.reflect.ConstantPool;32/*[ENDIF]*/333435import com.ibm.oti.vm.*;36import com.ibm.jit.JITHelpers;3738/**39* Helper class to allow privileged access to classes40* from outside the java.lang package. Based on sun.misc.SharedSecrets41* implementation.42*/43final class VMAccess implements VMLangAccess {4445private static ClassLoader extClassLoader;4647/**48* Native used to find and load a class using the VM49*50* @return java.lang.Class51* the class or null.52* @param className String53* the name of the class to search for.54* @param classLoader55* the classloader to do the work56*/57static native Class<?> findClassOrNull(String className, ClassLoader classLoader);5859@Override60public Class<?> findClassOrNullHelper(String className, ClassLoader classLoader) {61return VMAccess.findClassOrNull(className, classLoader);62}636465/*[IF Sidecar19-SE]*/66/**67* Answer the platform class loader.68*/69@Override70public ClassLoader getPlatformClassLoader() {71return jdk.internal.loader.ClassLoaders.platformClassLoader();72}73/*[ENDIF] Sidecar19-SE*/7475/**76* Set the extension class loader. It can only be set once.77*78* @param loader the extension class loader79*/80static void setExtClassLoader(ClassLoader loader) {81extClassLoader = loader;82}8384/**85* Answer the extension class loader.86*/87@Override88public ClassLoader getExtClassLoader() {89return extClassLoader;90}9192/**93* Returns true if parent is the ancestor of child.94* Parent and child must not be null.95*/96/*[PR CMVC 191554] Provide access to ClassLoader methods to improve performance */97@Override98public boolean isAncestor(java.lang.ClassLoader parent, java.lang.ClassLoader child) {99return parent.isAncestorOf(child);100}101102/**103* Returns the ClassLoader off clazz.104*/105/*[PR CMVC 191554] Provide access to ClassLoader methods to improve performance */106@Override107public java.lang.ClassLoader getClassloader(java.lang.Class clazz) {108return clazz.getClassLoaderImpl();109}110111/**112* Returns the package name for a given class.113* clazz must not be null.114*/115/*[PR CMVC 191554] Provide access to ClassLoader methods to improve performance */116@Override117public java.lang.String getPackageName(java.lang.Class clazz) {118return clazz.getPackageName();119}120121/**122* Returns a MethodHandle cache for a given class.123*/124@Override125public java.lang.Object getMethodHandleCache(java.lang.Class<?> clazz) {126return clazz.getMethodHandleCache();127}128129/**130* Set a MethodHandle cache to a given class.131*/132@Override133public java.lang.Object setMethodHandleCache(java.lang.Class<?> clazz, java.lang.Object object) {134return clazz.setMethodHandleCache(object);135}136137/**138* Returns a {@code java.util.Map} from method descriptor string to the equivalent {@code MethodType} as generated by {@code MethodType.fromMethodDescriptorString}.139* @param loader The {@code ClassLoader} used to get the MethodType.140* @return A {@code java.util.Map} from method descriptor string to the equivalent {@code MethodType}.141*/142@Override143public java.util.Map<String, java.lang.invoke.MethodType> getMethodTypeCache(ClassLoader loader) {144return loader != null ? loader.getMethodTypeCache() : null;145}146147/**148* Provide internal access to the system properties without going through SecurityManager149*150* Important notes:151* 1. This API must NOT be exposed to application code directly or indirectly;152* 2. This method can only be used to retrieve system properties for internal usage,153* i.e., there is no security exception expected;154* 3. If there is an application caller in the call stack, AND the application caller(s)155* have to be check for permission to retrieve the system properties specified,156* then this API should NOT be used even though the immediate caller is in boot strap path.157*158* @return the system properties159*/160@Override161public Properties internalGetProperties() {162return System.internalGetProperties();163}164165166/*[IF !Sidecar19-SE]*/167/**168* Returns the system packages for the bootloader169* @return An array of packages defined by the bootloader170*/171@Override172public Package[] getSystemPackages() {173return Package.getSystemPackages();174}175176/**177* Returns the system package for the 'name'178* @param name must not be null179* @return The package180*/181@Override182public Package getSystemPackage(String name) {183return Package.getSystemPackage(name);184}185/*[ENDIF]*/186187/**188* Returns an InternalConstantPool object.189*190* @param addr - the native addr of the J9ConstantPool191* @return An InternalConstantPool reference object192*/193@Override194public Object createInternalConstantPool(long addr) {195return new InternalConstantPool(addr);196}197198/**199* Returns a ConstantPool object200* @param internalConstantPool An object ref to a j9constantpool201* @return ConstantPool instance202*/203@Override204public ConstantPool getConstantPool(Object internalConstantPool) {205return Access.getConstantPool(internalConstantPool);206}207208/**209* Returns an InternalConstantPool object from a J9Class address. The ConstantPool210* natives expect an InternalConstantPool as the constantPoolOop parameter.211*212* @param j9class the native address of the J9Class213* @return InternalConstantPool a wrapper for a j9constantpool214*/215public Object getInternalConstantPoolFromJ9Class(long j9class) {216long j9constantpool = VM.getJ9ConstantPoolFromJ9Class(j9class);217return createInternalConstantPool(j9constantpool);218}219220/**221* Returns an InternalConstantPool object from a Class. The ConstantPool222* natives expect an InternalConstantPool as the constantPoolOop parameter.223*224* @param clazz the Class to fetch the constant pool from225* @return an InternalConstantPool wrapper for a j9constantpool226*/227public Object getInternalConstantPoolFromClass(Class clazz) {228JITHelpers helpers = JITHelpers.getHelpers();229long j9class;230if (helpers.is32Bit()) {231j9class = helpers.getJ9ClassFromClass32(clazz);232} else {233j9class = helpers.getJ9ClassFromClass64(clazz);234}235return getInternalConstantPoolFromJ9Class(j9class);236}237238/*[IF Sidecar19-SE]*/239@Override240public void addPackageToList(java.lang.Class<?> newClass, ClassLoader loader) {241java.lang.ClassLoader packageLoader = loader;242if (Objects.isNull(packageLoader)) {243packageLoader = ClassLoader.getSystemClassLoader();244}245packageLoader.addPackageToList(newClass);246}247/*[ENDIF] Sidecar19-SE */248249@Override250public Thread createThread(Runnable runnable, String threadName, boolean isSystemThreadGroup, boolean inheritThreadLocals, boolean isDaemon, ClassLoader contextClassLoader) {251return new Thread(runnable, threadName, isSystemThreadGroup, inheritThreadLocals, isDaemon, contextClassLoader);252}253254@Override255public void prepare(Class<?> theClass) {256J9VMInternals.prepare(theClass);257}258259/*[IF JAVA_SPEC_VERSION >= 11]*/260/**261* Returns whether the classloader name should be included in the stack trace for the provided StackTraceElement.262*263* @param element The StackTraceElement to check264* @return true if the classloader name should be included, false otherwise265*/266@Override267public boolean getIncludeClassLoaderName(StackTraceElement element) {268return element.getIncludeClassLoaderName();269}270271/**272* Returns whether the module version should be included in the stack trace for the provided StackTraceElement.273*274* @param element The StackTraceElement to check275* @return true if the module version should be included, false otherwise276*/277@Override278public boolean getIncludeModuleVersion(StackTraceElement element) {279return element.getIncludeModuleVersion();280}281/*[ENDIF] JAVA_SPEC_VERSION >= 11*/282}283284285