blob: c8431c05196ae968e36fc4de1ce5e0b44ab5d816 [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
Nicolas Geoffray0e7fa6b2016-01-13 15:28:58 +000019#include <iostream>
20
Mathieu Chartierc7853442015-03-27 14:35:38 -070021#include "art_field-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070022#include "base/enums.h"
Elliott Hughesd369bb72011-09-12 14:41:14 -070023#include "class_linker.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070024#include "common_throws.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070025#include "dex_file-inl.h"
David Sehr9323e6e2016-09-13 08:58:35 -070026#include "dex_file_annotations.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070027#include "jni_internal.h"
Elliott Hughes6a144332012-04-03 13:07:11 -070028#include "nth_caller_visitor.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070029#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030#include "mirror/class_loader.h"
Hiroshi Yamauchi02d2f292015-04-03 13:35:16 -070031#include "mirror/field-inl.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070032#include "mirror/method.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033#include "mirror/object-inl.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070034#include "mirror/object_array-inl.h"
35#include "mirror/string-inl.h"
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070036#include "obj_ptr-inl.h"
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -070037#include "reflection.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070038#include "scoped_thread_state_change-inl.h"
39#include "scoped_fast_native_object_access-inl.h"
Elliott Hughes80609252011-09-23 17:24:51 -070040#include "ScopedLocalRef.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070041#include "ScopedUtfChars.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070042#include "utf.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070043#include "well_known_classes.h"
Elliott Hughesd369bb72011-09-12 14:41:14 -070044
45namespace art {
46
Mathieu Chartier0795f232016-09-27 18:43:30 -070047ALWAYS_INLINE static inline ObjPtr<mirror::Class> DecodeClass(
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070048 const ScopedFastNativeObjectAccess& soa, jobject java_class)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070049 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier0795f232016-09-27 18:43:30 -070050 ObjPtr<mirror::Class> c = soa.Decode<mirror::Class>(java_class);
Mathieu Chartier2cebb242015-04-21 16:50:40 -070051 DCHECK(c != nullptr);
Elliott Hughes15216932012-03-21 21:53:06 -070052 DCHECK(c->IsClass());
Elliott Hughes923e8b82012-03-23 11:44:07 -070053 // TODO: we could EnsureInitialized here, rather than on every reflective get/set or invoke .
54 // For now, we conservatively preserve the old dalvik behavior. A quick "IsInitialized" check
55 // every time probably doesn't make much difference to reflection performance anyway.
56 return c;
Elliott Hughes15216932012-03-21 21:53:06 -070057}
58
Brian Carlstromf91c8c32011-09-21 17:30:34 -070059// "name" is in "binary name" format, e.g. "dalvik.system.Debug$1".
Ian Rogers98379392014-02-24 16:53:16 -080060static jclass Class_classForName(JNIEnv* env, jclass, jstring javaName, jboolean initialize,
61 jobject javaLoader) {
Ian Rogers53b8b092014-03-13 23:45:53 -070062 ScopedFastNativeObjectAccess soa(env);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070063 ScopedUtfChars name(env, javaName);
Mathieu Chartierc528dba2013-11-26 12:00:11 -080064 if (name.c_str() == nullptr) {
65 return nullptr;
Brian Carlstromf91c8c32011-09-21 17:30:34 -070066 }
67
68 // We need to validate and convert the name (from x.y.z to x/y/z). This
69 // is especially handy for array types, since we want to avoid
70 // auto-generating bogus array classes.
Elliott Hughes906e6852011-10-28 14:52:10 -070071 if (!IsValidBinaryClassName(name.c_str())) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000072 soa.Self()->ThrowNewExceptionF("Ljava/lang/ClassNotFoundException;",
Ian Rogers62d6c772013-02-27 08:32:07 -080073 "Invalid name: %s", name.c_str());
Mathieu Chartierc528dba2013-11-26 12:00:11 -080074 return nullptr;
Brian Carlstromf91c8c32011-09-21 17:30:34 -070075 }
76
77 std::string descriptor(DotToDescriptor(name.c_str()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070078 StackHandleScope<2> hs(soa.Self());
Mathieu Chartier0795f232016-09-27 18:43:30 -070079 Handle<mirror::ClassLoader> class_loader(
80 hs.NewHandle(soa.Decode<mirror::ClassLoader>(javaLoader)));
Brian Carlstromf91c8c32011-09-21 17:30:34 -070081 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070082 Handle<mirror::Class> c(
83 hs.NewHandle(class_linker->FindClass(soa.Self(), descriptor.c_str(), class_loader)));
Andreas Gampefa4333d2017-02-14 11:10:34 -080084 if (c == nullptr) {
Elliott Hughes844f9a02012-01-24 20:19:58 -080085 ScopedLocalRef<jthrowable> cause(env, env->ExceptionOccurred());
Brian Carlstrom395520e2011-09-25 19:35:00 -070086 env->ExceptionClear();
Mathieu Chartier0795f232016-09-27 18:43:30 -070087 jthrowable cnfe = reinterpret_cast<jthrowable>(
88 env->NewObject(WellKnownClasses::java_lang_ClassNotFoundException,
89 WellKnownClasses::java_lang_ClassNotFoundException_init,
90 javaName,
91 cause.get()));
Ian Rogersc114b5f2014-07-21 08:55:01 -070092 if (cnfe != nullptr) {
93 // Make sure allocation didn't fail with an OOME.
94 env->Throw(cnfe);
95 }
Mathieu Chartierc528dba2013-11-26 12:00:11 -080096 return nullptr;
Brian Carlstrom395520e2011-09-25 19:35:00 -070097 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070098 if (initialize) {
Ian Rogers7b078e82014-09-10 14:44:24 -070099 class_linker->EnsureInitialized(soa.Self(), c, true, true);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700100 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700101 return soa.AddLocalReference<jclass>(c.Get());
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700102}
103
Elliott Hughes0512f022012-03-15 22:10:52 -0700104static jstring Class_getNameNative(JNIEnv* env, jobject javaThis) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700105 ScopedFastNativeObjectAccess soa(env);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700106 StackHandleScope<1> hs(soa.Self());
Mathieu Chartier0795f232016-09-27 18:43:30 -0700107 ObjPtr<mirror::Class> c = DecodeClass(soa, javaThis);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700108 return soa.AddLocalReference<jstring>(mirror::Class::ComputeName(hs.NewHandle(c)));
Elliott Hughes6bdc3b22011-09-16 19:24:10 -0700109}
110
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700111static jobjectArray Class_getProxyInterfaces(JNIEnv* env, jobject javaThis) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700112 ScopedFastNativeObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700113 ObjPtr<mirror::Class> c = DecodeClass(soa, javaThis);
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000114 return soa.AddLocalReference<jobjectArray>(c->GetInterfaces()->Clone(soa.Self()));
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700115}
116
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700117static mirror::ObjectArray<mirror::Field>* GetDeclaredFields(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700118 Thread* self, ObjPtr<mirror::Class> klass, bool public_only, bool force_resolve)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700119 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700120 StackHandleScope<1> hs(self);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700121 IterationRange<StrideIterator<ArtField>> ifields = klass->GetIFields();
122 IterationRange<StrideIterator<ArtField>> sfields = klass->GetSFields();
123 size_t array_size = klass->NumInstanceFields() + klass->NumStaticFields();
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700124 if (public_only) {
125 // Lets go subtract all the non public fields.
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700126 for (ArtField& field : ifields) {
127 if (!field.IsPublic()) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700128 --array_size;
129 }
130 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700131 for (ArtField& field : sfields) {
132 if (!field.IsPublic()) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700133 --array_size;
134 }
135 }
136 }
Mathieu Chartierc7853442015-03-27 14:35:38 -0700137 size_t array_idx = 0;
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700138 auto object_array = hs.NewHandle(mirror::ObjectArray<mirror::Field>::Alloc(
139 self, mirror::Field::ArrayClass(), array_size));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800140 if (object_array == nullptr) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700141 return nullptr;
142 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700143 for (ArtField& field : ifields) {
144 if (!public_only || field.IsPublic()) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700145 auto* reflect_field = mirror::Field::CreateFromArtField<kRuntimePointerSize>(self,
146 &field,
147 force_resolve);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700148 if (reflect_field == nullptr) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700149 if (kIsDebugBuild) {
150 self->AssertPendingException();
151 }
152 // Maybe null due to OOME or type resolving exception.
153 return nullptr;
154 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700155 object_array->SetWithoutChecks<false>(array_idx++, reflect_field);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700156 }
157 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700158 for (ArtField& field : sfields) {
159 if (!public_only || field.IsPublic()) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700160 auto* reflect_field = mirror::Field::CreateFromArtField<kRuntimePointerSize>(self,
161 &field,
162 force_resolve);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700163 if (reflect_field == nullptr) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700164 if (kIsDebugBuild) {
165 self->AssertPendingException();
166 }
167 return nullptr;
168 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700169 object_array->SetWithoutChecks<false>(array_idx++, reflect_field);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700170 }
171 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700172 DCHECK_EQ(array_idx, array_size);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700173 return object_array.Get();
174}
175
176static jobjectArray Class_getDeclaredFieldsUnchecked(JNIEnv* env, jobject javaThis,
177 jboolean publicOnly) {
178 ScopedFastNativeObjectAccess soa(env);
179 return soa.AddLocalReference<jobjectArray>(
180 GetDeclaredFields(soa.Self(), DecodeClass(soa, javaThis), publicOnly != JNI_FALSE, false));
181}
182
183static jobjectArray Class_getDeclaredFields(JNIEnv* env, jobject javaThis) {
184 ScopedFastNativeObjectAccess soa(env);
185 return soa.AddLocalReference<jobjectArray>(
186 GetDeclaredFields(soa.Self(), DecodeClass(soa, javaThis), false, true));
187}
188
189static jobjectArray Class_getPublicDeclaredFields(JNIEnv* env, jobject javaThis) {
190 ScopedFastNativeObjectAccess soa(env);
191 return soa.AddLocalReference<jobjectArray>(
192 GetDeclaredFields(soa.Self(), DecodeClass(soa, javaThis), true, true));
193}
194
195// Performs a binary search through an array of fields, TODO: Is this fast enough if we don't use
196// the dex cache for lookups? I think CompareModifiedUtf8ToUtf16AsCodePointValues should be fairly
197// fast.
Mathieu Chartier0795f232016-09-27 18:43:30 -0700198ALWAYS_INLINE static inline ArtField* FindFieldByName(ObjPtr<mirror::String> name,
199 LengthPrefixedArray<ArtField>* fields)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700200 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700201 if (fields == nullptr) {
202 return nullptr;
203 }
Mathieu Chartierc7853442015-03-27 14:35:38 -0700204 size_t low = 0;
Vladimir Marko35831e82015-09-11 11:59:18 +0100205 size_t high = fields->size();
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700206 const bool is_name_compressed = name->IsCompressed();
207 const uint16_t* const data = (is_name_compressed) ? nullptr : name->GetValue();
208 const uint8_t* const data_compressed = (is_name_compressed) ? name->GetValueCompressed()
209 : nullptr;
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700210 const size_t length = name->GetLength();
211 while (low < high) {
212 auto mid = (low + high) / 2;
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700213 ArtField& field = fields->At(mid);
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700214 int result = 0;
215 if (is_name_compressed) {
216 size_t field_length = strlen(field.GetName());
217 size_t min_size = (length < field_length) ? length : field_length;
218 result = memcmp(field.GetName(), data_compressed, min_size);
219 if (result == 0) {
220 result = field_length - length;
221 }
222 } else {
223 result = CompareModifiedUtf8ToUtf16AsCodePointValues(field.GetName(), data, length);
224 }
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700225 // Alternate approach, only a few % faster at the cost of more allocations.
226 // int result = field->GetStringName(self, true)->CompareTo(name);
227 if (result < 0) {
228 low = mid + 1;
229 } else if (result > 0) {
230 high = mid;
231 } else {
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700232 return &field;
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700233 }
234 }
235 if (kIsDebugBuild) {
Vladimir Markocf36d492015-08-12 19:27:26 +0100236 for (ArtField& field : MakeIterationRangeFromLengthPrefixedArray(fields)) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700237 CHECK_NE(field.GetName(), name->ToModifiedUtf8());
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700238 }
239 }
240 return nullptr;
241}
242
Mathieu Chartier0795f232016-09-27 18:43:30 -0700243ALWAYS_INLINE static inline mirror::Field* GetDeclaredField(Thread* self,
244 ObjPtr<mirror::Class> c,
245 ObjPtr<mirror::String> name)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700246 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier0795f232016-09-27 18:43:30 -0700247 ArtField* art_field = FindFieldByName(name, c->GetIFieldsPtr());
Mathieu Chartierc7853442015-03-27 14:35:38 -0700248 if (art_field != nullptr) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700249 return mirror::Field::CreateFromArtField<kRuntimePointerSize>(self, art_field, true);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700250 }
Mathieu Chartier0795f232016-09-27 18:43:30 -0700251 art_field = FindFieldByName(name, c->GetSFieldsPtr());
Mathieu Chartierc7853442015-03-27 14:35:38 -0700252 if (art_field != nullptr) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700253 return mirror::Field::CreateFromArtField<kRuntimePointerSize>(self, art_field, true);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700254 }
255 return nullptr;
256}
257
Alex Light72f90752015-10-01 13:35:09 -0700258static mirror::Field* GetPublicFieldRecursive(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700259 Thread* self, ObjPtr<mirror::Class> clazz, ObjPtr<mirror::String> name)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700260 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Light72f90752015-10-01 13:35:09 -0700261 DCHECK(clazz != nullptr);
262 DCHECK(name != nullptr);
263 DCHECK(self != nullptr);
264
Nicolas Geoffraya96f3162016-05-03 14:04:02 +0100265 StackHandleScope<2> hs(self);
Alex Light72f90752015-10-01 13:35:09 -0700266 MutableHandle<mirror::Class> h_clazz(hs.NewHandle(clazz));
Nicolas Geoffraya96f3162016-05-03 14:04:02 +0100267 Handle<mirror::String> h_name(hs.NewHandle(name));
Alex Light72f90752015-10-01 13:35:09 -0700268
269 // We search the current class, its direct interfaces then its superclass.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800270 while (h_clazz != nullptr) {
Nicolas Geoffraya96f3162016-05-03 14:04:02 +0100271 mirror::Field* result = GetDeclaredField(self, h_clazz.Get(), h_name.Get());
Alex Light72f90752015-10-01 13:35:09 -0700272 if ((result != nullptr) && (result->GetAccessFlags() & kAccPublic)) {
273 return result;
274 } else if (UNLIKELY(self->IsExceptionPending())) {
275 // Something went wrong. Bail out.
276 return nullptr;
277 }
278
279 uint32_t num_direct_interfaces = h_clazz->NumDirectInterfaces();
280 for (uint32_t i = 0; i < num_direct_interfaces; i++) {
Vladimir Marko19a4d372016-12-08 14:41:46 +0000281 ObjPtr<mirror::Class> iface = mirror::Class::ResolveDirectInterface(self, h_clazz, i);
Alex Light72f90752015-10-01 13:35:09 -0700282 if (UNLIKELY(iface == nullptr)) {
283 self->AssertPendingException();
284 return nullptr;
285 }
Nicolas Geoffraya96f3162016-05-03 14:04:02 +0100286 result = GetPublicFieldRecursive(self, iface, h_name.Get());
Alex Light72f90752015-10-01 13:35:09 -0700287 if (result != nullptr) {
288 DCHECK(result->GetAccessFlags() & kAccPublic);
289 return result;
290 } else if (UNLIKELY(self->IsExceptionPending())) {
291 // Something went wrong. Bail out.
292 return nullptr;
293 }
294 }
295
296 // We don't try the superclass if we are an interface.
297 if (h_clazz->IsInterface()) {
298 break;
299 }
300
301 // Get the next class.
302 h_clazz.Assign(h_clazz->GetSuperClass());
303 }
304 return nullptr;
305}
306
307static jobject Class_getPublicFieldRecursive(JNIEnv* env, jobject javaThis, jstring name) {
308 ScopedFastNativeObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700309 auto name_string = soa.Decode<mirror::String>(name);
Alex Light72f90752015-10-01 13:35:09 -0700310 if (UNLIKELY(name_string == nullptr)) {
311 ThrowNullPointerException("name == null");
312 return nullptr;
313 }
314 return soa.AddLocalReference<jobject>(
315 GetPublicFieldRecursive(soa.Self(), DecodeClass(soa, javaThis), name_string));
316}
317
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700318static jobject Class_getDeclaredField(JNIEnv* env, jobject javaThis, jstring name) {
319 ScopedFastNativeObjectAccess soa(env);
Nicolas Geoffray9d074142016-09-30 09:51:32 +0100320 StackHandleScope<3> hs(soa.Self());
321 Handle<mirror::String> h_string = hs.NewHandle(soa.Decode<mirror::String>(name));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800322 if (h_string == nullptr) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700323 ThrowNullPointerException("name == null");
324 return nullptr;
325 }
Nicolas Geoffray9d074142016-09-30 09:51:32 +0100326 Handle<mirror::Class> h_klass = hs.NewHandle(DecodeClass(soa, javaThis));
327 Handle<mirror::Field> result =
328 hs.NewHandle(GetDeclaredField(soa.Self(), h_klass.Get(), h_string.Get()));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800329 if (result == nullptr) {
Nicolas Geoffray9d074142016-09-30 09:51:32 +0100330 std::string name_str = h_string->ToModifiedUtf8();
331 if (name_str == "value" && h_klass->IsStringClass()) {
Nicolas Geoffray7ea4c092016-01-07 10:55:14 +0000332 // We log the error for this specific case, as the user might just swallow the exception.
333 // This helps diagnose crashes when applications rely on the String#value field being
334 // there.
Nicolas Geoffray0e7fa6b2016-01-13 15:28:58 +0000335 // Also print on the error stream to test it through run-test.
336 std::string message("The String#value field is not present on Android versions >= 6.0");
337 LOG(ERROR) << message;
338 std::cerr << message << std::endl;
Nicolas Geoffray7ea4c092016-01-07 10:55:14 +0000339 }
Mathieu Chartierca239af2015-03-29 18:27:50 -0700340 // We may have a pending exception if we failed to resolve.
341 if (!soa.Self()->IsExceptionPending()) {
Nicolas Geoffray9d074142016-09-30 09:51:32 +0100342 ThrowNoSuchFieldException(h_klass.Get(), name_str.c_str());
Mathieu Chartierca239af2015-03-29 18:27:50 -0700343 }
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700344 return nullptr;
345 }
Nicolas Geoffray9d074142016-09-30 09:51:32 +0100346 return soa.AddLocalReference<jobject>(result.Get());
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700347}
348
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700349static jobject Class_getDeclaredConstructorInternal(
350 JNIEnv* env, jobject javaThis, jobjectArray args) {
351 ScopedFastNativeObjectAccess soa(env);
Andreas Gampe542451c2016-07-26 09:02:02 -0700352 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampee01e3642016-07-25 13:06:04 -0700353 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartier0795f232016-09-27 18:43:30 -0700354 ObjPtr<mirror::Constructor> result =
355 mirror::Class::GetDeclaredConstructorInternal<kRuntimePointerSize, false>(
Andreas Gampe6039e562016-04-05 18:18:43 -0700356 soa.Self(),
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700357 DecodeClass(soa, javaThis),
358 soa.Decode<mirror::ObjectArray<mirror::Class>>(args));
Andreas Gampe6039e562016-04-05 18:18:43 -0700359 return soa.AddLocalReference<jobject>(result);
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700360}
361
Mathieu Chartiere401d142015-04-22 13:56:20 -0700362static ALWAYS_INLINE inline bool MethodMatchesConstructor(ArtMethod* m, bool public_only)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700363 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700364 DCHECK(m != nullptr);
365 return (!public_only || m->IsPublic()) && !m->IsStatic() && m->IsConstructor();
366}
367
368static jobjectArray Class_getDeclaredConstructorsInternal(
369 JNIEnv* env, jobject javaThis, jboolean publicOnly) {
370 ScopedFastNativeObjectAccess soa(env);
Mathieu Chartier05b72262015-06-17 16:11:12 -0700371 StackHandleScope<2> hs(soa.Self());
372 Handle<mirror::Class> h_klass = hs.NewHandle(DecodeClass(soa, javaThis));
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700373 size_t constructor_count = 0;
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700374 // Two pass approach for speed.
Andreas Gampe542451c2016-07-26 09:02:02 -0700375 for (auto& m : h_klass->GetDirectMethods(kRuntimePointerSize)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700376 constructor_count += MethodMatchesConstructor(&m, publicOnly != JNI_FALSE) ? 1u : 0u;
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700377 }
378 auto h_constructors = hs.NewHandle(mirror::ObjectArray<mirror::Constructor>::Alloc(
379 soa.Self(), mirror::Constructor::ArrayClass(), constructor_count));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800380 if (UNLIKELY(h_constructors == nullptr)) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700381 soa.Self()->AssertPendingException();
382 return nullptr;
383 }
384 constructor_count = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700385 for (auto& m : h_klass->GetDirectMethods(kRuntimePointerSize)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700386 if (MethodMatchesConstructor(&m, publicOnly != JNI_FALSE)) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700387 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampee01e3642016-07-25 13:06:04 -0700388 DCHECK(!Runtime::Current()->IsActiveTransaction());
Andreas Gampe542451c2016-07-26 09:02:02 -0700389 auto* constructor = mirror::Constructor::CreateFromArtMethod<kRuntimePointerSize, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -0700390 soa.Self(), &m);
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700391 if (UNLIKELY(constructor == nullptr)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700392 soa.Self()->AssertPendingOOMException();
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700393 return nullptr;
394 }
395 h_constructors->SetWithoutChecks<false>(constructor_count++, constructor);
396 }
397 }
398 return soa.AddLocalReference<jobjectArray>(h_constructors.Get());
399}
400
401static jobject Class_getDeclaredMethodInternal(JNIEnv* env, jobject javaThis,
402 jobject name, jobjectArray args) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700403 ScopedFastNativeObjectAccess soa(env);
Andreas Gampe542451c2016-07-26 09:02:02 -0700404 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampee01e3642016-07-25 13:06:04 -0700405 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700406 ObjPtr<mirror::Method> result =
407 mirror::Class::GetDeclaredMethodInternal<kRuntimePointerSize, false>(
408 soa.Self(),
409 DecodeClass(soa, javaThis),
410 soa.Decode<mirror::String>(name),
411 soa.Decode<mirror::ObjectArray<mirror::Class>>(args));
Andreas Gampebc4d2182016-02-22 10:03:12 -0800412 return soa.AddLocalReference<jobject>(result);
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700413}
414
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700415static jobjectArray Class_getDeclaredMethodsUnchecked(JNIEnv* env, jobject javaThis,
416 jboolean publicOnly) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700417 ScopedFastNativeObjectAccess soa(env);
Mathieu Chartier281e5002015-06-11 16:21:42 -0700418 StackHandleScope<2> hs(soa.Self());
Mathieu Chartier05b72262015-06-17 16:11:12 -0700419 Handle<mirror::Class> klass = hs.NewHandle(DecodeClass(soa, javaThis));
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700420 size_t num_methods = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700421 for (auto& m : klass->GetDeclaredMethods(kRuntimePointerSize)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700422 auto modifiers = m.GetAccessFlags();
Alex Lighte64300b2015-12-15 15:02:47 -0800423 // Add non-constructor declared methods.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700424 if ((publicOnly == JNI_FALSE || (modifiers & kAccPublic) != 0) &&
425 (modifiers & kAccConstructor) == 0) {
426 ++num_methods;
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700427 }
428 }
429 auto ret = hs.NewHandle(mirror::ObjectArray<mirror::Method>::Alloc(
430 soa.Self(), mirror::Method::ArrayClass(), num_methods));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800431 if (ret == nullptr) {
Vladimir Markoa35510d2017-01-16 22:42:09 +0000432 soa.Self()->AssertPendingOOMException();
433 return nullptr;
434 }
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700435 num_methods = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700436 for (auto& m : klass->GetDeclaredMethods(kRuntimePointerSize)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700437 auto modifiers = m.GetAccessFlags();
438 if ((publicOnly == JNI_FALSE || (modifiers & kAccPublic) != 0) &&
Mathieu Chartiere401d142015-04-22 13:56:20 -0700439 (modifiers & kAccConstructor) == 0) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700440 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampee01e3642016-07-25 13:06:04 -0700441 DCHECK(!Runtime::Current()->IsActiveTransaction());
Andreas Gampe542451c2016-07-26 09:02:02 -0700442 auto* method =
443 mirror::Method::CreateFromArtMethod<kRuntimePointerSize, false>(soa.Self(), &m);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700444 if (method == nullptr) {
445 soa.Self()->AssertPendingException();
446 return nullptr;
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700447 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700448 ret->SetWithoutChecks<false>(num_methods++, method);
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700449 }
450 }
451 return soa.AddLocalReference<jobjectArray>(ret.Get());
452}
453
Igor Murashkindf187e42016-02-01 11:43:26 -0800454static jobject Class_getDeclaredAnnotation(JNIEnv* env, jobject javaThis, jclass annotationClass) {
Jeff Hao13e748b2015-08-25 20:44:19 +0000455 ScopedFastNativeObjectAccess soa(env);
456 StackHandleScope<2> hs(soa.Self());
457 Handle<mirror::Class> klass(hs.NewHandle(DecodeClass(soa, javaThis)));
Igor Murashkindf187e42016-02-01 11:43:26 -0800458
459 // Handle public contract to throw NPE if the "annotationClass" argument was null.
460 if (UNLIKELY(annotationClass == nullptr)) {
461 ThrowNullPointerException("annotationClass");
462 return nullptr;
463 }
464
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000465 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
Jeff Hao13e748b2015-08-25 20:44:19 +0000466 return nullptr;
467 }
Mathieu Chartier0795f232016-09-27 18:43:30 -0700468 Handle<mirror::Class> annotation_class(hs.NewHandle(soa.Decode<mirror::Class>(annotationClass)));
Jeff Hao13e748b2015-08-25 20:44:19 +0000469 return soa.AddLocalReference<jobject>(
David Sehr9323e6e2016-09-13 08:58:35 -0700470 annotations::GetAnnotationForClass(klass, annotation_class));
Jeff Hao13e748b2015-08-25 20:44:19 +0000471}
472
473static jobjectArray Class_getDeclaredAnnotations(JNIEnv* env, jobject javaThis) {
474 ScopedFastNativeObjectAccess soa(env);
475 StackHandleScope<1> hs(soa.Self());
476 Handle<mirror::Class> klass(hs.NewHandle(DecodeClass(soa, javaThis)));
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000477 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
Jeff Hao13e748b2015-08-25 20:44:19 +0000478 // Return an empty array instead of a null pointer.
Mathieu Chartier0795f232016-09-27 18:43:30 -0700479 ObjPtr<mirror::Class> annotation_array_class =
480 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_annotation_Annotation__array);
Jeff Hao13e748b2015-08-25 20:44:19 +0000481 mirror::ObjectArray<mirror::Object>* empty_array =
Mathieu Chartier0795f232016-09-27 18:43:30 -0700482 mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(),
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700483 annotation_array_class.Ptr(),
Mathieu Chartier0795f232016-09-27 18:43:30 -0700484 0);
Jeff Hao13e748b2015-08-25 20:44:19 +0000485 return soa.AddLocalReference<jobjectArray>(empty_array);
486 }
David Sehr9323e6e2016-09-13 08:58:35 -0700487 return soa.AddLocalReference<jobjectArray>(annotations::GetAnnotationsForClass(klass));
Jeff Hao13e748b2015-08-25 20:44:19 +0000488}
489
Jeff Hao2a5892f2015-08-31 15:00:40 -0700490static jobjectArray Class_getDeclaredClasses(JNIEnv* env, jobject javaThis) {
491 ScopedFastNativeObjectAccess soa(env);
492 StackHandleScope<1> hs(soa.Self());
493 Handle<mirror::Class> klass(hs.NewHandle(DecodeClass(soa, javaThis)));
494 mirror::ObjectArray<mirror::Class>* classes = nullptr;
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000495 if (!klass->IsProxyClass() && klass->GetDexCache() != nullptr) {
David Sehr9323e6e2016-09-13 08:58:35 -0700496 classes = annotations::GetDeclaredClasses(klass);
Jeff Hao2a5892f2015-08-31 15:00:40 -0700497 }
498 if (classes == nullptr) {
499 // Return an empty array instead of a null pointer.
Mathieu Chartier6cfc2c02015-10-12 15:06:16 -0700500 if (soa.Self()->IsExceptionPending()) {
501 // Pending exception from GetDeclaredClasses.
502 return nullptr;
503 }
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700504 ObjPtr<mirror::Class> class_class = mirror::Class::GetJavaLangClass();
505 ObjPtr<mirror::Class> class_array_class =
Jeff Hao2a5892f2015-08-31 15:00:40 -0700506 Runtime::Current()->GetClassLinker()->FindArrayClass(soa.Self(), &class_class);
507 if (class_array_class == nullptr) {
508 return nullptr;
509 }
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700510 ObjPtr<mirror::ObjectArray<mirror::Class>> empty_array =
Jeff Hao2a5892f2015-08-31 15:00:40 -0700511 mirror::ObjectArray<mirror::Class>::Alloc(soa.Self(), class_array_class, 0);
512 return soa.AddLocalReference<jobjectArray>(empty_array);
513 }
514 return soa.AddLocalReference<jobjectArray>(classes);
515}
516
517static jclass Class_getEnclosingClass(JNIEnv* env, jobject javaThis) {
518 ScopedFastNativeObjectAccess soa(env);
519 StackHandleScope<1> hs(soa.Self());
520 Handle<mirror::Class> klass(hs.NewHandle(DecodeClass(soa, javaThis)));
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000521 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
Jeff Hao2a5892f2015-08-31 15:00:40 -0700522 return nullptr;
523 }
David Sehr9323e6e2016-09-13 08:58:35 -0700524 return soa.AddLocalReference<jclass>(annotations::GetEnclosingClass(klass));
Jeff Hao2a5892f2015-08-31 15:00:40 -0700525}
526
527static jobject Class_getEnclosingConstructorNative(JNIEnv* env, jobject javaThis) {
528 ScopedFastNativeObjectAccess soa(env);
529 StackHandleScope<1> hs(soa.Self());
530 Handle<mirror::Class> klass(hs.NewHandle(DecodeClass(soa, javaThis)));
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000531 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
Jeff Hao2a5892f2015-08-31 15:00:40 -0700532 return nullptr;
533 }
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700534 ObjPtr<mirror::Object> method = annotations::GetEnclosingMethod(klass);
Jeff Hao2a5892f2015-08-31 15:00:40 -0700535 if (method != nullptr) {
Mathieu Chartier0795f232016-09-27 18:43:30 -0700536 if (soa.Decode<mirror::Class>(WellKnownClasses::java_lang_reflect_Constructor) ==
537 method->GetClass()) {
Jeff Hao2a5892f2015-08-31 15:00:40 -0700538 return soa.AddLocalReference<jobject>(method);
539 }
540 }
541 return nullptr;
542}
543
544static jobject Class_getEnclosingMethodNative(JNIEnv* env, jobject javaThis) {
545 ScopedFastNativeObjectAccess soa(env);
546 StackHandleScope<1> hs(soa.Self());
547 Handle<mirror::Class> klass(hs.NewHandle(DecodeClass(soa, javaThis)));
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000548 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
Jeff Hao2a5892f2015-08-31 15:00:40 -0700549 return nullptr;
550 }
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700551 ObjPtr<mirror::Object> method = annotations::GetEnclosingMethod(klass);
Jeff Hao2a5892f2015-08-31 15:00:40 -0700552 if (method != nullptr) {
Mathieu Chartier0795f232016-09-27 18:43:30 -0700553 if (soa.Decode<mirror::Class>(WellKnownClasses::java_lang_reflect_Method) ==
554 method->GetClass()) {
Jeff Hao2a5892f2015-08-31 15:00:40 -0700555 return soa.AddLocalReference<jobject>(method);
556 }
557 }
558 return nullptr;
559}
560
561static jint Class_getInnerClassFlags(JNIEnv* env, jobject javaThis, jint defaultValue) {
562 ScopedFastNativeObjectAccess soa(env);
563 StackHandleScope<1> hs(soa.Self());
564 Handle<mirror::Class> klass(hs.NewHandle(DecodeClass(soa, javaThis)));
Andreas Gampe715fdc22016-04-18 17:07:30 -0700565 return mirror::Class::GetInnerClassFlags(klass, defaultValue);
Jeff Hao2a5892f2015-08-31 15:00:40 -0700566}
567
568static jstring Class_getInnerClassName(JNIEnv* env, jobject javaThis) {
569 ScopedFastNativeObjectAccess soa(env);
570 StackHandleScope<1> hs(soa.Self());
571 Handle<mirror::Class> klass(hs.NewHandle(DecodeClass(soa, javaThis)));
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000572 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
Jeff Hao2a5892f2015-08-31 15:00:40 -0700573 return nullptr;
574 }
575 mirror::String* class_name = nullptr;
David Sehr9323e6e2016-09-13 08:58:35 -0700576 if (!annotations::GetInnerClass(klass, &class_name)) {
Jeff Hao2a5892f2015-08-31 15:00:40 -0700577 return nullptr;
578 }
579 return soa.AddLocalReference<jstring>(class_name);
580}
581
Jeff Hao1133db72016-04-04 19:50:14 -0700582static jobjectArray Class_getSignatureAnnotation(JNIEnv* env, jobject javaThis) {
583 ScopedFastNativeObjectAccess soa(env);
584 StackHandleScope<1> hs(soa.Self());
585 Handle<mirror::Class> klass(hs.NewHandle(DecodeClass(soa, javaThis)));
586 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
587 return nullptr;
588 }
589 return soa.AddLocalReference<jobjectArray>(
David Sehr9323e6e2016-09-13 08:58:35 -0700590 annotations::GetSignatureAnnotationForClass(klass));
Jeff Hao1133db72016-04-04 19:50:14 -0700591}
592
Jeff Hao2a5892f2015-08-31 15:00:40 -0700593static jboolean Class_isAnonymousClass(JNIEnv* env, jobject javaThis) {
594 ScopedFastNativeObjectAccess soa(env);
595 StackHandleScope<1> hs(soa.Self());
596 Handle<mirror::Class> klass(hs.NewHandle(DecodeClass(soa, javaThis)));
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000597 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
Jeff Hao2a5892f2015-08-31 15:00:40 -0700598 return false;
599 }
600 mirror::String* class_name = nullptr;
David Sehr9323e6e2016-09-13 08:58:35 -0700601 if (!annotations::GetInnerClass(klass, &class_name)) {
Jeff Hao2a5892f2015-08-31 15:00:40 -0700602 return false;
603 }
604 return class_name == nullptr;
605}
606
Jeff Hao13e748b2015-08-25 20:44:19 +0000607static jboolean Class_isDeclaredAnnotationPresent(JNIEnv* env, jobject javaThis,
608 jclass annotationType) {
609 ScopedFastNativeObjectAccess soa(env);
610 StackHandleScope<2> hs(soa.Self());
611 Handle<mirror::Class> klass(hs.NewHandle(DecodeClass(soa, javaThis)));
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000612 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
Jeff Hao13e748b2015-08-25 20:44:19 +0000613 return false;
614 }
Mathieu Chartier0795f232016-09-27 18:43:30 -0700615 Handle<mirror::Class> annotation_class(hs.NewHandle(soa.Decode<mirror::Class>(annotationType)));
David Sehr9323e6e2016-09-13 08:58:35 -0700616 return annotations::IsClassAnnotationPresent(klass, annotation_class);
Jeff Hao13e748b2015-08-25 20:44:19 +0000617}
618
Jeff Hao2a5892f2015-08-31 15:00:40 -0700619static jclass Class_getDeclaringClass(JNIEnv* env, jobject javaThis) {
620 ScopedFastNativeObjectAccess soa(env);
621 StackHandleScope<1> hs(soa.Self());
622 Handle<mirror::Class> klass(hs.NewHandle(DecodeClass(soa, javaThis)));
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000623 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
Jeff Hao2a5892f2015-08-31 15:00:40 -0700624 return nullptr;
625 }
626 // Return null for anonymous classes.
627 if (Class_isAnonymousClass(env, javaThis)) {
628 return nullptr;
629 }
David Sehr9323e6e2016-09-13 08:58:35 -0700630 return soa.AddLocalReference<jclass>(annotations::GetDeclaringClass(klass));
Jeff Hao2a5892f2015-08-31 15:00:40 -0700631}
632
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700633static jobject Class_newInstance(JNIEnv* env, jobject javaThis) {
634 ScopedFastNativeObjectAccess soa(env);
635 StackHandleScope<4> hs(soa.Self());
Mathieu Chartier05b72262015-06-17 16:11:12 -0700636 Handle<mirror::Class> klass = hs.NewHandle(DecodeClass(soa, javaThis));
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700637 if (UNLIKELY(klass->GetPrimitiveType() != 0 || klass->IsInterface() || klass->IsArrayClass() ||
638 klass->IsAbstract())) {
639 soa.Self()->ThrowNewExceptionF("Ljava/lang/InstantiationException;",
David Sehr709b0702016-10-13 09:12:37 -0700640 "%s cannot be instantiated",
641 klass->PrettyClass().c_str());
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700642 return nullptr;
643 }
644 auto caller = hs.NewHandle<mirror::Class>(nullptr);
645 // Verify that we can access the class.
646 if (!klass->IsPublic()) {
647 caller.Assign(GetCallingClass(soa.Self(), 1));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800648 if (caller != nullptr && !caller->CanAccess(klass.Get())) {
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700649 soa.Self()->ThrowNewExceptionF(
650 "Ljava/lang/IllegalAccessException;", "%s is not accessible from %s",
David Sehr709b0702016-10-13 09:12:37 -0700651 klass->PrettyClass().c_str(), caller->PrettyClass().c_str());
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700652 return nullptr;
653 }
654 }
655 auto* constructor = klass->GetDeclaredConstructor(
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800656 soa.Self(),
Andreas Gampe6039e562016-04-05 18:18:43 -0700657 ScopedNullHandle<mirror::ObjectArray<mirror::Class>>(),
Andreas Gampe542451c2016-07-26 09:02:02 -0700658 kRuntimePointerSize);
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700659 if (UNLIKELY(constructor == nullptr)) {
660 soa.Self()->ThrowNewExceptionF("Ljava/lang/InstantiationException;",
661 "%s has no zero argument constructor",
David Sehr709b0702016-10-13 09:12:37 -0700662 klass->PrettyClass().c_str());
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700663 return nullptr;
664 }
Jeff Hao69bdcb22015-04-27 20:54:07 -0700665 // Invoke the string allocator to return an empty string for the string class.
666 if (klass->IsStringClass()) {
667 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700668 ObjPtr<mirror::Object> obj = mirror::String::AllocEmptyString<true>(soa.Self(), allocator_type);
Jeff Hao69bdcb22015-04-27 20:54:07 -0700669 if (UNLIKELY(soa.Self()->IsExceptionPending())) {
670 return nullptr;
671 } else {
672 return soa.AddLocalReference<jobject>(obj);
673 }
674 }
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700675 auto receiver = hs.NewHandle(klass->AllocObject(soa.Self()));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800676 if (UNLIKELY(receiver == nullptr)) {
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700677 soa.Self()->AssertPendingOOMException();
678 return nullptr;
679 }
680 // Verify that we can access the constructor.
681 auto* declaring_class = constructor->GetDeclaringClass();
682 if (!constructor->IsPublic()) {
Andreas Gampefa4333d2017-02-14 11:10:34 -0800683 if (caller == nullptr) {
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700684 caller.Assign(GetCallingClass(soa.Self(), 1));
685 }
Andreas Gampefa4333d2017-02-14 11:10:34 -0800686 if (UNLIKELY(caller != nullptr && !VerifyAccess(receiver.Get(),
Mathieu Chartierf8ac97f2016-10-05 15:56:52 -0700687 declaring_class,
688 constructor->GetAccessFlags(),
689 caller.Get()))) {
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700690 soa.Self()->ThrowNewExceptionF(
691 "Ljava/lang/IllegalAccessException;", "%s is not accessible from %s",
David Sehr709b0702016-10-13 09:12:37 -0700692 constructor->PrettyMethod().c_str(), caller->PrettyClass().c_str());
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700693 return nullptr;
694 }
695 }
696 // Ensure that we are initialized.
697 if (UNLIKELY(!declaring_class->IsInitialized())) {
698 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(
699 soa.Self(), hs.NewHandle(declaring_class), true, true)) {
700 soa.Self()->AssertPendingException();
701 return nullptr;
702 }
703 }
704 // Invoke the constructor.
705 JValue result;
706 uint32_t args[1] = { static_cast<uint32_t>(reinterpret_cast<uintptr_t>(receiver.Get())) };
707 constructor->Invoke(soa.Self(), args, sizeof(args), &result, "V");
708 if (UNLIKELY(soa.Self()->IsExceptionPending())) {
709 return nullptr;
710 }
711 // Constructors are ()V methods, so we shouldn't touch the result of InvokeMethod.
712 return soa.AddLocalReference<jobject>(receiver.Get());
713}
714
Elliott Hughesd369bb72011-09-12 14:41:14 -0700715static JNINativeMethod gMethods[] = {
Igor Murashkin3b6f4402017-02-16 16:13:17 -0800716 FAST_NATIVE_METHOD(Class, classForName,
717 "(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;"),
718 FAST_NATIVE_METHOD(Class, getDeclaredAnnotation,
719 "(Ljava/lang/Class;)Ljava/lang/annotation/Annotation;"),
720 FAST_NATIVE_METHOD(Class, getDeclaredAnnotations, "()[Ljava/lang/annotation/Annotation;"),
721 FAST_NATIVE_METHOD(Class, getDeclaredClasses, "()[Ljava/lang/Class;"),
722 FAST_NATIVE_METHOD(Class, getDeclaredConstructorInternal,
723 "([Ljava/lang/Class;)Ljava/lang/reflect/Constructor;"),
724 FAST_NATIVE_METHOD(Class, getDeclaredConstructorsInternal, "(Z)[Ljava/lang/reflect/Constructor;"),
725 FAST_NATIVE_METHOD(Class, getDeclaredField, "(Ljava/lang/String;)Ljava/lang/reflect/Field;"),
726 FAST_NATIVE_METHOD(Class, getPublicFieldRecursive, "(Ljava/lang/String;)Ljava/lang/reflect/Field;"),
727 FAST_NATIVE_METHOD(Class, getDeclaredFields, "()[Ljava/lang/reflect/Field;"),
728 FAST_NATIVE_METHOD(Class, getDeclaredFieldsUnchecked, "(Z)[Ljava/lang/reflect/Field;"),
729 FAST_NATIVE_METHOD(Class, getDeclaredMethodInternal,
730 "(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;"),
731 FAST_NATIVE_METHOD(Class, getDeclaredMethodsUnchecked,
732 "(Z)[Ljava/lang/reflect/Method;"),
733 FAST_NATIVE_METHOD(Class, getDeclaringClass, "()Ljava/lang/Class;"),
734 FAST_NATIVE_METHOD(Class, getEnclosingClass, "()Ljava/lang/Class;"),
735 FAST_NATIVE_METHOD(Class, getEnclosingConstructorNative, "()Ljava/lang/reflect/Constructor;"),
736 FAST_NATIVE_METHOD(Class, getEnclosingMethodNative, "()Ljava/lang/reflect/Method;"),
737 FAST_NATIVE_METHOD(Class, getInnerClassFlags, "(I)I"),
738 FAST_NATIVE_METHOD(Class, getInnerClassName, "()Ljava/lang/String;"),
739 FAST_NATIVE_METHOD(Class, getNameNative, "()Ljava/lang/String;"),
740 FAST_NATIVE_METHOD(Class, getProxyInterfaces, "()[Ljava/lang/Class;"),
741 FAST_NATIVE_METHOD(Class, getPublicDeclaredFields, "()[Ljava/lang/reflect/Field;"),
742 FAST_NATIVE_METHOD(Class, getSignatureAnnotation, "()[Ljava/lang/String;"),
743 FAST_NATIVE_METHOD(Class, isAnonymousClass, "()Z"),
744 FAST_NATIVE_METHOD(Class, isDeclaredAnnotationPresent, "(Ljava/lang/Class;)Z"),
745 FAST_NATIVE_METHOD(Class, newInstance, "()Ljava/lang/Object;"),
Elliott Hughesd369bb72011-09-12 14:41:14 -0700746};
747
Elliott Hughesd369bb72011-09-12 14:41:14 -0700748void register_java_lang_Class(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700749 REGISTER_NATIVE_METHODS("java/lang/Class");
Elliott Hughesd369bb72011-09-12 14:41:14 -0700750}
751
752} // namespace art