|
2 | 2 |
|
3 | 3 | using System; |
4 | 4 | using System.Collections.Generic; |
| 5 | +using System.Runtime.CompilerServices; |
5 | 6 | using System.Diagnostics; |
6 | 7 | using System.Linq.Expressions; |
7 | 8 | using System.Reflection; |
|
11 | 12 | namespace Java.Interop { |
12 | 13 |
|
13 | 14 | partial class JniRuntime { |
| 15 | + static JniTypeSignature __StringTypeSignature; |
| 16 | + static JniTypeSignature __VoidTypeSignature; |
| 17 | + static JniTypeSignature __BooleanTypeSignature; |
| 18 | + static JniTypeSignature __BooleanNullableTypeSignature; |
| 19 | + static JniTypeSignature __SByteTypeSignature; |
| 20 | + static JniTypeSignature __SByteNullableTypeSignature; |
| 21 | + static JniTypeSignature __CharTypeSignature; |
| 22 | + static JniTypeSignature __CharNullableTypeSignature; |
| 23 | + static JniTypeSignature __Int16TypeSignature; |
| 24 | + static JniTypeSignature __Int16NullableTypeSignature; |
| 25 | + static JniTypeSignature __Int32TypeSignature; |
| 26 | + static JniTypeSignature __Int32NullableTypeSignature; |
| 27 | + static JniTypeSignature __Int64TypeSignature; |
| 28 | + static JniTypeSignature __Int64NullableTypeSignature; |
| 29 | + static JniTypeSignature __SingleTypeSignature; |
| 30 | + static JniTypeSignature __SingleNullableTypeSignature; |
| 31 | + static JniTypeSignature __DoubleTypeSignature; |
| 32 | + static JniTypeSignature __DoubleNullableTypeSignature; |
| 33 | + |
| 34 | + [MethodImpl (MethodImplOptions.AggressiveInlining)] |
| 35 | + static JniTypeSignature GetCachedTypeSignature (ref JniTypeSignature field, string signature, int arrayRank = 0, bool keyword = false) |
| 36 | + { |
| 37 | + if (!field.IsValid) |
| 38 | + field = new JniTypeSignature (signature, arrayRank, keyword); |
| 39 | + return field; |
| 40 | + } |
| 41 | + |
| 42 | + static bool GetBuiltInTypeSignature (Type type, ref JniTypeSignature signature) |
| 43 | + { |
| 44 | + switch (Type.GetTypeCode (type)) { |
| 45 | + case TypeCode.String: |
| 46 | + signature = GetCachedTypeSignature (ref __StringTypeSignature, "java/lang/String"); |
| 47 | + return true; |
| 48 | + case TypeCode.Boolean: |
| 49 | + signature = GetCachedTypeSignature (ref __BooleanTypeSignature, "Z", arrayRank: 0, keyword: true); |
| 50 | + return true; |
| 51 | + case TypeCode.SByte: |
| 52 | + signature = GetCachedTypeSignature (ref __SByteTypeSignature, "B", arrayRank: 0, keyword: true); |
| 53 | + return true; |
| 54 | + case TypeCode.Char: |
| 55 | + signature = GetCachedTypeSignature (ref __CharTypeSignature, "C", arrayRank: 0, keyword: true); |
| 56 | + return true; |
| 57 | + case TypeCode.Int16: |
| 58 | + signature = GetCachedTypeSignature (ref __Int16TypeSignature, "S", arrayRank: 0, keyword: true); |
| 59 | + return true; |
| 60 | + case TypeCode.Int32: |
| 61 | + signature = GetCachedTypeSignature (ref __Int32TypeSignature, "I", arrayRank: 0, keyword: true); |
| 62 | + return true; |
| 63 | + case TypeCode.Int64: |
| 64 | + signature = GetCachedTypeSignature (ref __Int64TypeSignature, "J", arrayRank: 0, keyword: true); |
| 65 | + return true; |
| 66 | + case TypeCode.Single: |
| 67 | + signature = GetCachedTypeSignature (ref __SingleTypeSignature, "F", arrayRank: 0, keyword: true); |
| 68 | + return true; |
| 69 | + case TypeCode.Double: |
| 70 | + signature = GetCachedTypeSignature (ref __DoubleTypeSignature, "D", arrayRank: 0, keyword: true); |
| 71 | + return true; |
| 72 | + case TypeCode.DateTime: |
| 73 | + case TypeCode.DBNull: |
| 74 | + case TypeCode.Decimal: |
| 75 | + case TypeCode.Empty: |
| 76 | + case TypeCode.UInt16: |
| 77 | + case TypeCode.UInt32: |
| 78 | + case TypeCode.UInt64: |
| 79 | + return false; |
| 80 | + } |
14 | 81 |
|
15 | | - static readonly Lazy<KeyValuePair<Type, JniTypeSignature>[]> JniBuiltinTypeNameMappings = new Lazy<KeyValuePair<Type, JniTypeSignature>[]> (InitJniBuiltinTypeNameMappings); |
| 82 | + if (type == typeof (void)) { |
| 83 | + signature = GetCachedTypeSignature (ref __VoidTypeSignature, "V", arrayRank: 0, keyword: true); |
| 84 | + return true; |
| 85 | + } |
16 | 86 |
|
17 | | - static KeyValuePair<Type, JniTypeSignature>[] InitJniBuiltinTypeNameMappings () |
18 | | - { |
19 | | - return new []{ |
20 | | - new KeyValuePair<Type, JniTypeSignature>(typeof (string), new JniTypeSignature ("java/lang/String")), |
21 | | - |
22 | | - new KeyValuePair<Type, JniTypeSignature>(typeof (void), new JniTypeSignature ("V", arrayRank: 0, keyword: true)), |
23 | | - new KeyValuePair<Type, JniTypeSignature>(typeof (void), new JniTypeSignature ("java/lang/Void")), |
24 | | - |
25 | | - new KeyValuePair<Type, JniTypeSignature>(typeof (Boolean), new JniTypeSignature ("Z", 0, keyword: true)), |
26 | | - new KeyValuePair<Type, JniTypeSignature>(typeof (Boolean?), new JniTypeSignature ("java/lang/Boolean")), |
27 | | - new KeyValuePair<Type, JniTypeSignature>(typeof (SByte), new JniTypeSignature ("B", 0, keyword: true)), |
28 | | - new KeyValuePair<Type, JniTypeSignature>(typeof (SByte?), new JniTypeSignature ("java/lang/Byte")), |
29 | | - new KeyValuePair<Type, JniTypeSignature>(typeof (Char), new JniTypeSignature ("C", 0, keyword: true)), |
30 | | - new KeyValuePair<Type, JniTypeSignature>(typeof (Char?), new JniTypeSignature ("java/lang/Character")), |
31 | | - new KeyValuePair<Type, JniTypeSignature>(typeof (Int16), new JniTypeSignature ("S", 0, keyword: true)), |
32 | | - new KeyValuePair<Type, JniTypeSignature>(typeof (Int16?), new JniTypeSignature ("java/lang/Short")), |
33 | | - new KeyValuePair<Type, JniTypeSignature>(typeof (Int32), new JniTypeSignature ("I", 0, keyword: true)), |
34 | | - new KeyValuePair<Type, JniTypeSignature>(typeof (Int32?), new JniTypeSignature ("java/lang/Integer")), |
35 | | - new KeyValuePair<Type, JniTypeSignature>(typeof (Int64), new JniTypeSignature ("J", 0, keyword: true)), |
36 | | - new KeyValuePair<Type, JniTypeSignature>(typeof (Int64?), new JniTypeSignature ("java/lang/Long")), |
37 | | - new KeyValuePair<Type, JniTypeSignature>(typeof (Single), new JniTypeSignature ("F", 0, keyword: true)), |
38 | | - new KeyValuePair<Type, JniTypeSignature>(typeof (Single?), new JniTypeSignature ("java/lang/Float")), |
39 | | - new KeyValuePair<Type, JniTypeSignature>(typeof (Double), new JniTypeSignature ("D", 0, keyword: true)), |
40 | | - new KeyValuePair<Type, JniTypeSignature>(typeof (Double?), new JniTypeSignature ("java/lang/Double")), |
41 | | - }; |
| 87 | + if (!type.IsValueType) |
| 88 | + return false; |
| 89 | + |
| 90 | + if (type == typeof (Boolean?)) { |
| 91 | + signature = GetCachedTypeSignature (ref __BooleanNullableTypeSignature, "java/lang/Boolean"); |
| 92 | + return true; |
| 93 | + } |
| 94 | + if (type == typeof (SByte?)) { |
| 95 | + signature = GetCachedTypeSignature (ref __SByteNullableTypeSignature, "java/lang/Byte"); |
| 96 | + return true; |
| 97 | + } |
| 98 | + if (type == typeof (Char?)) { |
| 99 | + signature = GetCachedTypeSignature (ref __CharNullableTypeSignature, "java/lang/Character"); |
| 100 | + return true; |
| 101 | + } |
| 102 | + if (type == typeof (Int16?)) { |
| 103 | + signature = GetCachedTypeSignature (ref __Int16NullableTypeSignature, "java/lang/Short"); |
| 104 | + return true; |
| 105 | + } |
| 106 | + if (type == typeof (Int32?)) { |
| 107 | + signature = GetCachedTypeSignature (ref __Int32NullableTypeSignature, "java/lang/Integer"); |
| 108 | + return true; |
| 109 | + } |
| 110 | + if (type == typeof (Int64?)) { |
| 111 | + signature = GetCachedTypeSignature (ref __Int64NullableTypeSignature, "java/lang/Long"); |
| 112 | + return true; |
| 113 | + } |
| 114 | + if (type == typeof (Single?)) { |
| 115 | + signature = GetCachedTypeSignature (ref __SingleNullableTypeSignature, "java/lang/Float"); |
| 116 | + return true; |
| 117 | + } |
| 118 | + if (type == typeof (Double?)) { |
| 119 | + signature = GetCachedTypeSignature (ref __DoubleNullableTypeSignature, "java/lang/Double"); |
| 120 | + return true; |
| 121 | + } |
| 122 | + |
| 123 | + return false; |
| 124 | + } |
| 125 | + |
| 126 | + static readonly Lazy<Dictionary<string, Type>> JniBuiltinSimpleReferenceToType = new Lazy<Dictionary<string, Type>> (InitJniBuiltinSimpleReferenceToType); |
| 127 | + |
| 128 | + static Dictionary<string, Type> InitJniBuiltinSimpleReferenceToType () |
| 129 | + { |
| 130 | + return new Dictionary<string, Type> (StringComparer.Ordinal) { |
| 131 | + {"java/lang/String", typeof (string)}, |
| 132 | + {"V", typeof (void)}, |
| 133 | + {"Z", typeof (Boolean)}, |
| 134 | + {"java/lang/Boolean", typeof (Boolean?)}, |
| 135 | + {"B", typeof (SByte)}, |
| 136 | + {"java/lang/Byte", typeof (SByte?)}, |
| 137 | + {"C", typeof (Char)}, |
| 138 | + {"java/lang/Character", typeof (Char?)}, |
| 139 | + {"S", typeof (Int16)}, |
| 140 | + {"java/lang/Short", typeof (Int16?)}, |
| 141 | + {"I", typeof (Int32)}, |
| 142 | + {"java/lang/Integer", typeof (Int32?)}, |
| 143 | + {"J", typeof (Int64)}, |
| 144 | + {"java/lang/Long", typeof (Int64?)}, |
| 145 | + {"F", typeof (Single)}, |
| 146 | + {"java/lang/Float", typeof (Single?)}, |
| 147 | + {"D", typeof (Double)}, |
| 148 | + {"java/lang/Double", typeof (Double?)}, |
| 149 | + }; |
42 | 150 | } |
43 | 151 |
|
44 | 152 | static readonly Lazy<KeyValuePair<Type, JniValueMarshaler>[]> JniBuiltinMarshalers = new Lazy<KeyValuePair<Type, JniValueMarshaler>[]> (InitJniBuiltinMarshalers); |
|
0 commit comments