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