Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/GetClassMethods/libOverpassMethods.cpp
66645 views
1
/*
2
* Copyright (c) 2020, 2022, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
#include <stdio.h>
27
#include <string.h>
28
#include "jvmti.h"
29
30
#ifdef __cplusplus
31
extern "C" {
32
#endif
33
34
#define ACC_STATIC 0x0008
35
36
static jvmtiEnv *jvmti = NULL;
37
38
JNIEXPORT
39
jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved) {
40
return JNI_VERSION_9;
41
}
42
43
JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *vm, char *options, void *reserved) {
44
vm->GetEnv((void **)&jvmti, JVMTI_VERSION_11);
45
46
if (options != NULL && strcmp(options, "maintain_original_method_order") == 0) {
47
printf("Enabled capability: maintain_original_method_order\n");
48
jvmtiCapabilities caps;
49
memset(&caps, 0, sizeof(caps));
50
caps.can_maintain_original_method_order = 1;
51
52
jvmtiError err = jvmti->AddCapabilities(&caps);
53
if (err != JVMTI_ERROR_NONE) {
54
printf("Agent_OnLoad: AddCapabilities failed with error: %d\n", err);
55
return JNI_ERR;
56
}
57
}
58
return JNI_OK;
59
}
60
61
JNIEXPORT jobjectArray JNICALL Java_OverpassMethods_getJVMTIDeclaredMethods(JNIEnv *env, jclass static_klass, jclass klass) {
62
jint method_count = 0;
63
jmethodID* methods = NULL;
64
jvmtiError err = jvmti->GetClassMethods(klass, &method_count, &methods);
65
if (err != JVMTI_ERROR_NONE) {
66
printf("GetClassMethods failed with error: %d\n", err);
67
return NULL;
68
}
69
70
jclass method_cls = env->FindClass("java/lang/reflect/Method");
71
if (method_cls == NULL) {
72
printf("FindClass (Method) failed\n");
73
return NULL;
74
}
75
jobjectArray array = env->NewObjectArray(method_count, method_cls, NULL);
76
if (array == NULL) {
77
printf("NewObjectArray failed\n");
78
return NULL;
79
}
80
81
for (int i = 0; i < method_count; i++) {
82
jint modifiers = 0;
83
err = jvmti->GetMethodModifiers(methods[i], &modifiers);
84
if (err != JVMTI_ERROR_NONE) {
85
printf("GetMethodModifiers failed with error: %d\n", err);
86
return NULL;
87
}
88
89
jobject m = env->ToReflectedMethod(klass, methods[i], (modifiers & ACC_STATIC) == ACC_STATIC);
90
if (array == NULL) {
91
printf("ToReflectedMethod failed\n");
92
return NULL;
93
}
94
env->SetObjectArrayElement(array, i, m);
95
96
env->DeleteLocalRef(m);
97
}
98
jvmti->Deallocate((unsigned char *)methods);
99
100
return array;
101
}
102
#ifdef __cplusplus
103
}
104
#endif
105
106