java_lang_String.cc revision 2cebb24bfc3247d3e9be138a3350106737455918
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/string-inl.h"
22#include "scoped_fast_native_object_access.h"
23#include "scoped_thread_state_change.h"
24#include "ScopedLocalRef.h"
25#include "verify_object-inl.h"
26
27namespace art {
28
29static jint String_compareTo(JNIEnv* env, jobject javaThis, jobject javaRhs) {
30  ScopedFastNativeObjectAccess soa(env);
31  if (UNLIKELY(javaRhs == nullptr)) {
32    ThrowNullPointerException("rhs == null");
33    return -1;
34  } else {
35    return soa.Decode<mirror::String*>(javaThis)->CompareTo(soa.Decode<mirror::String*>(javaRhs));
36  }
37}
38
39static jint String_fastIndexOf(JNIEnv* env, jobject java_this, jint ch, jint start) {
40  ScopedFastNativeObjectAccess soa(env);
41  // This method does not handle supplementary characters. They're dealt with in managed code.
42  DCHECK_LE(ch, 0xffff);
43
44  mirror::String* s = soa.Decode<mirror::String*>(java_this);
45  return s->FastIndexOf(ch, start);
46}
47
48static jstring String_intern(JNIEnv* env, jobject javaThis) {
49  ScopedFastNativeObjectAccess soa(env);
50  mirror::String* s = soa.Decode<mirror::String*>(javaThis);
51  mirror::String* result = s->Intern();
52  return soa.AddLocalReference<jstring>(result);
53}
54
55static JNINativeMethod gMethods[] = {
56  NATIVE_METHOD(String, compareTo, "!(Ljava/lang/String;)I"),
57  NATIVE_METHOD(String, fastIndexOf, "!(II)I"),
58  NATIVE_METHOD(String, intern, "!()Ljava/lang/String;"),
59};
60
61void register_java_lang_String(JNIEnv* env) {
62  REGISTER_NATIVE_METHODS("java/lang/String");
63}
64
65}  // namespace art
66