From 2141a5fb6f7e2bcf1bb9638b59de028a33084f8f Mon Sep 17 00:00:00 2001 From: Salikh Zakirov Date: Fri, 30 Jun 2006 18:07:27 +0400 Subject: [PATCH] Moved intern() native implementation from String to VM --- vm/tests/smoke/util/StringIntern.java | 41 +++++++++++++ .../javasrc/org/apache/harmony/kernel/vm/VM.java | 14 +++++ .../src/kernel_classes/native/java_lang_String.cpp | 42 ---------------- .../src/kernel_classes/native/java_lang_String.h | 61 ----------------------- .../native/org_apache_harmony_kernel_vm_VM.cpp | 12 +++++ .../native/org_apache_harmony_kernel_vm_VM.h | 6 ++ 6 files changed, 73 insertions(+), 103 deletions(-) diff --git a/vm/tests/smoke/util/StringIntern.java b/vm/tests/smoke/util/StringIntern.java new file mode 100644 index 0000000..a417e14 --- /dev/null +++ b/vm/tests/smoke/util/StringIntern.java @@ -0,0 +1,41 @@ +/* + * Copyright 2005-2006 The Apache Software Foundation or its licensors, as applicable. + * + * 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. + */ +/** + * @author Salikh Zakirov + * @version 2006-06-30 + */ +package util; + +/** + * Tests the correctness of string interning. + * + * @keyword + */ +public class StringIntern { + public static void main(String[] args) { + String s = "abcde"; + String s0 = "abcde".intern(); + String s1 = ("abc" + "de").intern(); + String s2 = ("ab" + "cde").intern(); + if (s0 != s1 || s1 != s2) { + System.out.println("FAIL: incorrect interning"); + } else if (s != s0) { + System.out.println("FAIL: literal is not interned"); + } else { + System.out.println("PASS"); + } + } +} diff --git a/vm/vmcore/src/kernel_classes/javasrc/org/apache/harmony/kernel/vm/VM.java b/vm/vmcore/src/kernel_classes/javasrc/org/apache/harmony/kernel/vm/VM.java index 8b6efc6..732abe0 100644 --- a/vm/vmcore/src/kernel_classes/javasrc/org/apache/harmony/kernel/vm/VM.java +++ b/vm/vmcore/src/kernel_classes/javasrc/org/apache/harmony/kernel/vm/VM.java @@ -114,4 +114,18 @@ public final class VM { Runtime.getRuntime().addShutdownHook(new Thread(new DeleteOnExitHook())); } } + + /** + * Returns an interned representation of a String. + * An interned representation of equal string values is guaranteed + * to be exactly the same object. + */ + public static String intern(String s) { + return intern0(s); + } + + /** + * Invokes native string interning service. + */ + private static native String intern0(String s); } diff --git a/vm/vmcore/src/kernel_classes/native/java_lang_String.cpp b/vm/vmcore/src/kernel_classes/native/java_lang_String.cpp deleted file mode 100644 index 14b6de6..0000000 --- a/vm/vmcore/src/kernel_classes/native/java_lang_String.cpp +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 2005-2006 The Apache Software Foundation or its licensors, as applicable. - * - * 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. - */ -/** - * @author Euguene Ostrovsky - * @version $Revision: 1.1.2.1.4.5 $ - */ - -/** - * @file java_lang_String.cpp - * - * This file is a part of kernel class natives VM core component. - * It contains implementation for native methods of java.lang.String kernel - * class. - */ - -#include "java_lang_String.h" - -#include "vm_strings.h" - -/** - * Implements java.lang.String.intern(..) method. - * For details see kernel classes component documentation. - */ -JNIEXPORT jstring JNICALL -Java_java_lang_String_intern(JNIEnv *jenv, jstring str) -{ - // just call corresponding VM internal function - return string_intern(jenv, str); -} diff --git a/vm/vmcore/src/kernel_classes/native/java_lang_String.h b/vm/vmcore/src/kernel_classes/native/java_lang_String.h deleted file mode 100644 index df33607..0000000 --- a/vm/vmcore/src/kernel_classes/native/java_lang_String.h +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright 2005-2006 The Apache Software Foundation or its licensors, as applicable. - * - * 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. - */ - -/** - * @author Euguene Ostrovsky - * @version $Revision: 1.1.2.1.4.6 $ - */ - -/* - * THE FILE HAS BEEN AUTOGENERATED BY INTEL IJH TOOL. - * Please be aware that all changes made to this file manually - * will be overwritten by the tool if it runs again. - */ - -#include - - -/* Header for class java.lang.String */ - -#ifndef _JAVA_LANG_STRING_H -#define _JAVA_LANG_STRING_H - -#ifdef __cplusplus -extern "C" { -#endif - - -/* Static final fields */ - -#undef java_lang_String_serialVersionUID -#define java_lang_String_serialVersionUID -6849794470754667710LL - - -/* Native methods */ - -/* - * Method: java.lang.String.intern()Ljava/lang/String; - */ -JNIEXPORT jstring JNICALL -Java_java_lang_String_intern(JNIEnv *, jstring); - - -#ifdef __cplusplus -} -#endif - -#endif /* _JAVA_LANG_STRING_H */ - diff --git a/vm/vmcore/src/kernel_classes/native/org_apache_harmony_kernel_vm_VM.cpp b/vm/vmcore/src/kernel_classes/native/org_apache_harmony_kernel_vm_VM.cpp index 13205b3..6e763ab 100644 --- a/vm/vmcore/src/kernel_classes/native/org_apache_harmony_kernel_vm_VM.cpp +++ b/vm/vmcore/src/kernel_classes/native/org_apache_harmony_kernel_vm_VM.cpp @@ -31,6 +31,7 @@ #include #define LOG_DOMAIN "vm.accessors" #include "jni_utils.h" +#include "vm_strings.h" #include "org_apache_harmony_kernel_vm_VM.h" #include "java_lang_VMClassRegistry.h" @@ -44,3 +45,14 @@ (JNIEnv *jenv, jclass, jclass clazz) // reuse similar method in VMClassRegistry return Java_java_lang_VMClassRegistry_getClassLoader(jenv, NULL, clazz); } + +/** + * Implements org.apache.harmony.vm.VM.intern0(..) method. + * For details see kernel classes component documentation. + */ +JNIEXPORT jstring JNICALL +Java_org_apache_harmony_kernel_vm_VM_intern0(JNIEnv *jenv, jclass, jstring str) +{ + // just call corresponding VM internal function + return string_intern(jenv, str); +} diff --git a/vm/vmcore/src/kernel_classes/native/org_apache_harmony_kernel_vm_VM.h b/vm/vmcore/src/kernel_classes/native/org_apache_harmony_kernel_vm_VM.h index ac8d5bf..00b9d30 100644 --- a/vm/vmcore/src/kernel_classes/native/org_apache_harmony_kernel_vm_VM.h +++ b/vm/vmcore/src/kernel_classes/native/org_apache_harmony_kernel_vm_VM.h @@ -47,6 +47,12 @@ JNIEXPORT jobject JNICALL Java_org_apache_harmony_kernel_vm_VM_getClassLoader(JNIEnv *, jclass, jclass); +/* + * Method: org.apache.harmony.kernel.vm.VM.intern0()Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL +Java_org_apache_harmony_kernel_vm_VM_intern0(JNIEnv *, jclass, jstring); + #ifdef __cplusplus } -- 1.4.0.g16c4