jdk/src/share/classes/sun/tools/asm/ConstantPool.java
author ntoda
Thu, 31 Jul 2014 17:01:24 -0700
changeset 25799 1afc4675dc75
parent 19226 d5febab8765d
permissions -rw-r--r--
8044867: Fix raw and unchecked lint warnings in sun.tools.* Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
19226
d5febab8765d 8022479: clean up warnings from sun.tools.asm
smarks
parents: 5506
diff changeset
     2
 * Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.tools.asm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import sun.tools.java.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import sun.tools.tree.StringExpression;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.DataOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * A table of constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * WARNING: The contents of this source file are not part of any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * supported API.  Code that depends on them does so at its own risk:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * they are subject to change or removal without notice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public final
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
class ConstantPool implements RuntimeConstants {
25799
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 19226
diff changeset
    45
    Hashtable<Object, ConstantPoolData> hash = new Hashtable<>(101);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * Find an entry, may return 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public int index(Object obj) {
19226
d5febab8765d 8022479: clean up warnings from sun.tools.asm
smarks
parents: 5506
diff changeset
    51
        return hash.get(obj).index;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * Add an entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public void put(Object obj) {
19226
d5febab8765d 8022479: clean up warnings from sun.tools.asm
smarks
parents: 5506
diff changeset
    58
        ConstantPoolData data = hash.get(obj);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        if (data == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            if (obj instanceof String) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                data = new StringConstantData(this, (String)obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            } else if (obj instanceof StringExpression) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                data = new StringExpressionConstantData(this, (StringExpression)obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            } else if (obj instanceof ClassDeclaration) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                data = new ClassConstantData(this, (ClassDeclaration)obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            } else if (obj instanceof Type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                data = new ClassConstantData(this, (Type)obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            } else if (obj instanceof MemberDefinition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                data = new FieldConstantData(this, (MemberDefinition)obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            } else if (obj instanceof NameAndTypeData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                data = new NameAndTypeConstantData(this, (NameAndTypeData)obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            } else if (obj instanceof Number) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                data = new NumberConstantData(this, (Number)obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            hash.put(obj, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * Write to output
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public void write(Environment env, DataOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        ConstantPoolData list[] = new ConstantPoolData[hash.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        String keys[] = new String[list.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        int index = 1, count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        // Make a list of all the constant pool items
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        for (int n = 0 ; n < 5 ; n++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            int first = count;
19226
d5febab8765d 8022479: clean up warnings from sun.tools.asm
smarks
parents: 5506
diff changeset
    90
            for (Enumeration<ConstantPoolData> e = hash.elements() ; e.hasMoreElements() ;) {
d5febab8765d 8022479: clean up warnings from sun.tools.asm
smarks
parents: 5506
diff changeset
    91
                ConstantPoolData data = e.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                if (data.order() == n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    keys[count] = sortKey(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    list[count++] = data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            xsort(list, keys, first, count-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        // Assign an index to each constant pool item
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        for (int n = 0 ; n < list.length ; n++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            ConstantPoolData data = list[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            data.index = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            index += data.width();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        // Write length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        out.writeShort(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        // Write each constant pool item
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        for (int n = 0 ; n < count ; n++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            list[n].write(env, out, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    static String sortKey(ConstantPoolData f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if (f instanceof NumberConstantData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            Number num = ((NumberConstantData)f).num;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            String str = num.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            int key = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            if (num instanceof Integer)  key = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            else if (num instanceof Float)  key = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            else if (num instanceof Long)  key = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            return "\0" + (char)(str.length() + key<<8) + str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        if (f instanceof StringExpressionConstantData)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            return (String)((StringExpressionConstantData)f).str.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (f instanceof FieldConstantData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            MemberDefinition fd = ((FieldConstantData)f).field;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            return fd.getName()+" "+fd.getType().getTypeSignature()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                +" "+fd.getClassDeclaration().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (f instanceof NameAndTypeConstantData)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            return  ((NameAndTypeConstantData)f).name+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                " "+((NameAndTypeConstantData)f).type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (f instanceof ClassConstantData)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            return ((ClassConstantData)f).name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        return ((StringConstantData)f).str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Quick sort an array of pool entries and a corresponding array of Strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * that are the sort keys for the field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    static void xsort(ConstantPoolData ff[], String ss[], int left, int right) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (left >= right)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        String pivot = ss[left];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        int l = left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        int r = right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        while (l < r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            while (l <= right && ss[l].compareTo(pivot) <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                l++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            while (r >= left && ss[r].compareTo(pivot) > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                r--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            if (l < r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                // swap items at l and at r
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                ConstantPoolData def = ff[l];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                String name = ss[l];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                ff[l] = ff[r]; ff[r] = def;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                ss[l] = ss[r]; ss[r] = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        int middle = r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        // swap left and middle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        ConstantPoolData def = ff[left];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        String name = ss[left];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        ff[left] = ff[middle]; ff[middle] = def;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        ss[left] = ss[middle]; ss[middle] = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        xsort(ff, ss, left, middle-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        xsort(ff, ss, middle + 1, right);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
}