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 "handle_scope-inl.h"
23 #include "jni/jni_internal.h"
24 #include "mirror/array.h"
25 #include "mirror/object-inl.h"
26 #include "mirror/string-alloc-inl.h"
27 #include "mirror/string-inl.h"
28 #include "native_util.h"
29 #include "nativehelper/scoped_local_ref.h"
30 #include "scoped_fast_native_object_access-inl.h"
31 #include "scoped_thread_state_change-inl.h"
32 #include "verify_object.h"
33 
34 namespace art {
35 
String_charAt(JNIEnv * env,jobject java_this,jint index)36 static jchar String_charAt(JNIEnv* env, jobject java_this, jint index) {
37   ScopedFastNativeObjectAccess soa(env);
38   return soa.Decode<mirror::String>(java_this)->CharAt(index);
39 }
40 
String_compareTo(JNIEnv * env,jobject java_this,jstring java_rhs)41 static jint String_compareTo(JNIEnv* env, jobject java_this, jstring java_rhs) {
42   ScopedFastNativeObjectAccess soa(env);
43   if (UNLIKELY(java_rhs == nullptr)) {
44     ThrowNullPointerException("rhs == null");
45     return -1;
46   } else {
47     return soa.Decode<mirror::String>(java_this)->CompareTo(soa.Decode<mirror::String>(java_rhs));
48   }
49 }
50 
String_concat(JNIEnv * env,jobject java_this,jstring java_string_arg)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 
String_fastSubstring(JNIEnv * env,jobject java_this,jint start,jint length)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(soa.Self(),
77                                                                   length,
78                                                                   string_this,
79                                                                   start,
80                                                                   allocator_type);
81   return soa.AddLocalReference<jstring>(result);
82 }
83 
String_getCharsNoCheck(JNIEnv * env,jobject java_this,jint start,jint end,jcharArray buffer,jint index)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 
String_intern(JNIEnv * env,jobject java_this)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 
String_doReplace(JNIEnv * env,jobject java_this,jchar old_c,jchar new_c)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 
String_toCharArray(JNIEnv * env,jobject java_this)106 static jcharArray String_toCharArray(JNIEnv* env, jobject java_this) {
107   ScopedFastNativeObjectAccess soa(env);
108   StackHandleScope<1u> hs(soa.Self());
109   Handle<mirror::String> s = hs.NewHandle(soa.Decode<mirror::String>(java_this));
110   return soa.AddLocalReference<jcharArray>(mirror::String::ToCharArray(s, soa.Self()));
111 }
112 
113 static JNINativeMethod gMethods[] = {
114   FAST_NATIVE_METHOD(String, charAt, "(I)C"),
115   FAST_NATIVE_METHOD(String, compareTo, "(Ljava/lang/String;)I"),
116   FAST_NATIVE_METHOD(String, concat, "(Ljava/lang/String;)Ljava/lang/String;"),
117   FAST_NATIVE_METHOD(String, doReplace, "(CC)Ljava/lang/String;"),
118   FAST_NATIVE_METHOD(String, fastSubstring, "(II)Ljava/lang/String;"),
119   FAST_NATIVE_METHOD(String, getCharsNoCheck, "(II[CI)V"),
120   FAST_NATIVE_METHOD(String, intern, "()Ljava/lang/String;"),
121   FAST_NATIVE_METHOD(String, toCharArray, "()[C"),
122 };
123 
register_java_lang_String(JNIEnv * env)124 void register_java_lang_String(JNIEnv* env) {
125   REGISTER_NATIVE_METHODS("java/lang/String");
126 }
127 
128 }  // namespace art
129