blob: c4d4fae17c377bd636ab8d290efceb91b04a2d49 [file] [log] [blame]
Mathieu Chartier76433272014-09-26 14:32:37 -07001/*
2 * Copyright (C) 2014 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_INL_H_
18#define ART_RUNTIME_REFLECTION_INL_H_
19
20#include "reflection.h"
21
22#include "base/stringprintf.h"
23#include "common_throws.h"
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070024#include "jvalue-inl.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070025#include "mirror/object-inl.h"
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070026#include "obj_ptr-inl.h"
Mathieu Chartier76433272014-09-26 14:32:37 -070027#include "primitive.h"
28#include "utils.h"
29
30namespace art {
31
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000032inline bool ConvertPrimitiveValue(bool unbox_for_result,
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070033 Primitive::Type srcType,
34 Primitive::Type dstType,
35 const JValue& src,
36 JValue* dst) {
Mathieu Chartier76433272014-09-26 14:32:37 -070037 DCHECK(srcType != Primitive::kPrimNot && dstType != Primitive::kPrimNot);
38 if (LIKELY(srcType == dstType)) {
39 dst->SetJ(src.GetJ());
40 return true;
41 }
42 switch (dstType) {
43 case Primitive::kPrimBoolean: // Fall-through.
44 case Primitive::kPrimChar: // Fall-through.
45 case Primitive::kPrimByte:
46 // Only expect assignment with source and destination of identical type.
47 break;
48 case Primitive::kPrimShort:
49 if (srcType == Primitive::kPrimByte) {
50 dst->SetS(src.GetI());
51 return true;
52 }
53 break;
54 case Primitive::kPrimInt:
55 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
56 srcType == Primitive::kPrimShort) {
57 dst->SetI(src.GetI());
58 return true;
59 }
60 break;
61 case Primitive::kPrimLong:
62 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
63 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
64 dst->SetJ(src.GetI());
65 return true;
66 }
67 break;
68 case Primitive::kPrimFloat:
69 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
70 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
71 dst->SetF(src.GetI());
72 return true;
73 } else if (srcType == Primitive::kPrimLong) {
74 dst->SetF(src.GetJ());
75 return true;
76 }
77 break;
78 case Primitive::kPrimDouble:
79 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
80 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
81 dst->SetD(src.GetI());
82 return true;
83 } else if (srcType == Primitive::kPrimLong) {
84 dst->SetD(src.GetJ());
85 return true;
86 } else if (srcType == Primitive::kPrimFloat) {
87 dst->SetD(src.GetF());
88 return true;
89 }
90 break;
91 default:
92 break;
93 }
94 if (!unbox_for_result) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000095 ThrowIllegalArgumentException(StringPrintf("Invalid primitive conversion from %s to %s",
Mathieu Chartier76433272014-09-26 14:32:37 -070096 PrettyDescriptor(srcType).c_str(),
97 PrettyDescriptor(dstType).c_str()).c_str());
98 } else {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000099 ThrowClassCastException(StringPrintf("Couldn't convert result of type %s to %s",
Mathieu Chartier76433272014-09-26 14:32:37 -0700100 PrettyDescriptor(srcType).c_str(),
101 PrettyDescriptor(dstType).c_str()).c_str());
102 }
103 return false;
104}
105
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700106inline bool VerifyObjectIsClass(ObjPtr<mirror::Object> o, ObjPtr<mirror::Class> c) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700107 if (UNLIKELY(o == nullptr)) {
108 ThrowNullPointerException("null receiver");
109 return false;
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700110 } else if (UNLIKELY(!o->InstanceOf(c.Ptr()))) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700111 InvalidReceiverError(o, c);
112 return false;
113 }
114 return true;
115}
116
Mathieu Chartier76433272014-09-26 14:32:37 -0700117} // namespace art
118
119#endif // ART_RUNTIME_REFLECTION_INL_H_