• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 #ifndef ART_RUNTIME_REFLECTION_H_
18 #define ART_RUNTIME_REFLECTION_H_
19 
20 #include "base/enums.h"
21 #include "base/locks.h"
22 #include "dex/primitive.h"
23 #include "jni.h"
24 #include "obj_ptr.h"
25 
26 namespace art {
27 namespace mirror {
28 class Class;
29 class Object;
30 }  // namespace mirror
31 class ArtField;
32 class ArtMethod;
33 union JValue;
34 class ScopedObjectAccessAlreadyRunnable;
35 class ShadowFrame;
36 
37 ObjPtr<mirror::Object> BoxPrimitive(Primitive::Type src_class, const JValue& value)
38     REQUIRES_SHARED(Locks::mutator_lock_);
39 
40 bool UnboxPrimitiveForField(ObjPtr<mirror::Object> o,
41                             ObjPtr<mirror::Class> dst_class,
42                             ArtField* f,
43                             JValue* unboxed_value)
44     REQUIRES_SHARED(Locks::mutator_lock_);
45 
46 bool UnboxPrimitiveForResult(ObjPtr<mirror::Object> o,
47                              ObjPtr<mirror::Class> dst_class,
48                              JValue* unboxed_value)
49     REQUIRES_SHARED(Locks::mutator_lock_);
50 
51 ALWAYS_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 
57 ALWAYS_INLINE bool ConvertPrimitiveValue(bool unbox_for_result,
58                                          Primitive::Type src_class,
59                                          Primitive::Type dst_class,
60                                          const JValue& src,
61                                          JValue* dst)
62     REQUIRES_SHARED(Locks::mutator_lock_);
63 
64 // Invokes the given method (either an ArtMethod or a jmethodID) with direct/static semantics.
65 template<typename MethodType>
66 JValue InvokeWithVarArgs(const ScopedObjectAccessAlreadyRunnable& soa,
67                          jobject obj,
68                          MethodType mid,
69                          va_list args)
70     REQUIRES_SHARED(Locks::mutator_lock_);
71 
72 // Invokes the given method (either an ArtMethod or a jmethodID) with reflection semantics.
73 template<typename MethodType>
74 JValue InvokeWithJValues(const ScopedObjectAccessAlreadyRunnable& soa,
75                          jobject obj,
76                          MethodType mid,
77                          const jvalue* args)
78     REQUIRES_SHARED(Locks::mutator_lock_);
79 
80 // 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.
83 template<typename MethodType>
84 JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccessAlreadyRunnable& soa,
85                                            jobject obj,
86                                            MethodType mid,
87                                            const jvalue* args)
88     REQUIRES_SHARED(Locks::mutator_lock_);
89 
90 // 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.
93 template<typename MethodType>
94 JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccessAlreadyRunnable& soa,
95                                            jobject obj,
96                                            MethodType mid,
97                                            va_list args)
98     REQUIRES_SHARED(Locks::mutator_lock_);
99 
100 // num_frames is number of frames we look up for access check.
101 template<PointerSize pointer_size>
102 NO_STACK_PROTECTOR
103 jobject InvokeMethod(const ScopedObjectAccessAlreadyRunnable& soa,
104                      jobject method,
105                      jobject receiver,
106                      jobject args,
107                      size_t num_frames = 1)
108     REQUIRES_SHARED(Locks::mutator_lock_);
109 
110 // Special-casing of the above. Assumes that the method is the correct constructor, the class is
111 // initialized, and that the receiver is an instance of the class.
112 void InvokeConstructor(const ScopedObjectAccessAlreadyRunnable& soa,
113                        ArtMethod* constructor,
114                        ObjPtr<mirror::Object> receiver,
115                        jobject args)
116     REQUIRES_SHARED(Locks::mutator_lock_);
117 
118 ALWAYS_INLINE bool VerifyObjectIsClass(ObjPtr<mirror::Object> o, ObjPtr<mirror::Class> c)
119     REQUIRES_SHARED(Locks::mutator_lock_);
120 
121 bool VerifyAccess(Thread* self,
122                   ObjPtr<mirror::Object> obj,
123                   ObjPtr<mirror::Class> declaring_class,
124                   uint32_t access_flags,
125                   ObjPtr<mirror::Class>* calling_class,
126                   size_t num_frames)
127     REQUIRES_SHARED(Locks::mutator_lock_);
128 
129 // This version takes a known calling class.
130 bool VerifyAccess(ObjPtr<mirror::Object> obj,
131                   ObjPtr<mirror::Class> declaring_class,
132                   uint32_t access_flags,
133                   ObjPtr<mirror::Class> calling_class)
134     REQUIRES_SHARED(Locks::mutator_lock_);
135 
136 // Get the calling class by using a stack visitor, may return null for unattached native threads.
137 ObjPtr<mirror::Class> GetCallingClass(Thread* self, size_t num_frames)
138     REQUIRES_SHARED(Locks::mutator_lock_);
139 
140 void InvalidReceiverError(ObjPtr<mirror::Object> o, ObjPtr<mirror::Class> c)
141     REQUIRES_SHARED(Locks::mutator_lock_);
142 
143 void UpdateReference(Thread* self, jobject obj, ObjPtr<mirror::Object> result)
144     REQUIRES_SHARED(Locks::mutator_lock_);
145 
146 }  // namespace art
147 
148 #endif  // ART_RUNTIME_REFLECTION_H_
149