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 <nativehelper/JNIHelp.h>
30 
31 #define NATIVE_METHOD(className, functionName, signature) \
32 { #functionName, signature, (void*)(className ## _ ## functionName) }
33 
34 static jclass noSuchMethodErrCl;
35 
ObjectStreamClass_initNative(JNIEnv * env)36 static void ObjectStreamClass_initNative(JNIEnv *env)
37 {
38     jclass cl = (*env)->FindClass(env, "java/lang/NoSuchMethodError");
39     if (cl == NULL) {           /* exception thrown */
40         return;
41     }
42     noSuchMethodErrCl = (*env)->NewGlobalRef(env, cl);
43 }
44 
45 /*
46  * Class:     java_io_ObjectStreamClass
47  * Method:    hasStaticInitializer
48  * Signature: (Ljava/lang/Class;Z)Z
49  *
50  * Returns true if the given class defines a <clinit>()V method; returns false
51  * otherwise.
52  */
53 JNIEXPORT jboolean JNICALL
54 // Android-changed: Added inheritStaticInitializer parameter.
55 // The inheritStaticInitializer parameter is set to JNI_TRUE when behavior compatible with
56 // Android version 23 is required.
ObjectStreamClass_hasStaticInitializer(JNIEnv * env,jclass this,jclass clazz,jboolean inheritStaticInitializer)57 ObjectStreamClass_hasStaticInitializer(JNIEnv *env, jclass this,
58                                        jclass clazz,
59                                        jboolean inheritStaticInitializer)
60 {
61     jclass superCl = NULL;
62     jmethodID superClinitId = NULL;
63 
64     // Android-changed: Added comment to explain behavior.
65     // Search for a static initializer method in this class and up through its
66     // ancestor super classes, returning the id of the first method found.
67     jmethodID clinitId =
68         (*env)->GetStaticMethodID(env, clazz, "<clinit>", "()V");
69     if (clinitId == NULL) {     /* error thrown */
70         jthrowable th = (*env)->ExceptionOccurred(env);
71         (*env)->ExceptionClear(env);    /* normal return */
72         if (!(*env)->IsInstanceOf(env, th, noSuchMethodErrCl)) {
73             (*env)->Throw(env, th);
74         }
75         return JNI_FALSE;
76     }
77 
78     // BEGIN Android-changed: Exit immediately if version 23 behavior is required.
79     // At this point the class or one of its ancestor super classes has a
80     // static initializer. If inheritStaticInitializer is true then this method
81     // can simply return true. Otherwise, it needs to check that the static
82     // initializer was not inherited (b/29064453).
83     if (inheritStaticInitializer == JNI_TRUE) {
84         return JNI_TRUE;
85     }
86     // END Android-changed: Exit immediately if version 23 behavior is required.
87 
88     /*
89      * Check superclass for static initializer as well--if the same method ID
90      * is returned, then the static initializer is from a superclass.
91      * Empirically, this step appears to be unnecessary in 1.4; however, the
92      * JNI spec makes no guarantee that GetStaticMethodID will not return the
93      * ID for a superclass initializer.
94      */
95 
96     if ((superCl = (*env)->GetSuperclass(env, clazz)) == NULL) {
97         return JNI_TRUE;
98     }
99     superClinitId =
100         (*env)->GetStaticMethodID(env, superCl, "<clinit>", "()V");
101     if (superClinitId == NULL) {        /* error thrown */
102         jthrowable th = (*env)->ExceptionOccurred(env);
103         (*env)->ExceptionClear(env);    /* normal return */
104         if (!(*env)->IsInstanceOf(env, th, noSuchMethodErrCl)) {
105             (*env)->Throw(env, th);
106         }
107         return JNI_TRUE;
108     }
109 
110     return (clinitId != superClinitId);
111 }
112 
113 static JNINativeMethod gMethods[] = {
114   NATIVE_METHOD(ObjectStreamClass, hasStaticInitializer, "(Ljava/lang/Class;Z)Z"),
115 };
116 
register_java_io_ObjectStreamClass(JNIEnv * env)117 void register_java_io_ObjectStreamClass(JNIEnv* env) {
118   jniRegisterNativeMethods(env, "java/io/ObjectStreamClass", gMethods, NELEM(gMethods));
119   ObjectStreamClass_initNative(env);
120 }
121