jdk/src/share/native/sun/misc/Version.c
author dxu
Mon, 13 Jan 2014 16:32:18 -0800
changeset 22266 d70900404b67
parent 5506 202f599c92aa
child 22637 d4b45e70a981
permissions -rw-r--r--
8029007: Check src/share/native/sun/misc code for JNI pending exceptions Reviewed-by: chegar, alanb, rriggs, mchung

/*
 * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

#include "jni.h"
#include "jni_util.h"
#include "jvm.h"
#include "jdk_util.h"

#include "sun_misc_Version.h"

char jvm_special_version = '\0';
char jdk_special_version = '\0';
static void setStaticIntField(JNIEnv* env, jclass cls, const char* name, jint value)
{
    jfieldID fid;
    fid = (*env)->GetStaticFieldID(env, cls, name, "I");
    if (fid != 0) {
        (*env)->SetStaticIntField(env, cls, fid, value);
    }
}

typedef void (JNICALL *GetJvmVersionInfo_fp)(JNIEnv*, jvm_version_info*, size_t);

JNIEXPORT jboolean JNICALL
Java_sun_misc_Version_getJvmVersionInfo(JNIEnv *env, jclass cls)
{
    jvm_version_info info;
    GetJvmVersionInfo_fp func_p;

    if (!JDK_InitJvmHandle()) {
        JNU_ThrowInternalError(env, "Handle for JVM not found for symbol lookup");
        return JNI_FALSE;
    }
    func_p = (GetJvmVersionInfo_fp) JDK_FindJvmEntry("JVM_GetVersionInfo");
    if (func_p == NULL) {
        return JNI_FALSE;
    }

    (*func_p)(env, &info, sizeof(info));
    setStaticIntField(env, cls, "jvm_major_version", JVM_VERSION_MAJOR(info.jvm_version));
    CHECK_EXCEPTION_RETURN(env, JNI_FALSE);
    setStaticIntField(env, cls, "jvm_minor_version", JVM_VERSION_MINOR(info.jvm_version));
    CHECK_EXCEPTION_RETURN(env, JNI_FALSE);
    setStaticIntField(env, cls, "jvm_micro_version", JVM_VERSION_MICRO(info.jvm_version));
    CHECK_EXCEPTION_RETURN(env, JNI_FALSE);
    setStaticIntField(env, cls, "jvm_build_number", JVM_VERSION_BUILD(info.jvm_version));
    CHECK_EXCEPTION_RETURN(env, JNI_FALSE);
    setStaticIntField(env, cls, "jvm_update_version", info.update_version);
    CHECK_EXCEPTION_RETURN(env, JNI_FALSE);
    jvm_special_version = info.special_update_version;

    return JNI_TRUE;
}

JNIEXPORT jstring JNICALL
Java_sun_misc_Version_getJvmSpecialVersion(JNIEnv *env, jclass cls) {
    char s[2];
    jstring special;
    s[0] = jvm_special_version;
    s[1] = '\0';
    special = (*env)->NewStringUTF(env, s);
    return special;
}

JNIEXPORT void JNICALL
Java_sun_misc_Version_getJdkVersionInfo(JNIEnv *env, jclass cls)
{
    jdk_version_info info;

    JDK_GetVersionInfo0(&info, sizeof(info));
    setStaticIntField(env, cls, "jdk_major_version", JDK_VERSION_MAJOR(info.jdk_version));
    CHECK_EXCEPTION(env);
    setStaticIntField(env, cls, "jdk_minor_version", JDK_VERSION_MINOR(info.jdk_version));
    CHECK_EXCEPTION(env);
    setStaticIntField(env, cls, "jdk_micro_version", JDK_VERSION_MICRO(info.jdk_version));
    CHECK_EXCEPTION(env);
    setStaticIntField(env, cls, "jdk_build_number", JDK_VERSION_BUILD(info.jdk_version));
    CHECK_EXCEPTION(env);
    setStaticIntField(env, cls, "jdk_update_version", info.update_version);
    CHECK_EXCEPTION(env);
    jdk_special_version = info.special_update_version;
}

JNIEXPORT jstring JNICALL
Java_sun_misc_Version_getJdkSpecialVersion(JNIEnv *env, jclass cls) {
    char s[2];
    jstring special;
    s[0] = jdk_special_version;
    s[1] = '\0';
    special = (*env)->NewStringUTF(env, s);
    return special;
}