1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "java_lang_String.h" 18 19 #include "common_throws.h" 20 #include "jni_internal.h" 21 #include "mirror/array.h" 22 #include "mirror/object-inl.h" 23 #include "mirror/string.h" 24 #include "mirror/string-inl.h" 25 #include "scoped_fast_native_object_access.h" 26 #include "scoped_thread_state_change.h" 27 #include "ScopedLocalRef.h" 28 #include "verify_object-inl.h" 29 30 namespace art { 31 32 static jchar String_charAt(JNIEnv* env, jobject java_this, jint index) { 33 ScopedFastNativeObjectAccess soa(env); 34 return soa.Decode<mirror::String*>(java_this)->CharAt(index); 35 } 36 37 static jint String_compareTo(JNIEnv* env, jobject java_this, jobject java_rhs) { 38 ScopedFastNativeObjectAccess soa(env); 39 if (UNLIKELY(java_rhs == nullptr)) { 40 ThrowNullPointerException("rhs == null"); 41 return -1; 42 } else { 43 return soa.Decode<mirror::String*>(java_this)->CompareTo(soa.Decode<mirror::String*>(java_rhs)); 44 } 45 } 46 47 static jstring String_concat(JNIEnv* env, jobject java_this, jobject java_string_arg) { 48 ScopedFastNativeObjectAccess soa(env); 49 if (UNLIKELY(java_string_arg == nullptr)) { 50 ThrowNullPointerException("string arg == null"); 51 return nullptr; 52 } 53 StackHandleScope<2> hs(soa.Self()); 54 Handle<mirror::String> string_this(hs.NewHandle(soa.Decode<mirror::String*>(java_this))); 55 Handle<mirror::String> string_arg(hs.NewHandle(soa.Decode<mirror::String*>(java_string_arg))); 56 int32_t length_this = string_this->GetLength(); 57 int32_t length_arg = string_arg->GetLength(); 58 if (length_arg > 0 && length_this > 0) { 59 mirror::String* result = mirror::String::AllocFromStrings(soa.Self(), string_this, string_arg); 60 return soa.AddLocalReference<jstring>(result); 61 } 62 jobject string_original = (length_this == 0) ? java_string_arg : java_this; 63 return reinterpret_cast<jstring>(string_original); 64 } 65 66 static jint String_fastIndexOf(JNIEnv* env, jobject java_this, jint ch, jint start) { 67 ScopedFastNativeObjectAccess soa(env); 68 // This method does not handle supplementary characters. They're dealt with in managed code. 69 DCHECK_LE(ch, 0xffff); 70 return soa.Decode<mirror::String*>(java_this)->FastIndexOf(ch, start); 71 } 72 73 static jstring String_fastSubstring(JNIEnv* env, jobject java_this, jint start, jint length) { 74 ScopedFastNativeObjectAccess soa(env); 75 StackHandleScope<1> hs(soa.Self()); 76 Handle<mirror::String> string_this(hs.NewHandle(soa.Decode<mirror::String*>(java_this))); 77 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator(); 78 mirror::String* result = mirror::String::AllocFromString<true>(soa.Self(), length, string_this, 79 start, allocator_type); 80 return soa.AddLocalReference<jstring>(result); 81 } 82 83 static void String_getCharsNoCheck(JNIEnv* env, jobject java_this, jint start, jint end, 84 jcharArray buffer, jint index) { 85 ScopedFastNativeObjectAccess soa(env); 86 StackHandleScope<1> hs(soa.Self()); 87 Handle<mirror::CharArray> char_array(hs.NewHandle(soa.Decode<mirror::CharArray*>(buffer))); 88 soa.Decode<mirror::String*>(java_this)->GetChars(start, end, char_array, index); 89 } 90 91 static jstring String_intern(JNIEnv* env, jobject java_this) { 92 ScopedFastNativeObjectAccess soa(env); 93 mirror::String* s = soa.Decode<mirror::String*>(java_this); 94 mirror::String* result = s->Intern(); 95 return soa.AddLocalReference<jstring>(result); 96 } 97 98 static void String_setCharAt(JNIEnv* env, jobject java_this, jint index, jchar c) { 99 ScopedFastNativeObjectAccess soa(env); 100 soa.Decode<mirror::String*>(java_this)->SetCharAt(index, c); 101 } 102 103 static jcharArray String_toCharArray(JNIEnv* env, jobject java_this) { 104 ScopedFastNativeObjectAccess soa(env); 105 mirror::String* s = soa.Decode<mirror::String*>(java_this); 106 return soa.AddLocalReference<jcharArray>(s->ToCharArray(soa.Self())); 107 } 108 109 static JNINativeMethod gMethods[] = { 110 NATIVE_METHOD(String, charAt, "!(I)C"), 111 NATIVE_METHOD(String, compareTo, "!(Ljava/lang/String;)I"), 112 NATIVE_METHOD(String, concat, "!(Ljava/lang/String;)Ljava/lang/String;"), 113 NATIVE_METHOD(String, fastIndexOf, "!(II)I"), 114 NATIVE_METHOD(String, fastSubstring, "!(II)Ljava/lang/String;"), 115 NATIVE_METHOD(String, getCharsNoCheck, "!(II[CI)V"), 116 NATIVE_METHOD(String, intern, "!()Ljava/lang/String;"), 117 NATIVE_METHOD(String, setCharAt, "!(IC)V"), 118 NATIVE_METHOD(String, toCharArray, "!()[C"), 119 }; 120 121 void register_java_lang_String(JNIEnv* env) { 122 REGISTER_NATIVE_METHODS("java/lang/String"); 123 } 124 125 } // namespace art 126