IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
RuntimeByteCodeJavaType.Metadata.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 IKVM.ByteCode.Buffers;
25using IKVM.ByteCode.Decoding;
26using IKVM.ByteCode.Encoding;
27
28namespace IKVM.Runtime
29{
30
31#if IMPORTER
32 abstract partial class RuntimeByteCodeJavaType : RuntimeJavaType
33#else
34#pragma warning disable 628 // don't complain about protected members in sealed type
35 sealed partial class RuntimeByteCodeJavaType
36#endif
37 {
38
39 sealed class Metadata
40 {
41
42 internal static Metadata Create(ClassFile classFile)
43 {
44 if (classFile.MajorVersion < 49)
45 return null;
46
47 string[][] genericMetaData = null;
48 object[][] annotations = null;
49 MethodParametersEntry[][] methodParameters = null;
50 TypeAnnotationTable runtimeVisibleTypeAnnotations = TypeAnnotationTable.Empty;
51 TypeAnnotationTable[] fieldRuntimeVisibleTypeAnnotations = null;
52 TypeAnnotationTable[] methodRuntimeVisibleTypeAnnotations = null;
53
54 if (classFile.EnclosingMethod != null)
55 {
56 genericMetaData ??= new string[4][];
57 genericMetaData[2] = classFile.EnclosingMethod;
58 }
59
60 if (classFile.GenericSignature != null)
61 {
62 genericMetaData ??= new string[4][];
63 genericMetaData[3] = new string[] { classFile.GenericSignature };
64 }
65
66 if (classFile.Annotations != null)
67 {
68 annotations ??= new object[5][];
69 annotations[0] = classFile.Annotations;
70 }
71
72 if (classFile.RuntimeVisibleTypeAnnotations.Count > 0)
73 {
74 runtimeVisibleTypeAnnotations = classFile.RuntimeVisibleTypeAnnotations;
75 }
76
77 for (int i = 0; i < classFile.Methods.Length; i++)
78 {
79 if (classFile.Methods[i].GenericSignature != null)
80 {
81 genericMetaData ??= new string[4][];
82 genericMetaData[0] ??= new string[classFile.Methods.Length];
83 genericMetaData[0][i] = classFile.Methods[i].GenericSignature;
84 }
85
86 if (classFile.Methods[i].Annotations != null)
87 {
88 annotations ??= new object[5][];
89 annotations[1] ??= new object[classFile.Methods.Length];
90 annotations[1][i] = classFile.Methods[i].Annotations;
91 }
92
93 if (classFile.Methods[i].ParameterAnnotations != null)
94 {
95 annotations ??= new object[5][];
96 annotations[2] ??= new object[classFile.Methods.Length];
97 annotations[2][i] = classFile.Methods[i].ParameterAnnotations;
98 }
99
100 if (classFile.Methods[i].AnnotationDefault != null)
101 {
102 annotations ??= new object[5][];
103 annotations[3] ??= new object[classFile.Methods.Length];
104 annotations[3][i] = classFile.Methods[i].AnnotationDefault;
105 }
106
107 if (classFile.Methods[i].MethodParameters != null)
108 {
109 methodParameters ??= new MethodParametersEntry[classFile.Methods.Length][];
110 methodParameters[i] = classFile.Methods[i].MethodParameters;
111 }
112
113 if (classFile.Methods[i].RuntimeVisibleTypeAnnotations.Count > 0)
114 {
115 methodRuntimeVisibleTypeAnnotations ??= new TypeAnnotationTable[classFile.Methods.Length];
116 methodRuntimeVisibleTypeAnnotations[i] = classFile.Methods[i].RuntimeVisibleTypeAnnotations;
117 }
118 }
119
120 for (int i = 0; i < classFile.Fields.Length; i++)
121 {
122 if (classFile.Fields[i].GenericSignature != null)
123 {
124 genericMetaData ??= new string[4][];
125 genericMetaData[1] ??= new string[classFile.Fields.Length];
126 genericMetaData[1][i] = classFile.Fields[i].GenericSignature;
127 }
128
129 if (classFile.Fields[i].Annotations != null)
130 {
131 annotations ??= new object[5][];
132 annotations[4] ??= new object[classFile.Fields.Length][];
133 annotations[4][i] = classFile.Fields[i].Annotations;
134 }
135
136 if (classFile.Fields[i].RuntimeVisibleTypeAnnotations.Count > 0)
137 {
138 fieldRuntimeVisibleTypeAnnotations ??= new TypeAnnotationTable[classFile.Fields.Length];
139 fieldRuntimeVisibleTypeAnnotations[i] = classFile.Fields[i].RuntimeVisibleTypeAnnotations;
140 }
141 }
142
143 if (genericMetaData != null ||
144 annotations != null ||
145 methodParameters != null ||
146 runtimeVisibleTypeAnnotations.Count > 0 ||
147 fieldRuntimeVisibleTypeAnnotations != null ||
148 methodRuntimeVisibleTypeAnnotations != null)
149 {
150 var constantPool = runtimeVisibleTypeAnnotations.Count > 0 || fieldRuntimeVisibleTypeAnnotations != null || methodRuntimeVisibleTypeAnnotations != null ? classFile.GetConstantPool() : null;
151 return new Metadata(genericMetaData, annotations, methodParameters, runtimeVisibleTypeAnnotations, fieldRuntimeVisibleTypeAnnotations, methodRuntimeVisibleTypeAnnotations, constantPool);
152 }
153
154 return null;
155 }
156
157 internal static string GetGenericSignature(Metadata m)
158 {
159 if (m != null && m.genericMetaData != null && m.genericMetaData[3] != null)
160 return m.genericMetaData[3][0];
161
162 return null;
163 }
164
165 internal static string[] GetEnclosingMethod(Metadata m)
166 {
167 if (m != null && m.genericMetaData != null)
168 return m.genericMetaData[2];
169
170 return null;
171 }
172
173 internal static string GetGenericMethodSignature(Metadata m, int index)
174 {
175 if (m != null && m.genericMetaData != null && m.genericMetaData[0] != null)
176 return m.genericMetaData[0][index];
177
178 return null;
179 }
180
181 internal static string GetGenericFieldSignature(Metadata m, int index)
182 {
183 if (m != null && m.genericMetaData != null && m.genericMetaData[1] != null)
184 return m.genericMetaData[1][index];
185
186 return null;
187 }
188
189 internal static object[] GetAnnotations(Metadata m)
190 {
191 if (m != null && m.annotations != null)
192 return m.annotations[0];
193
194 return null;
195 }
196
197 internal static object[] GetMethodAnnotations(Metadata m, int index)
198 {
199 if (m != null && m.annotations != null && m.annotations[1] != null)
200 return (object[])m.annotations[1][index];
201
202 return null;
203 }
204
205 internal static object[][] GetMethodParameterAnnotations(Metadata m, int index)
206 {
207 if (m != null && m.annotations != null && m.annotations[2] != null)
208 return (object[][])m.annotations[2][index];
209
210 return null;
211 }
212
213 internal static MethodParametersEntry[] GetMethodParameters(Metadata m, int index)
214 {
215 if (m != null && m.methodParameters != null)
216 return m.methodParameters[index];
217
218 return null;
219 }
220
221 internal static object GetMethodDefaultValue(Metadata m, int index)
222 {
223 if (m != null && m.annotations != null && m.annotations[3] != null)
224 return m.annotations[3][index];
225
226 return null;
227 }
228
229 // note that unlike GetGenericFieldSignature, the index is simply the field index
230 internal static object[] GetFieldAnnotations(Metadata m, int index)
231 {
232 if (m != null && m.annotations != null && m.annotations[4] != null)
233 return (object[])m.annotations[4][index];
234
235 return null;
236 }
237
238 internal static object[] GetConstantPool(Metadata m)
239 {
240 return m.constantPool;
241 }
242
243 internal static byte[] GetRawTypeAnnotations(Metadata m)
244 {
245 if (m != null)
246 return SerializeTypeAnnotations(in m.runtimeVisibleTypeAnnotations);
247
248 return null;
249 }
250
251 internal static byte[] GetMethodRawTypeAnnotations(Metadata m, int index)
252 {
253 if (m != null && m.methodRuntimeVisibleTypeAnnotations != null)
254 return SerializeTypeAnnotations(in m.methodRuntimeVisibleTypeAnnotations[index]);
255
256 return null;
257 }
258
259 internal static byte[] GetFieldRawTypeAnnotations(Metadata m, int index)
260 {
261 if (m != null && m.fieldRuntimeVisibleTypeAnnotations != null)
262 return SerializeTypeAnnotations(in m.fieldRuntimeVisibleTypeAnnotations[index]);
263
264 return null;
265 }
266
267 static byte[] SerializeTypeAnnotations(ref readonly TypeAnnotationTable annotations)
268 {
269 if (annotations.Count == 0)
270 return null;
271
272 var builder = new BlobBuilder();
273 var encoder = new TypeAnnotationTableEncoder(builder);
274 annotations.WriteTo(ref encoder);
275 return builder.ToArray();
276 }
277
278 readonly string[][] genericMetaData;
279 readonly object[][] annotations;
280 readonly MethodParametersEntry[][] methodParameters;
281 readonly TypeAnnotationTable runtimeVisibleTypeAnnotations;
282 readonly TypeAnnotationTable[] fieldRuntimeVisibleTypeAnnotations;
283 readonly TypeAnnotationTable[] methodRuntimeVisibleTypeAnnotations;
284 readonly object[] constantPool;
285
294 Metadata(string[][] genericMetaData, object[][] annotations, MethodParametersEntry[][] methodParameters, TypeAnnotationTable runtimeVisibleTypeAnnotations, TypeAnnotationTable[] fieldRuntimeVisibleTypeAnnotations, TypeAnnotationTable[] methodRuntimeVisibleTypeAnnotations, object[] constantPool)
295 {
296 this.genericMetaData = genericMetaData;
297 this.annotations = annotations;
298 this.methodParameters = methodParameters;
299 this.runtimeVisibleTypeAnnotations = runtimeVisibleTypeAnnotations;
300 this.fieldRuntimeVisibleTypeAnnotations = fieldRuntimeVisibleTypeAnnotations;
301 this.methodRuntimeVisibleTypeAnnotations = methodRuntimeVisibleTypeAnnotations;
302 this.constantPool = constantPool;
303 }
304
305 }
306
307 }
308
309}
int MajorVersion
Gets the major version of the class.
Definition ClassFile.cs:960