Path: blob/master/runtime/jcl/common/annparser.c
6000 views
/*******************************************************************************1* Copyright (c) 1998, 2018 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 "jcl_internal.h"2627#include "vmaccess.h"2829typedef enum METHOD_OBJECT_TYPE {30OBJECT_TYPE_METHOD,31OBJECT_TYPE_CONSTRUCTOR,32OBJECT_TYPE_UNKNOWN33} METHOD_OBJECT_TYPE;3435jobject JNICALL36Java_com_ibm_oti_reflect_AnnotationParser_getAnnotationsData__Ljava_lang_reflect_Field_2(JNIEnv *env, jclass unusedClass, jobject jlrField)37{38jobject result = NULL;39j9object_t fieldObject = NULL;40J9VMThread *vmThread = (J9VMThread *) env;4142enterVMFromJNI(vmThread);43fieldObject = J9_JNI_UNWRAP_REFERENCE(jlrField);44if (NULL != fieldObject) {45J9JNIFieldID *fieldID = vmThread->javaVM->reflectFunctions.idFromFieldObject(vmThread, NULL, fieldObject);4647j9object_t annotationsData = getFieldAnnotationData(vmThread, fieldID->declaringClass, fieldID);48if (NULL != annotationsData) {49result = vmThread->javaVM->internalVMFunctions->j9jni_createLocalRef(env, annotationsData);50}51}52exitVMToJNI(vmThread);53return result;54}5556jobject JNICALL57Java_com_ibm_oti_reflect_TypeAnnotationParser_getTypeAnnotationsDataImpl__Ljava_lang_reflect_Field_2(JNIEnv *env, jclass unusedClass, jobject jlrField)58{59return getFieldTypeAnnotationsAsByteArray(env, jlrField);60}6162static jobject63getExecutableAnnotationDataHelper(JNIEnv *env, jobject jlrMethod, METHOD_OBJECT_TYPE objType,64j9object_t (*getAnnotationData)(struct J9VMThread *vmThread, struct J9Class *declaringClass, J9Method *ramMethod))65{66jobject result = NULL;67j9object_t methodObject = NULL;68J9VMThread *vmThread = (J9VMThread *) env;6970enterVMFromJNI(vmThread);71methodObject = J9_JNI_UNWRAP_REFERENCE(jlrMethod);72if (NULL != methodObject) {73J9JNIMethodID *methodID = NULL;74J9Method *ramMethod = NULL;75J9Class *declaringClass = NULL;76j9object_t annotationsData = NULL;7778if ((OBJECT_TYPE_UNKNOWN == objType)) {79objType = (J9OBJECT_CLAZZ(vmThread, methodObject) == J9VMJAVALANGREFLECTCONSTRUCTOR_OR_NULL(vmThread->javaVM)) ? OBJECT_TYPE_CONSTRUCTOR: OBJECT_TYPE_METHOD;80}81if (OBJECT_TYPE_CONSTRUCTOR == objType) {82methodID = vmThread->javaVM->reflectFunctions.idFromConstructorObject(vmThread, methodObject);83} else {84methodID = vmThread->javaVM->reflectFunctions.idFromMethodObject(vmThread, methodObject);85}8687ramMethod = methodID->method;88declaringClass = J9_CURRENT_CLASS(J9_CLASS_FROM_METHOD(ramMethod));8990annotationsData = getAnnotationData(vmThread, declaringClass, ramMethod);91if (NULL != annotationsData) {92result = vmThread->javaVM->internalVMFunctions->j9jni_createLocalRef(env, annotationsData);93}94}95exitVMToJNI(vmThread);96return result;97}9899jbyteArray getMethodTypeAnnotationsAsByteArray(JNIEnv *env, jobject jlrMethod) {100return getExecutableAnnotationDataHelper(env, jlrMethod, OBJECT_TYPE_UNKNOWN, getMethodTypeAnnotationData);101}102103jobject JNICALL104Java_com_ibm_oti_reflect_AnnotationParser_getAnnotationsData__Ljava_lang_reflect_Constructor_2(JNIEnv *env, jclass unusedClass, jobject jlrConstructor)105{106return getExecutableAnnotationDataHelper(env, jlrConstructor, OBJECT_TYPE_CONSTRUCTOR, getMethodAnnotationData);107}108109jobject JNICALL110Java_com_ibm_oti_reflect_TypeAnnotationParser_getTypeAnnotationsDataImpl__Ljava_lang_reflect_Constructor_2(JNIEnv *env, jclass unusedClass, jobject jlrConstructor)111{112return getExecutableAnnotationDataHelper(env, jlrConstructor, OBJECT_TYPE_CONSTRUCTOR, getMethodTypeAnnotationData);113}114115jobject JNICALL116Java_com_ibm_oti_reflect_AnnotationParser_getAnnotationsData__Ljava_lang_reflect_Method_2(JNIEnv *env, jclass unusedClass, jobject jlrMethod)117{118return getExecutableAnnotationDataHelper(env, jlrMethod, OBJECT_TYPE_METHOD, getMethodAnnotationData);119}120121jobject JNICALL122Java_com_ibm_oti_reflect_TypeAnnotationParser_getTypeAnnotationsDataImpl__Ljava_lang_reflect_Method_2(JNIEnv *env, jclass unusedClass, jobject jlrMethod)123{124return getExecutableAnnotationDataHelper(env, jlrMethod, OBJECT_TYPE_UNKNOWN, getMethodTypeAnnotationData);125}126127jobject JNICALL128Java_com_ibm_oti_reflect_AnnotationParser_getAnnotationsDataImpl__Ljava_lang_Class_2(JNIEnv *env, jclass unusedClass, jclass jlClass)129{130return Java_java_lang_Class_getDeclaredAnnotationsData(env, jlClass);131}132133jobject JNICALL134Java_com_ibm_oti_reflect_TypeAnnotationParser_getTypeAnnotationsDataImpl__Ljava_lang_Class_2(JNIEnv *env, jclass unusedClass, jclass jlClass)135{136return getClassTypeAnnotationsAsByteArray(env, jlClass);137}138139jobject JNICALL140Java_com_ibm_oti_reflect_AnnotationParser_getParameterAnnotationsData__Ljava_lang_reflect_Constructor_2(JNIEnv *env, jclass unusedClass, jobject jlrConstructor)141{142return getExecutableAnnotationDataHelper(env, jlrConstructor, OBJECT_TYPE_CONSTRUCTOR, getMethodParametersAnnotationData);143}144145jobject JNICALL146Java_com_ibm_oti_reflect_AnnotationParser_getParameterAnnotationsData__Ljava_lang_reflect_Method_2(JNIEnv *env, jclass unusedClass, jobject jlrMethod)147{148return getExecutableAnnotationDataHelper(env, jlrMethod, OBJECT_TYPE_METHOD, getMethodParametersAnnotationData);149}150151jobject JNICALL152Java_com_ibm_oti_reflect_AnnotationParser_getDefaultValueData(JNIEnv *env, jclass unusedClass, jobject jlrMethod)153{154return getExecutableAnnotationDataHelper(env, jlrMethod, OBJECT_TYPE_METHOD, getMethodDefaultAnnotationData);155}156157jobject JNICALL158Java_com_ibm_oti_reflect_AnnotationParser_getConstantPool(JNIEnv *env, jclass unusedClass, jobject classToIntrospect)159{160return Java_java_lang_Access_getConstantPool(env, unusedClass, classToIntrospect);161}162163164