Home | History | Annotate | Download | only in native
      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 "nativehelper/jni_macros.h"
     20 
     21 #include "common_throws.h"
     22 #include "jni_internal.h"
     23 #include "mirror/array.h"
     24 #include "mirror/object-inl.h"
     25 #include "mirror/string-inl.h"
     26 #include "mirror/string.h"
     27 #include "native_util.h"
     28 #include "nativehelper/ScopedLocalRef.h"
     29 #include "scoped_fast_native_object_access-inl.h"
     30 #include "scoped_thread_state_change-inl.h"
     31 #include "verify_object.h"
     32 
     33 namespace art {
     34 
     35 static jchar String_charAt(JNIEnv* env, jobject java_this, jint index) {
     36   ScopedFastNativeObjectAccess soa(env);
     37   return soa.Decode<mirror::String>(java_this)->CharAt(index);
     38 }
     39 
     40 static jint String_compareTo(JNIEnv* env, jobject java_this, jobject java_rhs) {
     41   ScopedFastNativeObjectAccess soa(env);
     42   if (UNLIKELY(java_rhs == nullptr)) {
     43     ThrowNullPointerException("rhs == null");
     44     return -1;
     45   } else {
     46     return soa.Decode<mirror::String>(java_this)->CompareTo(
     47         soa.Decode<mirror::String>(java_rhs).Ptr());
     48   }
     49 }
     50 
     51 static jstring String_concat(JNIEnv* env, jobject java_this, jobject java_string_arg) {
     52   ScopedFastNativeObjectAccess soa(env);
     53   if (UNLIKELY(java_string_arg == nullptr)) {
     54     ThrowNullPointerException("string arg == null");
     55     return nullptr;
     56   }
     57   StackHandleScope<2> hs(soa.Self());
     58   Handle<mirror::String> string_this(hs.NewHandle(soa.Decode<mirror::String>(java_this)));
     59   Handle<mirror::String> string_arg(hs.NewHandle(soa.Decode<mirror::String>(java_string_arg)));
     60   int32_t length_this = string_this->GetLength();
     61   int32_t length_arg = string_arg->GetLength();
     62   if (length_arg > 0 && length_this > 0) {
     63     ObjPtr<mirror::String> result =
     64         mirror::String::AllocFromStrings(soa.Self(), string_this, string_arg);
     65     return soa.AddLocalReference<jstring>(result);
     66   }
     67   jobject string_original = (length_this == 0) ? java_string_arg : java_this;
     68   return reinterpret_cast<jstring>(string_original);
     69 }
     70 
     71 static jint String_fastIndexOf(JNIEnv* env, jobject java_this, jint ch, jint start) {
     72   ScopedFastNativeObjectAccess soa(env);
     73   // This method does not handle supplementary characters. They're dealt with in managed code.
     74   DCHECK_LE(ch, 0xffff);
     75   return soa.Decode<mirror::String>(java_this)->FastIndexOf(ch, start);
     76 }
     77 
     78 static jstring String_fastSubstring(JNIEnv* env, jobject java_this, jint start, jint length) {
     79   ScopedFastNativeObjectAccess soa(env);
     80   StackHandleScope<1> hs(soa.Self());
     81   Handle<mirror::String> string_this(hs.NewHandle(soa.Decode<mirror::String>(java_this)));
     82   gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
     83   ObjPtr<mirror::String> result = mirror::String::AllocFromString<true>(soa.Self(),
     84                                                                         length,
     85                                                                         string_this,
     86                                                                         start,
     87                                                                         allocator_type);
     88   return soa.AddLocalReference<jstring>(result);
     89 }
     90 
     91 static void String_getCharsNoCheck(JNIEnv* env, jobject java_this, jint start, jint end,
     92                                    jcharArray buffer, jint index) {
     93   ScopedFastNativeObjectAccess soa(env);
     94   StackHandleScope<1> hs(soa.Self());
     95   Handle<mirror::CharArray> char_array(hs.NewHandle(soa.Decode<mirror::CharArray>(buffer)));
     96   soa.Decode<mirror::String>(java_this)->GetChars(start, end, char_array, index);
     97 }
     98 
     99 static jstring String_intern(JNIEnv* env, jobject java_this) {
    100   ScopedFastNativeObjectAccess soa(env);
    101   ObjPtr<mirror::String> result = soa.Decode<mirror::String>(java_this)->Intern();
    102   return soa.AddLocalReference<jstring>(result);
    103 }
    104 
    105 static jstring String_doReplace(JNIEnv* env, jobject java_this, jchar old_c, jchar new_c) {
    106   ScopedFastNativeObjectAccess soa(env);
    107   StackHandleScope<1> hs(soa.Self());
    108   Handle<mirror::String> string = hs.NewHandle(soa.Decode<mirror::String>(java_this));
    109   ObjPtr<mirror::String> result = mirror::String::DoReplace(soa.Self(), string, old_c, new_c);
    110   return soa.AddLocalReference<jstring>(result);
    111 }
    112 
    113 static jcharArray String_toCharArray(JNIEnv* env, jobject java_this) {
    114   ScopedFastNativeObjectAccess soa(env);
    115   ObjPtr<mirror::String> s = soa.Decode<mirror::String>(java_this);
    116   return soa.AddLocalReference<jcharArray>(s->ToCharArray(soa.Self()));
    117 }
    118 
    119 static JNINativeMethod gMethods[] = {
    120   FAST_NATIVE_METHOD(String, charAt, "(I)C"),
    121   FAST_NATIVE_METHOD(String, compareTo, "(Ljava/lang/String;)I"),
    122   FAST_NATIVE_METHOD(String, concat, "(Ljava/lang/String;)Ljava/lang/String;"),
    123   FAST_NATIVE_METHOD(String, doReplace, "(CC)Ljava/lang/String;"),
    124   FAST_NATIVE_METHOD(String, fastIndexOf, "(II)I"),
    125   FAST_NATIVE_METHOD(String, fastSubstring, "(II)Ljava/lang/String;"),
    126   FAST_NATIVE_METHOD(String, getCharsNoCheck, "(II[CI)V"),
    127   FAST_NATIVE_METHOD(String, intern, "()Ljava/lang/String;"),
    128   FAST_NATIVE_METHOD(String, toCharArray, "()[C"),
    129 };
    130 
    131 void register_java_lang_String(JNIEnv* env) {
    132   REGISTER_NATIVE_METHODS("java/lang/String");
    133 }
    134 
    135 }  // namespace art
    136