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/mutex.h"
21 #include "jni.h"
22 #include "obj_ptr.h"
23 #include "primitive.h"
24 
25 namespace art {
26 namespace mirror {
27   class Class;
28   class Object;
29 }  // namespace mirror
30 class ArtField;
31 class ArtMethod;
32 union JValue;
33 class ScopedObjectAccessAlreadyRunnable;
34 class ShadowFrame;
35 
36 ObjPtr<mirror::Object> BoxPrimitive(Primitive::Type src_class, const JValue& value)
37     REQUIRES_SHARED(Locks::mutator_lock_);
38 
39 bool UnboxPrimitiveForField(ObjPtr<mirror::Object> o,
40                             ObjPtr<mirror::Class> dst_class,
41                             ArtField* f,
42                             JValue* unboxed_value)
43     REQUIRES_SHARED(Locks::mutator_lock_);
44 
45 bool UnboxPrimitiveForResult(ObjPtr<mirror::Object> o,
46                              ObjPtr<mirror::Class> dst_class,
47                              JValue* unboxed_value)
48     REQUIRES_SHARED(Locks::mutator_lock_);
49 
50 ALWAYS_INLINE bool ConvertPrimitiveValueNoThrow(Primitive::Type src_class,
51                                                 Primitive::Type dst_class,
52                                                 const JValue& src,
53                                                 JValue* dst)
54     REQUIRES_SHARED(Locks::mutator_lock_);
55 
56 ALWAYS_INLINE bool ConvertPrimitiveValue(bool unbox_for_result,
57                                          Primitive::Type src_class,
58                                          Primitive::Type dst_class,
59                                          const JValue& src,
60                                          JValue* dst)
61     REQUIRES_SHARED(Locks::mutator_lock_);
62 
63 JValue InvokeWithVarArgs(const ScopedObjectAccessAlreadyRunnable& soa,
64                          jobject obj,
65                          jmethodID mid,
66                          va_list args)
67     REQUIRES_SHARED(Locks::mutator_lock_);
68 
69 JValue InvokeWithJValues(const ScopedObjectAccessAlreadyRunnable& soa,
70                          jobject obj,
71                          jmethodID mid,
72                          jvalue* args)
73     REQUIRES_SHARED(Locks::mutator_lock_);
74 
75 JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccessAlreadyRunnable& soa,
76                                            jobject obj,
77                                            jmethodID mid,
78                                            jvalue* args)
79     REQUIRES_SHARED(Locks::mutator_lock_);
80 
81 JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccessAlreadyRunnable& soa,
82                                            jobject obj,
83                                            jmethodID mid,
84                                            va_list args)
85     REQUIRES_SHARED(Locks::mutator_lock_);
86 
87 // num_frames is number of frames we look up for access check.
88 jobject InvokeMethod(const ScopedObjectAccessAlreadyRunnable& soa,
89                      jobject method,
90                      jobject receiver,
91                      jobject args,
92                      size_t num_frames = 1)
93     REQUIRES_SHARED(Locks::mutator_lock_);
94 
95 ALWAYS_INLINE bool VerifyObjectIsClass(ObjPtr<mirror::Object> o, ObjPtr<mirror::Class> c)
96     REQUIRES_SHARED(Locks::mutator_lock_);
97 
98 bool VerifyAccess(Thread* self,
99                   ObjPtr<mirror::Object> obj,
100                   ObjPtr<mirror::Class> declaring_class,
101                   uint32_t access_flags,
102                   ObjPtr<mirror::Class>* calling_class,
103                   size_t num_frames)
104     REQUIRES_SHARED(Locks::mutator_lock_);
105 
106 // This version takes a known calling class.
107 bool VerifyAccess(ObjPtr<mirror::Object> obj,
108                   ObjPtr<mirror::Class> declaring_class,
109                   uint32_t access_flags,
110                   ObjPtr<mirror::Class> calling_class)
111     REQUIRES_SHARED(Locks::mutator_lock_);
112 
113 // Get the calling class by using a stack visitor, may return null for unattached native threads.
114 ObjPtr<mirror::Class> GetCallingClass(Thread* self, size_t num_frames)
115     REQUIRES_SHARED(Locks::mutator_lock_);
116 
117 void InvalidReceiverError(ObjPtr<mirror::Object> o, ObjPtr<mirror::Class> c)
118     REQUIRES_SHARED(Locks::mutator_lock_);
119 
120 void UpdateReference(Thread* self, jobject obj, ObjPtr<mirror::Object> result)
121     REQUIRES_SHARED(Locks::mutator_lock_);
122 
123 }  // namespace art
124 
125 #endif  // ART_RUNTIME_REFLECTION_H_
126