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 | |
| 22 | #include "base/stringprintf.h" |
| 23 | #include "common_throws.h" |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 24 | #include "jvalue-inl.h" |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 25 | #include "mirror/object-inl.h" |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 26 | #include "obj_ptr-inl.h" |
Mathieu Chartier | 7643327 | 2014-09-26 14:32:37 -0700 | [diff] [blame] | 27 | #include "primitive.h" |
| 28 | #include "utils.h" |
| 29 | |
| 30 | namespace art { |
| 31 | |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 32 | inline bool ConvertPrimitiveValue(bool unbox_for_result, |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 33 | 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 | } |
| 94 | if (!unbox_for_result) { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 95 | ThrowIllegalArgumentException(StringPrintf("Invalid primitive conversion from %s to %s", |
Mathieu Chartier | 7643327 | 2014-09-26 14:32:37 -0700 | [diff] [blame] | 96 | PrettyDescriptor(srcType).c_str(), |
| 97 | PrettyDescriptor(dstType).c_str()).c_str()); |
| 98 | } else { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 99 | ThrowClassCastException(StringPrintf("Couldn't convert result of type %s to %s", |
Mathieu Chartier | 7643327 | 2014-09-26 14:32:37 -0700 | [diff] [blame] | 100 | PrettyDescriptor(srcType).c_str(), |
| 101 | PrettyDescriptor(dstType).c_str()).c_str()); |
| 102 | } |
| 103 | return false; |
| 104 | } |
| 105 | |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 106 | inline bool VerifyObjectIsClass(ObjPtr<mirror::Object> o, ObjPtr<mirror::Class> c) { |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 107 | if (UNLIKELY(o == nullptr)) { |
| 108 | ThrowNullPointerException("null receiver"); |
| 109 | return false; |
Mathieu Chartier | 1cc62e4 | 2016-10-03 18:01:28 -0700 | [diff] [blame] | 110 | } else if (UNLIKELY(!o->InstanceOf(c.Ptr()))) { |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 111 | InvalidReceiverError(o, c); |
| 112 | return false; |
| 113 | } |
| 114 | return true; |
| 115 | } |
| 116 | |
Mathieu Chartier | 7643327 | 2014-09-26 14:32:37 -0700 | [diff] [blame] | 117 | } // namespace art |
| 118 | |
| 119 | #endif // ART_RUNTIME_REFLECTION_INL_H_ |