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 HIDDEN {
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 = mirror::String::DoConcat(soa.Self(), string_this, string_arg);
64     return soa.AddLocalReference<jstring>(result);
65   }
66   jobject string_original = (length_this == 0) ? java_string_arg : java_this;
67   return reinterpret_cast<jstring>(string_original);
68 }
69 
String_fastSubstring(JNIEnv * env,jobject java_this,jint start,jint length)70 static jstring String_fastSubstring(JNIEnv* env, jobject java_this, jint start, jint length) {
71   ScopedFastNativeObjectAccess soa(env);
72   StackHandleScope<1> hs(soa.Self());
73   Handle<mirror::String> string_this(hs.NewHandle(soa.Decode<mirror::String>(java_this)));
74   gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
75   ObjPtr<mirror::String> result = mirror::String::AllocFromString(soa.Self(),
76                                                                   length,
77                                                                   string_this,
78                                                                   start,
79                                                                   allocator_type);
80   return soa.AddLocalReference<jstring>(result);
81 }
82 
String_getCharsNoCheck(JNIEnv * env,jobject java_this,jint start,jint end,jcharArray buffer,jint index)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 
String_fillBytesLatin1(JNIEnv * env,jobject java_this,jbyteArray buffer,jint byteIndex)91 static void String_fillBytesLatin1(JNIEnv* env, jobject java_this,
92                                    jbyteArray buffer, jint byteIndex) {
93   ScopedFastNativeObjectAccess soa(env);
94   StackHandleScope<1> hs(soa.Self());
95   Handle<mirror::ByteArray> byte_array(hs.NewHandle(soa.Decode<mirror::ByteArray>(buffer)));
96   soa.Decode<mirror::String>(java_this)->FillBytesLatin1(byte_array, byteIndex);
97 }
98 
String_fillBytesUTF16(JNIEnv * env,jobject java_this,jbyteArray buffer,jint byteIndex)99 static void String_fillBytesUTF16(JNIEnv* env, jobject java_this,
100                                    jbyteArray buffer, jint byteIndex) {
101   ScopedFastNativeObjectAccess soa(env);
102   StackHandleScope<1> hs(soa.Self());
103   Handle<mirror::ByteArray> byte_array(hs.NewHandle(soa.Decode<mirror::ByteArray>(buffer)));
104   soa.Decode<mirror::String>(java_this)->FillBytesUTF16(byte_array, byteIndex);
105 }
106 
String_intern(JNIEnv * env,jobject java_this)107 static jstring String_intern(JNIEnv* env, jobject java_this) {
108   ScopedFastNativeObjectAccess soa(env);
109   ObjPtr<mirror::String> result = soa.Decode<mirror::String>(java_this)->Intern();
110   return soa.AddLocalReference<jstring>(result);
111 }
112 
String_doReplace(JNIEnv * env,jobject java_this,jchar old_c,jchar new_c)113 static jstring String_doReplace(JNIEnv* env, jobject java_this, jchar old_c, jchar new_c) {
114   ScopedFastNativeObjectAccess soa(env);
115   StackHandleScope<1> hs(soa.Self());
116   Handle<mirror::String> string = hs.NewHandle(soa.Decode<mirror::String>(java_this));
117   ObjPtr<mirror::String> result = mirror::String::DoReplace(soa.Self(), string, old_c, new_c);
118   return soa.AddLocalReference<jstring>(result);
119 }
120 
String_toCharArray(JNIEnv * env,jobject java_this)121 static jcharArray String_toCharArray(JNIEnv* env, jobject java_this) {
122   ScopedFastNativeObjectAccess soa(env);
123   StackHandleScope<1u> hs(soa.Self());
124   Handle<mirror::String> s = hs.NewHandle(soa.Decode<mirror::String>(java_this));
125   return soa.AddLocalReference<jcharArray>(mirror::String::ToCharArray(s, soa.Self()));
126 }
127 
String_doRepeat(JNIEnv * env,jobject java_this,jint count)128 static jstring String_doRepeat(JNIEnv* env, jobject java_this, jint count) {
129   ScopedFastNativeObjectAccess soa(env);
130   StackHandleScope<1> hs(soa.Self());
131   Handle<mirror::String> string = hs.NewHandle(soa.Decode<mirror::String>(java_this));
132   ObjPtr<mirror::String> result = mirror::String::DoRepeat(soa.Self(), string, count);
133   return soa.AddLocalReference<jstring>(result);
134 }
135 
136 static JNINativeMethod gMethods[] = {
137   FAST_NATIVE_METHOD(String, charAt, "(I)C"),
138   FAST_NATIVE_METHOD(String, compareTo, "(Ljava/lang/String;)I"),
139   FAST_NATIVE_METHOD(String, concat, "(Ljava/lang/String;)Ljava/lang/String;"),
140   FAST_NATIVE_METHOD(String, doRepeat, "(I)Ljava/lang/String;"),
141   FAST_NATIVE_METHOD(String, doReplace, "(CC)Ljava/lang/String;"),
142   FAST_NATIVE_METHOD(String, fastSubstring, "(II)Ljava/lang/String;"),
143   FAST_NATIVE_METHOD(String, getCharsNoCheck, "(II[CI)V"),
144   FAST_NATIVE_METHOD(String, fillBytesLatin1, "([BI)V"),
145   FAST_NATIVE_METHOD(String, fillBytesUTF16, "([BI)V"),
146   FAST_NATIVE_METHOD(String, intern, "()Ljava/lang/String;"),
147   FAST_NATIVE_METHOD(String, toCharArray, "()[C"),
148 };
149 
register_java_lang_String(JNIEnv * env)150 void register_java_lang_String(JNIEnv* env) {
151   REGISTER_NATIVE_METHODS("java/lang/String");
152 }
153 
154 }  // namespace art
155