blob: 5ad18f87edf6e595667e765f0a92cac15a46b6b2 [file] [log] [blame]
Elliott Hughesd369bb72011-09-12 14:41:14 -07001/*
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
Andreas Gampe277ccbd2014-11-03 21:36:10 -080017#include "java_lang_Class.h"
18
Mathieu Chartierc7853442015-03-27 14:35:38 -070019#include "art_field-inl.h"
Elliott Hughesd369bb72011-09-12 14:41:14 -070020#include "class_linker.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070021#include "common_throws.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070022#include "dex_file-inl.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070023#include "jni_internal.h"
Elliott Hughes6a144332012-04-03 13:07:11 -070024#include "nth_caller_visitor.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070025#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026#include "mirror/class_loader.h"
Hiroshi Yamauchi02d2f292015-04-03 13:35:16 -070027#include "mirror/field-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "mirror/object-inl.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070029#include "mirror/object_array-inl.h"
30#include "mirror/string-inl.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070031#include "scoped_thread_state_change.h"
Ian Rogers1eb512d2013-10-18 15:42:20 -070032#include "scoped_fast_native_object_access.h"
Elliott Hughes80609252011-09-23 17:24:51 -070033#include "ScopedLocalRef.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070034#include "ScopedUtfChars.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070035#include "utf.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070036#include "well_known_classes.h"
Elliott Hughesd369bb72011-09-12 14:41:14 -070037
38namespace art {
39
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070040ALWAYS_INLINE static inline mirror::Class* DecodeClass(
41 const ScopedFastNativeObjectAccess& soa, jobject java_class)
Ian Rogersb726dcb2012-09-05 08:57:23 -070042 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080043 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughes15216932012-03-21 21:53:06 -070044 DCHECK(c != NULL);
45 DCHECK(c->IsClass());
Elliott Hughes923e8b82012-03-23 11:44:07 -070046 // TODO: we could EnsureInitialized here, rather than on every reflective get/set or invoke .
47 // For now, we conservatively preserve the old dalvik behavior. A quick "IsInitialized" check
48 // every time probably doesn't make much difference to reflection performance anyway.
49 return c;
Elliott Hughes15216932012-03-21 21:53:06 -070050}
51
Brian Carlstromf91c8c32011-09-21 17:30:34 -070052// "name" is in "binary name" format, e.g. "dalvik.system.Debug$1".
Ian Rogers98379392014-02-24 16:53:16 -080053static jclass Class_classForName(JNIEnv* env, jclass, jstring javaName, jboolean initialize,
54 jobject javaLoader) {
Ian Rogers53b8b092014-03-13 23:45:53 -070055 ScopedFastNativeObjectAccess soa(env);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070056 ScopedUtfChars name(env, javaName);
Mathieu Chartierc528dba2013-11-26 12:00:11 -080057 if (name.c_str() == nullptr) {
58 return nullptr;
Brian Carlstromf91c8c32011-09-21 17:30:34 -070059 }
60
61 // We need to validate and convert the name (from x.y.z to x/y/z). This
62 // is especially handy for array types, since we want to avoid
63 // auto-generating bogus array classes.
Elliott Hughes906e6852011-10-28 14:52:10 -070064 if (!IsValidBinaryClassName(name.c_str())) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000065 soa.Self()->ThrowNewExceptionF("Ljava/lang/ClassNotFoundException;",
Ian Rogers62d6c772013-02-27 08:32:07 -080066 "Invalid name: %s", name.c_str());
Mathieu Chartierc528dba2013-11-26 12:00:11 -080067 return nullptr;
Brian Carlstromf91c8c32011-09-21 17:30:34 -070068 }
69
70 std::string descriptor(DotToDescriptor(name.c_str()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070071 StackHandleScope<2> hs(soa.Self());
72 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(soa.Decode<mirror::ClassLoader*>(javaLoader)));
Brian Carlstromf91c8c32011-09-21 17:30:34 -070073 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070074 Handle<mirror::Class> c(
75 hs.NewHandle(class_linker->FindClass(soa.Self(), descriptor.c_str(), class_loader)));
76 if (c.Get() == nullptr) {
Elliott Hughes844f9a02012-01-24 20:19:58 -080077 ScopedLocalRef<jthrowable> cause(env, env->ExceptionOccurred());
Brian Carlstrom395520e2011-09-25 19:35:00 -070078 env->ExceptionClear();
Elliott Hugheseac76672012-05-24 21:56:51 -070079 jthrowable cnfe = reinterpret_cast<jthrowable>(env->NewObject(WellKnownClasses::java_lang_ClassNotFoundException,
80 WellKnownClasses::java_lang_ClassNotFoundException_init,
81 javaName, cause.get()));
Ian Rogersc114b5f2014-07-21 08:55:01 -070082 if (cnfe != nullptr) {
83 // Make sure allocation didn't fail with an OOME.
84 env->Throw(cnfe);
85 }
Mathieu Chartierc528dba2013-11-26 12:00:11 -080086 return nullptr;
Brian Carlstrom395520e2011-09-25 19:35:00 -070087 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070088 if (initialize) {
Ian Rogers7b078e82014-09-10 14:44:24 -070089 class_linker->EnsureInitialized(soa.Self(), c, true, true);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070090 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070091 return soa.AddLocalReference<jclass>(c.Get());
Brian Carlstromf91c8c32011-09-21 17:30:34 -070092}
93
Vladimir Markoc1363122015-04-09 14:13:13 +010094static jobject Class_findOverriddenMethodIfProxy(JNIEnv* env, jclass, jobject art_method) {
95 ScopedFastNativeObjectAccess soa(env);
96 mirror::ArtMethod* method = soa.Decode<mirror::ArtMethod*>(art_method);
97 mirror::Class* declaring_klass = method->GetDeclaringClass();
98 if (!declaring_klass->IsProxyClass()) {
99 return art_method;
100 }
101 uint32_t dex_method_index = method->GetDexMethodIndex();
102 mirror::ArtMethod* overriden_method = method->GetDexCacheResolvedMethods()->Get(dex_method_index);
103 return soa.AddLocalReference<jobject>(overriden_method);
104}
105
Elliott Hughes0512f022012-03-15 22:10:52 -0700106static jstring Class_getNameNative(JNIEnv* env, jobject javaThis) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700107 ScopedFastNativeObjectAccess soa(env);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700108 StackHandleScope<1> hs(soa.Self());
109 mirror::Class* const c = DecodeClass(soa, javaThis);
110 return soa.AddLocalReference<jstring>(mirror::Class::ComputeName(hs.NewHandle(c)));
Elliott Hughes6bdc3b22011-09-16 19:24:10 -0700111}
112
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700113static jobjectArray Class_getProxyInterfaces(JNIEnv* env, jobject javaThis) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700114 ScopedFastNativeObjectAccess soa(env);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700115 mirror::Class* c = DecodeClass(soa, javaThis);
Ian Rogers50b35e22012-10-04 10:09:15 -0700116 return soa.AddLocalReference<jobjectArray>(c->GetInterfaces()->Clone(soa.Self()));
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700117}
118
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700119static mirror::ObjectArray<mirror::Field>* GetDeclaredFields(
120 Thread* self, mirror::Class* klass, bool public_only, bool force_resolve)
121 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700122 StackHandleScope<1> hs(self);
123 auto* ifields = klass->GetIFields();
124 auto* sfields = klass->GetSFields();
125 const auto num_ifields = klass->NumInstanceFields();
126 const auto num_sfields = klass->NumStaticFields();
127 size_t array_size = num_ifields + num_sfields;
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700128 if (public_only) {
129 // Lets go subtract all the non public fields.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700130 for (size_t i = 0; i < num_ifields; ++i) {
131 if (!ifields[i].IsPublic()) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700132 --array_size;
133 }
134 }
Mathieu Chartierc7853442015-03-27 14:35:38 -0700135 for (size_t i = 0; i < num_sfields; ++i) {
136 if (!sfields[i].IsPublic()) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700137 --array_size;
138 }
139 }
140 }
Mathieu Chartierc7853442015-03-27 14:35:38 -0700141 size_t array_idx = 0;
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700142 auto object_array = hs.NewHandle(mirror::ObjectArray<mirror::Field>::Alloc(
143 self, mirror::Field::ArrayClass(), array_size));
144 if (object_array.Get() == nullptr) {
145 return nullptr;
146 }
Mathieu Chartierc7853442015-03-27 14:35:38 -0700147 for (size_t i = 0; i < num_ifields; ++i) {
148 auto* art_field = &ifields[i];
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700149 if (!public_only || art_field->IsPublic()) {
150 auto* field = mirror::Field::CreateFromArtField(self, art_field, force_resolve);
151 if (field == nullptr) {
152 if (kIsDebugBuild) {
153 self->AssertPendingException();
154 }
155 // Maybe null due to OOME or type resolving exception.
156 return nullptr;
157 }
158 object_array->SetWithoutChecks<false>(array_idx++, field);
159 }
160 }
Mathieu Chartierc7853442015-03-27 14:35:38 -0700161 for (size_t i = 0; i < num_sfields; ++i) {
162 auto* art_field = &sfields[i];
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700163 if (!public_only || art_field->IsPublic()) {
164 auto* field = mirror::Field::CreateFromArtField(self, art_field, force_resolve);
165 if (field == nullptr) {
166 if (kIsDebugBuild) {
167 self->AssertPendingException();
168 }
169 return nullptr;
170 }
171 object_array->SetWithoutChecks<false>(array_idx++, field);
172 }
173 }
174 CHECK_EQ(array_idx, array_size);
175 return object_array.Get();
176}
177
178static jobjectArray Class_getDeclaredFieldsUnchecked(JNIEnv* env, jobject javaThis,
179 jboolean publicOnly) {
180 ScopedFastNativeObjectAccess soa(env);
181 return soa.AddLocalReference<jobjectArray>(
182 GetDeclaredFields(soa.Self(), DecodeClass(soa, javaThis), publicOnly != JNI_FALSE, false));
183}
184
185static jobjectArray Class_getDeclaredFields(JNIEnv* env, jobject javaThis) {
186 ScopedFastNativeObjectAccess soa(env);
187 return soa.AddLocalReference<jobjectArray>(
188 GetDeclaredFields(soa.Self(), DecodeClass(soa, javaThis), false, true));
189}
190
191static jobjectArray Class_getPublicDeclaredFields(JNIEnv* env, jobject javaThis) {
192 ScopedFastNativeObjectAccess soa(env);
193 return soa.AddLocalReference<jobjectArray>(
194 GetDeclaredFields(soa.Self(), DecodeClass(soa, javaThis), true, true));
195}
196
197// Performs a binary search through an array of fields, TODO: Is this fast enough if we don't use
198// the dex cache for lookups? I think CompareModifiedUtf8ToUtf16AsCodePointValues should be fairly
199// fast.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700200ALWAYS_INLINE static inline ArtField* FindFieldByName(
201 Thread* self ATTRIBUTE_UNUSED, mirror::String* name, ArtField* fields, size_t num_fields)
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700202 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700203 size_t low = 0;
204 size_t high = num_fields;
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700205 const uint16_t* const data = name->GetCharArray()->GetData() + name->GetOffset();
206 const size_t length = name->GetLength();
207 while (low < high) {
208 auto mid = (low + high) / 2;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700209 ArtField* const field = &fields[mid];
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700210 int result = CompareModifiedUtf8ToUtf16AsCodePointValues(field->GetName(), data, length);
211 // Alternate approach, only a few % faster at the cost of more allocations.
212 // int result = field->GetStringName(self, true)->CompareTo(name);
213 if (result < 0) {
214 low = mid + 1;
215 } else if (result > 0) {
216 high = mid;
217 } else {
218 return field;
219 }
220 }
221 if (kIsDebugBuild) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700222 for (size_t i = 0; i < num_fields; ++i) {
223 CHECK_NE(fields[i].GetName(), name->ToModifiedUtf8());
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700224 }
225 }
226 return nullptr;
227}
228
229ALWAYS_INLINE static inline mirror::Field* GetDeclaredField(
230 Thread* self, mirror::Class* c, mirror::String* name)
231 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
232 auto* instance_fields = c->GetIFields();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700233 auto* art_field = FindFieldByName(self, name, instance_fields, c->NumInstanceFields());
234 if (art_field != nullptr) {
235 return mirror::Field::CreateFromArtField(self, art_field, true);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700236 }
237 auto* static_fields = c->GetSFields();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700238 art_field = FindFieldByName(self, name, static_fields, c->NumStaticFields());
239 if (art_field != nullptr) {
240 return mirror::Field::CreateFromArtField(self, art_field, true);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700241 }
242 return nullptr;
243}
244
245static jobject Class_getDeclaredFieldInternal(JNIEnv* env, jobject javaThis, jstring name) {
246 ScopedFastNativeObjectAccess soa(env);
247 auto* name_string = soa.Decode<mirror::String*>(name);
248 return soa.AddLocalReference<jobject>(
249 GetDeclaredField(soa.Self(), DecodeClass(soa, javaThis), name_string));
250}
251
252static jobject Class_getDeclaredField(JNIEnv* env, jobject javaThis, jstring name) {
253 ScopedFastNativeObjectAccess soa(env);
254 auto* name_string = soa.Decode<mirror::String*>(name);
255 if (name == nullptr) {
256 ThrowNullPointerException("name == null");
257 return nullptr;
258 }
259 auto* klass = DecodeClass(soa, javaThis);
260 mirror::Field* result = GetDeclaredField(soa.Self(), klass, name_string);
261 if (result == nullptr) {
262 std::string name_str = name_string->ToModifiedUtf8();
Mathieu Chartierca239af2015-03-29 18:27:50 -0700263 // We may have a pending exception if we failed to resolve.
264 if (!soa.Self()->IsExceptionPending()) {
265 soa.Self()->ThrowNewException("Ljava/lang/NoSuchFieldException;", name_str.c_str());
266 }
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700267 return nullptr;
268 }
269 return soa.AddLocalReference<jobject>(result);
270}
271
Elliott Hughesd369bb72011-09-12 14:41:14 -0700272static JNINativeMethod gMethods[] = {
Ian Rogers53b8b092014-03-13 23:45:53 -0700273 NATIVE_METHOD(Class, classForName, "!(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;"),
Vladimir Markoc1363122015-04-09 14:13:13 +0100274 NATIVE_METHOD(Class, findOverriddenMethodIfProxy,
275 "!(Ljava/lang/reflect/ArtMethod;)Ljava/lang/reflect/ArtMethod;"),
Ian Rogers1eb512d2013-10-18 15:42:20 -0700276 NATIVE_METHOD(Class, getNameNative, "!()Ljava/lang/String;"),
277 NATIVE_METHOD(Class, getProxyInterfaces, "!()[Ljava/lang/Class;"),
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700278 NATIVE_METHOD(Class, getDeclaredFields, "!()[Ljava/lang/reflect/Field;"),
279 NATIVE_METHOD(Class, getPublicDeclaredFields, "!()[Ljava/lang/reflect/Field;"),
280 NATIVE_METHOD(Class, getDeclaredFieldsUnchecked, "!(Z)[Ljava/lang/reflect/Field;"),
281 NATIVE_METHOD(Class, getDeclaredFieldInternal, "!(Ljava/lang/String;)Ljava/lang/reflect/Field;"),
282 NATIVE_METHOD(Class, getDeclaredField, "!(Ljava/lang/String;)Ljava/lang/reflect/Field;"),
Elliott Hughesd369bb72011-09-12 14:41:14 -0700283};
284
Elliott Hughesd369bb72011-09-12 14:41:14 -0700285void register_java_lang_Class(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700286 REGISTER_NATIVE_METHODS("java/lang/Class");
Elliott Hughesd369bb72011-09-12 14:41:14 -0700287}
288
289} // namespace art