• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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 #ifndef ART_RUNTIME_MIRROR_STRING_H_
18 #define ART_RUNTIME_MIRROR_STRING_H_
19 
20 #include "class.h"
21 #include "gtest/gtest.h"
22 
23 namespace art {
24 
25 struct StringClassOffsets;
26 struct StringOffsets;
27 class StringPiece;
28 
29 namespace mirror {
30 
31 // C++ mirror of java.lang.String
32 class MANAGED String : public Object {
33  public:
CountOffset()34   static MemberOffset CountOffset() {
35     return OFFSET_OF_OBJECT_MEMBER(String, count_);
36   }
37 
ValueOffset()38   static MemberOffset ValueOffset() {
39     return OFFSET_OF_OBJECT_MEMBER(String, array_);
40   }
41 
OffsetOffset()42   static MemberOffset OffsetOffset() {
43     return OFFSET_OF_OBJECT_MEMBER(String, offset_);
44   }
45 
46   const CharArray* GetCharArray() const;
47 
GetOffset()48   int32_t GetOffset() const {
49     int32_t result = GetField32(OffsetOffset(), false);
50     DCHECK_LE(0, result);
51     return result;
52   }
53 
54   int32_t GetLength() const;
55 
56   int32_t GetHashCode() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
57 
58   void ComputeHashCode() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
59 
60   int32_t GetUtfLength() const;
61 
62   uint16_t CharAt(int32_t index) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
63 
64   String* Intern() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
65 
66   static String* AllocFromUtf16(Thread* self,
67                                 int32_t utf16_length,
68                                 const uint16_t* utf16_data_in,
69                                 int32_t hash_code = 0)
70       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
71 
72   static String* AllocFromModifiedUtf8(Thread* self, const char* utf)
73       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
74 
75   static String* AllocFromModifiedUtf8(Thread* self, int32_t utf16_length,
76                                        const char* utf8_data_in)
77       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
78 
79   static String* Alloc(Thread* self, Class* java_lang_String, int32_t utf16_length)
80       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
81 
82   static String* Alloc(Thread* self, Class* java_lang_String, CharArray* array)
83       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
84 
85   bool Equals(const char* modified_utf8) const
86       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
87 
88   // TODO: do we need this overload? give it a more intention-revealing name.
89   bool Equals(const StringPiece& modified_utf8) const
90       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
91 
92   bool Equals(const String* that) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
93 
94   // Compare UTF-16 code point values not in a locale-sensitive manner
95   int Compare(int32_t utf16_length, const char* utf8_data_in);
96 
97   // TODO: do we need this overload? give it a more intention-revealing name.
98   bool Equals(const uint16_t* that_chars, int32_t that_offset,
99               int32_t that_length) const
100       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
101 
102   // Create a modified UTF-8 encoded std::string from a java/lang/String object.
103   std::string ToModifiedUtf8() const;
104 
105   int32_t FastIndexOf(int32_t ch, int32_t start) const;
106 
107   int32_t CompareTo(String* other) const;
108 
GetJavaLangString()109   static Class* GetJavaLangString() {
110     DCHECK(java_lang_String_ != NULL);
111     return java_lang_String_;
112   }
113 
114   static void SetClass(Class* java_lang_String);
115   static void ResetClass();
116 
117  private:
SetHashCode(int32_t new_hash_code)118   void SetHashCode(int32_t new_hash_code) {
119     DCHECK_EQ(0u,
120               GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false));
121     SetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_),
122                new_hash_code, false);
123   }
124 
SetCount(int32_t new_count)125   void SetCount(int32_t new_count) {
126     DCHECK_LE(0, new_count);
127     SetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), new_count, false);
128   }
129 
SetOffset(int32_t new_offset)130   void SetOffset(int32_t new_offset) {
131     DCHECK_LE(0, new_offset);
132     DCHECK_GE(GetLength(), new_offset);
133     SetField32(OFFSET_OF_OBJECT_MEMBER(String, offset_), new_offset, false);
134   }
135 
136   void SetArray(CharArray* new_array) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
137 
138   // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
139   CharArray* array_;
140 
141   int32_t count_;
142 
143   uint32_t hash_code_;
144 
145   int32_t offset_;
146 
147   static Class* java_lang_String_;
148 
149   friend struct art::StringOffsets;  // for verifying offset information
150   FRIEND_TEST(ObjectTest, StringLength);  // for SetOffset and SetCount
151   DISALLOW_IMPLICIT_CONSTRUCTORS(String);
152 };
153 
154 class MANAGED StringClass : public Class {
155  private:
156   CharArray* ASCII_;
157   Object* CASE_INSENSITIVE_ORDER_;
158   int64_t serialVersionUID_;
159   uint32_t REPLACEMENT_CHAR_;
160   friend struct art::StringClassOffsets;  // for verifying offset information
161   DISALLOW_IMPLICIT_CONSTRUCTORS(StringClass);
162 };
163 
164 }  // namespace mirror
165 }  // namespace art
166 
167 #endif  // ART_RUNTIME_MIRROR_STRING_H_
168