IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
ConstantPoolAttribute.cs
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2014 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.Runtime;
28
29namespace IKVM.Attributes
30{
31
32 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface)]
33 public sealed class ConstantPoolAttribute : Attribute
34 {
35
36 internal static object[] Decompress(object[] constantPool)
37 {
38 var list = new List<object>();
39
40 foreach (var obj in constantPool)
41 {
42 int emptySlots = obj as byte? ?? obj as ushort? ?? 0;
43 if (emptySlots == 0)
44 {
45 list.Add(Unescape(obj));
46 }
47 else
48 {
49 for (int i = 0; i < emptySlots; i++)
50 list.Add(null);
51 }
52 }
53
54 return list.ToArray();
55 }
56
57 static object Unescape(object obj)
58 {
59 if (obj is string str)
60 obj = UnicodeUtil.UnescapeInvalidSurrogates(str);
61
62 return obj;
63 }
64
65 internal static object[] Compress(object[] constantPool, bool[] inUse)
66 {
67 int length = constantPool.Length;
68 while (!inUse[length - 1])
69 length--;
70
71 int write = 0;
72 for (int read = 0; read < length; read++)
73 {
74 int start = read;
75 while (!inUse[read])
76 read++;
77
78 int emptySlots = read - start;
79 if (emptySlots > 255)
80 {
81 constantPool[write++] = (ushort)emptySlots;
82 }
83 else if (emptySlots > 0)
84 {
85 constantPool[write++] = (byte)emptySlots;
86 }
87
88 constantPool[write++] = Escape(constantPool[read]);
89 }
90
91 Array.Resize(ref constantPool, write);
92 return constantPool;
93 }
94
95 static object Escape(object obj)
96 {
97 if (obj is string str)
98 obj = UnicodeUtil.EscapeInvalidSurrogates(str);
99
100 return obj;
101 }
102
103 internal readonly object[] constantPool;
104
109 public ConstantPoolAttribute(object[] constantPool)
110 {
111 this.constantPool = Decompress(constantPool);
112 }
113
114 }
115
116}
ConstantPoolAttribute(object[] constantPool)
Initializes a new instance.