1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "java_lang_Class.h"
18 
19 #include <iostream>
20 
21 #include "art_field-inl.h"
22 #include "art_method-alloc-inl.h"
23 #include "base/pointer_size.h"
24 #include "class_linker-inl.h"
25 #include "class_root-inl.h"
26 #include "common_throws.h"
27 #include "compat_framework.h"
28 #include "dex/descriptors_names.h"
29 #include "dex/dex_file-inl.h"
30 #include "dex/dex_file_annotations.h"
31 #include "dex/utf.h"
32 #include "hidden_api.h"
33 #include "jni/jni_internal.h"
34 #include "mirror/class-alloc-inl.h"
35 #include "mirror/class_ext.h"
36 #include "mirror/class-inl.h"
37 #include "mirror/class_loader.h"
38 #include "mirror/field.h"
39 #include "mirror/method.h"
40 #include "mirror/method_handles_lookup.h"
41 #include "mirror/object-inl.h"
42 #include "mirror/object_array-alloc-inl.h"
43 #include "mirror/object_array-inl.h"
44 #include "mirror/proxy.h"
45 #include "mirror/string-alloc-inl.h"
46 #include "mirror/string-inl.h"
47 #include "native_util.h"
48 #include "nativehelper/jni_macros.h"
49 #include "nativehelper/scoped_local_ref.h"
50 #include "nativehelper/scoped_utf_chars.h"
51 #include "nth_caller_visitor.h"
52 #include "obj_ptr-inl.h"
53 #include "reflection.h"
54 #include "reflective_handle_scope-inl.h"
55 #include "scoped_fast_native_object_access-inl.h"
56 #include "scoped_thread_state_change-inl.h"
57 #include "well_known_classes-inl.h"
58 
59 namespace art HIDDEN {
60 
GetHiddenapiAccessContextFunction(Thread * self)61 static std::function<hiddenapi::AccessContext()> GetHiddenapiAccessContextFunction(Thread* self) {
62   return [=]() REQUIRES_SHARED(Locks::mutator_lock_) {
63     return hiddenapi::GetReflectionCallerAccessContext(self);
64   };
65 }
66 
67 // Returns true if the first non-ClassClass caller up the stack should not be
68 // allowed access to `member`.
69 template<typename T>
ShouldDenyAccessToMember(T * member,Thread * self)70 ALWAYS_INLINE static bool ShouldDenyAccessToMember(T* member, Thread* self)
71     REQUIRES_SHARED(Locks::mutator_lock_) {
72   return hiddenapi::ShouldDenyAccessToMember(member,
73                                              GetHiddenapiAccessContextFunction(self),
74                                              hiddenapi::AccessMethod::kReflection);
75 }
76 
DecodeClass(const ScopedFastNativeObjectAccess & soa,jobject java_class)77 ALWAYS_INLINE static inline ObjPtr<mirror::Class> DecodeClass(
78     const ScopedFastNativeObjectAccess& soa, jobject java_class)
79     REQUIRES_SHARED(Locks::mutator_lock_) {
80   ObjPtr<mirror::Class> c = soa.Decode<mirror::Class>(java_class);
81   DCHECK(c != nullptr);
82   DCHECK(c->IsClass());
83   // TODO: we could EnsureInitialized here, rather than on every reflective get/set or invoke .
84   // For now, we conservatively preserve the old dalvik behavior. A quick "IsInitialized" check
85   // every time probably doesn't make much difference to reflection performance anyway.
86   return c;
87 }
88 
89 // "name" is in "binary name" format, e.g. "dalvik.system.Debug$1".
Class_classForName(JNIEnv * env,jclass,jstring javaName,jboolean initialize,jobject javaLoader)90 static jclass Class_classForName(JNIEnv* env, jclass, jstring javaName, jboolean initialize,
91                                  jobject javaLoader) {
92   ScopedFastNativeObjectAccess soa(env);
93   StackHandleScope<3> hs(soa.Self());
94   Handle<mirror::String> mirror_name = hs.NewHandle(soa.Decode<mirror::String>(javaName));
95   if (mirror_name == nullptr) {
96     soa.Self()->ThrowNewWrappedException("Ljava/lang/NullPointerException;", /*msg=*/ nullptr);
97     return nullptr;
98   }
99 
100   // We need to validate and convert the name (from x.y.z to x/y/z).  This
101   // is especially handy for array types, since we want to avoid
102   // auto-generating bogus array classes.
103   std::string name = mirror_name->ToModifiedUtf8();
104   if (!IsValidBinaryClassName(name.c_str())) {
105     soa.Self()->ThrowNewExceptionF("Ljava/lang/ClassNotFoundException;",
106                                    "Invalid name: %s", name.c_str());
107     return nullptr;
108   }
109 
110   std::string descriptor(DotToDescriptor(name.c_str()));
111   Handle<mirror::ClassLoader> class_loader(
112       hs.NewHandle(soa.Decode<mirror::ClassLoader>(javaLoader)));
113   ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
114   Handle<mirror::Class> c(
115       hs.NewHandle(class_linker->FindClass(soa.Self(), descriptor.c_str(), class_loader)));
116   if (UNLIKELY(c == nullptr)) {
117     StackHandleScope<2> hs2(soa.Self());
118     Handle<mirror::Object> cause = hs2.NewHandle(soa.Self()->GetException());
119     soa.Self()->ClearException();
120     Handle<mirror::Object> cnfe =
121         WellKnownClasses::java_lang_ClassNotFoundException_init->NewObject<'L', 'L'>(
122             hs2, soa.Self(), mirror_name, cause);
123     if (cnfe != nullptr) {
124       // Make sure allocation didn't fail with an OOME.
125       soa.Self()->SetException(ObjPtr<mirror::Throwable>::DownCast(cnfe.Get()));
126     }
127     return nullptr;
128   }
129   if (initialize) {
130     class_linker->EnsureInitialized(soa.Self(), c, true, true);
131   }
132 
133   // java.lang.ClassValue was added in Android U, and proguarding tools
134   // used that as justification to remove computeValue method implementation.
135   // Usual pattern was to check that Class.forName("java.lang.ClassValue")
136   // call does not throw and use ClassValue-based implementation or fallback
137   // to other solution if it does throw.
138   // So far ClassValue is the only class with such a problem and hence this
139   // ad-hoc check.
140   // See b/259501764.
141   if (!c->CheckIsVisibleWithTargetSdk(soa.Self())) {
142     DCHECK(soa.Self()->IsExceptionPending());
143     return nullptr;
144   }
145 
146   return soa.AddLocalReference<jclass>(c.Get());
147 }
148 
Class_getPrimitiveClass(JNIEnv * env,jclass,jstring name)149 static jclass Class_getPrimitiveClass(JNIEnv* env, jclass, jstring name) {
150   ScopedFastNativeObjectAccess soa(env);
151   ObjPtr<mirror::Class> klass = mirror::Class::GetPrimitiveClass(soa.Decode<mirror::String>(name));
152   return soa.AddLocalReference<jclass>(klass);
153 }
154 
Class_getNameNative(JNIEnv * env,jobject javaThis)155 static jstring Class_getNameNative(JNIEnv* env, jobject javaThis) {
156   ScopedFastNativeObjectAccess soa(env);
157   StackHandleScope<1> hs(soa.Self());
158   ObjPtr<mirror::Class> c = DecodeClass(soa, javaThis);
159   return soa.AddLocalReference<jstring>(mirror::Class::ComputeName(hs.NewHandle(c)));
160 }
161 
Class_getInterfacesInternal(JNIEnv * env,jobject javaThis)162 static jobjectArray Class_getInterfacesInternal(JNIEnv* env, jobject javaThis) {
163   ScopedFastNativeObjectAccess soa(env);
164   StackHandleScope<1> hs(soa.Self());
165   Handle<mirror::Class> klass = hs.NewHandle(DecodeClass(soa, javaThis));
166   if (klass->IsObsoleteObject()) {
167     ThrowRuntimeException("Obsolete Object!");
168     return nullptr;
169   }
170 
171   if (klass->IsProxyClass()) {
172     StackHandleScope<1> hs2(soa.Self());
173     Handle<mirror::ObjectArray<mirror::Class>> interfaces =
174         hs2.NewHandle(klass->GetProxyInterfaces());
175     return soa.AddLocalReference<jobjectArray>(
176         mirror::ObjectArray<mirror::Class>::Clone(interfaces, soa.Self()));
177   }
178 
179   const dex::TypeList* iface_list = klass->GetInterfaceTypeList();
180   if (iface_list == nullptr) {
181     return nullptr;
182   }
183 
184   ClassLinker* linker = Runtime::Current()->GetClassLinker();
185   const uint32_t num_ifaces = iface_list->Size();
186   ObjPtr<mirror::Class> class_array_class =
187       GetClassRoot<mirror::ObjectArray<mirror::Class>>(linker);
188   ObjPtr<mirror::ObjectArray<mirror::Class>> ifaces =
189       mirror::ObjectArray<mirror::Class>::Alloc(soa.Self(), class_array_class, num_ifaces);
190   if (ifaces.IsNull()) {
191     DCHECK(soa.Self()->IsExceptionPending());
192     return nullptr;
193   }
194 
195   // Check that we aren't in an active transaction, we call SetWithoutChecks
196   // with kActiveTransaction == false.
197   DCHECK(!Runtime::Current()->IsActiveTransaction());
198 
199   for (uint32_t i = 0; i < num_ifaces; ++i) {
200     const dex::TypeIndex type_idx = iface_list->GetTypeItem(i).type_idx_;
201     ObjPtr<mirror::Class> interface = linker->LookupResolvedType(type_idx, klass.Get());
202     DCHECK(interface != nullptr);
203     ifaces->SetWithoutChecks<false>(i, interface);
204   }
205 
206   return soa.AddLocalReference<jobjectArray>(ifaces);
207 }
208 
Class_getDeclaredFieldsUnchecked(JNIEnv * env,jobject javaThis,jboolean publicOnly)209 static jobjectArray Class_getDeclaredFieldsUnchecked(JNIEnv* env, jobject javaThis,
210                                                      jboolean publicOnly) {
211   ScopedFastNativeObjectAccess soa(env);
212   ObjPtr<mirror::Class> klass = DecodeClass(soa, javaThis);
213   return soa.AddLocalReference<jobjectArray>(
214       klass->GetDeclaredFields(soa.Self(),
215                                publicOnly != JNI_FALSE,
216                                /*force_resolve=*/ false));
217 }
218 
Class_getDeclaredFields(JNIEnv * env,jobject javaThis)219 static jobjectArray Class_getDeclaredFields(JNIEnv* env, jobject javaThis) {
220   ScopedFastNativeObjectAccess soa(env);
221   ObjPtr<mirror::Class> klass = DecodeClass(soa, javaThis);
222   return soa.AddLocalReference<jobjectArray>(
223       klass->GetDeclaredFields(soa.Self(),
224                                /*public_only=*/ false,
225                                /*force_resolve=*/ true));
226 }
227 
Class_getPublicDeclaredFields(JNIEnv * env,jobject javaThis)228 static jobjectArray Class_getPublicDeclaredFields(JNIEnv* env, jobject javaThis) {
229   ScopedFastNativeObjectAccess soa(env);
230   ObjPtr<mirror::Class> klass = DecodeClass(soa, javaThis);
231   return soa.AddLocalReference<jobjectArray>(
232       klass->GetDeclaredFields(soa.Self(),
233                                /*public_only=*/ true,
234                                /*force_resolve=*/ true));
235 }
236 
237 // Performs a binary search through an array of fields, TODO: Is this fast enough if we don't use
238 // the dex cache for lookups? I think CompareModifiedUtf8ToUtf16AsCodePointValues should be fairly
239 // fast.
FindFieldByName(ObjPtr<mirror::String> name,LengthPrefixedArray<ArtField> * fields)240 ALWAYS_INLINE static inline ArtField* FindFieldByName(ObjPtr<mirror::String> name,
241                                                       LengthPrefixedArray<ArtField>* fields)
242     REQUIRES_SHARED(Locks::mutator_lock_) {
243   if (fields == nullptr) {
244     return nullptr;
245   }
246   size_t low = 0;
247   size_t high = fields->size();
248   const bool is_name_compressed = name->IsCompressed();
249   const uint16_t* const data = (is_name_compressed) ? nullptr : name->GetValue();
250   const uint8_t* const data_compressed = (is_name_compressed) ? name->GetValueCompressed()
251                                                               : nullptr;
252   const size_t length = name->GetLength();
253   while (low < high) {
254     auto mid = (low + high) / 2;
255     ArtField& field = fields->At(mid);
256     int result = 0;
257     if (is_name_compressed) {
258       size_t field_length = strlen(field.GetName());
259       size_t min_size = (length < field_length) ? length : field_length;
260       result = memcmp(field.GetName(), data_compressed, min_size);
261       if (result == 0) {
262         result = field_length - length;
263       }
264     } else {
265       result = CompareModifiedUtf8ToUtf16AsCodePointValues(field.GetName(), data, length);
266     }
267     // Alternate approach, only a few % faster at the cost of more allocations.
268     // int result = field->GetStringName(self, true)->CompareTo(name);
269     if (result < 0) {
270       low = mid + 1;
271     } else if (result > 0) {
272       high = mid;
273     } else {
274       return &field;
275     }
276   }
277   if (kIsDebugBuild) {
278     for (ArtField& field : MakeIterationRangeFromLengthPrefixedArray(fields)) {
279       CHECK_NE(field.GetName(), name->ToModifiedUtf8());
280     }
281   }
282   return nullptr;
283 }
284 
GetDeclaredField(Thread * self,ObjPtr<mirror::Class> c,ObjPtr<mirror::String> name)285 ALWAYS_INLINE static inline ObjPtr<mirror::Field> GetDeclaredField(Thread* self,
286                                                                    ObjPtr<mirror::Class> c,
287                                                                    ObjPtr<mirror::String> name)
288     REQUIRES_SHARED(Locks::mutator_lock_) {
289   if (UNLIKELY(c->IsObsoleteObject())) {
290     ThrowRuntimeException("Obsolete Object!");
291     return nullptr;
292   }
293   ArtField* art_field = FindFieldByName(name, c->GetIFieldsPtr());
294   if (art_field != nullptr) {
295     return mirror::Field::CreateFromArtField(self, art_field, true);
296   }
297   art_field = FindFieldByName(name, c->GetSFieldsPtr());
298   if (art_field != nullptr) {
299     return mirror::Field::CreateFromArtField(self, art_field, true);
300   }
301   return nullptr;
302 }
303 
GetPublicFieldRecursive(Thread * self,ObjPtr<mirror::Class> clazz,ObjPtr<mirror::String> name)304 static ObjPtr<mirror::Field> GetPublicFieldRecursive(
305     Thread* self, ObjPtr<mirror::Class> clazz, ObjPtr<mirror::String> name)
306     REQUIRES_SHARED(Locks::mutator_lock_) {
307   DCHECK(clazz != nullptr);
308   DCHECK(name != nullptr);
309   DCHECK(self != nullptr);
310 
311   if (UNLIKELY(clazz->IsObsoleteObject())) {
312     ThrowRuntimeException("Obsolete Object!");
313     return nullptr;
314   }
315   StackHandleScope<2> hs(self);
316   MutableHandle<mirror::Class> h_clazz(hs.NewHandle(clazz));
317   Handle<mirror::String> h_name(hs.NewHandle(name));
318 
319   // We search the current class, its direct interfaces then its superclass.
320   while (h_clazz != nullptr) {
321     ObjPtr<mirror::Field> result = GetDeclaredField(self, h_clazz.Get(), h_name.Get());
322     if ((result != nullptr) && (result->GetAccessFlags() & kAccPublic)) {
323       return result;
324     } else if (UNLIKELY(self->IsExceptionPending())) {
325       // Something went wrong. Bail out.
326       return nullptr;
327     }
328 
329     uint32_t num_direct_interfaces = h_clazz->NumDirectInterfaces();
330     for (uint32_t i = 0; i < num_direct_interfaces; i++) {
331       ObjPtr<mirror::Class> iface = mirror::Class::ResolveDirectInterface(self, h_clazz, i);
332       if (UNLIKELY(iface == nullptr)) {
333         self->AssertPendingException();
334         return nullptr;
335       }
336       result = GetPublicFieldRecursive(self, iface, h_name.Get());
337       if (result != nullptr) {
338         DCHECK(result->GetAccessFlags() & kAccPublic);
339         return result;
340       } else if (UNLIKELY(self->IsExceptionPending())) {
341         // Something went wrong. Bail out.
342         return nullptr;
343       }
344     }
345 
346     // We don't try the superclass if we are an interface.
347     if (h_clazz->IsInterface()) {
348       break;
349     }
350 
351     // Get the next class.
352     h_clazz.Assign(h_clazz->GetSuperClass());
353   }
354   return nullptr;
355 }
356 
Class_getPublicFieldRecursive(JNIEnv * env,jobject javaThis,jstring name)357 static jobject Class_getPublicFieldRecursive(JNIEnv* env, jobject javaThis, jstring name) {
358   ScopedFastNativeObjectAccess soa(env);
359   auto name_string = soa.Decode<mirror::String>(name);
360   if (UNLIKELY(name_string == nullptr)) {
361     ThrowNullPointerException("name == null");
362     return nullptr;
363   }
364 
365   StackHandleScope<1> hs(soa.Self());
366   Handle<mirror::Field> field = hs.NewHandle(GetPublicFieldRecursive(
367       soa.Self(), DecodeClass(soa, javaThis), name_string));
368   if (field.Get() == nullptr || ShouldDenyAccessToMember(field->GetArtField(), soa.Self())) {
369     return nullptr;
370   }
371   return soa.AddLocalReference<jobject>(field.Get());
372 }
373 
Class_getDeclaredField(JNIEnv * env,jobject javaThis,jstring name)374 static jobject Class_getDeclaredField(JNIEnv* env, jobject javaThis, jstring name) {
375   ScopedFastNativeObjectAccess soa(env);
376   StackHandleScope<3> hs(soa.Self());
377   Handle<mirror::String> h_string = hs.NewHandle(soa.Decode<mirror::String>(name));
378   if (h_string == nullptr) {
379     ThrowNullPointerException("name == null");
380     return nullptr;
381   }
382   Handle<mirror::Class> h_klass = hs.NewHandle(DecodeClass(soa, javaThis));
383   Handle<mirror::Field> result =
384       hs.NewHandle(GetDeclaredField(soa.Self(), h_klass.Get(), h_string.Get()));
385   if (result == nullptr || ShouldDenyAccessToMember(result->GetArtField(), soa.Self())) {
386     std::string name_str = h_string->ToModifiedUtf8();
387     if (name_str == "value" && h_klass->IsStringClass()) {
388       // We log the error for this specific case, as the user might just swallow the exception.
389       // This helps diagnose crashes when applications rely on the String#value field being
390       // there.
391       // Also print on the error stream to test it through run-test.
392       std::string message("The String#value field is not present on Android versions >= 6.0");
393       LOG(ERROR) << message;
394       std::cerr << message << std::endl;
395     }
396     // We may have a pending exception if we failed to resolve.
397     if (!soa.Self()->IsExceptionPending()) {
398       ThrowNoSuchFieldException(h_klass.Get(), name_str);
399     }
400     return nullptr;
401   }
402   return soa.AddLocalReference<jobject>(result.Get());
403 }
404 
Class_getDeclaredConstructorInternal(JNIEnv * env,jobject javaThis,jobjectArray args)405 static jobject Class_getDeclaredConstructorInternal(
406     JNIEnv* env, jobject javaThis, jobjectArray args) {
407   ScopedFastNativeObjectAccess soa(env);
408   DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
409   DCHECK(!Runtime::Current()->IsActiveTransaction());
410 
411   StackHandleScope<1> hs(soa.Self());
412   ObjPtr<mirror::Class> klass = DecodeClass(soa, javaThis);
413   if (UNLIKELY(klass->IsObsoleteObject())) {
414     ThrowRuntimeException("Obsolete Object!");
415     return nullptr;
416   }
417   Handle<mirror::Constructor> result = hs.NewHandle(
418       mirror::Class::GetDeclaredConstructorInternal<kRuntimePointerSize>(
419           soa.Self(),
420           klass,
421           soa.Decode<mirror::ObjectArray<mirror::Class>>(args)));
422   if (result == nullptr || ShouldDenyAccessToMember(result->GetArtMethod(), soa.Self())) {
423     return nullptr;
424   }
425   return soa.AddLocalReference<jobject>(result.Get());
426 }
427 
MethodMatchesConstructor(ArtMethod * m,bool public_only,const hiddenapi::AccessContext & hiddenapi_context)428 static ALWAYS_INLINE inline bool MethodMatchesConstructor(
429     ArtMethod* m,
430     bool public_only,
431     const hiddenapi::AccessContext& hiddenapi_context) REQUIRES_SHARED(Locks::mutator_lock_) {
432   DCHECK(m != nullptr);
433   return m->IsConstructor() &&
434          !m->IsStatic() &&
435          mirror::Class::IsDiscoverable(public_only, hiddenapi_context, m);
436 }
437 
Class_getDeclaredConstructorsInternal(JNIEnv * env,jobject javaThis,jboolean publicOnly)438 static jobjectArray Class_getDeclaredConstructorsInternal(
439     JNIEnv* env, jobject javaThis, jboolean publicOnly) {
440   ScopedFastNativeObjectAccess soa(env);
441   StackHandleScope<2> hs(soa.Self());
442   bool public_only = (publicOnly != JNI_FALSE);
443   auto hiddenapi_context = hiddenapi::GetReflectionCallerAccessContext(soa.Self());
444   Handle<mirror::Class> h_klass = hs.NewHandle(DecodeClass(soa, javaThis));
445   if (UNLIKELY(h_klass->IsObsoleteObject())) {
446     ThrowRuntimeException("Obsolete Object!");
447     return nullptr;
448   }
449   size_t constructor_count = 0;
450   // Two pass approach for speed.
451   for (auto& m : h_klass->GetDirectMethods(kRuntimePointerSize)) {
452     constructor_count += MethodMatchesConstructor(&m, public_only, hiddenapi_context) ? 1u : 0u;
453   }
454   auto h_constructors = hs.NewHandle(mirror::ObjectArray<mirror::Constructor>::Alloc(
455       soa.Self(), GetClassRoot<mirror::ObjectArray<mirror::Constructor>>(), constructor_count));
456   if (UNLIKELY(h_constructors == nullptr)) {
457     soa.Self()->AssertPendingException();
458     return nullptr;
459   }
460   constructor_count = 0;
461   for (auto& m : h_klass->GetDirectMethods(kRuntimePointerSize)) {
462     if (MethodMatchesConstructor(&m, public_only, hiddenapi_context)) {
463       DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
464       DCHECK(!Runtime::Current()->IsActiveTransaction());
465       ObjPtr<mirror::Constructor> constructor =
466           mirror::Constructor::CreateFromArtMethod<kRuntimePointerSize>(soa.Self(), &m);
467       if (UNLIKELY(constructor == nullptr)) {
468         soa.Self()->AssertPendingOOMException();
469         return nullptr;
470       }
471       h_constructors->SetWithoutChecks<false>(constructor_count++, constructor);
472     }
473   }
474   return soa.AddLocalReference<jobjectArray>(h_constructors.Get());
475 }
476 
Class_getDeclaredMethodInternal(JNIEnv * env,jobject javaThis,jstring name,jobjectArray args)477 static jobject Class_getDeclaredMethodInternal(JNIEnv* env, jobject javaThis,
478                                                jstring name, jobjectArray args) {
479   ScopedFastNativeObjectAccess soa(env);
480   StackHandleScope<1> hs(soa.Self());
481   DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
482   DCHECK(!Runtime::Current()->IsActiveTransaction());
483   ObjPtr<mirror::Class> klass = DecodeClass(soa, javaThis);
484   if (UNLIKELY(klass->IsObsoleteObject())) {
485     ThrowRuntimeException("Obsolete Object!");
486     return nullptr;
487   }
488   Handle<mirror::Method> result = hs.NewHandle(
489       mirror::Class::GetDeclaredMethodInternal<kRuntimePointerSize>(
490           soa.Self(),
491           klass,
492           soa.Decode<mirror::String>(name),
493           soa.Decode<mirror::ObjectArray<mirror::Class>>(args),
494           GetHiddenapiAccessContextFunction(soa.Self())));
495   if (result == nullptr || ShouldDenyAccessToMember(result->GetArtMethod(), soa.Self())) {
496     return nullptr;
497   }
498   return soa.AddLocalReference<jobject>(result.Get());
499 }
500 
Class_getDeclaredMethodsUnchecked(JNIEnv * env,jobject javaThis,jboolean publicOnly)501 static jobjectArray Class_getDeclaredMethodsUnchecked(JNIEnv* env, jobject javaThis,
502                                                       jboolean publicOnly) {
503   ScopedFastNativeObjectAccess soa(env);
504   StackHandleScope<2> hs(soa.Self());
505 
506   auto hiddenapi_context = hiddenapi::GetReflectionCallerAccessContext(soa.Self());
507   bool public_only = (publicOnly != JNI_FALSE);
508 
509   Handle<mirror::Class> klass = hs.NewHandle(DecodeClass(soa, javaThis));
510   if (klass->IsObsoleteObject()) {
511     ThrowRuntimeException("Obsolete Object!");
512     return nullptr;
513   }
514   size_t num_methods = 0;
515   for (ArtMethod& m : klass->GetDeclaredMethods(kRuntimePointerSize)) {
516     uint32_t modifiers = m.GetAccessFlags();
517     // Add non-constructor declared methods.
518     if ((modifiers & kAccConstructor) == 0 &&
519         mirror::Class::IsDiscoverable(public_only, hiddenapi_context, &m)) {
520       ++num_methods;
521     }
522   }
523   auto ret = hs.NewHandle(mirror::ObjectArray<mirror::Method>::Alloc(
524       soa.Self(), GetClassRoot<mirror::ObjectArray<mirror::Method>>(), num_methods));
525   if (ret == nullptr) {
526     soa.Self()->AssertPendingOOMException();
527     return nullptr;
528   }
529   num_methods = 0;
530   for (ArtMethod& m : klass->GetDeclaredMethods(kRuntimePointerSize)) {
531     uint32_t modifiers = m.GetAccessFlags();
532     if ((modifiers & kAccConstructor) == 0 &&
533         mirror::Class::IsDiscoverable(public_only, hiddenapi_context, &m)) {
534       DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
535       DCHECK(!Runtime::Current()->IsActiveTransaction());
536       ObjPtr<mirror::Method> method =
537           mirror::Method::CreateFromArtMethod<kRuntimePointerSize>(soa.Self(), &m);
538       if (method == nullptr) {
539         soa.Self()->AssertPendingException();
540         return nullptr;
541       }
542       ret->SetWithoutChecks<false>(num_methods++, method);
543     }
544   }
545   return soa.AddLocalReference<jobjectArray>(ret.Get());
546 }
547 
Class_getDeclaredAnnotation(JNIEnv * env,jobject javaThis,jclass annotationClass)548 static jobject Class_getDeclaredAnnotation(JNIEnv* env, jobject javaThis, jclass annotationClass) {
549   ScopedFastNativeObjectAccess soa(env);
550   StackHandleScope<2> hs(soa.Self());
551   Handle<mirror::Class> klass(hs.NewHandle(DecodeClass(soa, javaThis)));
552   if (klass->IsObsoleteObject()) {
553     ThrowRuntimeException("Obsolete Object!");
554     return nullptr;
555   }
556 
557   // Handle public contract to throw NPE if the "annotationClass" argument was null.
558   if (UNLIKELY(annotationClass == nullptr)) {
559     ThrowNullPointerException("annotationClass");
560     return nullptr;
561   }
562 
563   if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
564     return nullptr;
565   }
566   Handle<mirror::Class> annotation_class(hs.NewHandle(soa.Decode<mirror::Class>(annotationClass)));
567   return soa.AddLocalReference<jobject>(
568       annotations::GetAnnotationForClass(klass, annotation_class));
569 }
570 
Class_getDeclaredAnnotations(JNIEnv * env,jobject javaThis)571 static jobjectArray Class_getDeclaredAnnotations(JNIEnv* env, jobject javaThis) {
572   ScopedFastNativeObjectAccess soa(env);
573   StackHandleScope<1> hs(soa.Self());
574   Handle<mirror::Class> klass(hs.NewHandle(DecodeClass(soa, javaThis)));
575   if (klass->IsObsoleteObject()) {
576     ThrowRuntimeException("Obsolete Object!");
577     return nullptr;
578   }
579   if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
580     // Return an empty array instead of a null pointer.
581     ObjPtr<mirror::Class>  annotation_array_class =
582         WellKnownClasses::ToClass(WellKnownClasses::java_lang_annotation_Annotation__array);
583     ObjPtr<mirror::ObjectArray<mirror::Object>> empty_array =
584         mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(),
585                                                    annotation_array_class,
586                                                    /* length= */ 0);
587     return soa.AddLocalReference<jobjectArray>(empty_array);
588   }
589   return soa.AddLocalReference<jobjectArray>(annotations::GetAnnotationsForClass(klass));
590 }
591 
Class_getDeclaredClasses(JNIEnv * env,jobject javaThis)592 static jobjectArray Class_getDeclaredClasses(JNIEnv* env, jobject javaThis) {
593   ScopedFastNativeObjectAccess soa(env);
594   StackHandleScope<1> hs(soa.Self());
595   Handle<mirror::Class> klass(hs.NewHandle(DecodeClass(soa, javaThis)));
596   if (klass->IsObsoleteObject()) {
597     ThrowRuntimeException("Obsolete Object!");
598     return nullptr;
599   }
600   ObjPtr<mirror::ObjectArray<mirror::Class>> classes = nullptr;
601   if (!klass->IsProxyClass() && klass->GetDexCache() != nullptr) {
602     classes = annotations::GetDeclaredClasses(klass);
603   }
604   if (classes == nullptr) {
605     // Return an empty array instead of a null pointer.
606     if (soa.Self()->IsExceptionPending()) {
607       // Pending exception from GetDeclaredClasses.
608       return nullptr;
609     }
610     ObjPtr<mirror::Class> class_array_class = GetClassRoot<mirror::ObjectArray<mirror::Class>>();
611     DCHECK(class_array_class != nullptr);
612     ObjPtr<mirror::ObjectArray<mirror::Class>> empty_array =
613         mirror::ObjectArray<mirror::Class>::Alloc(soa.Self(), class_array_class, 0);
614     return soa.AddLocalReference<jobjectArray>(empty_array);
615   }
616   return soa.AddLocalReference<jobjectArray>(classes);
617 }
618 
Class_getEnclosingClass(JNIEnv * env,jobject javaThis)619 static jclass Class_getEnclosingClass(JNIEnv* env, jobject javaThis) {
620   ScopedFastNativeObjectAccess soa(env);
621   StackHandleScope<1> hs(soa.Self());
622   Handle<mirror::Class> klass(hs.NewHandle(DecodeClass(soa, javaThis)));
623   if (klass->IsObsoleteObject()) {
624     ThrowRuntimeException("Obsolete Object!");
625     return nullptr;
626   }
627   if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
628     return nullptr;
629   }
630   return soa.AddLocalReference<jclass>(annotations::GetEnclosingClass(klass));
631 }
632 
Class_getEnclosingConstructorNative(JNIEnv * env,jobject javaThis)633 static jobject Class_getEnclosingConstructorNative(JNIEnv* env, jobject javaThis) {
634   ScopedFastNativeObjectAccess soa(env);
635   StackHandleScope<1> hs(soa.Self());
636   Handle<mirror::Class> klass(hs.NewHandle(DecodeClass(soa, javaThis)));
637   if (klass->IsObsoleteObject()) {
638     ThrowRuntimeException("Obsolete Object!");
639     return nullptr;
640   }
641   if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
642     return nullptr;
643   }
644   ObjPtr<mirror::Object> method = annotations::GetEnclosingMethod(klass);
645   if (method != nullptr) {
646     if (GetClassRoot<mirror::Constructor>() == method->GetClass()) {
647       return soa.AddLocalReference<jobject>(method);
648     }
649   }
650   return nullptr;
651 }
652 
Class_getEnclosingMethodNative(JNIEnv * env,jobject javaThis)653 static jobject Class_getEnclosingMethodNative(JNIEnv* env, jobject javaThis) {
654   ScopedFastNativeObjectAccess soa(env);
655   StackHandleScope<1> hs(soa.Self());
656   Handle<mirror::Class> klass(hs.NewHandle(DecodeClass(soa, javaThis)));
657   if (klass->IsObsoleteObject()) {
658     ThrowRuntimeException("Obsolete Object!");
659     return nullptr;
660   }
661   if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
662     return nullptr;
663   }
664   ObjPtr<mirror::Object> method = annotations::GetEnclosingMethod(klass);
665   if (method != nullptr) {
666     if (GetClassRoot<mirror::Method>() == method->GetClass()) {
667       return soa.AddLocalReference<jobject>(method);
668     }
669   }
670   return nullptr;
671 }
672 
Class_getInnerClassFlags(JNIEnv * env,jobject javaThis,jint defaultValue)673 static jint Class_getInnerClassFlags(JNIEnv* env, jobject javaThis, jint defaultValue) {
674   ScopedFastNativeObjectAccess soa(env);
675   StackHandleScope<1> hs(soa.Self());
676   Handle<mirror::Class> klass(hs.NewHandle(DecodeClass(soa, javaThis)));
677   if (klass->IsObsoleteObject()) {
678     ThrowRuntimeException("Obsolete Object!");
679     return 0;
680   }
681   return mirror::Class::GetInnerClassFlags(klass, defaultValue);
682 }
683 
Class_getSimpleNameNative(JNIEnv * env,jobject javaThis)684 static jstring Class_getSimpleNameNative(JNIEnv* env, jobject javaThis) {
685   ScopedFastNativeObjectAccess soa(env);
686   StackHandleScope<3> hs(soa.Self());
687   Handle<mirror::Class> klass(hs.NewHandle(DecodeClass(soa, javaThis)));
688   if (klass->IsObsoleteObject()) {
689     ThrowRuntimeException("Obsolete Object!");
690     return nullptr;
691   }
692   if (!klass->IsProxyClass() && klass->GetDexCache() != nullptr) {
693     ObjPtr<mirror::String> class_name = nullptr;
694     if (annotations::GetInnerClass(klass, &class_name)) {
695       if (class_name == nullptr) {  // Anonymous class
696         ObjPtr<mirror::Class> j_l_String =
697             WellKnownClasses::java_lang_String_EMPTY->GetDeclaringClass();
698         ObjPtr<mirror::Object> empty_string =
699             WellKnownClasses::java_lang_String_EMPTY->GetObject(j_l_String);
700         DCHECK(empty_string != nullptr);
701         return soa.AddLocalReference<jstring>(empty_string);
702       }
703       Handle<mirror::String> h_inner_name(hs.NewHandle<mirror::String>(class_name));
704       if (annotations::GetDeclaringClass(klass) != nullptr ||   // member class
705           annotations::GetEnclosingMethod(klass) != nullptr) {  // local class
706         return soa.AddLocalReference<jstring>(h_inner_name.Get());
707       }
708     }
709   }
710 
711   Handle<mirror::String> h_name(hs.NewHandle<mirror::String>(mirror::Class::ComputeName(klass)));
712   if (h_name == nullptr) {
713     return nullptr;
714   }
715   int32_t dot_index = h_name->LastIndexOf('.');
716   if (dot_index < 0) {
717     return soa.AddLocalReference<jstring>(h_name.Get());
718   }
719   int32_t start_index = dot_index + 1;
720   int32_t length = h_name->GetLength() - start_index;
721   gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
722   return soa.AddLocalReference<jstring>(
723       mirror::String::AllocFromString(soa.Self(), length, h_name, start_index, allocator_type));
724 }
725 
Class_getSignatureAnnotation(JNIEnv * env,jobject javaThis)726 static jobjectArray Class_getSignatureAnnotation(JNIEnv* env, jobject javaThis) {
727   ScopedFastNativeObjectAccess soa(env);
728   StackHandleScope<1> hs(soa.Self());
729   Handle<mirror::Class> klass(hs.NewHandle(DecodeClass(soa, javaThis)));
730   if (klass->IsObsoleteObject()) {
731     ThrowRuntimeException("Obsolete Object!");
732     return nullptr;
733   }
734   if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
735     return nullptr;
736   }
737   return soa.AddLocalReference<jobjectArray>(
738       annotations::GetSignatureAnnotationForClass(klass));
739 }
740 
Class_isAnonymousClass(JNIEnv * env,jobject javaThis)741 static jboolean Class_isAnonymousClass(JNIEnv* env, jobject javaThis) {
742   ScopedFastNativeObjectAccess soa(env);
743   StackHandleScope<1> hs(soa.Self());
744   Handle<mirror::Class> klass(hs.NewHandle(DecodeClass(soa, javaThis)));
745   if (klass->IsObsoleteObject()) {
746     ThrowRuntimeException("Obsolete Object!");
747     return 0;
748   }
749   if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
750     return false;
751   }
752   ObjPtr<mirror::String> class_name = nullptr;
753   if (!annotations::GetInnerClass(klass, &class_name)) {
754     return false;
755   }
756   return class_name == nullptr;
757 }
758 
Class_isRecord0(JNIEnv * env,jobject javaThis)759 static jboolean Class_isRecord0(JNIEnv* env, jobject javaThis) {
760   ScopedFastNativeObjectAccess soa(env);
761   StackHandleScope<1> hs(soa.Self());
762   Handle<mirror::Class> klass(hs.NewHandle(DecodeClass(soa, javaThis)));
763   return klass->IsRecordClass();
764 }
765 
Class_isDeclaredAnnotationPresent(JNIEnv * env,jobject javaThis,jclass annotationType)766 static jboolean Class_isDeclaredAnnotationPresent(JNIEnv* env, jobject javaThis,
767                                                   jclass annotationType) {
768   ScopedFastNativeObjectAccess soa(env);
769   StackHandleScope<2> hs(soa.Self());
770   Handle<mirror::Class> klass(hs.NewHandle(DecodeClass(soa, javaThis)));
771   if (klass->IsObsoleteObject()) {
772     ThrowRuntimeException("Obsolete Object!");
773     return false;
774   }
775   if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
776     return false;
777   }
778   Handle<mirror::Class> annotation_class(hs.NewHandle(soa.Decode<mirror::Class>(annotationType)));
779   return annotations::IsClassAnnotationPresent(klass, annotation_class);
780 }
781 
Class_getDeclaringClass(JNIEnv * env,jobject javaThis)782 static jclass Class_getDeclaringClass(JNIEnv* env, jobject javaThis) {
783   ScopedFastNativeObjectAccess soa(env);
784   StackHandleScope<1> hs(soa.Self());
785   Handle<mirror::Class> klass(hs.NewHandle(DecodeClass(soa, javaThis)));
786   if (klass->IsObsoleteObject()) {
787     ThrowRuntimeException("Obsolete Object!");
788     return nullptr;
789   }
790   if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
791     return nullptr;
792   }
793   // Return null for anonymous classes.
794   if (Class_isAnonymousClass(env, javaThis)) {
795     return nullptr;
796   }
797   return soa.AddLocalReference<jclass>(annotations::GetDeclaringClass(klass));
798 }
799 
Class_getNestHostFromAnnotation(JNIEnv * env,jobject javaThis)800 static jclass Class_getNestHostFromAnnotation(JNIEnv* env, jobject javaThis) {
801   ScopedFastNativeObjectAccess soa(env);
802   StackHandleScope<1> hs(soa.Self());
803   Handle<mirror::Class> klass(hs.NewHandle(DecodeClass(soa, javaThis)));
804   if (klass->IsObsoleteObject()) {
805     ThrowRuntimeException("Obsolete Object!");
806     return nullptr;
807   }
808   if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
809     return nullptr;
810   }
811   ObjPtr<mirror::Class> hostClass = annotations::GetNestHost(klass);
812   if (hostClass == nullptr) {
813     return nullptr;
814   }
815   return soa.AddLocalReference<jclass>(hostClass);
816 }
817 
Class_getNestMembersFromAnnotation(JNIEnv * env,jobject javaThis)818 static jobjectArray Class_getNestMembersFromAnnotation(JNIEnv* env, jobject javaThis) {
819   ScopedFastNativeObjectAccess soa(env);
820   StackHandleScope<1> hs(soa.Self());
821   Handle<mirror::Class> klass(hs.NewHandle(DecodeClass(soa, javaThis)));
822   if (klass->IsObsoleteObject()) {
823     ThrowRuntimeException("Obsolete Object!");
824     return nullptr;
825   }
826   if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
827     return nullptr;
828   }
829   ObjPtr<mirror::ObjectArray<mirror::Class>> classes = annotations::GetNestMembers(klass);
830   if (classes == nullptr) {
831     return nullptr;
832   }
833   return soa.AddLocalReference<jobjectArray>(classes);
834 }
835 
Class_getRecordAnnotationElement(JNIEnv * env,jobject javaThis,jstring element_name,jclass array_class)836 static jobjectArray Class_getRecordAnnotationElement(JNIEnv* env,
837                                                      jobject javaThis,
838                                                      jstring element_name,
839                                                      jclass array_class) {
840   ScopedFastNativeObjectAccess soa(env);
841   ScopedUtfChars name(env, element_name);
842   StackHandleScope<2> hs(soa.Self());
843   Handle<mirror::Class> klass(hs.NewHandle(DecodeClass(soa, javaThis)));
844   if (!(klass->IsRecordClass())) {
845     return nullptr;
846   }
847 
848   Handle<mirror::Class> a_class(hs.NewHandle(DecodeClass(soa, array_class)));
849   ObjPtr<mirror::Object> element_array =
850       annotations::getRecordAnnotationElement(klass, a_class, name.c_str());
851   if (element_array == nullptr || !(element_array->IsObjectArray())) {
852     return nullptr;
853   }
854   return soa.AddLocalReference<jobjectArray>(element_array);
855 }
856 
Class_getPermittedSubclassesFromAnnotation(JNIEnv * env,jobject javaThis)857 static jobjectArray Class_getPermittedSubclassesFromAnnotation(JNIEnv* env, jobject javaThis) {
858   ScopedFastNativeObjectAccess soa(env);
859   StackHandleScope<1> hs(soa.Self());
860   Handle<mirror::Class> klass(hs.NewHandle(DecodeClass(soa, javaThis)));
861   if (klass->IsObsoleteObject()) {
862     ThrowRuntimeException("Obsolete Object!");
863     return nullptr;
864   }
865   if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
866     return nullptr;
867   }
868   ObjPtr<mirror::ObjectArray<mirror::Class>> classes = annotations::GetPermittedSubclasses(klass);
869   if (classes == nullptr) {
870     return nullptr;
871   }
872   return soa.AddLocalReference<jobjectArray>(classes);
873 }
874 
Class_ensureExtDataPresent(JNIEnv * env,jobject javaThis)875 static jobject Class_ensureExtDataPresent(JNIEnv* env, jobject javaThis) {
876   ScopedFastNativeObjectAccess soa(env);
877   StackHandleScope<2> hs(soa.Self());
878   Handle<mirror::Class> klass = hs.NewHandle(DecodeClass(soa, javaThis));
879 
880   ObjPtr<mirror::Object> extDataPtr =
881     mirror::Class::EnsureExtDataPresent(klass, Thread::Current());
882 
883   return soa.AddLocalReference<jobject>(extDataPtr);
884 }
885 
Class_newInstance(JNIEnv * env,jobject javaThis)886 static jobject Class_newInstance(JNIEnv* env, jobject javaThis) {
887   ScopedFastNativeObjectAccess soa(env);
888   StackHandleScope<4> hs(soa.Self());
889   Handle<mirror::Class> klass = hs.NewHandle(DecodeClass(soa, javaThis));
890   if (klass->IsObsoleteObject()) {
891     ThrowRuntimeException("Obsolete Object!");
892     return nullptr;
893   }
894   if (UNLIKELY(klass->GetPrimitiveType() != 0 || klass->IsInterface() || klass->IsArrayClass() ||
895                klass->IsAbstract())) {
896     soa.Self()->ThrowNewExceptionF("Ljava/lang/InstantiationException;",
897                                    "%s cannot be instantiated",
898                                    klass->PrettyClass().c_str());
899     return nullptr;
900   }
901   auto caller = hs.NewHandle<mirror::Class>(nullptr);
902   // Verify that we can access the class.
903   if (!klass->IsPublic()) {
904     caller.Assign(GetCallingClass(soa.Self(), 1));
905     if (caller != nullptr && !caller->CanAccess(klass.Get())) {
906       soa.Self()->ThrowNewExceptionF(
907           "Ljava/lang/IllegalAccessException;", "%s is not accessible from %s",
908           klass->PrettyClass().c_str(), caller->PrettyClass().c_str());
909       return nullptr;
910     }
911   }
912   StackArtMethodHandleScope<1> mhs(soa.Self());
913   ReflectiveHandle<ArtMethod> constructor(mhs.NewMethodHandle(klass->GetDeclaredConstructor(
914       soa.Self(), ScopedNullHandle<mirror::ObjectArray<mirror::Class>>(), kRuntimePointerSize)));
915   if (UNLIKELY(constructor == nullptr) || ShouldDenyAccessToMember(constructor.Get(), soa.Self())) {
916     soa.Self()->ThrowNewExceptionF("Ljava/lang/InstantiationException;",
917                                    "%s has no zero argument constructor",
918                                    klass->PrettyClass().c_str());
919     return nullptr;
920   }
921   // Invoke the string allocator to return an empty string for the string class.
922   if (klass->IsStringClass()) {
923     gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
924     ObjPtr<mirror::Object> obj = mirror::String::AllocEmptyString(soa.Self(), allocator_type);
925     if (UNLIKELY(soa.Self()->IsExceptionPending())) {
926       return nullptr;
927     } else {
928       return soa.AddLocalReference<jobject>(obj);
929     }
930   }
931   auto receiver = hs.NewHandle(klass->AllocObject(soa.Self()));
932   if (UNLIKELY(receiver == nullptr)) {
933     soa.Self()->AssertPendingOOMException();
934     return nullptr;
935   }
936   // Verify that we can access the constructor.
937   ObjPtr<mirror::Class> declaring_class = constructor->GetDeclaringClass();
938   if (!constructor->IsPublic()) {
939     if (caller == nullptr) {
940       caller.Assign(GetCallingClass(soa.Self(), 1));
941     }
942     if (UNLIKELY(caller != nullptr && !VerifyAccess(receiver.Get(),
943                                                     declaring_class,
944                                                     constructor->GetAccessFlags(),
945                                                     caller.Get()))) {
946       soa.Self()->ThrowNewExceptionF(
947           "Ljava/lang/IllegalAccessException;", "%s is not accessible from %s",
948           constructor->PrettyMethod().c_str(), caller->PrettyClass().c_str());
949       return nullptr;
950     }
951   }
952   // Ensure that we are initialized.
953   if (UNLIKELY(!declaring_class->IsVisiblyInitialized())) {
954     Thread* self = soa.Self();
955     Handle<mirror::Class> h_class = hs.NewHandle(declaring_class);
956     if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(
957                       self, h_class, /*can_init_fields=*/ true, /*can_init_parents=*/ true))) {
958       DCHECK(self->IsExceptionPending());
959       return nullptr;
960     }
961     DCHECK(h_class->IsInitializing());
962   }
963   // Invoke the constructor.
964   JValue result;
965   uint32_t args[1] = { static_cast<uint32_t>(reinterpret_cast<uintptr_t>(receiver.Get())) };
966   constructor->Invoke(soa.Self(), args, sizeof(args), &result, "V");
967   if (UNLIKELY(soa.Self()->IsExceptionPending())) {
968     return nullptr;
969   }
970   // Constructors are ()V methods, so we shouldn't touch the result of InvokeMethod.
971   return soa.AddLocalReference<jobject>(receiver.Get());
972 }
973 
974 static JNINativeMethod gMethods[] = {
975   FAST_NATIVE_METHOD(Class, classForName,
976                 "(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;"),
977   FAST_NATIVE_METHOD(Class, ensureExtDataPresent, "()Ldalvik/system/ClassExt;"),
978   FAST_NATIVE_METHOD(Class, getDeclaredAnnotation,
979                 "(Ljava/lang/Class;)Ljava/lang/annotation/Annotation;"),
980   FAST_NATIVE_METHOD(Class, getDeclaredAnnotations, "()[Ljava/lang/annotation/Annotation;"),
981   FAST_NATIVE_METHOD(Class, getDeclaredClasses, "()[Ljava/lang/Class;"),
982   FAST_NATIVE_METHOD(Class, getDeclaredConstructorInternal,
983                 "([Ljava/lang/Class;)Ljava/lang/reflect/Constructor;"),
984   FAST_NATIVE_METHOD(Class, getDeclaredConstructorsInternal, "(Z)[Ljava/lang/reflect/Constructor;"),
985   FAST_NATIVE_METHOD(Class, getDeclaredField, "(Ljava/lang/String;)Ljava/lang/reflect/Field;"),
986   FAST_NATIVE_METHOD(Class, getPublicFieldRecursive, "(Ljava/lang/String;)Ljava/lang/reflect/Field;"),
987   FAST_NATIVE_METHOD(Class, getDeclaredFields, "()[Ljava/lang/reflect/Field;"),
988   FAST_NATIVE_METHOD(Class, getDeclaredFieldsUnchecked, "(Z)[Ljava/lang/reflect/Field;"),
989   FAST_NATIVE_METHOD(Class, getDeclaredMethodInternal,
990                 "(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;"),
991   FAST_NATIVE_METHOD(Class, getDeclaredMethodsUnchecked,
992                 "(Z)[Ljava/lang/reflect/Method;"),
993   FAST_NATIVE_METHOD(Class, getDeclaringClass, "()Ljava/lang/Class;"),
994   FAST_NATIVE_METHOD(Class, getEnclosingClass, "()Ljava/lang/Class;"),
995   FAST_NATIVE_METHOD(Class, getEnclosingConstructorNative, "()Ljava/lang/reflect/Constructor;"),
996   FAST_NATIVE_METHOD(Class, getEnclosingMethodNative, "()Ljava/lang/reflect/Method;"),
997   FAST_NATIVE_METHOD(Class, getInnerClassFlags, "(I)I"),
998   FAST_NATIVE_METHOD(Class, getInterfacesInternal, "()[Ljava/lang/Class;"),
999   FAST_NATIVE_METHOD(Class, getPrimitiveClass, "(Ljava/lang/String;)Ljava/lang/Class;"),
1000   FAST_NATIVE_METHOD(Class, getNameNative, "()Ljava/lang/String;"),
1001   FAST_NATIVE_METHOD(Class, getNestHostFromAnnotation, "()Ljava/lang/Class;"),
1002   FAST_NATIVE_METHOD(Class, getNestMembersFromAnnotation, "()[Ljava/lang/Class;"),
1003   FAST_NATIVE_METHOD(Class, getPermittedSubclassesFromAnnotation, "()[Ljava/lang/Class;"),
1004   FAST_NATIVE_METHOD(Class, getPublicDeclaredFields, "()[Ljava/lang/reflect/Field;"),
1005   FAST_NATIVE_METHOD(Class, getRecordAnnotationElement, "(Ljava/lang/String;Ljava/lang/Class;)[Ljava/lang/Object;"),
1006   FAST_NATIVE_METHOD(Class, getSignatureAnnotation, "()[Ljava/lang/String;"),
1007   FAST_NATIVE_METHOD(Class, getSimpleNameNative, "()Ljava/lang/String;"),
1008   FAST_NATIVE_METHOD(Class, isAnonymousClass, "()Z"),
1009   FAST_NATIVE_METHOD(Class, isDeclaredAnnotationPresent, "(Ljava/lang/Class;)Z"),
1010   FAST_NATIVE_METHOD(Class, isRecord0, "()Z"),
1011   FAST_NATIVE_METHOD(Class, newInstance, "()Ljava/lang/Object;"),
1012 };
1013 
register_java_lang_Class(JNIEnv * env)1014 void register_java_lang_Class(JNIEnv* env) {
1015   REGISTER_NATIVE_METHODS("java/lang/Class");
1016 }
1017 
1018 }  // namespace art
1019