Path: blob/master/jcl/src/java.base/share/classes/java/lang/Access.java
12513 views
/*[INCLUDE-IF JAVA_SPEC_VERSION >= 8]*/1/*******************************************************************************2* Copyright (c) 2007, 2022 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*******************************************************************************/22package java.lang;2324import java.lang.annotation.Annotation;25/*[IF JAVA_SPEC_VERSION >= 15]*/26import java.lang.invoke.MethodHandle;27import java.lang.invoke.MethodType;28import jdk.internal.misc.Unsafe;29import java.lang.StringConcatHelper;30/*[ENDIF] JAVA_SPEC_VERSION >= 15 */31import java.lang.reflect.Constructor;32import java.lang.reflect.Executable;33import java.lang.reflect.Method;34import java.security.AccessControlContext;35import java.util.Map;3637import com.ibm.oti.reflect.AnnotationParser;38import com.ibm.oti.reflect.TypeAnnotationParser;3940/*[IF Sidecar19-SE]*/41import java.io.IOException;42import java.lang.module.ModuleDescriptor;43import java.net.URL;44import java.net.URI;45import java.security.ProtectionDomain;46import java.util.Iterator;47import java.util.List;48/*[IF JAVA_SPEC_VERSION >= 15]*/49import java.util.Set;50/*[ENDIF] JAVA_SPEC_VERSION >= 15 */51import java.util.concurrent.ConcurrentHashMap;52import java.util.stream.Stream;53/*[IF JAVA_SPEC_VERSION >= 11]*/54import java.nio.charset.Charset;55import java.nio.charset.CharacterCodingException;56/*[ENDIF] JAVA_SPEC_VERSION >= 11 */57/*[IF JAVA_SPEC_VERSION >= 12]*/58import jdk.internal.access.JavaLangAccess;59/*[ELSE] JAVA_SPEC_VERSION >= 12 */60import jdk.internal.misc.JavaLangAccess;61/*[ENDIF] JAVA_SPEC_VERSION >= 12 */62import jdk.internal.module.ServicesCatalog;63import jdk.internal.reflect.ConstantPool;64/*[ELSE] Sidecar19-SE */65import sun.misc.JavaLangAccess;66import sun.reflect.ConstantPool;67/*[ENDIF] Sidecar19-SE */68import sun.nio.ch.Interruptible;69import sun.reflect.annotation.AnnotationType;7071/**72* Helper class to allow privileged access to classes73* from outside the java.lang package. The sun.misc.SharedSecrets class74* uses an instance of this class to access private java.lang members.75*/76final class Access implements JavaLangAccess {7778/** Set thread's blocker field. */79public void blockedOn(java.lang.Thread thread, Interruptible interruptable) {80/*[IF JAVA_SPEC_VERSION >= 11]*/81Thread.blockedOn(interruptable);82/*[ELSE] JAVA_SPEC_VERSION >= 11 */83thread.blockedOn(interruptable);84/*[ENDIF] JAVA_SPEC_VERSION >= 11 */85}8687/**88* Get the AnnotationType instance corresponding to this class.89* (This method only applies to annotation types.)90*/91public AnnotationType getAnnotationType(java.lang.Class<?> arg0) {92return arg0.getAnnotationType();93}9495/** Return the constant pool for a class. */96public native ConstantPool getConstantPool(java.lang.Class<?> arg0);9798/**99* Return the constant pool for a class. This will call the exact same100* native as 'getConstantPool(java.lang.Class<?> arg0)'. We need this101* version so we can call it with InternRamClass instead of j.l.Class102* (which is required by JAVALANGACCESS).103*/104public static native ConstantPool getConstantPool(Object arg0);105106/**107* Returns the elements of an enum class or null if the108* Class object does not represent an enum type;109* the result is uncloned, cached, and shared by all callers.110*/111public <E extends Enum<E>> E[] getEnumConstantsShared(java.lang.Class<E> arg0) {112/*[PR CMVC 189091] Perf: EnumSet.allOf() is slow */113return arg0.getEnumConstantsShared();114}115116public void registerShutdownHook(int arg0, boolean arg1, Runnable arg2) {117Shutdown.add(arg0, arg1, arg2);118}119120/**121* Set the AnnotationType instance corresponding to this class.122* (This method only applies to annotation types.)123*/124public void setAnnotationType(java.lang.Class<?> arg0, AnnotationType arg1) {125arg0.setAnnotationType(arg1);126}127128/**129* Compare-And-Swap the AnnotationType instance corresponding to this class.130* (This method only applies to annotation types.)131*/132public boolean casAnnotationType(final Class<?> clazz, AnnotationType oldType, AnnotationType newType) {133return clazz.casAnnotationType(oldType, newType);134}135136/**137* @param clazz Class object138* @return the array of bytes for the Annotations for clazz, or null if clazz is null139*/140public byte[] getRawClassAnnotations(Class<?> clazz) {141return AnnotationParser.getAnnotationsData(clazz);142}143144public int getStackTraceDepth(java.lang.Throwable arg0) {145return arg0.getInternalStackTrace().length;146}147148public java.lang.StackTraceElement getStackTraceElement(java.lang.Throwable arg0, int arg1) {149return arg0.getInternalStackTrace()[arg1];150}151152/*[PR CMVC 199693] Prevent trusted method chain attack. */153public Thread newThreadWithAcc(Runnable runnable, AccessControlContext acc) {154return new Thread(runnable, acc);155}156157/**158* Returns a directly present annotation instance of annotationClass type from clazz.159*160* @param clazz Class that will be searched for given annotationClass161* @param annotationClass annotation class that is being searched on the clazz declaration162*163* @return declared annotation of annotationClass type for clazz, otherwise return null164*/165public <A extends Annotation> A getDirectDeclaredAnnotation(Class<?> clazz, Class<A> annotationClass) {166return clazz.getDeclaredAnnotation(annotationClass);167}168169@Override170public Map<java.lang.Class<? extends Annotation>, Annotation> getDeclaredAnnotationMap(171java.lang.Class<?> arg0) {172throw new Error("getDeclaredAnnotationMap unimplemented"); //$NON-NLS-1$173}174175@Override176public byte[] getRawClassTypeAnnotations(java.lang.Class<?> clazz) {177return TypeAnnotationParser.getTypeAnnotationsData(clazz);178}179180@Override181public byte[] getRawExecutableTypeAnnotations(Executable exec) {182byte[] result = null;183if (Method.class.isInstance(exec)) {184Method jlrMethod = (Method) exec;185result = TypeAnnotationParser.getTypeAnnotationsData(jlrMethod);186} else if (Constructor.class.isInstance(exec)) {187Constructor<?> jlrConstructor = (Constructor<?>) exec;188result = TypeAnnotationParser.getTypeAnnotationsData(jlrConstructor);189} else {190throw new Error("getRawExecutableTypeAnnotations not defined for "+exec.getClass().getName()); //$NON-NLS-1$191}192return result;193}194195/*[IF JAVA_SPEC_VERSION < 10]*/196/**197* Return a newly created String that uses the passed in char[]198* without copying. The array must not be modified after creating199* the String.200*201* @param data The char[] for the String202* @return a new String using the char[].203*/204@Override205/*[PR Jazz 87855] Implement j.l.Access.newStringUnsafe()]*/206public java.lang.String newStringUnsafe(char[] data) {207return new String(data, true /*ignored*/);208}209/*[ENDIF] JAVA_SPEC_VERSION < 10 */210211@Override212public void invokeFinalize(java.lang.Object arg0)213throws java.lang.Throwable {214/*215* Not required for J9.216*/217throw new Error("invokeFinalize unimplemented"); //$NON-NLS-1$218}219220/*[IF Sidecar19-SE]*/221public Class<?> findBootstrapClassOrNull(ClassLoader classLoader, String name) {222return VMAccess.findClassOrNull(name, ClassLoader.bootstrapClassLoader);223}224225public ModuleLayer getBootLayer() {226return System.bootLayer;227}228229public ServicesCatalog createOrGetServicesCatalog(ClassLoader classLoader) {230return classLoader.createOrGetServicesCatalog();231}232233/*[IF JAVA_SPEC_VERSION < 10]*/234@Deprecated235public ServicesCatalog getServicesCatalog(ClassLoader classLoader) {236return classLoader.getServicesCatalog();237}238/*[ENDIF] JAVA_SPEC_VERSION < 10 */239240public String fastUUID(long param1, long param2) {241return Long.fastUUID(param1, param2);242}243244public Package definePackage(ClassLoader classLoader, String name, Module module) {245if (null == classLoader) {246classLoader = ClassLoader.bootstrapClassLoader;247}248return classLoader.definePackage(name, module);249}250251public URL findResource(ClassLoader classLoader, String moduleName, String name) throws IOException {252if (null == classLoader) {253classLoader = ClassLoader.bootstrapClassLoader;254}255return classLoader.findResource(moduleName, name);256}257258public Stream<Package> packages(ClassLoader classLoader) {259if (null == classLoader) {260classLoader = ClassLoader.bootstrapClassLoader;261}262return classLoader.packages();263}264265public ConcurrentHashMap<?, ?> createOrGetClassLoaderValueMap(266java.lang.ClassLoader classLoader) {267return classLoader.createOrGetClassLoaderValueMap();268}269270public Method getMethodOrNull(Class<?> clz, String name, Class<?>... parameterTypes) {271try {272return clz.getMethodHelper(false, false, null, name, parameterTypes);273} catch (NoSuchMethodException ex) {274return null;275}276}277278public void invalidatePackageAccessCache() {279/*[IF JAVA_SPEC_VERSION >= 10]*/280java.lang.SecurityManager.invalidatePackageAccessCache();281/*[ELSE] JAVA_SPEC_VERSION >= 10 */282return;283/*[ENDIF] JAVA_SPEC_VERSION >= 10 */284}285286public Class<?> defineClass(ClassLoader classLoader, String className, byte[] classRep, ProtectionDomain protectionDomain, String str) {287ClassLoader targetClassLoader = (null == classLoader) ? ClassLoader.bootstrapClassLoader : classLoader;288return targetClassLoader.defineClassInternal(className, classRep, 0, classRep.length, protectionDomain, true /* allowNullProtectionDomain */);289}290291/*[IF Sidecar19-SE-OpenJ9]*/292public Stream<ModuleLayer> layers(ModuleLayer ml) {293return ml.layers();294}295296public Stream<ModuleLayer> layers(ClassLoader cl) {297return ModuleLayer.layers(cl);298}299300public Module defineModule(ClassLoader cl, ModuleDescriptor md, URI uri) {301return new Module(System.bootLayer, cl, md, uri);302}303304public Module defineUnnamedModule(ClassLoader cl) {305return new Module(cl);306}307308public void addReads(Module fromModule, Module toModule) {309fromModule.implAddReads(toModule);310}311312public void addReadsAllUnnamed(Module module) {313module.implAddReadsAllUnnamed();314}315316public void addExports(Module fromModule, String pkg, Module toModule) {317fromModule.implAddExports(pkg, toModule);318}319320public void addExportsToAllUnnamed(Module fromModule, String pkg) {321fromModule.implAddExportsToAllUnnamed(pkg);322}323324public void addOpens(Module fromModule, String pkg, Module toModule) {325fromModule.implAddOpens(pkg, toModule);326}327328public void addOpensToAllUnnamed(Module fromModule, String pkg) {329fromModule.implAddOpensToAllUnnamed(pkg);330}331332public void addUses(Module fromModule, Class<?> service) {333fromModule.implAddUses(service);334}335public ServicesCatalog getServicesCatalog(ModuleLayer ml) {336return ml.getServicesCatalog();337}338339public void addNonExportedPackages(ModuleLayer ml) {340SecurityManager.addNonExportedPackages(ml);341}342343public List<Method> getDeclaredPublicMethods(Class<?> clz, String name, Class<?>... types) {344return clz.getDeclaredPublicMethods(name, types);345}346347/*[IF JAVA_SPEC_VERSION >= 15]*/348public void addOpensToAllUnnamed(Module fromModule, Set<String> concealedPackages, Set<String> exportedPackages) {349fromModule.implAddOpensToAllUnnamed(concealedPackages, exportedPackages);350}351/*[ELSE] JAVA_SPEC_VERSION >= 15 */352public void addOpensToAllUnnamed(Module fromModule, Iterator<String> packages) {353fromModule.implAddOpensToAllUnnamed(packages);354}355/*[ENDIF] JAVA_SPEC_VERSION >= 15 */356357public boolean isReflectivelyOpened(Module fromModule, String pkg, Module toModule) {358return fromModule.isReflectivelyOpened(pkg, toModule);359}360361public boolean isReflectivelyExported(Module fromModule, String pkg, Module toModule) {362return fromModule.isReflectivelyExported(pkg, toModule);363}364/*[ENDIF] Sidecar19-SE-OpenJ9 */365366/*[IF JAVA_SPEC_VERSION >= 10]*/367public String newStringUTF8NoRepl(byte[] bytes, int offset, int length) {368/*[IF JAVA_SPEC_VERSION < 17]*/369return StringCoding.newStringUTF8NoRepl(bytes, offset, length);370/*[ELSE] JAVA_SPEC_VERSION < 17 */371return String.newStringUTF8NoRepl(bytes, offset, length);372/*[ENDIF] JAVA_SPEC_VERSION < 17 */373}374public byte[] getBytesUTF8NoRepl(String str) {375/*[IF JAVA_SPEC_VERSION < 17]*/376return StringCoding.getBytesUTF8NoRepl(str);377/*[ELSE] JAVA_SPEC_VERSION < 17 */378return String.getBytesUTF8NoRepl(str);379/*[ENDIF] JAVA_SPEC_VERSION < 17 */380}381/*[ENDIF] JAVA_SPEC_VERSION >= 10 */382383/*[IF JAVA_SPEC_VERSION >= 11]*/384public void blockedOn(Interruptible interruptible) {385Thread.blockedOn(interruptible);386}387public byte[] getBytesNoRepl(String str, Charset charset) throws CharacterCodingException {388/*[IF JAVA_SPEC_VERSION < 17]*/389return StringCoding.getBytesNoRepl(str, charset);390/*[ELSE] JAVA_SPEC_VERSION < 17 */391return String.getBytesNoRepl(str, charset);392/*[ENDIF] JAVA_SPEC_VERSION < 17 */393}394public String newStringNoRepl(byte[] bytes, Charset charset) throws CharacterCodingException {395/*[IF JAVA_SPEC_VERSION < 17]*/396return StringCoding.newStringNoRepl(bytes, charset);397/*[ELSE] JAVA_SPEC_VERSION < 17 */398return String.newStringNoRepl(bytes, charset);399/*[ENDIF] JAVA_SPEC_VERSION < 17 */400}401/*[ENDIF] JAVA_SPEC_VERSION >= 11 */402403/*[IF JAVA_SPEC_VERSION >= 12]*/404public void setCause(Throwable throwable, Throwable cause) {405throwable.setCause(cause);406}407/*[ENDIF] JAVA_SPEC_VERSION >= 12 */408409/*[IF JAVA_SPEC_VERSION >= 14]*/410public void loadLibrary(Class<?> caller, String library) {411System.loadLibrary(library);412}413/*[ENDIF] JAVA_SPEC_VERSION >= 14 */414415/*[IF JAVA_SPEC_VERSION >= 15]*/416public Class<?> defineClass(ClassLoader classLoader, Class<?> clazz, String className, byte[] classRep, ProtectionDomain protectionDomain, boolean init, int flags, Object classData) {417ClassLoader targetClassLoader = (null == classLoader) ? ClassLoader.bootstrapClassLoader : classLoader;418return targetClassLoader.defineClassInternal(clazz, className, classRep, protectionDomain, init, flags, classData);419}420421/**422* Returns the classData stored in the class.423*424* @param the class from where to retrieve the classData.425*426* @return the classData (Object).427*/428public Object classData(Class<?> clazz) {429return clazz.getClassData();430}431432public ProtectionDomain protectionDomain(Class<?> clazz) {433return clazz.getProtectionDomainInternal();434}435436public MethodHandle stringConcatHelper(String arg0, MethodType type) {437return StringConcatHelper.lookupStatic(arg0, type);438}439440public long stringConcatInitialCoder() {441return StringConcatHelper.initialCoder();442}443444public long stringConcatMix(long arg0, String string) {445return StringConcatHelper.mix(arg0, string);446}447/*[ENDIF] JAVA_SPEC_VERSION >= 15 */448449/*[IF JAVA_SPEC_VERSION >= 16]*/450public void bindToLoader(ModuleLayer ml, ClassLoader cl) {451ml.bindToLoader(cl);452}453454public void addExports(Module fromModule, String pkg) {455fromModule.implAddExports(pkg);456}457/*[ENDIF] JAVA_SPEC_VERSION >= 16 */458459/*[IF JAVA_SPEC_VERSION >= 17]*/460public int decodeASCII(byte[] srcBytes, int srcPos, char[] dstChars, int dstPos, int length) {461return String.decodeASCII(srcBytes, srcPos, dstChars, dstPos, length);462}463464public void inflateBytesToChars(byte[] srcBytes, int srcOffset, char[] dstChars, int dstOffset, int length) {465StringLatin1.inflate(srcBytes, srcOffset, dstChars, dstOffset, length);466}467468// The method findBootstrapClassOrNull(ClassLoader classLoader, String name) can be removed469// after following API change is promoted into extension repo openj9 branch.470public Class<?> findBootstrapClassOrNull(String name) {471return VMAccess.findClassOrNull(name, ClassLoader.bootstrapClassLoader);472}473474public String join(String prefix, String suffix, String delimiter, String[] elements, int size) {475return String.join(prefix, suffix, delimiter, elements, size);476}477478public boolean isEnableNativeAccess(Module mod) {479return mod.implIsEnableNativeAccess();480}481482public void addEnableNativeAccessAllUnnamed() {483Module.implAddEnableNativeAccessAllUnnamed();484}485486public Module addEnableNativeAccess(Module mod) {487return mod.implAddEnableNativeAccess();488}489490public long findNative(ClassLoader loader, String entryName) {491return ClassLoader.findNative(loader, entryName);492}493494@Override495public void exit(int status) {496Shutdown.exit(status);497}498499public int encodeASCII(char[] sa, int sp, byte[] da, int dp, int len) {500return StringCoding.implEncodeAsciiArray(sa, sp, da, dp, len);501}502/*[ENDIF] JAVA_SPEC_VERSION >= 17 */503504/*[ENDIF] Sidecar19-SE */505}506507508