Path: blob/master/runtime/jcl/common/java_lang_Access.c
6000 views
/*******************************************************************************1* Copyright (c) 1998, 2021 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* 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-exception20*******************************************************************************/2122#include "jni.h"23#include "jcl.h"24#include "jclprots.h"25#include "jclglob.h"26#include "jniidcacheinit.h"2728/**29* Return the constant pool for a class.30* Note: This method is also called from annparser.c:Java_com_ibm_oti_reflect_AnnotationParser_getConstantPool.31*32* @param env The current thread.33* @param unusedClass See note above - this parameter should not be used.34* @param classToIntrospect The Class or InternalConstantPool instance the ConstantPool should represent.35*36* @return a ConstantPool object reference or NULL on error.37*/38jobject JNICALL39Java_java_lang_Access_getConstantPool(JNIEnv *env, jclass unusedClass, jobject classToIntrospect)40{41jclass sun_reflect_ConstantPool = JCL_CACHE_GET(env,CLS_sun_reflect_ConstantPool);42jfieldID constantPoolOop;43jobject constantPool;44J9VMThread *vmThread = (J9VMThread *)env;45J9InternalVMFunctions *vmFunctions = vmThread->javaVM->internalVMFunctions;46J9MemoryManagerFunctions *gcFunctions = vmThread->javaVM->memoryManagerFunctions;47j9object_t classObject = NULL;4849/* lazy-initialize the cached field IDs */50if (NULL == sun_reflect_ConstantPool) {51if (!initializeSunReflectConstantPoolIDCache(env)) {52return NULL;53}54sun_reflect_ConstantPool = JCL_CACHE_GET(env, CLS_sun_reflect_ConstantPool);55}5657/* allocate the new ConstantPool object */58constantPool = (*env)->AllocObject(env, sun_reflect_ConstantPool);59if (NULL == constantPool) {60return NULL;61}6263/* if this method is called with java/lang/Class, allocate an InternalConstantPool and store the J9ConstantPool */64vmFunctions->internalEnterVMFromJNI(vmThread);65classObject = J9_JNI_UNWRAP_REFERENCE(classToIntrospect);66if (J9VMJAVALANGCLASS_OR_NULL(vmThread->javaVM) == J9OBJECT_CLAZZ(vmThread, classObject)) {67J9Class *clazz = J9VM_J9CLASS_FROM_HEAPCLASS(vmThread, classObject);68J9ConstantPool *constantPool = (J9ConstantPool *)clazz->ramConstantPool;69J9Class *internalConstantPool = J9VMJAVALANGINTERNALCONSTANTPOOL_OR_NULL(vmThread->javaVM);70Assert_JCL_notNull(internalConstantPool);71j9object_t internalConstantPoolObject = gcFunctions->J9AllocateObject(vmThread, internalConstantPool, J9_GC_ALLOCATE_OBJECT_NON_INSTRUMENTABLE);72if (NULL == internalConstantPoolObject) {73vmFunctions->setHeapOutOfMemoryError(vmThread);74vmFunctions->internalExitVMToJNI(vmThread);75return NULL;76}77J9VMJAVALANGINTERNALCONSTANTPOOL_SET_VMREF(vmThread, internalConstantPoolObject, constantPool);78classToIntrospect = vmFunctions->j9jni_createLocalRef(env, internalConstantPoolObject);79}80vmFunctions->internalExitVMToJNI(vmThread);8182/* and set the private constantPoolOop member */83constantPoolOop = JCL_CACHE_GET(env, FID_sun_reflect_ConstantPool_constantPoolOop);84(*env)->SetObjectField(env, constantPool, constantPoolOop, classToIntrospect);8586return constantPool;87}888990