ObjectStreamClass.c revision 8d05e88f57c1ea5543d4012687c70cd64efcada0
1/*
2 * Copyright (c) 2001, 2003, 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 "jni.h"
27#include "jvm.h"
28
29#include "java_io_ObjectStreamClass.h"
30#include "JNIHelp.h"
31
32#define NATIVE_METHOD(className, functionName, signature) \
33{ #functionName, signature, (void*)(className ## _ ## functionName) }
34
35static jclass noSuchMethodErrCl;
36
37/*
38 * Class:     java_io_ObjectStreamClass
39 * Method:    initNative
40 * Signature: ()V
41 *
42 * Native code initialization hook.
43 */
44JNIEXPORT void JNICALL
45ObjectStreamClass_initNative(JNIEnv *env, jclass this)
46{
47    jclass cl = (*env)->FindClass(env, "java/lang/NoSuchMethodError");
48    if (cl == NULL) {           /* exception thrown */
49        return;
50    }
51    noSuchMethodErrCl = (*env)->NewGlobalRef(env, cl);
52}
53
54/*
55 * Class:     java_io_ObjectStreamClass
56 * Method:    hasStaticInitializer
57 * Signature: (Ljava/lang/Class;)Z
58 *
59 * Returns true if the given class defines a <clinit>()V method; returns false
60 * otherwise.
61 */
62JNIEXPORT jboolean JNICALL
63ObjectStreamClass_hasStaticInitializer(JNIEnv *env, jclass this,
64                                                    jclass clazz)
65{
66    jclass superCl = NULL;
67    jmethodID superClinitId = NULL;
68    jmethodID clinitId =
69        (*env)->GetStaticMethodID(env, clazz, "<clinit>", "()V");
70    if (clinitId == NULL) {     /* error thrown */
71        jthrowable th = (*env)->ExceptionOccurred(env);
72        (*env)->ExceptionClear(env);    /* normal return */
73        if (!(*env)->IsInstanceOf(env, th, noSuchMethodErrCl)) {
74            (*env)->Throw(env, th);
75        }
76        return JNI_FALSE;
77    }
78
79    /*
80     * Check superclass for static initializer as well--if the same method ID
81     * is returned, then the static initializer is from a superclass.
82     * Empirically, this step appears to be unnecessary in 1.4; however, the
83     * JNI spec makes no guarantee that GetStaticMethodID will not return the
84     * ID for a superclass initializer.
85     */
86
87    if ((superCl = (*env)->GetSuperclass(env, clazz)) == NULL) {
88        return JNI_TRUE;
89    }
90    superClinitId =
91        (*env)->GetStaticMethodID(env, superCl, "<clinit>", "()V");
92    if (superClinitId == NULL) {        /* error thrown */
93        jthrowable th = (*env)->ExceptionOccurred(env);
94        (*env)->ExceptionClear(env);    /* normal return */
95        if (!(*env)->IsInstanceOf(env, th, noSuchMethodErrCl)) {
96            (*env)->Throw(env, th);
97        }
98        return JNI_TRUE;
99    }
100
101    return (clinitId != superClinitId);
102}
103
104static JNINativeMethod gMethods[] = {
105  NATIVE_METHOD(ObjectStreamClass, initNative, "()V"),
106  NATIVE_METHOD(ObjectStreamClass, hasStaticInitializer, "(Ljava/lang/Class;)Z"),
107};
108
109void register_java_io_ObjectStreamClass(JNIEnv* env) {
110  jniRegisterNativeMethods(env, "java/io/ObjectStreamClass", gMethods, NELEM(gMethods));
111}
112