blob: b0e27da321e6dc5fd32927a024d232f34bbe03cf [file] [log] [blame]
Elliott Hughes418d20f2011-09-22 14:00:39 -07001/*
2 * Copyright (C) 2011 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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_REFLECTION_H_
18#define ART_RUNTIME_REFLECTION_H_
Elliott Hughes418d20f2011-09-22 14:00:39 -070019
liulvpingfff1d8f2020-12-21 09:43:37 +080020#include "base/enums.h"
Andreas Gampe7fbc4a52018-11-28 08:26:47 -080021#include "base/locks.h"
David Sehr67bf42e2018-02-26 16:43:04 -080022#include "dex/primitive.h"
Elliott Hughes418d20f2011-09-22 14:00:39 -070023#include "jni.h"
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070024#include "obj_ptr.h"
Elliott Hughes418d20f2011-09-22 14:00:39 -070025
26namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027namespace mirror {
Igor Murashkin2ffb7032017-11-08 13:35:21 -080028class Class;
29class Object;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030} // namespace mirror
Mathieu Chartierc7853442015-03-27 14:35:38 -070031class ArtField;
Mathieu Chartiere401d142015-04-22 13:56:20 -070032class ArtMethod;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033union JValue;
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -070034class ScopedObjectAccessAlreadyRunnable;
Ian Rogers53b8b092014-03-13 23:45:53 -070035class ShadowFrame;
Elliott Hughes418d20f2011-09-22 14:00:39 -070036
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070037ObjPtr<mirror::Object> BoxPrimitive(Primitive::Type src_class, const JValue& value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070038 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070039
40bool UnboxPrimitiveForField(ObjPtr<mirror::Object> o,
41 ObjPtr<mirror::Class> dst_class,
42 ArtField* f,
Ian Rogers84956ff2014-03-26 23:52:41 -070043 JValue* unboxed_value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070044 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070045
46bool UnboxPrimitiveForResult(ObjPtr<mirror::Object> o,
47 ObjPtr<mirror::Class> dst_class,
48 JValue* unboxed_value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070049 REQUIRES_SHARED(Locks::mutator_lock_);
Elliott Hughes418d20f2011-09-22 14:00:39 -070050
Orion Hodsonba28f9f2016-10-26 10:56:25 +010051ALWAYS_INLINE bool ConvertPrimitiveValueNoThrow(Primitive::Type src_class,
52 Primitive::Type dst_class,
53 const JValue& src,
54 JValue* dst)
55 REQUIRES_SHARED(Locks::mutator_lock_);
56
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000057ALWAYS_INLINE bool ConvertPrimitiveValue(bool unbox_for_result,
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070058 Primitive::Type src_class,
59 Primitive::Type dst_class,
60 const JValue& src,
61 JValue* dst)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070062 REQUIRES_SHARED(Locks::mutator_lock_);
Elliott Hughes418d20f2011-09-22 14:00:39 -070063
Alex Light01fbfbe2019-06-27 10:47:04 -070064// Invokes the given method (either an ArtMethod or a jmethodID) with direct/static semantics.
65template<typename MethodType>
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070066JValue InvokeWithVarArgs(const ScopedObjectAccessAlreadyRunnable& soa,
67 jobject obj,
Alex Light01fbfbe2019-06-27 10:47:04 -070068 MethodType mid,
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -070069 va_list args)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070070 REQUIRES_SHARED(Locks::mutator_lock_);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070071
Alex Light01fbfbe2019-06-27 10:47:04 -070072// Invokes the given method (either an ArtMethod or a jmethodID) with reflection semantics.
73template<typename MethodType>
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070074JValue InvokeWithJValues(const ScopedObjectAccessAlreadyRunnable& soa,
75 jobject obj,
Alex Light01fbfbe2019-06-27 10:47:04 -070076 MethodType mid,
Elliott Hughes22352f32018-06-15 17:33:58 -070077 const jvalue* args)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070078 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers53b8b092014-03-13 23:45:53 -070079
Alex Light01fbfbe2019-06-27 10:47:04 -070080// Invokes the given method (either an ArtMethod or a jmethodID) with virtual/interface semantics.
81// Note this will perform lookup based on the 'obj' to determine which implementation of the given
82// method should be invoked.
83template<typename MethodType>
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -070084JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccessAlreadyRunnable& soa,
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070085 jobject obj,
Alex Light01fbfbe2019-06-27 10:47:04 -070086 MethodType mid,
Elliott Hughes22352f32018-06-15 17:33:58 -070087 const jvalue* args)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070088 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers53b8b092014-03-13 23:45:53 -070089
Alex Light01fbfbe2019-06-27 10:47:04 -070090// Invokes the given method (either an ArtMethod or a jmethodID) with virtual/interface semantics.
91// Note this will perform lookup based on the 'obj' to determine which implementation of the given
92// method should be invoked.
93template<typename MethodType>
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -070094JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccessAlreadyRunnable& soa,
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070095 jobject obj,
Alex Light01fbfbe2019-06-27 10:47:04 -070096 MethodType mid,
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070097 va_list args)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070098 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers53b8b092014-03-13 23:45:53 -070099
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700100// num_frames is number of frames we look up for access check.
liulvpingfff1d8f2020-12-21 09:43:37 +0800101template<PointerSize pointer_size>
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700102jobject InvokeMethod(const ScopedObjectAccessAlreadyRunnable& soa,
103 jobject method,
104 jobject receiver,
105 jobject args,
106 size_t num_frames = 1)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700107 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers53b8b092014-03-13 23:45:53 -0700108
Andreas Gampe8ad7a3b2017-05-22 16:08:52 -0700109// Special-casing of the above. Assumes that the method is the correct constructor, the class is
110// initialized, and that the receiver is an instance of the class.
111void InvokeConstructor(const ScopedObjectAccessAlreadyRunnable& soa,
112 ArtMethod* constructor,
113 ObjPtr<mirror::Object> receiver,
114 jobject args)
115 REQUIRES_SHARED(Locks::mutator_lock_);
116
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700117ALWAYS_INLINE bool VerifyObjectIsClass(ObjPtr<mirror::Object> o, ObjPtr<mirror::Class> c)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700118 REQUIRES_SHARED(Locks::mutator_lock_);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700119
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700120bool VerifyAccess(Thread* self,
121 ObjPtr<mirror::Object> obj,
122 ObjPtr<mirror::Class> declaring_class,
123 uint32_t access_flags,
124 ObjPtr<mirror::Class>* calling_class,
125 size_t num_frames)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700126 REQUIRES_SHARED(Locks::mutator_lock_);
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700127
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700128// This version takes a known calling class.
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700129bool VerifyAccess(ObjPtr<mirror::Object> obj,
130 ObjPtr<mirror::Class> declaring_class,
Mathieu Chartier268764d2016-09-13 12:09:38 -0700131 uint32_t access_flags,
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700132 ObjPtr<mirror::Class> calling_class)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700133 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700134
135// Get the calling class by using a stack visitor, may return null for unattached native threads.
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700136ObjPtr<mirror::Class> GetCallingClass(Thread* self, size_t num_frames)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700137 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierf36cb5f2015-04-24 16:55:16 -0700138
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700139void InvalidReceiverError(ObjPtr<mirror::Object> o, ObjPtr<mirror::Class> c)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700140 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700141
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700142void UpdateReference(Thread* self, jobject obj, ObjPtr<mirror::Object> result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700143 REQUIRES_SHARED(Locks::mutator_lock_);
Jeff Hao83c81952015-05-27 19:29:29 -0700144
Elliott Hughes418d20f2011-09-22 14:00:39 -0700145} // namespace art
146
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700147#endif // ART_RUNTIME_REFLECTION_H_