IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
RuntimeArrayJavaType.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
27using IKVM.Attributes;
28
29#if IMPORTER || EXPORTER
30using IKVM.Reflection;
32
33using Type = IKVM.Reflection.Type;
34#else
35using System.Reflection;
36#endif
37
38namespace IKVM.Runtime
39{
40
41 sealed class RuntimeArrayJavaType : RuntimeJavaType
42 {
43
44 volatile RuntimeJavaType[] interfaces;
45 readonly RuntimeJavaType ultimateElementTypeWrapper;
46 Type arrayType;
47 bool finished;
48
55 internal RuntimeArrayJavaType(RuntimeContext context, RuntimeJavaType ultimateElementTypeWrapper, string name) :
56 base(context, ultimateElementTypeWrapper.IsInternal ? TypeFlags.InternalAccess : TypeFlags.None, Modifiers.Final | Modifiers.Abstract | (ultimateElementTypeWrapper.Modifiers & Modifiers.Public), name)
57 {
58 Debug.Assert(!ultimateElementTypeWrapper.IsArray);
59 this.ultimateElementTypeWrapper = ultimateElementTypeWrapper;
60 }
61
62 internal override RuntimeJavaType BaseTypeWrapper
63 {
64 get { return Context.JavaBase.TypeOfJavaLangObject; }
65 }
66
67 internal override RuntimeClassLoader ClassLoader => ultimateElementTypeWrapper.ClassLoader;
68
69 internal static MethodInfo GetCloneMethod(RuntimeContext context)
70 {
71 return context.Types.Array.GetMethod("Clone", BindingFlags.Public | BindingFlags.Instance, null, Type.EmptyTypes, null);
72 }
73
74 protected override void LazyPublishMembers()
75 {
76 var mw = new RuntimeSimpleCallJavaMethod(this, "clone", "()Ljava.lang.Object;", GetCloneMethod(Context), Context.JavaBase.TypeOfJavaLangObject, Array.Empty<RuntimeJavaType>(), Modifiers.Public, MemberFlags.HideFromReflection, SimpleOpCode.Callvirt, SimpleOpCode.Callvirt);
77 mw.Link();
78 SetMethods(new RuntimeJavaMethod[] { mw });
79 SetFields(Array.Empty<RuntimeJavaField>());
80 }
81
82 internal override Modifiers ReflectiveModifiers
83 {
84 get
85 {
86 return Modifiers.Final | Modifiers.Abstract | (ultimateElementTypeWrapper.ReflectiveModifiers & Modifiers.AccessMask);
87 }
88 }
89
90 internal override string SigName
91 {
92 get
93 {
94 // for arrays the signature name is the same as the normal name
95 return Name;
96 }
97 }
98
99 internal override RuntimeJavaType[] Interfaces
100 {
101 get
102 {
103 if (interfaces == null)
104 {
105 RuntimeJavaType[] tw = new RuntimeJavaType[2];
106 tw[0] = Context.JavaBase.TypeOfJavaLangCloneable;
107 tw[1] = Context.JavaBase.TypeOfJavaIoSerializable;
108 interfaces = tw;
109 }
110 return interfaces;
111 }
112 }
113
114 internal override Type TypeAsTBD
115 {
116 get
117 {
118 while (arrayType == null)
119 {
120 bool prevFinished = finished;
121 Type type = MakeArrayType(ultimateElementTypeWrapper.TypeAsArrayType, this.ArrayRank);
122 if (prevFinished)
123 {
124 // We were already finished prior to the call to MakeArrayType, so we can safely
125 // set arrayType to the finished type.
126 // Note that this takes advantage of the fact that once we've been finished,
127 // we can never become unfinished.
128 arrayType = type;
129 }
130 else
131 {
132 lock (this)
133 {
134 // To prevent a race with Finish, we can only set arrayType in this case
135 // (inside the locked region) if we've not already finished. If we have
136 // finished, we need to rerun MakeArrayType on the now finished element type.
137 // Note that there is a benign race left, because it is possible that another
138 // thread finishes right after we've set arrayType and exited the locked
139 // region. This is not problem, because TypeAsTBD is only guaranteed to
140 // return a finished type *after* Finish has been called.
141 if (!finished)
142 {
143 arrayType = type;
144 }
145 }
146 }
147 }
148
149 return arrayType;
150 }
151 }
152
153 internal override void Finish()
154 {
155 if (!finished)
156 {
157 ultimateElementTypeWrapper.Finish();
158 lock (this)
159 {
160 // Now that we've finished the element type, we must clear arrayType,
161 // because it may still refer to a TypeBuilder. Note that we have to
162 // do this atomically with setting "finished", to prevent a race
163 // with TypeAsTBD.
164 finished = true;
165 arrayType = null;
166 }
167 }
168 }
169
170 internal override bool IsFastClassLiteralSafe
171 {
172 // here we have to deal with the somewhat strange fact that in Java you cannot represent primitive type class literals,
173 // but you can represent arrays of primitive types as a class literal
174 get { return ultimateElementTypeWrapper.IsFastClassLiteralSafe || ultimateElementTypeWrapper.IsPrimitive; }
175 }
176
177 internal override RuntimeJavaType GetUltimateElementTypeWrapper()
178 {
179 return ultimateElementTypeWrapper;
180 }
181
182 internal static Type MakeArrayType(Type type, int dims)
183 {
184 // NOTE this is not just an optimization, but it is also required to
185 // make sure that ReflectionOnly types stay ReflectionOnly types
186 // (in particular instantiations of generic types from mscorlib that
187 // have ReflectionOnly type parameters).
188 for (int i = 0; i < dims; i++)
189 {
190 type = type.MakeArrayType();
191 }
192 return type;
193 }
194 }
195
196}
IKVM.Reflection.Type Type
IKVM.Reflection.MethodInfo MethodInfo
global::java.lang.invoke.LambdaForm.Name Name
Runtime support for a class loader.
Maintains services relevant to an instane of the IKVM runtime.
Types Types
Gets the Types associated with this instance of the runtime.
CoreClasses JavaBase
Gets the CoreClasses associated with this instance of the runtime.
MemberFlags
Describes various options applied to a member.