IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
Util.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;
25
26using IKVM.Attributes;
27using IKVM.Runtime;
28
30{
31
32 static class Util
33 {
34
35 public static global::java.lang.Class getClassFromObject(object o)
36 {
37#if FIRST_PASS
38 throw new NotImplementedException();
39#else
40 return GetTypeWrapperFromObject(JVM.Context, o).ClassObject;
41#endif
42 }
43
44 internal static RuntimeJavaType GetTypeWrapperFromObject(RuntimeContext context, object o)
45 {
46 var ghostType = GhostTag.GetTag(o);
47 if (ghostType != null)
48 return ghostType;
49
50 var t = o.GetType();
51 if (t.IsPrimitive || context.ClassLoaderFactory.IsRemappedType(t) && !t.IsSealed)
53
54 for (; ; )
55 {
56 // if GetWrapperFromType returns null (or if tw.IsAbstract), that
57 // must mean that the Type of the object is an implementation helper class
58 // (e.g. an AtomicReferenceFieldUpdater or ThreadLocal instrinsic subclass)
59 var tw = context.ClassLoaderFactory.GetJavaTypeFromType(t);
60 if (tw != null && (!tw.IsAbstract || tw.IsArray))
61 return tw;
62
63 t = t.BaseType;
64 }
65 }
66
67 public static global::java.lang.Class getClassFromTypeHandle(RuntimeTypeHandle handle)
68 {
69#if FIRST_PASS
70 throw new NotImplementedException();
71#else
72 var t = Type.GetTypeFromHandle(handle);
73 if (t.IsPrimitive || JVM.Context.ClassLoaderFactory.IsRemappedType(t) || t == typeof(void))
74 return JVM.Context.ManagedJavaTypeFactory.GetJavaTypeFromManagedType(t).ClassObject;
75
76 if (!IsVisibleAsClass(t))
77 return null;
78
79 var tw = JVM.Context.ClassLoaderFactory.GetJavaTypeFromType(t);
80 if (tw != null)
81 return tw.ClassObject;
82
83 return null;
84#endif
85 }
86
87 public static global::java.lang.Class getClassFromTypeHandle(RuntimeTypeHandle handle, int rank)
88 {
89#if FIRST_PASS
90 throw new NotImplementedException();
91#else
92 var t = Type.GetTypeFromHandle(handle);
93 if (t.IsPrimitive || JVM.Context.ClassLoaderFactory.IsRemappedType(t) || t == typeof(void))
94 return JVM.Context.ManagedJavaTypeFactory.GetJavaTypeFromManagedType(t).MakeArrayType(rank).ClassObject;
95
96 if (!IsVisibleAsClass(t))
97 return null;
98
99 var tw = JVM.Context.ClassLoaderFactory.GetJavaTypeFromType(t);
100 if (tw != null)
101 return tw.MakeArrayType(rank).ClassObject;
102
103 return null;
104#endif
105 }
106
107 public static global::java.lang.Class getFriendlyClassFromType(Type type)
108 {
109#if FIRST_PASS
110 throw new NotImplementedException();
111#else
112 int rank = 0;
113 while (ReflectUtil.IsVector(type))
114 {
115 type = type.GetElementType();
116 rank++;
117 }
118
119 if (type.DeclaringType != null && JVM.Context.AttributeHelper.IsGhostInterface(type.DeclaringType))
120 type = type.DeclaringType;
121
122 if (!IsVisibleAsClass(type))
123 return null;
124
125 var wrapper = JVM.Context.ClassLoaderFactory.GetJavaTypeFromType(type);
126 if (wrapper == null)
127 return null;
128
129 if (rank > 0)
130 wrapper = wrapper.MakeArrayType(rank);
131
132 return wrapper.ClassObject;
133#endif
134 }
135
136 private static bool IsVisibleAsClass(Type type)
137 {
138 while (type.HasElementType)
139 {
140 if (type.IsPointer || type.IsByRef)
141 {
142 return false;
143 }
144 type = type.GetElementType();
145 }
146 if (type.ContainsGenericParameters && !type.IsGenericTypeDefinition)
147 {
148 return false;
149 }
150 System.Reflection.Emit.TypeBuilder tb = type as System.Reflection.Emit.TypeBuilder;
151 if (tb != null && !tb.IsCreated())
152 {
153 return false;
154 }
155 return true;
156 }
157
163 public static Type getInstanceTypeFromClass(global::java.lang.Class classObject)
164 {
165 var wrapper = RuntimeJavaType.FromClass(classObject);
166 if (wrapper.IsRemapped && wrapper.IsFinal)
167 return wrapper.TypeAsTBD;
168 else
169 return wrapper.TypeAsBaseType;
170 }
171
177 public static Type getRuntimeTypeFromClass(global::java.lang.Class classObject)
178 {
179 var wrapper = RuntimeJavaType.FromClass(classObject);
180 wrapper.Finish();
181
182 if (wrapper.IsRemapped && wrapper.IsFinal)
183 return wrapper.TypeAsTBD;
184 else
185 return wrapper.TypeAsBaseType;
186 }
187
188 [HideFromJava]
189 public static Exception mapException(Exception e)
190 {
191#if FIRST_PASS
192 throw new NotImplementedException();
193#else
194 return JVM.Context.ExceptionHelper.MapException<Exception>(e, true, false);
195#endif
196 }
197
198 public static Exception unmapException(Exception e)
199 {
200#if FIRST_PASS
201 throw new NotImplementedException();
202#else
203 return ExceptionHelper.UnmapException(e);
204#endif
205 }
206
207 }
208
209}
IKVM.Reflection.Type Type
static Exception mapException(Exception e)
Definition Util.cs:189
static global::java.lang.Class getClassFromObject(object o)
Definition Util.cs:35
static Type getInstanceTypeFromClass(global::java.lang.Class classObject)
Gets the underlying CLR Type instance.
Definition Util.cs:163
static global::java.lang.Class getClassFromTypeHandle(RuntimeTypeHandle handle)
Definition Util.cs:67
static Exception unmapException(Exception e)
Definition Util.cs:198
static global::java.lang.Class getFriendlyClassFromType(Type type)
Definition Util.cs:107
static Type getRuntimeTypeFromClass(global::java.lang.Class classObject)
Gets the underlying CLR Type instance resovled fully at runtime.
Definition Util.cs:177
static global::java.lang.Class getClassFromTypeHandle(RuntimeTypeHandle handle, int rank)
Definition Util.cs:87
Main state of the running JVM.
Maintains services relevant to an instane of the IKVM runtime.
AttributeHelper AttributeHelper
Gets the AttributeHelper associated with this instance of the runtime.
RuntimeManagedJavaTypeFactory ManagedJavaTypeFactory
Gets the RuntimeManagedJavaTypeFactory associated with this instance of the runtime.
ExceptionHelper ExceptionHelper
Gets the ExceptionHelper associated with this instance of the runtime.
RuntimeClassLoaderFactory ClassLoaderFactory
Gets the RuntimeClassLoaderFactory associated with this instance of the runtime.
RuntimeJavaType GetJavaTypeFromManagedType(Type type)
Gets the RuntimeJavaType associated with the specified managed type, or creates one on demand.