Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/sourcetools/j9constantpool/com/ibm/oti/VMCPTool/ConstantPool.java
6004 views
1
/*******************************************************************************
2
* Copyright (c) 2004, 2021 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
21
*******************************************************************************/
22
package com.ibm.oti.VMCPTool;
23
24
import java.io.PrintWriter;
25
import java.util.ArrayList;
26
import java.util.Iterator;
27
import java.util.List;
28
import java.util.Set;
29
30
public class ConstantPool {
31
32
private final List<PrimaryItem> primaryItems = new ArrayList<>();
33
34
public int constantPoolSize() {
35
return primaryItems.size() + 1;
36
}
37
38
public void add(PrimaryItem item) {
39
primaryItems.add(item);
40
}
41
42
public PrimaryItem findPrimaryItem(Object obj) {
43
for (PrimaryItem item : primaryItems) {
44
if (obj.equals(item)) {
45
return item;
46
}
47
}
48
return null;
49
}
50
51
public void writeMacros(PrintWriter out) {
52
for (PrimaryItem item : primaryItems) {
53
item.writeMacros(this, out);
54
}
55
}
56
57
public void writeForClassLibrary(int version, Set<String> flags, PrintWriter out) {
58
// Two pass process:
59
// 1) Compute offsets by writing to a disconnected stream.
60
// 2) Connect stream output, reset offset and write to the connected stream.
61
ConstantPoolStream ds = new ConstantPoolStream(version, flags, this, primaryItems.size() + 1);
62
writeConstantPool(ds);
63
64
// Now the offsets have been calculated, start over again.
65
ds.open(out);
66
writeConstantPool(ds);
67
ds.close();
68
}
69
70
private void writeConstantPool(ConstantPoolStream ds) {
71
// CP0 is reserved
72
ds.writeInt(0);
73
ds.writeInt(0);
74
75
// Write the primary items.
76
int cpIndex = 1;
77
for (PrimaryItem item : primaryItems) {
78
ds.comment("cp[" + cpIndex + "] = " + item.commentText()); //$NON-NLS-1$ //$NON-NLS-2$
79
item.write(ds);
80
cpIndex += 1;
81
}
82
83
// Write the secondary items.
84
for (PrimaryItem item : primaryItems) {
85
item.writeSecondaryItems(ds);
86
}
87
88
ds.alignTo(ConstantPoolStream.ITEM_SIZE);
89
}
90
91
public int getIndex(PrimaryItem item) {
92
return 1 + primaryItems.indexOf(item);
93
}
94
95
public void removeNonApplicableItems(int version, Set<String> flags) {
96
for (Iterator<PrimaryItem> items = primaryItems.iterator(); items.hasNext();) {
97
PrimaryItem item = items.next();
98
99
if (item.alias(version, flags) == null) {
100
items.remove();
101
}
102
}
103
}
104
105
}
106
107