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
31static jclass noSuchMethodErrCl;
32
33/*
34 * Class:     java_io_ObjectStreamClass
35 * Method:    initNative
36 * Signature: ()V
37 *
38 * Native code initialization hook.
39 */
40JNIEXPORT void JNICALL
41Java_java_io_ObjectStreamClass_initNative(JNIEnv *env, jclass this)
42{
43    jclass cl = (*env)->FindClass(env, "java/lang/NoSuchMethodError");
44    if (cl == NULL) {           /* exception thrown */
45        return;
46    }
47    noSuchMethodErrCl = (*env)->NewGlobalRef(env, cl);
48}
49
50/*
51 * Class:     java_io_ObjectStreamClass
52 * Method:    hasStaticInitializer
53 * Signature: (Ljava/lang/Class;)Z
54 *
55 * Returns true if the given class defines a <clinit>()V method; returns false
56 * otherwise.
57 */
58JNIEXPORT jboolean JNICALL
59Java_java_io_ObjectStreamClass_hasStaticInitializer(JNIEnv *env, jclass this,
60                                                    jclass clazz)
61{
62    jclass superCl = NULL;
63    jmethodID superClinitId = NULL;
64    jmethodID clinitId =
65        (*env)->GetStaticMethodID(env, clazz, "<clinit>", "()V");
66    if (clinitId == NULL) {     /* error thrown */
67        jthrowable th = (*env)->ExceptionOccurred(env);
68        (*env)->ExceptionClear(env);    /* normal return */
69        if (!(*env)->IsInstanceOf(env, th, noSuchMethodErrCl)) {
70            (*env)->Throw(env, th);
71        }
72        return JNI_FALSE;
73    }
74
75    /*
76     * Check superclass for static initializer as well--if the same method ID
77     * is returned, then the static initializer is from a superclass.
78     * Empirically, this step appears to be unnecessary in 1.4; however, the
79     * JNI spec makes no guarantee that GetStaticMethodID will not return the
80     * ID for a superclass initializer.
81     */
82
83    if ((superCl = (*env)->GetSuperclass(env, clazz)) == NULL) {
84        return JNI_TRUE;
85    }
86    superClinitId =
87        (*env)->GetStaticMethodID(env, superCl, "<clinit>", "()V");
88    if (superClinitId == NULL) {        /* error thrown */
89        jthrowable th = (*env)->ExceptionOccurred(env);
90        (*env)->ExceptionClear(env);    /* normal return */
91        if (!(*env)->IsInstanceOf(env, th, noSuchMethodErrCl)) {
92            (*env)->Throw(env, th);
93        }
94        return JNI_TRUE;
95    }
96
97    return (clinitId != superClinitId);
98}
99