-
Notifications
You must be signed in to change notification settings - Fork 157
Expand file tree
/
Copy pathAbstractConstantGroup.java
More file actions
337 lines (286 loc) · 11.2 KB
/
AbstractConstantGroup.java
File metadata and controls
337 lines (286 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
* Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.lang.invoke;
import java.util.*;
import jdk.internal.vm.annotation.Stable;
/** Utility class for implementing ConstantGroup. */
/*non-public*/
abstract class AbstractConstantGroup implements ConstantGroup {
/** The size of this constant group, set permanently by the constructor. */
protected final int size;
/** The constructor requires the size of the constant group being represented.
* @param size the size of this constant group, set permanently by the constructor
*/
AbstractConstantGroup(int size) {
this.size = size;
}
@Override public final int size() {
return size;
}
public abstract Object get(int index) throws LinkageError;
public abstract Object get(int index, Object ifNotPresent);
public abstract boolean isPresent(int index);
// Do not override equals or hashCode, since this type is stateful.
/**
* Produce a string using the non-resolving list view,
* where unresolved elements are presented as asterisks.
* @return {@code this.asList("*").toString()}
*/
@Override public String toString() {
return asList("*").toString();
}
static class AsIterator implements Iterator<Object> {
private final ConstantGroup self;
private final int end;
private final boolean resolving;
private final Object ifNotPresent;
// Mutable state:
private int index;
private AsIterator(ConstantGroup self, int start, int end,
boolean resolving, Object ifNotPresent) {
this.self = self;
this.end = end;
this.index = start;
this.resolving = resolving;
this.ifNotPresent = ifNotPresent;
}
AsIterator(ConstantGroup self, int start, int end) {
this(self, start, end, true, null);
}
AsIterator(ConstantGroup self, int start, int end,
Object ifNotPresent) {
this(self, start, end, false, ifNotPresent);
}
@Override
public boolean hasNext() {
return index < end;
}
@Override
public Object next() {
int i = bumpIndex();
if (resolving)
return self.get(i);
else
return self.get(i, ifNotPresent);
}
private int bumpIndex() {
int i = index;
if (i >= end) throw new NoSuchElementException();
index = i+1;
return i;
}
}
static class SubGroup extends AbstractConstantGroup {
private final ConstantGroup self; // the real CG
private final int offset; // offset within myself
SubGroup(ConstantGroup self, int start, int end) {
super(end - start);
this.self = self;
this.offset = start;
Objects.checkFromToIndex(start, end, size);
}
private int mapIndex(int index) {
return Objects.checkIndex(index, size) + offset;
}
@Override
public Object get(int index) {
return self.get(mapIndex(index));
}
@Override
public Object get(int index, Object ifNotPresent) {
return self.get(mapIndex(index), ifNotPresent);
}
@Override
public boolean isPresent(int index) {
return self.isPresent(mapIndex(index));
}
@Override
public ConstantGroup subGroup(int start, int end) {
Objects.checkFromToIndex(start, end, size);
return new SubGroup(self, offset + start, offset + end);
}
@Override
public List<Object> asList() {
return new AsList(self, offset, offset + size);
}
@Override
public List<Object> asList(Object ifNotPresent) {
return new AsList(self, offset, offset + size, ifNotPresent);
}
@Override
public int copyConstants(int start, int end,
Object[] buf, int pos) throws LinkageError {
Objects.checkFromToIndex(start, end, size);
return self.copyConstants(offset + start, offset + end,
buf, pos);
}
@Override
public int copyConstants(int start, int end,
Object[] buf, int pos,
Object ifNotPresent) {
Objects.checkFromToIndex(start, end, size);
return self.copyConstants(offset + start, offset + end,
buf, pos, ifNotPresent);
}
}
static class AsList extends AbstractList<Object> {
private final ConstantGroup self;
private final int size;
private final int offset;
private final boolean resolving;
private final Object ifNotPresent;
private AsList(ConstantGroup self, int start, int end,
boolean resolving, Object ifNotPresent) {
this.self = self;
this.size = end - start;
this.offset = start;
this.resolving = resolving;
this.ifNotPresent = ifNotPresent;
Objects.checkFromToIndex(start, end, self.size());
}
AsList(ConstantGroup self, int start, int end) {
this(self, start, end, true, null);
}
AsList(ConstantGroup self, int start, int end,
Object ifNotPresent) {
this(self, start, end, false, ifNotPresent);
}
private int mapIndex(int index) {
return Objects.checkIndex(index, size) + offset;
}
@Override public final int size() {
return size;
}
@Override public Object get(int index) {
if (resolving)
return self.get(mapIndex(index));
else
return self.get(mapIndex(index), ifNotPresent);
}
@Override
public Iterator<Object>! iterator() {
if (resolving)
return new AsIterator(self, offset, offset + size);
else
return new AsIterator(self, offset, offset + size, ifNotPresent);
}
@Override public List<Object> subList(int start, int end) {
Objects.checkFromToIndex(start, end, size);
return new AsList(self, offset + start, offset + end,
resolving, ifNotPresent);
}
@Override public Object[]! toArray() {
return toArray(new Object[size]);
}
@Override public <T> T[]! toArray(T[]! a) {
int pad = a.length - size;
if (pad < 0) {
pad = 0;
a = Arrays.copyOf(a, size);
}
if (resolving)
self.copyConstants(offset, offset + size, a, 0);
else
self.copyConstants(offset, offset + size, a, 0,
ifNotPresent);
if (pad > 0) a[size] = null;
return a;
}
}
abstract static
class WithCache extends AbstractConstantGroup {
@Stable final Object[] cache;
WithCache(int size) {
super(size);
// It is caller's responsibility to initialize the cache.
// Initial contents are all-null, which means nothing is present.
cache = new Object[size];
}
void initializeCache(List<Object> cacheContents, Object ifNotPresent) {
// Replace ifNotPresent with NOT_PRESENT,
// and null with RESOLVED_TO_NULL.
// Then forget about the user-provided ifNotPresent.
for (int i = 0; i < cache.length; i++) {
Object x = cacheContents.get(i);
if (x == ifNotPresent)
continue; // leave the null in place
if (x == null)
x = RESOLVED_TO_NULL;
cache[i] = x;
}
}
@Override public Object get(int i) {
Object x = cache[i];
// @Stable array must use null for sentinel
if (x == null) x = fillCache(i);
return unwrapNull(x);
}
@Override public Object get(int i, Object ifNotAvailable) {
Object x = cache[i];
// @Stable array must use null for sentinel
if (x == null) return ifNotAvailable;
return unwrapNull(x);
}
@Override
public boolean isPresent(int i) {
return cache[i] != null;
}
/** hook for local subclasses */
Object fillCache(int i) {
throw new NoSuchElementException("constant group does not contain element #"+i);
}
/// routines for mapping between null sentinel and true resolved null
static Object wrapNull(Object x) {
return x == null ? RESOLVED_TO_NULL : x;
}
static Object unwrapNull(Object x) {
assert(x != null);
return x == RESOLVED_TO_NULL ? null : x;
}
// secret sentinel for an actual null resolved value, in the cache
static final Object RESOLVED_TO_NULL = new Object();
// secret sentinel for a "hole" in the cache:
static final Object NOT_PRESENT = new Object();
}
/** Skeleton implementation of BootstrapCallInfo. */
static class BSCIWithCache<T> extends WithCache implements BootstrapCallInfo<T> {
private final MethodHandle bsm;
private final String name;
private final T type;
@Override public String toString() {
return bsm+"/"+name+":"+type+super.toString();
}
BSCIWithCache(MethodHandle bsm, String name, T type, int size) {
super(size);
this.type = type;
this.bsm = bsm;
this.name = name;
assert(type instanceof Class || type instanceof MethodType);
}
@Override public MethodHandle bootstrapMethod() { return bsm; }
@Override public String invocationName() { return name; }
@Override public T invocationType() { return type; }
}
}