Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/tools/asm/ConstantPool.java
38918 views
/*1* Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.tools.asm;2627import sun.tools.java.*;28import sun.tools.tree.StringExpression;29import java.util.Enumeration;30import java.util.Hashtable;31import java.util.Vector;32import java.io.IOException;33import java.io.DataOutputStream;3435/**36* A table of constants37*38* WARNING: The contents of this source file are not part of any39* supported API. Code that depends on them does so at its own risk:40* they are subject to change or removal without notice.41*/42public final43class ConstantPool implements RuntimeConstants {44Hashtable<Object,ConstantPoolData> hash = new Hashtable<>(101);4546/**47* Find an entry, may return 048*/49public int index(Object obj) {50return hash.get(obj).index;51}5253/**54* Add an entry55*/56public void put(Object obj) {57ConstantPoolData data = hash.get(obj);58if (data == null) {59if (obj instanceof String) {60data = new StringConstantData(this, (String)obj);61} else if (obj instanceof StringExpression) {62data = new StringExpressionConstantData(this, (StringExpression)obj);63} else if (obj instanceof ClassDeclaration) {64data = new ClassConstantData(this, (ClassDeclaration)obj);65} else if (obj instanceof Type) {66data = new ClassConstantData(this, (Type)obj);67} else if (obj instanceof MemberDefinition) {68data = new FieldConstantData(this, (MemberDefinition)obj);69} else if (obj instanceof NameAndTypeData) {70data = new NameAndTypeConstantData(this, (NameAndTypeData)obj);71} else if (obj instanceof Number) {72data = new NumberConstantData(this, (Number)obj);73}74hash.put(obj, data);75}76}7778/**79* Write to output80*/81public void write(Environment env, DataOutputStream out) throws IOException {82ConstantPoolData list[] = new ConstantPoolData[hash.size()];83String keys[] = new String[list.length];84int index = 1, count = 0;8586// Make a list of all the constant pool items87for (int n = 0 ; n < 5 ; n++) {88int first = count;89for (Enumeration<ConstantPoolData> e = hash.elements() ; e.hasMoreElements() ;) {90ConstantPoolData data = e.nextElement();91if (data.order() == n) {92keys[count] = sortKey(data);93list[count++] = data;94}95}96xsort(list, keys, first, count-1);97}9899// Assign an index to each constant pool item100for (int n = 0 ; n < list.length ; n++) {101ConstantPoolData data = list[n];102data.index = index;103index += data.width();104}105106// Write length107out.writeShort(index);108109// Write each constant pool item110for (int n = 0 ; n < count ; n++) {111list[n].write(env, out, this);112}113}114115private116static String sortKey(ConstantPoolData f) {117if (f instanceof NumberConstantData) {118Number num = ((NumberConstantData)f).num;119String str = num.toString();120int key = 3;121if (num instanceof Integer) key = 0;122else if (num instanceof Float) key = 1;123else if (num instanceof Long) key = 2;124return "\0" + (char)(str.length() + key<<8) + str;125}126if (f instanceof StringExpressionConstantData)127return (String)((StringExpressionConstantData)f).str.getValue();128if (f instanceof FieldConstantData) {129MemberDefinition fd = ((FieldConstantData)f).field;130return fd.getName()+" "+fd.getType().getTypeSignature()131+" "+fd.getClassDeclaration().getName();132}133if (f instanceof NameAndTypeConstantData)134return ((NameAndTypeConstantData)f).name+135" "+((NameAndTypeConstantData)f).type;136if (f instanceof ClassConstantData)137return ((ClassConstantData)f).name;138return ((StringConstantData)f).str;139}140141/**142* Quick sort an array of pool entries and a corresponding array of Strings143* that are the sort keys for the field.144*/145private146static void xsort(ConstantPoolData ff[], String ss[], int left, int right) {147if (left >= right)148return;149String pivot = ss[left];150int l = left;151int r = right;152while (l < r) {153while (l <= right && ss[l].compareTo(pivot) <= 0)154l++;155while (r >= left && ss[r].compareTo(pivot) > 0)156r--;157if (l < r) {158// swap items at l and at r159ConstantPoolData def = ff[l];160String name = ss[l];161ff[l] = ff[r]; ff[r] = def;162ss[l] = ss[r]; ss[r] = name;163}164}165int middle = r;166// swap left and middle167ConstantPoolData def = ff[left];168String name = ss[left];169ff[left] = ff[middle]; ff[middle] = def;170ss[left] = ss[middle]; ss[middle] = name;171xsort(ff, ss, left, middle-1);172xsort(ff, ss, middle + 1, right);173}174175}176177178