Path: blob/master/sourcetools/j9constantpool/com/ibm/oti/VMCPTool/ConstantPool.java
6004 views
/*******************************************************************************1* Copyright (c) 2004, 2021 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* 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-exception20*******************************************************************************/21package com.ibm.oti.VMCPTool;2223import java.io.PrintWriter;24import java.util.ArrayList;25import java.util.Iterator;26import java.util.List;27import java.util.Set;2829public class ConstantPool {3031private final List<PrimaryItem> primaryItems = new ArrayList<>();3233public int constantPoolSize() {34return primaryItems.size() + 1;35}3637public void add(PrimaryItem item) {38primaryItems.add(item);39}4041public PrimaryItem findPrimaryItem(Object obj) {42for (PrimaryItem item : primaryItems) {43if (obj.equals(item)) {44return item;45}46}47return null;48}4950public void writeMacros(PrintWriter out) {51for (PrimaryItem item : primaryItems) {52item.writeMacros(this, out);53}54}5556public void writeForClassLibrary(int version, Set<String> flags, PrintWriter out) {57// Two pass process:58// 1) Compute offsets by writing to a disconnected stream.59// 2) Connect stream output, reset offset and write to the connected stream.60ConstantPoolStream ds = new ConstantPoolStream(version, flags, this, primaryItems.size() + 1);61writeConstantPool(ds);6263// Now the offsets have been calculated, start over again.64ds.open(out);65writeConstantPool(ds);66ds.close();67}6869private void writeConstantPool(ConstantPoolStream ds) {70// CP0 is reserved71ds.writeInt(0);72ds.writeInt(0);7374// Write the primary items.75int cpIndex = 1;76for (PrimaryItem item : primaryItems) {77ds.comment("cp[" + cpIndex + "] = " + item.commentText()); //$NON-NLS-1$ //$NON-NLS-2$78item.write(ds);79cpIndex += 1;80}8182// Write the secondary items.83for (PrimaryItem item : primaryItems) {84item.writeSecondaryItems(ds);85}8687ds.alignTo(ConstantPoolStream.ITEM_SIZE);88}8990public int getIndex(PrimaryItem item) {91return 1 + primaryItems.indexOf(item);92}9394public void removeNonApplicableItems(int version, Set<String> flags) {95for (Iterator<PrimaryItem> items = primaryItems.iterator(); items.hasNext();) {96PrimaryItem item = items.next();9798if (item.alias(version, flags) == null) {99items.remove();100}101}102}103104}105106107