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/scoped_local_ref.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, jstring 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, jstring 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 jstring String_fastSubstring(JNIEnv* env, jobject java_this, jint start, jint length) {
     72   ScopedFastNativeObjectAccess soa(env);
     73   StackHandleScope<1> hs(soa.Self());
     74   Handle<mirror::String> string_this(hs.NewHandle(soa.Decode<mirror::String>(java_this)));
     75   gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
     76   ObjPtr<mirror::String> result = mirror::String::AllocFromString<true>(soa.Self(),
     77                                                                         length,
     78                                                                         string_this,
     79                                                                         start,
     80                                                                         allocator_type);
     81   return soa.AddLocalReference<jstring>(result);
     82 }
     83 
     84 static void String_getCharsNoCheck(JNIEnv* env, jobject java_this, jint start, jint end,
     85                                    jcharArray buffer, jint index) {
     86   ScopedFastNativeObjectAccess soa(env);
     87   StackHandleScope<1> hs(soa.Self());
     88   Handle<mirror::CharArray> char_array(hs.NewHandle(soa.Decode<mirror::CharArray>(buffer)));
     89   soa.Decode<mirror::String>(java_this)->GetChars(start, end, char_array, index);
     90 }
     91 
     92 static jstring String_intern(JNIEnv* env, jobject java_this) {
     93   ScopedFastNativeObjectAccess soa(env);
     94   ObjPtr<mirror::String> result = soa.Decode<mirror::String>(java_this)->Intern();
     95   return soa.AddLocalReference<jstring>(result);
     96 }
     97 
     98 static jstring String_doReplace(JNIEnv* env, jobject java_this, jchar old_c, jchar new_c) {
     99   ScopedFastNativeObjectAccess soa(env);
    100   StackHandleScope<1> hs(soa.Self());
    101   Handle<mirror::String> string = hs.NewHandle(soa.Decode<mirror::String>(java_this));
    102   ObjPtr<mirror::String> result = mirror::String::DoReplace(soa.Self(), string, old_c, new_c);
    103   return soa.AddLocalReference<jstring>(result);
    104 }
    105 
    106 static jcharArray String_toCharArray(JNIEnv* env, jobject java_this) {
    107   ScopedFastNativeObjectAccess soa(env);
    108   ObjPtr<mirror::String> s = soa.Decode<mirror::String>(java_this);
    109   return soa.AddLocalReference<jcharArray>(s->ToCharArray(soa.Self()));
    110 }
    111 
    112 static JNINativeMethod gMethods[] = {
    113   FAST_NATIVE_METHOD(String, charAt, "(I)C"),
    114   FAST_NATIVE_METHOD(String, compareTo, "(Ljava/lang/String;)I"),
    115   FAST_NATIVE_METHOD(String, concat, "(Ljava/lang/String;)Ljava/lang/String;"),
    116   FAST_NATIVE_METHOD(String, doReplace, "(CC)Ljava/lang/String;"),
    117   FAST_NATIVE_METHOD(String, fastSubstring, "(II)Ljava/lang/String;"),
    118   FAST_NATIVE_METHOD(String, getCharsNoCheck, "(II[CI)V"),
    119   FAST_NATIVE_METHOD(String, intern, "()Ljava/lang/String;"),
    120   FAST_NATIVE_METHOD(String, toCharArray, "()[C"),
    121 };
    122 
    123 void register_java_lang_String(JNIEnv* env) {
    124   REGISTER_NATIVE_METHODS("java/lang/String");
    125 }
    126 
    127 }  // namespace art
    128