blob: 2a5c04d54be0e002bcfa0fb43a0dbbe2f034050a [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)));
84 if (c.Get() == 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));
140 if (object_array.Get() == nullptr) {
141 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.
270 while (h_clazz.Get() != 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++) {
281 mirror::Class *iface = mirror::Class::GetDirectInterface(self, h_clazz, i);
282 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));
322 if (h_string.Get() == 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()));
329 if (result.Get() == nullptr) {
330 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 Chartier1cc62e42016-10-03 18:01:28 -0700357 DecodeClass(soa, javaThis).Ptr(),
358 soa.Decode<mirror::ObjectArray<mirror::Class>>(args).Ptr());
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));
380 if (UNLIKELY(h_constructors.Get() == nullptr)) {
381 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());
Andreas Gampe542451c2016-07-26 09:02:02 -0700406 mirror::Method* result = mirror::Class::GetDeclaredMethodInternal<kRuntimePointerSize, false>(
Andreas Gampebc4d2182016-02-22 10:03:12 -0800407 soa.Self(),
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700408 DecodeClass(soa, javaThis).Ptr(),
409 soa.Decode<mirror::String>(name).Ptr(),
410 soa.Decode<mirror::ObjectArray<mirror::Class>>(args).Ptr());
Andreas Gampebc4d2182016-02-22 10:03:12 -0800411 return soa.AddLocalReference<jobject>(result);
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700412}
413
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700414static jobjectArray Class_getDeclaredMethodsUnchecked(JNIEnv* env, jobject javaThis,
415 jboolean publicOnly) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700416 ScopedFastNativeObjectAccess soa(env);
Mathieu Chartier281e5002015-06-11 16:21:42 -0700417 StackHandleScope<2> hs(soa.Self());
Mathieu Chartier05b72262015-06-17 16:11:12 -0700418 Handle<mirror::Class> klass = hs.NewHandle(DecodeClass(soa, javaThis));
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700419 size_t num_methods = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700420 for (auto& m : klass->GetDeclaredMethods(kRuntimePointerSize)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700421 auto modifiers = m.GetAccessFlags();
Alex Lighte64300b2015-12-15 15:02:47 -0800422 // Add non-constructor declared methods.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700423 if ((publicOnly == JNI_FALSE || (modifiers & kAccPublic) != 0) &&
424 (modifiers & kAccConstructor) == 0) {
425 ++num_methods;
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700426 }
427 }
428 auto ret = hs.NewHandle(mirror::ObjectArray<mirror::Method>::Alloc(
429 soa.Self(), mirror::Method::ArrayClass(), num_methods));
430 num_methods = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700431 for (auto& m : klass->GetDeclaredMethods(kRuntimePointerSize)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700432 auto modifiers = m.GetAccessFlags();
433 if ((publicOnly == JNI_FALSE || (modifiers & kAccPublic) != 0) &&
Mathieu Chartiere401d142015-04-22 13:56:20 -0700434 (modifiers & kAccConstructor) == 0) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700435 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampee01e3642016-07-25 13:06:04 -0700436 DCHECK(!Runtime::Current()->IsActiveTransaction());
Andreas Gampe542451c2016-07-26 09:02:02 -0700437 auto* method =
438 mirror::Method::CreateFromArtMethod<kRuntimePointerSize, false>(soa.Self(), &m);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700439 if (method == nullptr) {
440 soa.Self()->AssertPendingException();
441 return nullptr;
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700442 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700443 ret->SetWithoutChecks<false>(num_methods++, method);
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700444 }
445 }
446 return soa.AddLocalReference<jobjectArray>(ret.Get());
447}
448
Igor Murashkindf187e42016-02-01 11:43:26 -0800449static jobject Class_getDeclaredAnnotation(JNIEnv* env, jobject javaThis, jclass annotationClass) {
Jeff Hao13e748b2015-08-25 20:44:19 +0000450 ScopedFastNativeObjectAccess soa(env);
451 StackHandleScope<2> hs(soa.Self());
452 Handle<mirror::Class> klass(hs.NewHandle(DecodeClass(soa, javaThis)));
Igor Murashkindf187e42016-02-01 11:43:26 -0800453
454 // Handle public contract to throw NPE if the "annotationClass" argument was null.
455 if (UNLIKELY(annotationClass == nullptr)) {
456 ThrowNullPointerException("annotationClass");
457 return nullptr;
458 }
459
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000460 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
Jeff Hao13e748b2015-08-25 20:44:19 +0000461 return nullptr;
462 }
Mathieu Chartier0795f232016-09-27 18:43:30 -0700463 Handle<mirror::Class> annotation_class(hs.NewHandle(soa.Decode<mirror::Class>(annotationClass)));
Jeff Hao13e748b2015-08-25 20:44:19 +0000464 return soa.AddLocalReference<jobject>(
David Sehr9323e6e2016-09-13 08:58:35 -0700465 annotations::GetAnnotationForClass(klass, annotation_class));
Jeff Hao13e748b2015-08-25 20:44:19 +0000466}
467
468static jobjectArray Class_getDeclaredAnnotations(JNIEnv* env, jobject javaThis) {
469 ScopedFastNativeObjectAccess soa(env);
470 StackHandleScope<1> hs(soa.Self());
471 Handle<mirror::Class> klass(hs.NewHandle(DecodeClass(soa, javaThis)));
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000472 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
Jeff Hao13e748b2015-08-25 20:44:19 +0000473 // Return an empty array instead of a null pointer.
Mathieu Chartier0795f232016-09-27 18:43:30 -0700474 ObjPtr<mirror::Class> annotation_array_class =
475 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_annotation_Annotation__array);
Jeff Hao13e748b2015-08-25 20:44:19 +0000476 mirror::ObjectArray<mirror::Object>* empty_array =
Mathieu Chartier0795f232016-09-27 18:43:30 -0700477 mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(),
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700478 annotation_array_class.Ptr(),
Mathieu Chartier0795f232016-09-27 18:43:30 -0700479 0);
Jeff Hao13e748b2015-08-25 20:44:19 +0000480 return soa.AddLocalReference<jobjectArray>(empty_array);
481 }
David Sehr9323e6e2016-09-13 08:58:35 -0700482 return soa.AddLocalReference<jobjectArray>(annotations::GetAnnotationsForClass(klass));
Jeff Hao13e748b2015-08-25 20:44:19 +0000483}
484
Jeff Hao2a5892f2015-08-31 15:00:40 -0700485static jobjectArray Class_getDeclaredClasses(JNIEnv* env, jobject javaThis) {
486 ScopedFastNativeObjectAccess soa(env);
487 StackHandleScope<1> hs(soa.Self());
488 Handle<mirror::Class> klass(hs.NewHandle(DecodeClass(soa, javaThis)));
489 mirror::ObjectArray<mirror::Class>* classes = nullptr;
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000490 if (!klass->IsProxyClass() && klass->GetDexCache() != nullptr) {
David Sehr9323e6e2016-09-13 08:58:35 -0700491 classes = annotations::GetDeclaredClasses(klass);
Jeff Hao2a5892f2015-08-31 15:00:40 -0700492 }
493 if (classes == nullptr) {
494 // Return an empty array instead of a null pointer.
Mathieu Chartier6cfc2c02015-10-12 15:06:16 -0700495 if (soa.Self()->IsExceptionPending()) {
496 // Pending exception from GetDeclaredClasses.
497 return nullptr;
498 }
Jeff Hao2a5892f2015-08-31 15:00:40 -0700499 mirror::Class* class_class = mirror::Class::GetJavaLangClass();
500 mirror::Class* class_array_class =
501 Runtime::Current()->GetClassLinker()->FindArrayClass(soa.Self(), &class_class);
502 if (class_array_class == nullptr) {
503 return nullptr;
504 }
505 mirror::ObjectArray<mirror::Class>* empty_array =
506 mirror::ObjectArray<mirror::Class>::Alloc(soa.Self(), class_array_class, 0);
507 return soa.AddLocalReference<jobjectArray>(empty_array);
508 }
509 return soa.AddLocalReference<jobjectArray>(classes);
510}
511
512static jclass Class_getEnclosingClass(JNIEnv* env, jobject javaThis) {
513 ScopedFastNativeObjectAccess soa(env);
514 StackHandleScope<1> hs(soa.Self());
515 Handle<mirror::Class> klass(hs.NewHandle(DecodeClass(soa, javaThis)));
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000516 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
Jeff Hao2a5892f2015-08-31 15:00:40 -0700517 return nullptr;
518 }
David Sehr9323e6e2016-09-13 08:58:35 -0700519 return soa.AddLocalReference<jclass>(annotations::GetEnclosingClass(klass));
Jeff Hao2a5892f2015-08-31 15:00:40 -0700520}
521
522static jobject Class_getEnclosingConstructorNative(JNIEnv* env, jobject javaThis) {
523 ScopedFastNativeObjectAccess soa(env);
524 StackHandleScope<1> hs(soa.Self());
525 Handle<mirror::Class> klass(hs.NewHandle(DecodeClass(soa, javaThis)));
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000526 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
Jeff Hao2a5892f2015-08-31 15:00:40 -0700527 return nullptr;
528 }
David Sehr9323e6e2016-09-13 08:58:35 -0700529 mirror::Object* method = annotations::GetEnclosingMethod(klass);
Jeff Hao2a5892f2015-08-31 15:00:40 -0700530 if (method != nullptr) {
Mathieu Chartier0795f232016-09-27 18:43:30 -0700531 if (soa.Decode<mirror::Class>(WellKnownClasses::java_lang_reflect_Constructor) ==
532 method->GetClass()) {
Jeff Hao2a5892f2015-08-31 15:00:40 -0700533 return soa.AddLocalReference<jobject>(method);
534 }
535 }
536 return nullptr;
537}
538
539static jobject Class_getEnclosingMethodNative(JNIEnv* env, jobject javaThis) {
540 ScopedFastNativeObjectAccess soa(env);
541 StackHandleScope<1> hs(soa.Self());
542 Handle<mirror::Class> klass(hs.NewHandle(DecodeClass(soa, javaThis)));
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000543 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
Jeff Hao2a5892f2015-08-31 15:00:40 -0700544 return nullptr;
545 }
David Sehr9323e6e2016-09-13 08:58:35 -0700546 mirror::Object* method = annotations::GetEnclosingMethod(klass);
Jeff Hao2a5892f2015-08-31 15:00:40 -0700547 if (method != nullptr) {
Mathieu Chartier0795f232016-09-27 18:43:30 -0700548 if (soa.Decode<mirror::Class>(WellKnownClasses::java_lang_reflect_Method) ==
549 method->GetClass()) {
Jeff Hao2a5892f2015-08-31 15:00:40 -0700550 return soa.AddLocalReference<jobject>(method);
551 }
552 }
553 return nullptr;
554}
555
556static jint Class_getInnerClassFlags(JNIEnv* env, jobject javaThis, jint defaultValue) {
557 ScopedFastNativeObjectAccess soa(env);
558 StackHandleScope<1> hs(soa.Self());
559 Handle<mirror::Class> klass(hs.NewHandle(DecodeClass(soa, javaThis)));
Andreas Gampe715fdc22016-04-18 17:07:30 -0700560 return mirror::Class::GetInnerClassFlags(klass, defaultValue);
Jeff Hao2a5892f2015-08-31 15:00:40 -0700561}
562
563static jstring Class_getInnerClassName(JNIEnv* env, jobject javaThis) {
564 ScopedFastNativeObjectAccess soa(env);
565 StackHandleScope<1> hs(soa.Self());
566 Handle<mirror::Class> klass(hs.NewHandle(DecodeClass(soa, javaThis)));
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000567 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
Jeff Hao2a5892f2015-08-31 15:00:40 -0700568 return nullptr;
569 }
570 mirror::String* class_name = nullptr;
David Sehr9323e6e2016-09-13 08:58:35 -0700571 if (!annotations::GetInnerClass(klass, &class_name)) {
Jeff Hao2a5892f2015-08-31 15:00:40 -0700572 return nullptr;
573 }
574 return soa.AddLocalReference<jstring>(class_name);
575}
576
Jeff Hao1133db72016-04-04 19:50:14 -0700577static jobjectArray Class_getSignatureAnnotation(JNIEnv* env, jobject javaThis) {
578 ScopedFastNativeObjectAccess soa(env);
579 StackHandleScope<1> hs(soa.Self());
580 Handle<mirror::Class> klass(hs.NewHandle(DecodeClass(soa, javaThis)));
581 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
582 return nullptr;
583 }
584 return soa.AddLocalReference<jobjectArray>(
David Sehr9323e6e2016-09-13 08:58:35 -0700585 annotations::GetSignatureAnnotationForClass(klass));
Jeff Hao1133db72016-04-04 19:50:14 -0700586}
587
Jeff Hao2a5892f2015-08-31 15:00:40 -0700588static jboolean Class_isAnonymousClass(JNIEnv* env, jobject javaThis) {
589 ScopedFastNativeObjectAccess soa(env);
590 StackHandleScope<1> hs(soa.Self());
591 Handle<mirror::Class> klass(hs.NewHandle(DecodeClass(soa, javaThis)));
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000592 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
Jeff Hao2a5892f2015-08-31 15:00:40 -0700593 return false;
594 }
595 mirror::String* class_name = nullptr;
David Sehr9323e6e2016-09-13 08:58:35 -0700596 if (!annotations::GetInnerClass(klass, &class_name)) {
Jeff Hao2a5892f2015-08-31 15:00:40 -0700597 return false;
598 }
599 return class_name == nullptr;
600}
601
Jeff Hao13e748b2015-08-25 20:44:19 +0000602static jboolean Class_isDeclaredAnnotationPresent(JNIEnv* env, jobject javaThis,
603 jclass annotationType) {
604 ScopedFastNativeObjectAccess soa(env);
605 StackHandleScope<2> hs(soa.Self());
606 Handle<mirror::Class> klass(hs.NewHandle(DecodeClass(soa, javaThis)));
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000607 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
Jeff Hao13e748b2015-08-25 20:44:19 +0000608 return false;
609 }
Mathieu Chartier0795f232016-09-27 18:43:30 -0700610 Handle<mirror::Class> annotation_class(hs.NewHandle(soa.Decode<mirror::Class>(annotationType)));
David Sehr9323e6e2016-09-13 08:58:35 -0700611 return annotations::IsClassAnnotationPresent(klass, annotation_class);
Jeff Hao13e748b2015-08-25 20:44:19 +0000612}
613
Jeff Hao2a5892f2015-08-31 15:00:40 -0700614static jclass Class_getDeclaringClass(JNIEnv* env, jobject javaThis) {
615 ScopedFastNativeObjectAccess soa(env);
616 StackHandleScope<1> hs(soa.Self());
617 Handle<mirror::Class> klass(hs.NewHandle(DecodeClass(soa, javaThis)));
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000618 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
Jeff Hao2a5892f2015-08-31 15:00:40 -0700619 return nullptr;
620 }
621 // Return null for anonymous classes.
622 if (Class_isAnonymousClass(env, javaThis)) {
623 return nullptr;
624 }
David Sehr9323e6e2016-09-13 08:58:35 -0700625 return soa.AddLocalReference<jclass>(annotations::GetDeclaringClass(klass));
Jeff Hao2a5892f2015-08-31 15:00:40 -0700626}
627
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700628static jobject Class_newInstance(JNIEnv* env, jobject javaThis) {
629 ScopedFastNativeObjectAccess soa(env);
630 StackHandleScope<4> hs(soa.Self());
Mathieu Chartier05b72262015-06-17 16:11:12 -0700631 Handle<mirror::Class> klass = hs.NewHandle(DecodeClass(soa, javaThis));
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700632 if (UNLIKELY(klass->GetPrimitiveType() != 0 || klass->IsInterface() || klass->IsArrayClass() ||
633 klass->IsAbstract())) {
634 soa.Self()->ThrowNewExceptionF("Ljava/lang/InstantiationException;",
635 "%s cannot be instantiated", PrettyClass(klass.Get()).c_str());
636 return nullptr;
637 }
638 auto caller = hs.NewHandle<mirror::Class>(nullptr);
639 // Verify that we can access the class.
640 if (!klass->IsPublic()) {
641 caller.Assign(GetCallingClass(soa.Self(), 1));
642 if (caller.Get() != nullptr && !caller->CanAccess(klass.Get())) {
643 soa.Self()->ThrowNewExceptionF(
644 "Ljava/lang/IllegalAccessException;", "%s is not accessible from %s",
645 PrettyClass(klass.Get()).c_str(), PrettyClass(caller.Get()).c_str());
646 return nullptr;
647 }
648 }
649 auto* constructor = klass->GetDeclaredConstructor(
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800650 soa.Self(),
Andreas Gampe6039e562016-04-05 18:18:43 -0700651 ScopedNullHandle<mirror::ObjectArray<mirror::Class>>(),
Andreas Gampe542451c2016-07-26 09:02:02 -0700652 kRuntimePointerSize);
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700653 if (UNLIKELY(constructor == nullptr)) {
654 soa.Self()->ThrowNewExceptionF("Ljava/lang/InstantiationException;",
655 "%s has no zero argument constructor",
656 PrettyClass(klass.Get()).c_str());
657 return nullptr;
658 }
Jeff Hao69bdcb22015-04-27 20:54:07 -0700659 // Invoke the string allocator to return an empty string for the string class.
660 if (klass->IsStringClass()) {
661 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700662 mirror::Object* obj = mirror::String::AllocEmptyString<true>(soa.Self(), allocator_type);
Jeff Hao69bdcb22015-04-27 20:54:07 -0700663 if (UNLIKELY(soa.Self()->IsExceptionPending())) {
664 return nullptr;
665 } else {
666 return soa.AddLocalReference<jobject>(obj);
667 }
668 }
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700669 auto receiver = hs.NewHandle(klass->AllocObject(soa.Self()));
670 if (UNLIKELY(receiver.Get() == nullptr)) {
671 soa.Self()->AssertPendingOOMException();
672 return nullptr;
673 }
674 // Verify that we can access the constructor.
675 auto* declaring_class = constructor->GetDeclaringClass();
676 if (!constructor->IsPublic()) {
677 if (caller.Get() == nullptr) {
678 caller.Assign(GetCallingClass(soa.Self(), 1));
679 }
Mathieu Chartierf8ac97f2016-10-05 15:56:52 -0700680 if (UNLIKELY(caller.Get() != nullptr && !VerifyAccess(receiver.Get(),
681 declaring_class,
682 constructor->GetAccessFlags(),
683 caller.Get()))) {
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700684 soa.Self()->ThrowNewExceptionF(
685 "Ljava/lang/IllegalAccessException;", "%s is not accessible from %s",
686 PrettyMethod(constructor).c_str(), PrettyClass(caller.Get()).c_str());
687 return nullptr;
688 }
689 }
690 // Ensure that we are initialized.
691 if (UNLIKELY(!declaring_class->IsInitialized())) {
692 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(
693 soa.Self(), hs.NewHandle(declaring_class), true, true)) {
694 soa.Self()->AssertPendingException();
695 return nullptr;
696 }
697 }
698 // Invoke the constructor.
699 JValue result;
700 uint32_t args[1] = { static_cast<uint32_t>(reinterpret_cast<uintptr_t>(receiver.Get())) };
701 constructor->Invoke(soa.Self(), args, sizeof(args), &result, "V");
702 if (UNLIKELY(soa.Self()->IsExceptionPending())) {
703 return nullptr;
704 }
705 // Constructors are ()V methods, so we shouldn't touch the result of InvokeMethod.
706 return soa.AddLocalReference<jobject>(receiver.Get());
707}
708
Elliott Hughesd369bb72011-09-12 14:41:14 -0700709static JNINativeMethod gMethods[] = {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700710 NATIVE_METHOD(Class, classForName,
711 "!(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;"),
Jeff Hao13e748b2015-08-25 20:44:19 +0000712 NATIVE_METHOD(Class, getDeclaredAnnotation,
713 "!(Ljava/lang/Class;)Ljava/lang/annotation/Annotation;"),
714 NATIVE_METHOD(Class, getDeclaredAnnotations, "!()[Ljava/lang/annotation/Annotation;"),
Jeff Hao2a5892f2015-08-31 15:00:40 -0700715 NATIVE_METHOD(Class, getDeclaredClasses, "!()[Ljava/lang/Class;"),
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700716 NATIVE_METHOD(Class, getDeclaredConstructorInternal,
717 "!([Ljava/lang/Class;)Ljava/lang/reflect/Constructor;"),
718 NATIVE_METHOD(Class, getDeclaredConstructorsInternal, "!(Z)[Ljava/lang/reflect/Constructor;"),
719 NATIVE_METHOD(Class, getDeclaredField, "!(Ljava/lang/String;)Ljava/lang/reflect/Field;"),
Alex Light72f90752015-10-01 13:35:09 -0700720 NATIVE_METHOD(Class, getPublicFieldRecursive, "!(Ljava/lang/String;)Ljava/lang/reflect/Field;"),
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700721 NATIVE_METHOD(Class, getDeclaredFields, "!()[Ljava/lang/reflect/Field;"),
722 NATIVE_METHOD(Class, getDeclaredFieldsUnchecked, "!(Z)[Ljava/lang/reflect/Field;"),
723 NATIVE_METHOD(Class, getDeclaredMethodInternal,
724 "!(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;"),
725 NATIVE_METHOD(Class, getDeclaredMethodsUnchecked,
Jeff Hao13e748b2015-08-25 20:44:19 +0000726 "!(Z)[Ljava/lang/reflect/Method;"),
Jeff Hao2a5892f2015-08-31 15:00:40 -0700727 NATIVE_METHOD(Class, getDeclaringClass, "!()Ljava/lang/Class;"),
728 NATIVE_METHOD(Class, getEnclosingClass, "!()Ljava/lang/Class;"),
729 NATIVE_METHOD(Class, getEnclosingConstructorNative, "!()Ljava/lang/reflect/Constructor;"),
730 NATIVE_METHOD(Class, getEnclosingMethodNative, "!()Ljava/lang/reflect/Method;"),
731 NATIVE_METHOD(Class, getInnerClassFlags, "!(I)I"),
732 NATIVE_METHOD(Class, getInnerClassName, "!()Ljava/lang/String;"),
Ian Rogers1eb512d2013-10-18 15:42:20 -0700733 NATIVE_METHOD(Class, getNameNative, "!()Ljava/lang/String;"),
734 NATIVE_METHOD(Class, getProxyInterfaces, "!()[Ljava/lang/Class;"),
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700735 NATIVE_METHOD(Class, getPublicDeclaredFields, "!()[Ljava/lang/reflect/Field;"),
Jeff Hao1133db72016-04-04 19:50:14 -0700736 NATIVE_METHOD(Class, getSignatureAnnotation, "!()[Ljava/lang/String;"),
Jeff Hao2a5892f2015-08-31 15:00:40 -0700737 NATIVE_METHOD(Class, isAnonymousClass, "!()Z"),
Jeff Hao13e748b2015-08-25 20:44:19 +0000738 NATIVE_METHOD(Class, isDeclaredAnnotationPresent, "!(Ljava/lang/Class;)Z"),
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700739 NATIVE_METHOD(Class, newInstance, "!()Ljava/lang/Object;"),
Elliott Hughesd369bb72011-09-12 14:41:14 -0700740};
741
Elliott Hughesd369bb72011-09-12 14:41:14 -0700742void register_java_lang_Class(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700743 REGISTER_NATIVE_METHODS("java/lang/Class");
Elliott Hughesd369bb72011-09-12 14:41:14 -0700744}
745
746} // namespace art