aboutsummaryrefslogtreecommitdiffstats
path: root/vpp-api/java/japi/vppjni_env.c
blob: 1c4ea6ebe1857e5a388c0b09683c17c0bd6241e3 (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
/*
 * 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.
 */
#include <jni.h>

#include "vppjni_env.h"

// Head of the class registration list.
static vppjni_class_t *class_head;
// Head of the class registration list.
static vppjni_field_t *field_head;

void vppjni_register_class(vppjni_class_t *ptr)
{
    vppjni_class_t **where = &class_head;
    while (*where != NULL) {
        where = &((*where)->next);
    }
    *where = ptr;
}

void vppjni_register_field(vppjni_field_t *ptr) {
    vppjni_field_t **where = &field_head;
    while (*where != NULL) {
        where = &((*where)->next);
    }
    *where = ptr;
}

jobject vppjni_new_object(JNIEnv *env, const vppjni_class_t *ptr, va_list ap) {
    jobject obj = (*env)->NewObjectV(env, ptr->jclass, ptr->jinit, ap);
    if ((*env)->ExceptionCheck(env)) {
        (*env)->ExceptionDescribe(env);
        return NULL;
    }

    return obj;
}

int vppjni_init(JNIEnv *env)
{
    vppjni_class_t *cwlk;
    vppjni_field_t *fwlk;

    for (cwlk = class_head; cwlk != NULL; cwlk = cwlk->next) {
        jclass cls;
        jmethodID method;

        cls = (*env)->FindClass(env, cwlk->fqcn);
        if ((*env)->ExceptionCheck(env)) {
            (*env)->ExceptionDescribe(env);
            vppjni_uninit(env);
            return JNI_ERR;
        }

        method = (*env)->GetMethodID(env, cls, "<init>", cwlk->init_sig);
        if ((*env)->ExceptionCheck(env)) {
            (*env)->ExceptionDescribe(env);
            vppjni_uninit(env);
            return JNI_ERR;
        }

        cwlk->jclass = (*env)->NewGlobalRef(env, cls);
        if (cwlk->jclass == NULL) {
            vppjni_uninit(env);
            return JNI_ERR;
        }
        cwlk->jinit = method;
    }

    for (fwlk = field_head; fwlk != NULL; fwlk = fwlk->next) {
        fwlk->jfield = (*env)->GetFieldID(env, fwlk->clsref->jclass, fwlk->name, fwlk->type);
        if ((*env)->ExceptionCheck(env)) {
            (*env)->ExceptionDescribe(env);
            vppjni_uninit(env);
            return JNI_ERR;
        }
    }

    return 0;
}

void vppjni_uninit(JNIEnv *env) {
    vppjni_class_t *cwlk;
    vppjni_field_t *fwlk;

    for (fwlk = field_head; fwlk != NULL; fwlk = fwlk->next) {
        fwlk->jfield = NULL;
    }

    for (cwlk = class_head; cwlk != NULL; cwlk = cwlk->next) {
        if (cwlk->jclass != NULL ) {
            (*env)->DeleteGlobalRef(env, cwlk->jclass);
        }

        cwlk->jclass = NULL;
        cwlk->jinit = NULL;
    }
}