summaryrefslogtreecommitdiffstats
path: root/vpp-japi/japi/vppjni_env.h
blob: 44029c2c9645bde2f9426a20610c410b572d1e93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
 * Copyright (c) 2016 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Utilities for accessing Java classes/method/fields in an efficient
 * manner.
 */

/*
 * A potentially-uninitialized reference to a Java class
 */
typedef struct vppjni_class {
    // Fully-Qualified Class Name
    const char          *fqcn;
    // Constructor signature
    const char          *init_sig;
    // Global reference to class handle
    jclass              jclass;
    // Constructor method handle
    jmethodID           jinit;
    // Next item in linked list
    struct vppjni_class *next;
} vppjni_class_t;

typedef struct jenv_field {
    // Field name
    const char           *name;
    // Field type
    const char           *type;
    // Defining class reference
    const vppjni_class_t *clsref;
    // Field handle
    jfieldID             jfield;
    // Next item in linked list
    struct jenv_field    *next;
} vppjni_field_t;

#define VPPJNI_CLASS_SYMBOL(name) vppjni_class_##name
#define VPPJNI_CLASS_INIT(name) vppjni_class_##name##_init
#define BIND_JAPI_CLASS(name, sig) \
    static vppjni_class_t VPPJNI_CLASS_SYMBOL(name); \
    static void VPPJNI_CLASS_INIT(name)(void) __attribute__((__constructor__)); \
    static void VPPJNI_CLASS_INIT(name)() \
    { \
        VPPJNI_CLASS_SYMBOL(name).fqcn = "org/openvpp/vppjapi/" #name; \
        VPPJNI_CLASS_SYMBOL(name).init_sig = sig; \
        vppjni_register_class(&VPPJNI_CLASS_SYMBOL(name)); \
    } \
    static __attribute__((unused)) jobject name##Array(JNIEnv *env, jsize length) \
    { \
        return (*env)->NewObjectArray(env, length, VPPJNI_CLASS_SYMBOL(name).jclass, NULL); \
    } \
    static jobject name##Object(JNIEnv *env, ...) \
    { \
        va_list ap; \
        va_start(ap, env); \
        jobject obj = vppjni_new_object(env, &VPPJNI_CLASS_SYMBOL(name), ap); \
        va_end(ap); \
        return obj; \
    }

#define VPPJNI_FIELD_SYMBOL(cls, name) vppjni_field_##cls##_##name
#define VPPJNI_FIELD_INIT(cls, name) vppjni_field_##cls##_##name##_init
#define BIND_JAPI_FIELD(cls, field, sig) \
    static vppjni_field_t VPPJNI_FIELD_SYMBOL(cls, field); \
        static void VPPJNI_FIELD_INIT(cls, field)(void) __attribute__((__constructor__)); \
    static void VPPJNI_FIELD_INIT(cls, field)() \
    { \
        VPPJNI_FIELD_SYMBOL(cls, field).name = #field; \
        VPPJNI_FIELD_SYMBOL(cls, field).type = sig; \
        VPPJNI_FIELD_SYMBOL(cls, field).clsref = &VPPJNI_CLASS_SYMBOL(cls); \
        vppjni_register_field(&VPPJNI_FIELD_SYMBOL(cls, field)); \
    }
#define BIND_JAPI_BOOL_FIELD(cls, field) \
    BIND_JAPI_FIELD(cls, field, "Z"); \
    static void set_##cls##_##field(JNIEnv *env, jobject obj, jboolean value) \
    { \
        (*env)->SetBooleanField(env, obj, VPPJNI_FIELD_SYMBOL(cls, field).jfield, value); \
    }
#define BIND_JAPI_BYTE_FIELD(cls, field) \
    BIND_JAPI_FIELD(cls, field, "B"); \
    static void set_##cls##_##field(JNIEnv *env, jobject obj, jbyte value) \
    { \
        (*env)->SetByteField(env, obj, VPPJNI_FIELD_SYMBOL(cls, field).jfield, value); \
    }
#define BIND_JAPI_INT_FIELD(cls, field) \
    BIND_JAPI_FIELD(cls, field, "I"); \
    static void set_##cls##_##field(JNIEnv *env, jobject obj, jint value) \
    { \
        (*env)->SetIntField(env, obj, VPPJNI_FIELD_SYMBOL(cls, field).jfield, value); \
    }
#define BIND_JAPI_OBJ_FIELD(cls, field, sig) \
    BIND_JAPI_FIELD(cls, field, sig); \
    static void set_##cls##_##field(JNIEnv *env, jobject obj, jobject value) \
    { \
        (*env)->SetObjectField(env, obj, VPPJNI_FIELD_SYMBOL(cls, field).jfield, value); \
    }
#define BIND_JAPI_STRING_FIELD(cls, field) \
    BIND_JAPI_OBJ_FIELD(cls, field, "Ljava/lang/String;")

jobject vppjni_new_object(JNIEnv *env, const vppjni_class_t *ptr, va_list ap) __attribute__ ((visibility ("hidden")));
void vppjni_register_class(vppjni_class_t *ptr) __attribute__ ((visibility ("hidden")));
void vppjni_register_field(vppjni_field_t *ptr) __attribute__ ((visibility ("hidden")));
int vppjni_init(JNIEnv *env) __attribute__ ((visibility ("hidden")));
void vppjni_uninit(JNIEnv *env) __attribute__ ((visibility ("hidden")));