Mathieu Chartier | 7643327 | 2014-09-26 14:32:37 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 22 | #include "android-base/stringprintf.h" |
| 23 | |
Mathieu Chartier | 7643327 | 2014-09-26 14:32:37 -0700 | [diff] [blame] | 24 | #include "common_throws.h" |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 25 | #include "jvalue-inl.h" |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 26 | #include "mirror/object-inl.h" |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 27 | #include "obj_ptr-inl.h" |
Mathieu Chartier | 7643327 | 2014-09-26 14:32:37 -0700 | [diff] [blame] | 28 | #include "primitive.h" |
| 29 | #include "utils.h" |
| 30 | |
| 31 | namespace art { |
| 32 | |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 33 | inline bool ConvertPrimitiveValueNoThrow(Primitive::Type srcType, |
| 34 | Primitive::Type dstType, |
| 35 | const JValue& src, |
| 36 | JValue* dst) { |
Mathieu Chartier | 7643327 | 2014-09-26 14:32:37 -0700 | [diff] [blame] | 37 | 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 | } |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 94 | return false; |
| 95 | } |
| 96 | |
| 97 | inline bool ConvertPrimitiveValue(bool unbox_for_result, |
| 98 | Primitive::Type srcType, |
| 99 | Primitive::Type dstType, |
| 100 | const JValue& src, |
| 101 | JValue* dst) { |
| 102 | if (ConvertPrimitiveValueNoThrow(srcType, dstType, src, dst)) { |
| 103 | return true; |
| 104 | } |
| 105 | |
Mathieu Chartier | 7643327 | 2014-09-26 14:32:37 -0700 | [diff] [blame] | 106 | if (!unbox_for_result) { |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 107 | ThrowIllegalArgumentException( |
| 108 | android::base::StringPrintf("Invalid primitive conversion from %s to %s", |
| 109 | PrettyDescriptor(srcType).c_str(), |
| 110 | PrettyDescriptor(dstType).c_str()).c_str()); |
Mathieu Chartier | 7643327 | 2014-09-26 14:32:37 -0700 | [diff] [blame] | 111 | } else { |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 112 | ThrowClassCastException(android::base::StringPrintf("Couldn't convert result of type %s to %s", |
| 113 | PrettyDescriptor(srcType).c_str(), |
| 114 | PrettyDescriptor(dstType).c_str()).c_str()); |
Mathieu Chartier | 7643327 | 2014-09-26 14:32:37 -0700 | [diff] [blame] | 115 | } |
| 116 | return false; |
| 117 | } |
| 118 | |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 119 | inline bool VerifyObjectIsClass(ObjPtr<mirror::Object> o, ObjPtr<mirror::Class> c) { |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 120 | if (UNLIKELY(o == nullptr)) { |
| 121 | ThrowNullPointerException("null receiver"); |
| 122 | return false; |
Mathieu Chartier | 1cc62e4 | 2016-10-03 18:01:28 -0700 | [diff] [blame] | 123 | } else if (UNLIKELY(!o->InstanceOf(c.Ptr()))) { |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 124 | InvalidReceiverError(o, c); |
| 125 | return false; |
| 126 | } |
| 127 | return true; |
| 128 | } |
| 129 | |
Mathieu Chartier | 7643327 | 2014-09-26 14:32:37 -0700 | [diff] [blame] | 130 | } // namespace art |
| 131 | |
| 132 | #endif // ART_RUNTIME_REFLECTION_INL_H_ |