jdk/make/modules/tools/src/com/sun/classanalyzer/ConstantPoolParser.java
author mchung
Fri, 18 Dec 2009 11:36:23 -0800
changeset 4524 697144bd8b04
child 5506 202f599c92aa
permissions -rw-r--r--
6909572: Add a new target for building modules Summary: Add a new "modules" build target that builds jdk and jre module image Reviewed-by: alanb, ohair
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4524
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
     1
/*
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
     2
 * Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
     4
 *
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
     7
 * published by the Free Software Foundation.
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
     8
 *
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    13
 * accompanied this code).
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    14
 *
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    18
 *
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    21
 * have any questions.
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    22
 */
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    23
package com.sun.classanalyzer;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    24
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    25
import com.sun.tools.classfile.*;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    26
import com.sun.tools.classfile.ConstantPool.*;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    27
import static com.sun.tools.classfile.ConstantPool.*;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    28
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    29
/**
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    30
 *
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    31
 * @author Mandy Chung
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    32
 */
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    33
public class ConstantPoolParser {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    34
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    35
    private final ClassFileParser cfparser;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    36
    private final StringValueVisitor visitor;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    37
    private final ConstantPool cpool;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    38
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    39
    ConstantPoolParser(ClassFileParser parser) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    40
        this.cfparser = parser;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    41
        this.cpool = cfparser.classfile.constant_pool;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    42
        this.visitor = new StringValueVisitor();
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    43
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    44
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    45
    public String stringValue(CPInfo cpInfo) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    46
        return visitor.visit(cpInfo);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    47
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    48
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    49
    public String stringValue(int constant_pool_index) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    50
        try {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    51
            return stringValue(cpool.get(constant_pool_index));
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    52
        } catch (ConstantPool.InvalidIndex e) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    53
            throw new RuntimeException(e);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    54
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    55
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    56
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    57
    public void parseDependency() {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    58
        ConstantPool.Visitor<Integer, Void> v = new ConstantPool.Visitor<Integer, Void>() {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    59
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    60
            public Integer visitClass(CONSTANT_Class_info info, Void p) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    61
                try {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    62
                    String classname = cfparser.checkClassName(info.getName());
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    63
                    if (classname.isEmpty()) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    64
                        return 1;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    65
                    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    66
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    67
                    Klass from = cfparser.this_klass;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    68
                    Klass to = Klass.getKlass(classname);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    69
                    ResolutionInfo resInfo = ResolutionInfo.resolvedConstantPool(from, to, info.name_index);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    70
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    71
                    from.addDep(to, resInfo);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    72
                    to.addReferrer(from, resInfo);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    73
                } catch (ConstantPoolException ex) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    74
                    throw new RuntimeException(ex);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    75
                }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    76
                return 1;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    77
            }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    78
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    79
            public Integer visitDouble(CONSTANT_Double_info info, Void p) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    80
                // skip
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    81
                return 2;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    82
            }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    83
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    84
            public Integer visitFieldref(CONSTANT_Fieldref_info info, Void p) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    85
                // skip
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    86
                return 1;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    87
            }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    88
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    89
            public Integer visitFloat(CONSTANT_Float_info info, Void p) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    90
                // skip
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    91
                return 1;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    92
            }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    93
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    94
            public Integer visitInteger(CONSTANT_Integer_info info, Void p) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    95
                // skip
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    96
                return 1;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    97
            }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    98
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    99
            public Integer visitInterfaceMethodref(CONSTANT_InterfaceMethodref_info info, Void p) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   100
                // skip
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   101
                return 1;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   102
            }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   103
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   104
            public Integer visitLong(CONSTANT_Long_info info, Void p) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   105
                // skip
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   106
                return 2;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   107
            }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   108
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   109
            public Integer visitNameAndType(CONSTANT_NameAndType_info info, Void p) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   110
                // skip
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   111
                return 1;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   112
            }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   113
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   114
            public Integer visitMethodref(CONSTANT_Methodref_info info, Void p) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   115
                // skip
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   116
                return 1;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   117
            }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   118
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   119
            public Integer visitString(CONSTANT_String_info info, Void p) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   120
                // skip
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   121
                return 1;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   122
            }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   123
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   124
            public Integer visitUtf8(CONSTANT_Utf8_info info, Void p) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   125
                // skip
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   126
                return 1;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   127
            }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   128
        };
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   129
        int cpx = 1;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   130
        while (cpx < cpool.size()) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   131
            try {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   132
                CPInfo cpInfo = cpool.get(cpx);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   133
                cpx += cpInfo.accept(v, null);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   134
            } catch (ConstantPool.InvalidIndex ex) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   135
                throw new RuntimeException(ex);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   136
            }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   137
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   138
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   139
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   140
    int getTag(int index) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   141
        try {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   142
            return cpool.get(index).getTag();
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   143
        } catch (ConstantPoolException ex) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   144
            throw new RuntimeException(ex);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   145
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   146
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   147
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   148
    String getDescriptor(int index) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   149
        CPInfo cpInfo;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   150
        try {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   151
            cpInfo = cpool.get(index);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   152
        } catch (ConstantPoolException ex) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   153
            throw new RuntimeException(ex);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   154
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   155
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   156
        int tag = cpInfo.getTag();
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   157
        switch (tag) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   158
            case CONSTANT_Methodref:
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   159
            case CONSTANT_InterfaceMethodref:
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   160
            case CONSTANT_Fieldref:
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   161
                // simplify references within this class
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   162
                CPRefInfo ref = (CPRefInfo) cpInfo;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   163
                try {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   164
                    return ref.getNameAndTypeInfo().getType();
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   165
                } catch (ConstantPoolException ex) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   166
                }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   167
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   168
        return stringValue(cpInfo);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   169
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   170
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   171
    String getMethodName(int index) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   172
        try {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   173
            CPInfo cpInfo = cpool.get(index);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   174
            if (cpInfo.getTag() == CONSTANT_Methodref ||
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   175
                    cpInfo.getTag() == CONSTANT_InterfaceMethodref) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   176
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   177
                // simplify references within this class
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   178
                CPRefInfo ref = (CPRefInfo) cpInfo;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   179
                String classname;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   180
                if (ref.class_index == cfparser.classfile.this_class) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   181
                    classname = cfparser.this_klass.getClassName();
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   182
                } else {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   183
                    classname = cfparser.checkClassName(ref.getClassName()).replace('/', '.');
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   184
                }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   185
                String methodname = ref.getNameAndTypeInfo().getName();
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   186
                return classname + "." + methodname;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   187
            } else {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   188
                return null;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   189
            }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   190
        } catch (InvalidIndex ex) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   191
            throw new RuntimeException(ex);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   192
        } catch (ConstantPoolException ex) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   193
            throw new RuntimeException(ex);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   194
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   195
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   196
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   197
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   198
    class StringValueVisitor implements ConstantPool.Visitor<String, Void> {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   199
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   200
        public StringValueVisitor() {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   201
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   202
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   203
        public String visit(CPInfo info) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   204
            return info.accept(this, null);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   205
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   206
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   207
        public String visitClass(CONSTANT_Class_info info, Void p) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   208
            return getCheckedName(info);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   209
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   210
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   211
        String getCheckedName(CONSTANT_Class_info info) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   212
            try {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   213
                return checkName(info.getName());
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   214
            } catch (ConstantPoolException e) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   215
                throw new RuntimeException(e);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   216
            }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   217
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   218
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   219
        public String visitDouble(CONSTANT_Double_info info, Void p) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   220
            return info.value + "d";
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   221
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   222
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   223
        public String visitFieldref(CONSTANT_Fieldref_info info, Void p) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   224
            return visitRef(info, p);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   225
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   226
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   227
        public String visitFloat(CONSTANT_Float_info info, Void p) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   228
            return info.value + "f";
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   229
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   230
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   231
        public String visitInteger(CONSTANT_Integer_info info, Void p) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   232
            return String.valueOf(info.value);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   233
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   234
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   235
        public String visitInterfaceMethodref(CONSTANT_InterfaceMethodref_info info, Void p) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   236
            return visitRef(info, p);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   237
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   238
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   239
        public String visitLong(CONSTANT_Long_info info, Void p) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   240
            return info.value + "l";
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   241
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   242
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   243
        public String visitNameAndType(CONSTANT_NameAndType_info info, Void p) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   244
            return getCheckedName(info) + ":" + getType(info);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   245
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   246
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   247
        String getCheckedName(CONSTANT_NameAndType_info info) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   248
            try {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   249
                return checkName(info.getName());
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   250
            } catch (ConstantPoolException e) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   251
                throw new RuntimeException(e);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   252
            }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   253
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   254
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   255
        String getType(CONSTANT_NameAndType_info info) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   256
            try {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   257
                return info.getType();
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   258
            } catch (ConstantPoolException e) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   259
                throw new RuntimeException(e);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   260
            }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   261
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   262
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   263
        public String visitMethodref(CONSTANT_Methodref_info info, Void p) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   264
            return visitRef(info, p);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   265
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   266
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   267
        public String visitString(CONSTANT_String_info info, Void p) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   268
            try {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   269
                int string_index = info.string_index;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   270
                return cpool.getUTF8Info(string_index).accept(this, p);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   271
            } catch (ConstantPoolException e) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   272
                throw new RuntimeException(e);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   273
            }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   274
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   275
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   276
        public String visitUtf8(CONSTANT_Utf8_info info, Void p) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   277
            String s = info.value;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   278
            StringBuilder sb = new StringBuilder();
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   279
            for (int i = 0; i < s.length(); i++) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   280
                char c = s.charAt(i);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   281
                switch (c) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   282
                    case '\t':
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   283
                        sb.append('\\').append('t');
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   284
                        break;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   285
                    case '\n':
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   286
                        sb.append('\\').append('n');
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   287
                        break;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   288
                    case '\r':
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   289
                        sb.append('\\').append('r');
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   290
                        break;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   291
                    case '\"':
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   292
                        sb.append('\\').append('\"');
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   293
                        break;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   294
                    default:
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   295
                        sb.append(c);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   296
                }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   297
            }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   298
            return sb.toString();
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   299
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   300
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   301
        String visitRef(CPRefInfo info, Void p) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   302
            String cn = getCheckedClassName(info);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   303
            String nat;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   304
            try {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   305
                nat = info.getNameAndTypeInfo().accept(this, p);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   306
            } catch (ConstantPoolException e) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   307
                nat = e.getMessage();
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   308
            }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   309
            return cn + "." + nat;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   310
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   311
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   312
        String getCheckedClassName(CPRefInfo info) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   313
            try {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   314
                return checkName(info.getClassName());
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   315
            } catch (ConstantPoolException e) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   316
                throw new RuntimeException(e);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   317
            }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   318
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   319
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   320
    /* If name is a valid binary name, return it; otherwise quote it. */
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   321
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   322
    private static String checkName(String name) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   323
        if (name == null) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   324
            return "null";
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   325
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   326
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   327
        int len = name.length();
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   328
        if (len == 0) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   329
            return "\"\"";
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   330
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   331
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   332
        int cc = '/';
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   333
        int cp;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   334
        for (int k = 0; k < len; k += Character.charCount(cp)) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   335
            cp = name.codePointAt(k);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   336
            if ((cc == '/' && !Character.isJavaIdentifierStart(cp)) || (cp != '/' && !Character.isJavaIdentifierPart(cp))) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   337
                return "\"" + name + "\"";
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   338
            }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   339
            cc = cp;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   340
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   341
        return name;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   342
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   343
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   344
    String tagName(int index) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   345
        try {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   346
            int tag = cpool.get(index).getTag();
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   347
            switch (tag) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   348
                case CONSTANT_Utf8:
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   349
                    return "Utf8";
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   350
                case CONSTANT_Integer:
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   351
                    return "int";
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   352
                case CONSTANT_Float:
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   353
                    return "float";
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   354
                case CONSTANT_Long:
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   355
                    return "long";
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   356
                case CONSTANT_Double:
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   357
                    return "double";
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   358
                case CONSTANT_Class:
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   359
                    return "class";
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   360
                case CONSTANT_String:
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   361
                    return "String";
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   362
                case CONSTANT_Fieldref:
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   363
                    return "Field";
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   364
                case CONSTANT_Methodref:
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   365
                    return "Method";
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   366
                case CONSTANT_InterfaceMethodref:
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   367
                    return "InterfaceMethod";
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   368
                case CONSTANT_NameAndType:
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   369
                    return "NameAndType";
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   370
                default:
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   371
                    return "(unknown tag)";
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   372
            }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   373
        } catch (InvalidIndex e) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   374
            throw new RuntimeException(e);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   375
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   376
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   377
}