blob: 68e7a10e01d137845f4b565b5662a870156e9373 [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
Orion Hodsonba28f9f2016-10-26 10:56:25 +010032inline bool ConvertPrimitiveValueNoThrow(Primitive::Type srcType,
33 Primitive::Type dstType,
34 const JValue& src,
35 JValue* dst) {
Mathieu Chartier76433272014-09-26 14:32:37 -070036 DCHECK(srcType != Primitive::kPrimNot && dstType != Primitive::kPrimNot);
37 if (LIKELY(srcType == dstType)) {
38 dst->SetJ(src.GetJ());
39 return true;
40 }
41 switch (dstType) {
42 case Primitive::kPrimBoolean: // Fall-through.
43 case Primitive::kPrimChar: // Fall-through.
44 case Primitive::kPrimByte:
45 // Only expect assignment with source and destination of identical type.
46 break;
47 case Primitive::kPrimShort:
48 if (srcType == Primitive::kPrimByte) {
49 dst->SetS(src.GetI());
50 return true;
51 }
52 break;
53 case Primitive::kPrimInt:
54 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
55 srcType == Primitive::kPrimShort) {
56 dst->SetI(src.GetI());
57 return true;
58 }
59 break;
60 case Primitive::kPrimLong:
61 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
62 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
63 dst->SetJ(src.GetI());
64 return true;
65 }
66 break;
67 case Primitive::kPrimFloat:
68 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
69 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
70 dst->SetF(src.GetI());
71 return true;
72 } else if (srcType == Primitive::kPrimLong) {
73 dst->SetF(src.GetJ());
74 return true;
75 }
76 break;
77 case Primitive::kPrimDouble:
78 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
79 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
80 dst->SetD(src.GetI());
81 return true;
82 } else if (srcType == Primitive::kPrimLong) {
83 dst->SetD(src.GetJ());
84 return true;
85 } else if (srcType == Primitive::kPrimFloat) {
86 dst->SetD(src.GetF());
87 return true;
88 }
89 break;
90 default:
91 break;
92 }
Orion Hodsonba28f9f2016-10-26 10:56:25 +010093 return false;
94}
95
96inline bool ConvertPrimitiveValue(bool unbox_for_result,
97 Primitive::Type srcType,
98 Primitive::Type dstType,
99 const JValue& src,
100 JValue* dst) {
101 if (ConvertPrimitiveValueNoThrow(srcType, dstType, src, dst)) {
102 return true;
103 }
104
Mathieu Chartier76433272014-09-26 14:32:37 -0700105 if (!unbox_for_result) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000106 ThrowIllegalArgumentException(StringPrintf("Invalid primitive conversion from %s to %s",
Mathieu Chartier76433272014-09-26 14:32:37 -0700107 PrettyDescriptor(srcType).c_str(),
108 PrettyDescriptor(dstType).c_str()).c_str());
109 } else {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000110 ThrowClassCastException(StringPrintf("Couldn't convert result of type %s to %s",
Mathieu Chartier76433272014-09-26 14:32:37 -0700111 PrettyDescriptor(srcType).c_str(),
112 PrettyDescriptor(dstType).c_str()).c_str());
113 }
114 return false;
115}
116
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700117inline bool VerifyObjectIsClass(ObjPtr<mirror::Object> o, ObjPtr<mirror::Class> c) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700118 if (UNLIKELY(o == nullptr)) {
119 ThrowNullPointerException("null receiver");
120 return false;
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700121 } else if (UNLIKELY(!o->InstanceOf(c.Ptr()))) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700122 InvalidReceiverError(o, c);
123 return false;
124 }
125 return true;
126}
127
Mathieu Chartier76433272014-09-26 14:32:37 -0700128} // namespace art
129
130#endif // ART_RUNTIME_REFLECTION_INL_H_