Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/jcl/src/java.base/share/classes/java/lang/Access.java
12513 views
1
/*[INCLUDE-IF JAVA_SPEC_VERSION >= 8]*/
2
/*******************************************************************************
3
* Copyright (c) 2007, 2022 IBM Corp. and others
4
*
5
* This program and the accompanying materials are made available under
6
* the terms of the Eclipse Public License 2.0 which accompanies this
7
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
8
* or the Apache License, Version 2.0 which accompanies this distribution and
9
* is available at https://www.apache.org/licenses/LICENSE-2.0.
10
*
11
* This Source Code may also be made available under the following
12
* Secondary Licenses when the conditions for such availability set
13
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
14
* General Public License, version 2 with the GNU Classpath
15
* Exception [1] and GNU General Public License, version 2 with the
16
* OpenJDK Assembly Exception [2].
17
*
18
* [1] https://www.gnu.org/software/classpath/license.html
19
* [2] http://openjdk.java.net/legal/assembly-exception.html
20
*
21
* 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-exception
22
*******************************************************************************/
23
package java.lang;
24
25
import java.lang.annotation.Annotation;
26
/*[IF JAVA_SPEC_VERSION >= 15]*/
27
import java.lang.invoke.MethodHandle;
28
import java.lang.invoke.MethodType;
29
import jdk.internal.misc.Unsafe;
30
import java.lang.StringConcatHelper;
31
/*[ENDIF] JAVA_SPEC_VERSION >= 15 */
32
import java.lang.reflect.Constructor;
33
import java.lang.reflect.Executable;
34
import java.lang.reflect.Method;
35
import java.security.AccessControlContext;
36
import java.util.Map;
37
38
import com.ibm.oti.reflect.AnnotationParser;
39
import com.ibm.oti.reflect.TypeAnnotationParser;
40
41
/*[IF Sidecar19-SE]*/
42
import java.io.IOException;
43
import java.lang.module.ModuleDescriptor;
44
import java.net.URL;
45
import java.net.URI;
46
import java.security.ProtectionDomain;
47
import java.util.Iterator;
48
import java.util.List;
49
/*[IF JAVA_SPEC_VERSION >= 15]*/
50
import java.util.Set;
51
/*[ENDIF] JAVA_SPEC_VERSION >= 15 */
52
import java.util.concurrent.ConcurrentHashMap;
53
import java.util.stream.Stream;
54
/*[IF JAVA_SPEC_VERSION >= 11]*/
55
import java.nio.charset.Charset;
56
import java.nio.charset.CharacterCodingException;
57
/*[ENDIF] JAVA_SPEC_VERSION >= 11 */
58
/*[IF JAVA_SPEC_VERSION >= 12]*/
59
import jdk.internal.access.JavaLangAccess;
60
/*[ELSE] JAVA_SPEC_VERSION >= 12 */
61
import jdk.internal.misc.JavaLangAccess;
62
/*[ENDIF] JAVA_SPEC_VERSION >= 12 */
63
import jdk.internal.module.ServicesCatalog;
64
import jdk.internal.reflect.ConstantPool;
65
/*[ELSE] Sidecar19-SE */
66
import sun.misc.JavaLangAccess;
67
import sun.reflect.ConstantPool;
68
/*[ENDIF] Sidecar19-SE */
69
import sun.nio.ch.Interruptible;
70
import sun.reflect.annotation.AnnotationType;
71
72
/**
73
* Helper class to allow privileged access to classes
74
* from outside the java.lang package. The sun.misc.SharedSecrets class
75
* uses an instance of this class to access private java.lang members.
76
*/
77
final class Access implements JavaLangAccess {
78
79
/** Set thread's blocker field. */
80
public void blockedOn(java.lang.Thread thread, Interruptible interruptable) {
81
/*[IF JAVA_SPEC_VERSION >= 11]*/
82
Thread.blockedOn(interruptable);
83
/*[ELSE] JAVA_SPEC_VERSION >= 11 */
84
thread.blockedOn(interruptable);
85
/*[ENDIF] JAVA_SPEC_VERSION >= 11 */
86
}
87
88
/**
89
* Get the AnnotationType instance corresponding to this class.
90
* (This method only applies to annotation types.)
91
*/
92
public AnnotationType getAnnotationType(java.lang.Class<?> arg0) {
93
return arg0.getAnnotationType();
94
}
95
96
/** Return the constant pool for a class. */
97
public native ConstantPool getConstantPool(java.lang.Class<?> arg0);
98
99
/**
100
* Return the constant pool for a class. This will call the exact same
101
* native as 'getConstantPool(java.lang.Class<?> arg0)'. We need this
102
* version so we can call it with InternRamClass instead of j.l.Class
103
* (which is required by JAVALANGACCESS).
104
*/
105
public static native ConstantPool getConstantPool(Object arg0);
106
107
/**
108
* Returns the elements of an enum class or null if the
109
* Class object does not represent an enum type;
110
* the result is uncloned, cached, and shared by all callers.
111
*/
112
public <E extends Enum<E>> E[] getEnumConstantsShared(java.lang.Class<E> arg0) {
113
/*[PR CMVC 189091] Perf: EnumSet.allOf() is slow */
114
return arg0.getEnumConstantsShared();
115
}
116
117
public void registerShutdownHook(int arg0, boolean arg1, Runnable arg2) {
118
Shutdown.add(arg0, arg1, arg2);
119
}
120
121
/**
122
* Set the AnnotationType instance corresponding to this class.
123
* (This method only applies to annotation types.)
124
*/
125
public void setAnnotationType(java.lang.Class<?> arg0, AnnotationType arg1) {
126
arg0.setAnnotationType(arg1);
127
}
128
129
/**
130
* Compare-And-Swap the AnnotationType instance corresponding to this class.
131
* (This method only applies to annotation types.)
132
*/
133
public boolean casAnnotationType(final Class<?> clazz, AnnotationType oldType, AnnotationType newType) {
134
return clazz.casAnnotationType(oldType, newType);
135
}
136
137
/**
138
* @param clazz Class object
139
* @return the array of bytes for the Annotations for clazz, or null if clazz is null
140
*/
141
public byte[] getRawClassAnnotations(Class<?> clazz) {
142
return AnnotationParser.getAnnotationsData(clazz);
143
}
144
145
public int getStackTraceDepth(java.lang.Throwable arg0) {
146
return arg0.getInternalStackTrace().length;
147
}
148
149
public java.lang.StackTraceElement getStackTraceElement(java.lang.Throwable arg0, int arg1) {
150
return arg0.getInternalStackTrace()[arg1];
151
}
152
153
/*[PR CMVC 199693] Prevent trusted method chain attack. */
154
public Thread newThreadWithAcc(Runnable runnable, AccessControlContext acc) {
155
return new Thread(runnable, acc);
156
}
157
158
/**
159
* Returns a directly present annotation instance of annotationClass type from clazz.
160
*
161
* @param clazz Class that will be searched for given annotationClass
162
* @param annotationClass annotation class that is being searched on the clazz declaration
163
*
164
* @return declared annotation of annotationClass type for clazz, otherwise return null
165
*/
166
public <A extends Annotation> A getDirectDeclaredAnnotation(Class<?> clazz, Class<A> annotationClass) {
167
return clazz.getDeclaredAnnotation(annotationClass);
168
}
169
170
@Override
171
public Map<java.lang.Class<? extends Annotation>, Annotation> getDeclaredAnnotationMap(
172
java.lang.Class<?> arg0) {
173
throw new Error("getDeclaredAnnotationMap unimplemented"); //$NON-NLS-1$
174
}
175
176
@Override
177
public byte[] getRawClassTypeAnnotations(java.lang.Class<?> clazz) {
178
return TypeAnnotationParser.getTypeAnnotationsData(clazz);
179
}
180
181
@Override
182
public byte[] getRawExecutableTypeAnnotations(Executable exec) {
183
byte[] result = null;
184
if (Method.class.isInstance(exec)) {
185
Method jlrMethod = (Method) exec;
186
result = TypeAnnotationParser.getTypeAnnotationsData(jlrMethod);
187
} else if (Constructor.class.isInstance(exec)) {
188
Constructor<?> jlrConstructor = (Constructor<?>) exec;
189
result = TypeAnnotationParser.getTypeAnnotationsData(jlrConstructor);
190
} else {
191
throw new Error("getRawExecutableTypeAnnotations not defined for "+exec.getClass().getName()); //$NON-NLS-1$
192
}
193
return result;
194
}
195
196
/*[IF JAVA_SPEC_VERSION < 10]*/
197
/**
198
* Return a newly created String that uses the passed in char[]
199
* without copying. The array must not be modified after creating
200
* the String.
201
*
202
* @param data The char[] for the String
203
* @return a new String using the char[].
204
*/
205
@Override
206
/*[PR Jazz 87855] Implement j.l.Access.newStringUnsafe()]*/
207
public java.lang.String newStringUnsafe(char[] data) {
208
return new String(data, true /*ignored*/);
209
}
210
/*[ENDIF] JAVA_SPEC_VERSION < 10 */
211
212
@Override
213
public void invokeFinalize(java.lang.Object arg0)
214
throws java.lang.Throwable {
215
/*
216
* Not required for J9.
217
*/
218
throw new Error("invokeFinalize unimplemented"); //$NON-NLS-1$
219
}
220
221
/*[IF Sidecar19-SE]*/
222
public Class<?> findBootstrapClassOrNull(ClassLoader classLoader, String name) {
223
return VMAccess.findClassOrNull(name, ClassLoader.bootstrapClassLoader);
224
}
225
226
public ModuleLayer getBootLayer() {
227
return System.bootLayer;
228
}
229
230
public ServicesCatalog createOrGetServicesCatalog(ClassLoader classLoader) {
231
return classLoader.createOrGetServicesCatalog();
232
}
233
234
/*[IF JAVA_SPEC_VERSION < 10]*/
235
@Deprecated
236
public ServicesCatalog getServicesCatalog(ClassLoader classLoader) {
237
return classLoader.getServicesCatalog();
238
}
239
/*[ENDIF] JAVA_SPEC_VERSION < 10 */
240
241
public String fastUUID(long param1, long param2) {
242
return Long.fastUUID(param1, param2);
243
}
244
245
public Package definePackage(ClassLoader classLoader, String name, Module module) {
246
if (null == classLoader) {
247
classLoader = ClassLoader.bootstrapClassLoader;
248
}
249
return classLoader.definePackage(name, module);
250
}
251
252
public URL findResource(ClassLoader classLoader, String moduleName, String name) throws IOException {
253
if (null == classLoader) {
254
classLoader = ClassLoader.bootstrapClassLoader;
255
}
256
return classLoader.findResource(moduleName, name);
257
}
258
259
public Stream<Package> packages(ClassLoader classLoader) {
260
if (null == classLoader) {
261
classLoader = ClassLoader.bootstrapClassLoader;
262
}
263
return classLoader.packages();
264
}
265
266
public ConcurrentHashMap<?, ?> createOrGetClassLoaderValueMap(
267
java.lang.ClassLoader classLoader) {
268
return classLoader.createOrGetClassLoaderValueMap();
269
}
270
271
public Method getMethodOrNull(Class<?> clz, String name, Class<?>... parameterTypes) {
272
try {
273
return clz.getMethodHelper(false, false, null, name, parameterTypes);
274
} catch (NoSuchMethodException ex) {
275
return null;
276
}
277
}
278
279
public void invalidatePackageAccessCache() {
280
/*[IF JAVA_SPEC_VERSION >= 10]*/
281
java.lang.SecurityManager.invalidatePackageAccessCache();
282
/*[ELSE] JAVA_SPEC_VERSION >= 10 */
283
return;
284
/*[ENDIF] JAVA_SPEC_VERSION >= 10 */
285
}
286
287
public Class<?> defineClass(ClassLoader classLoader, String className, byte[] classRep, ProtectionDomain protectionDomain, String str) {
288
ClassLoader targetClassLoader = (null == classLoader) ? ClassLoader.bootstrapClassLoader : classLoader;
289
return targetClassLoader.defineClassInternal(className, classRep, 0, classRep.length, protectionDomain, true /* allowNullProtectionDomain */);
290
}
291
292
/*[IF Sidecar19-SE-OpenJ9]*/
293
public Stream<ModuleLayer> layers(ModuleLayer ml) {
294
return ml.layers();
295
}
296
297
public Stream<ModuleLayer> layers(ClassLoader cl) {
298
return ModuleLayer.layers(cl);
299
}
300
301
public Module defineModule(ClassLoader cl, ModuleDescriptor md, URI uri) {
302
return new Module(System.bootLayer, cl, md, uri);
303
}
304
305
public Module defineUnnamedModule(ClassLoader cl) {
306
return new Module(cl);
307
}
308
309
public void addReads(Module fromModule, Module toModule) {
310
fromModule.implAddReads(toModule);
311
}
312
313
public void addReadsAllUnnamed(Module module) {
314
module.implAddReadsAllUnnamed();
315
}
316
317
public void addExports(Module fromModule, String pkg, Module toModule) {
318
fromModule.implAddExports(pkg, toModule);
319
}
320
321
public void addExportsToAllUnnamed(Module fromModule, String pkg) {
322
fromModule.implAddExportsToAllUnnamed(pkg);
323
}
324
325
public void addOpens(Module fromModule, String pkg, Module toModule) {
326
fromModule.implAddOpens(pkg, toModule);
327
}
328
329
public void addOpensToAllUnnamed(Module fromModule, String pkg) {
330
fromModule.implAddOpensToAllUnnamed(pkg);
331
}
332
333
public void addUses(Module fromModule, Class<?> service) {
334
fromModule.implAddUses(service);
335
}
336
public ServicesCatalog getServicesCatalog(ModuleLayer ml) {
337
return ml.getServicesCatalog();
338
}
339
340
public void addNonExportedPackages(ModuleLayer ml) {
341
SecurityManager.addNonExportedPackages(ml);
342
}
343
344
public List<Method> getDeclaredPublicMethods(Class<?> clz, String name, Class<?>... types) {
345
return clz.getDeclaredPublicMethods(name, types);
346
}
347
348
/*[IF JAVA_SPEC_VERSION >= 15]*/
349
public void addOpensToAllUnnamed(Module fromModule, Set<String> concealedPackages, Set<String> exportedPackages) {
350
fromModule.implAddOpensToAllUnnamed(concealedPackages, exportedPackages);
351
}
352
/*[ELSE] JAVA_SPEC_VERSION >= 15 */
353
public void addOpensToAllUnnamed(Module fromModule, Iterator<String> packages) {
354
fromModule.implAddOpensToAllUnnamed(packages);
355
}
356
/*[ENDIF] JAVA_SPEC_VERSION >= 15 */
357
358
public boolean isReflectivelyOpened(Module fromModule, String pkg, Module toModule) {
359
return fromModule.isReflectivelyOpened(pkg, toModule);
360
}
361
362
public boolean isReflectivelyExported(Module fromModule, String pkg, Module toModule) {
363
return fromModule.isReflectivelyExported(pkg, toModule);
364
}
365
/*[ENDIF] Sidecar19-SE-OpenJ9 */
366
367
/*[IF JAVA_SPEC_VERSION >= 10]*/
368
public String newStringUTF8NoRepl(byte[] bytes, int offset, int length) {
369
/*[IF JAVA_SPEC_VERSION < 17]*/
370
return StringCoding.newStringUTF8NoRepl(bytes, offset, length);
371
/*[ELSE] JAVA_SPEC_VERSION < 17 */
372
return String.newStringUTF8NoRepl(bytes, offset, length);
373
/*[ENDIF] JAVA_SPEC_VERSION < 17 */
374
}
375
public byte[] getBytesUTF8NoRepl(String str) {
376
/*[IF JAVA_SPEC_VERSION < 17]*/
377
return StringCoding.getBytesUTF8NoRepl(str);
378
/*[ELSE] JAVA_SPEC_VERSION < 17 */
379
return String.getBytesUTF8NoRepl(str);
380
/*[ENDIF] JAVA_SPEC_VERSION < 17 */
381
}
382
/*[ENDIF] JAVA_SPEC_VERSION >= 10 */
383
384
/*[IF JAVA_SPEC_VERSION >= 11]*/
385
public void blockedOn(Interruptible interruptible) {
386
Thread.blockedOn(interruptible);
387
}
388
public byte[] getBytesNoRepl(String str, Charset charset) throws CharacterCodingException {
389
/*[IF JAVA_SPEC_VERSION < 17]*/
390
return StringCoding.getBytesNoRepl(str, charset);
391
/*[ELSE] JAVA_SPEC_VERSION < 17 */
392
return String.getBytesNoRepl(str, charset);
393
/*[ENDIF] JAVA_SPEC_VERSION < 17 */
394
}
395
public String newStringNoRepl(byte[] bytes, Charset charset) throws CharacterCodingException {
396
/*[IF JAVA_SPEC_VERSION < 17]*/
397
return StringCoding.newStringNoRepl(bytes, charset);
398
/*[ELSE] JAVA_SPEC_VERSION < 17 */
399
return String.newStringNoRepl(bytes, charset);
400
/*[ENDIF] JAVA_SPEC_VERSION < 17 */
401
}
402
/*[ENDIF] JAVA_SPEC_VERSION >= 11 */
403
404
/*[IF JAVA_SPEC_VERSION >= 12]*/
405
public void setCause(Throwable throwable, Throwable cause) {
406
throwable.setCause(cause);
407
}
408
/*[ENDIF] JAVA_SPEC_VERSION >= 12 */
409
410
/*[IF JAVA_SPEC_VERSION >= 14]*/
411
public void loadLibrary(Class<?> caller, String library) {
412
System.loadLibrary(library);
413
}
414
/*[ENDIF] JAVA_SPEC_VERSION >= 14 */
415
416
/*[IF JAVA_SPEC_VERSION >= 15]*/
417
public Class<?> defineClass(ClassLoader classLoader, Class<?> clazz, String className, byte[] classRep, ProtectionDomain protectionDomain, boolean init, int flags, Object classData) {
418
ClassLoader targetClassLoader = (null == classLoader) ? ClassLoader.bootstrapClassLoader : classLoader;
419
return targetClassLoader.defineClassInternal(clazz, className, classRep, protectionDomain, init, flags, classData);
420
}
421
422
/**
423
* Returns the classData stored in the class.
424
*
425
* @param the class from where to retrieve the classData.
426
*
427
* @return the classData (Object).
428
*/
429
public Object classData(Class<?> clazz) {
430
return clazz.getClassData();
431
}
432
433
public ProtectionDomain protectionDomain(Class<?> clazz) {
434
return clazz.getProtectionDomainInternal();
435
}
436
437
public MethodHandle stringConcatHelper(String arg0, MethodType type) {
438
return StringConcatHelper.lookupStatic(arg0, type);
439
}
440
441
public long stringConcatInitialCoder() {
442
return StringConcatHelper.initialCoder();
443
}
444
445
public long stringConcatMix(long arg0, String string) {
446
return StringConcatHelper.mix(arg0, string);
447
}
448
/*[ENDIF] JAVA_SPEC_VERSION >= 15 */
449
450
/*[IF JAVA_SPEC_VERSION >= 16]*/
451
public void bindToLoader(ModuleLayer ml, ClassLoader cl) {
452
ml.bindToLoader(cl);
453
}
454
455
public void addExports(Module fromModule, String pkg) {
456
fromModule.implAddExports(pkg);
457
}
458
/*[ENDIF] JAVA_SPEC_VERSION >= 16 */
459
460
/*[IF JAVA_SPEC_VERSION >= 17]*/
461
public int decodeASCII(byte[] srcBytes, int srcPos, char[] dstChars, int dstPos, int length) {
462
return String.decodeASCII(srcBytes, srcPos, dstChars, dstPos, length);
463
}
464
465
public void inflateBytesToChars(byte[] srcBytes, int srcOffset, char[] dstChars, int dstOffset, int length) {
466
StringLatin1.inflate(srcBytes, srcOffset, dstChars, dstOffset, length);
467
}
468
469
// The method findBootstrapClassOrNull(ClassLoader classLoader, String name) can be removed
470
// after following API change is promoted into extension repo openj9 branch.
471
public Class<?> findBootstrapClassOrNull(String name) {
472
return VMAccess.findClassOrNull(name, ClassLoader.bootstrapClassLoader);
473
}
474
475
public String join(String prefix, String suffix, String delimiter, String[] elements, int size) {
476
return String.join(prefix, suffix, delimiter, elements, size);
477
}
478
479
public boolean isEnableNativeAccess(Module mod) {
480
return mod.implIsEnableNativeAccess();
481
}
482
483
public void addEnableNativeAccessAllUnnamed() {
484
Module.implAddEnableNativeAccessAllUnnamed();
485
}
486
487
public Module addEnableNativeAccess(Module mod) {
488
return mod.implAddEnableNativeAccess();
489
}
490
491
public long findNative(ClassLoader loader, String entryName) {
492
return ClassLoader.findNative(loader, entryName);
493
}
494
495
@Override
496
public void exit(int status) {
497
Shutdown.exit(status);
498
}
499
500
public int encodeASCII(char[] sa, int sp, byte[] da, int dp, int len) {
501
return StringCoding.implEncodeAsciiArray(sa, sp, da, dp, len);
502
}
503
/*[ENDIF] JAVA_SPEC_VERSION >= 17 */
504
505
/*[ENDIF] Sidecar19-SE */
506
}
507
508