IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
RuntimeManagedJavaType.EnumEnumJavaType.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.Collections.Generic;
26
27using IKVM.Attributes;
28
29#if IMPORTER || EXPORTER
30using IKVM.Reflection;
32
33using Type = IKVM.Reflection.Type;
34#else
35using System.Reflection;
36using System.Reflection.Emit;
37#endif
38
39#if IMPORTER
41#endif
42
43namespace IKVM.Runtime
44{
45
46 sealed partial class RuntimeManagedJavaType
47 {
48
49 sealed partial class EnumEnumJavaType : FakeJavaType
50 {
51
52 readonly Type fakeType;
53
60 internal EnumEnumJavaType(RuntimeContext context, string name, Type enumType) :
61 base(context, Modifiers.Public | Modifiers.Enum | Modifiers.Final, name, context.ClassLoaderFactory.LoadClassCritical("java.lang.Enum"))
62 {
63#if IMPORTER || EXPORTER
64 this.fakeType = context.FakeTypes.GetEnumType(enumType);
65#elif !FIRST_PASS
66 this.fakeType = typeof(ikvm.@internal.EnumEnum<>).MakeGenericType(enumType);
67#endif
68 }
69
70#if !IMPORTER && !EXPORTER && !FIRST_PASS
71
72 internal object GetUnspecifiedValue()
73 {
74 return GetFieldWrapper("__unspecified", this.SigName).GetValue(null);
75 }
76
77#endif
78
79 sealed class EnumJavaField : RuntimeJavaField
80 {
81
82#if !IMPORTER && !EXPORTER
83
84 readonly int ordinal;
85 object val;
86
87#endif
88
95 internal EnumJavaField(RuntimeJavaType tw, string name, int ordinal) :
96 base(tw, tw, name, tw.SigName, Modifiers.Public | Modifiers.Static | Modifiers.Final | Modifiers.Enum, null, MemberFlags.None)
97 {
98#if !IMPORTER && !EXPORTER
99 this.ordinal = ordinal;
100#endif
101 }
102
103#if !IMPORTER && !EXPORTER && !FIRST_PASS
104
105 internal override object GetValue(object obj)
106 {
107 if (val == null)
108 System.Threading.Interlocked.CompareExchange(ref val, Activator.CreateInstance(this.DeclaringType.TypeAsTBD, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, new object[] { this.Name, ordinal }, null), null);
109
110 return val;
111 }
112
113 internal override void SetValue(object obj, object value)
114 {
115
116 }
117
118#endif
119
120#if EMITTERS
121 protected override void EmitGetImpl(CodeEmitter ilgen)
122 {
123 var typeofByteCodeHelper = DeclaringType.Context.Resolver.ResolveRuntimeType("IKVM.Runtime.ByteCodeHelper").AsReflection();
124 ilgen.Emit(OpCodes.Ldstr, Name);
125 ilgen.Emit(OpCodes.Call, typeofByteCodeHelper.GetMethod("GetDotNetEnumField").MakeGenericMethod(DeclaringType.TypeAsBaseType));
126 }
127
128 protected override void EmitSetImpl(CodeEmitter ilgen)
129 {
130 throw new NotImplementedException();
131 }
132
133#endif
134
135 }
136
137 sealed class EnumValuesJavaMethod : RuntimeJavaMethod
138 {
139
144 internal EnumValuesJavaMethod(RuntimeJavaType declaringType) :
145 base(declaringType, "values", "()[" + declaringType.SigName, null, declaringType.MakeArrayType(1), Array.Empty<RuntimeJavaType>(), Modifiers.Public | Modifiers.Static, MemberFlags.None)
146 {
147
148 }
149
150 internal override bool IsDynamicOnly => true;
151
152#if !IMPORTER && !FIRST_PASS && !EXPORTER
153
154 internal override object Invoke(object obj, object[] args)
155 {
156 RuntimeJavaField[] values = this.DeclaringType.GetFields();
157 object[] array = (object[])Array.CreateInstance(this.DeclaringType.TypeAsArrayType, values.Length);
158 for (int i = 0; i < values.Length; i++)
159 {
160 array[i] = values[i].GetValue(null);
161 }
162 return array;
163 }
164
165#endif
166
167 }
168
169 protected override void LazyPublishMembers()
170 {
171 var fields = new List<RuntimeJavaField>();
172 int ordinal = 0;
173 foreach (var field in DeclaringTypeWrapper.TypeAsTBD.GetFields(BindingFlags.Static | BindingFlags.Public))
174 if (field.IsLiteral)
175 fields.Add(new EnumJavaField(this, field.Name, ordinal++));
176
177 // TODO if the enum already has an __unspecified value, rename this one
178 fields.Add(new EnumJavaField(this, "__unspecified", ordinal++));
179 SetFields(fields.ToArray());
180 SetMethods(new RuntimeJavaMethod[] { new EnumValuesJavaMethod(this), new EnumValueOfJavaMethod(this) });
181 base.LazyPublishMembers();
182 }
183
184 internal override RuntimeJavaType DeclaringTypeWrapper => Context.ClassLoaderFactory.GetJavaTypeFromType(fakeType.GetGenericArguments()[0]);
185
186 internal override RuntimeClassLoader ClassLoader => DeclaringTypeWrapper.ClassLoader;
187
188 internal override Type TypeAsTBD => fakeType;
189
190 internal override bool IsFastClassLiteralSafe => true;
191
192 }
193
194 }
195
196}
IKVM.Reflection.Type Type
global::java.lang.invoke.LambdaForm.Name Name
ISymbolResolver Resolver
Gets the ISymbolResolver associated with this instance of the runtime.
MemberFlags
Describes various options applied to a member.