IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
EnumHelper.cs
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2015 Jeroen Frijters
3
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
11
12 1. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
16 2. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
18 3. This notice may not be removed or altered from any source distribution.
19
20 Jeroen Frijters
21 jeroen@frijters.net
22
23*/
24using System;
25using System.Diagnostics;
26
27#if IMPORTER || EXPORTER
28using IKVM.Reflection;
29
30using Type = IKVM.Reflection.Type;
31#endif
32
33namespace IKVM.Runtime
34{
35
36 static class EnumHelper
37 {
38
39 internal static Type GetUnderlyingType(Type enumType)
40 {
41
42#if IMPORTER || EXPORTER
43 return enumType.GetEnumUnderlyingType();
44#else
45 return Enum.GetUnderlyingType(enumType);
46#endif
47 }
48
49#if IMPORTER
50
51 internal static object Parse(RuntimeContext context, Type type, string value)
52 {
53 object retval = null;
54 foreach (string str in value.Split(','))
55 {
56 FieldInfo field = type.GetField(str.Trim(), BindingFlags.Public | BindingFlags.Static);
57 if (field == null)
58 {
59 throw new InvalidOperationException("Enum value '" + str + "' not found in " + type.FullName);
60 }
61 if (retval == null)
62 {
63 retval = field.GetRawConstantValue();
64 }
65 else
66 {
67 retval = OrBoxedIntegrals(context, retval, field.GetRawConstantValue());
68 }
69 }
70
71 return retval;
72 }
73#endif
74
75 // note that we only support the integer types that C# supports
76 // (the CLI also supports bool, char, IntPtr & UIntPtr)
77 internal static object OrBoxedIntegrals(RuntimeContext context, object v1, object v2)
78 {
79 Debug.Assert(v1.GetType() == v2.GetType());
80 if (v1 is ulong)
81 {
82 ulong l1 = (ulong)v1;
83 ulong l2 = (ulong)v2;
84 return l1 | l2;
85 }
86 else
87 {
88 long v = ((IConvertible)v1).ToInt64(null) | ((IConvertible)v2).ToInt64(null);
89 switch (Type.GetTypeCode(context.Resolver.ResolveCoreType(v1.GetType().FullName).AsReflection()))
90 {
91 case TypeCode.SByte:
92 return (sbyte)v;
93 case TypeCode.Byte:
94 return (byte)v;
95 case TypeCode.Int16:
96 return (short)v;
97 case TypeCode.UInt16:
98 return (ushort)v;
99 case TypeCode.Int32:
100 return (int)v;
101 case TypeCode.UInt32:
102 return (uint)v;
103 case TypeCode.Int64:
104 return (long)v;
105 default:
106 throw new InvalidOperationException();
107 }
108 }
109 }
110
111 // this method can be used to convert an enum value or its underlying value to a Java primitive
112 internal static object GetPrimitiveValue(RuntimeContext context, Type underlyingType, object obj)
113 {
114 // Note that this method doesn't trust that obj is of the correct type,
115 // because it turns out there exist assemblies (e.g. gtk-sharp.dll) that
116 // have incorrectly typed enum constant values (e.g. int32 instead of uint32).
117 long value;
118 if (obj is ulong || (obj is Enum && underlyingType == context.Types.UInt64))
119 {
120 value = unchecked((long)((IConvertible)obj).ToUInt64(null));
121 }
122 else
123 {
124 value = ((IConvertible)obj).ToInt64(null);
125 }
126 if (underlyingType == context.Types.SByte || underlyingType == context.Types.Byte)
127 {
128 return unchecked((byte)value);
129 }
130 else if (underlyingType == context.Types.Int16 || underlyingType == context.Types.UInt16)
131 {
132 return unchecked((short)value);
133 }
134 else if (underlyingType == context.Types.Int32 || underlyingType == context.Types.UInt32)
135 {
136 return unchecked((int)value);
137 }
138 else if (underlyingType == context.Types.Int64 || underlyingType == context.Types.UInt64)
139 {
140 return value;
141 }
142 else
143 {
144 throw new InvalidOperationException();
145 }
146 }
147
148 }
149
150}
IKVM.Reflection.Type Type
IKVM.Reflection.FieldInfo FieldInfo
Maintains services relevant to an instane of the IKVM runtime.
Types Types
Gets the Types associated with this instance of the runtime.
ISymbolResolver Resolver
Gets the ISymbolResolver associated with this instance of the runtime.