rt/emul/compact/src/main/java/sun/invoke/anon/ConstantPoolVisitor.java
branchjdk8-b132
changeset 1646 c880a8a8803b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/emul/compact/src/main/java/sun/invoke/anon/ConstantPoolVisitor.java	Sat Aug 09 11:11:13 2014 +0200
     1.3 @@ -0,0 +1,192 @@
     1.4 +/*
     1.5 + * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package sun.invoke.anon;
    1.30 +
    1.31 +/**
    1.32 + * A visitor called by {@link ConstantPoolParser#parse(ConstantPoolVisitor)}
    1.33 + * when a constant pool entry is parsed.
    1.34 + * <p>
    1.35 + * A visit* method is called when a constant pool entry is parsed.
    1.36 + * The first argument is always the constant pool index.
    1.37 + * The second argument is always the constant pool tag,
    1.38 + * even for methods like {@link #visitUTF8(int, byte, String)} which only apply to one tag.
    1.39 + * String arguments refer to Utf8 or NameAndType entries declared elsewhere,
    1.40 + * and are always accompanied by the indexes of those entries.
    1.41 + * <p>
    1.42 + * The order of the calls to the visit* methods is not necessarily related
    1.43 + * to the order of the entries in the constant pool.
    1.44 + * If one entry has a reference to another entry, the latter (lower-level)
    1.45 + * entry will be visited first.
    1.46 + * <p>
    1.47 + * The following table shows the relation between constant pool entry
    1.48 + * types and the corresponding visit* methods:
    1.49 + *
    1.50 + * <table border=1 cellpadding=5 summary="constant pool visitor methods">
    1.51 + * <tr><th>Tag(s)</th><th>Method</th></tr>
    1.52 + * <tr>
    1.53 + *   <td>{@link #CONSTANT_Utf8}</td>
    1.54 + *   <td>{@link #visitUTF8(int, byte, String)}</td>
    1.55 + * </tr><tr>
    1.56 + *   <td>{@link #CONSTANT_Integer}, {@link #CONSTANT_Float},
    1.57 + *       {@link #CONSTANT_Long}, {@link #CONSTANT_Double}</td>
    1.58 + *   <td>{@link #visitConstantValue(int, byte, Object)}</td>
    1.59 + * </tr><tr>
    1.60 + *   <td>{@link #CONSTANT_String}, {@link #CONSTANT_Class}</td>
    1.61 + *   <td>{@link #visitConstantString(int, byte, String, int)}</td>
    1.62 + * </tr><tr>
    1.63 + *   <td>{@link #CONSTANT_NameAndType}</td>
    1.64 + *   <td>{@link #visitDescriptor(int, byte, String, String, int, int)}</td>
    1.65 + * </tr><tr>
    1.66 + *   <td>{@link #CONSTANT_Fieldref},
    1.67 + *       {@link #CONSTANT_Methodref},
    1.68 + *       {@link #CONSTANT_InterfaceMethodref}</td>
    1.69 + *   <td>{@link #visitMemberRef(int, byte, String, String, String, int, int)}</td>
    1.70 + * </tr>
    1.71 + * </table>
    1.72 + *
    1.73 + * @see ConstantPoolPatch
    1.74 + * @author Remi Forax
    1.75 + * @author jrose
    1.76 + */
    1.77 +public class ConstantPoolVisitor {
    1.78 +  /** Called each time an UTF8 constant pool entry is found.
    1.79 +   * @param index the constant pool index
    1.80 +   * @param tag always {@link #CONSTANT_Utf8}
    1.81 +   * @param utf8 string encoded in modified UTF-8 format passed as a {@code String}
    1.82 +   *
    1.83 +   * @see ConstantPoolPatch#putUTF8(int, String)
    1.84 +   */
    1.85 +  public void visitUTF8(int index, byte tag, String utf8) {
    1.86 +    // do nothing
    1.87 +  }
    1.88 +
    1.89 +  /** Called for each constant pool entry that encodes an integer,
    1.90 +   *  a float, a long, or a double.
    1.91 +   *  Constant strings and classes are not managed by this method but
    1.92 +   *  by {@link #visitConstantString(int, byte, String, int)}.
    1.93 +   *
    1.94 +   * @param index the constant pool index
    1.95 +   * @param tag one of {@link #CONSTANT_Integer},
    1.96 +   *            {@link #CONSTANT_Float},
    1.97 +   *            {@link #CONSTANT_Long},
    1.98 +   *            or {@link #CONSTANT_Double}
    1.99 +   * @param value encoded value
   1.100 +   *
   1.101 +   * @see ConstantPoolPatch#putConstantValue(int, Object)
   1.102 +   */
   1.103 +  public void visitConstantValue(int index, byte tag, Object value) {
   1.104 +    // do nothing
   1.105 +  }
   1.106 +
   1.107 +  /** Called for each constant pool entry that encodes a string or a class.
   1.108 +   * @param index the constant pool index
   1.109 +   * @param tag one of {@link #CONSTANT_String},
   1.110 +   *            {@link #CONSTANT_Class},
   1.111 +   * @param name string body or class name (using dot separator)
   1.112 +   * @param nameIndex the index of the Utf8 string for the name
   1.113 +   *
   1.114 +   * @see ConstantPoolPatch#putConstantValue(int, byte, Object)
   1.115 +   */
   1.116 +  public void visitConstantString(int index, byte tag,
   1.117 +                                  String name, int nameIndex) {
   1.118 +    // do nothing
   1.119 +  }
   1.120 +
   1.121 +  /** Called for each constant pool entry that encodes a name and type.
   1.122 +   * @param index the constant pool index
   1.123 +   * @param tag always {@link #CONSTANT_NameAndType}
   1.124 +   * @param memberName a field or method name
   1.125 +   * @param signature the member signature
   1.126 +   * @param memberNameIndex index of the Utf8 string for the member name
   1.127 +   * @param signatureIndex index of the Utf8 string for the signature
   1.128 +   *
   1.129 +   * @see ConstantPoolPatch#putDescriptor(int, String, String)
   1.130 +   */
   1.131 +  public void visitDescriptor(int index, byte tag,
   1.132 +                              String memberName, String signature,
   1.133 +                              int memberNameIndex, int signatureIndex) {
   1.134 +    // do nothing
   1.135 +  }
   1.136 +
   1.137 +  /** Called for each constant pool entry that encodes a field or method.
   1.138 +   * @param index the constant pool index
   1.139 +   * @param tag one of {@link #CONSTANT_Fieldref},
   1.140 +   *            or {@link #CONSTANT_Methodref},
   1.141 +   *            or {@link #CONSTANT_InterfaceMethodref}
   1.142 +   * @param className the class name (using dot separator)
   1.143 +   * @param memberName name of the field or method
   1.144 +   * @param signature the field or method signature
   1.145 +   * @param classNameIndex index of the Utf8 string for the class name
   1.146 +   * @param descriptorIndex index of the NameAndType descriptor constant
   1.147 +   *
   1.148 +   * @see ConstantPoolPatch#putMemberRef(int, byte, String, String, String)
   1.149 +   */
   1.150 +  public void visitMemberRef(int index, byte tag,
   1.151 +                             String className, String memberName, String signature,
   1.152 +                             int classNameIndex, int descriptorIndex) {
   1.153 +    // do nothing
   1.154 +  }
   1.155 +
   1.156 +    public static final byte
   1.157 +      CONSTANT_None = 0,
   1.158 +      CONSTANT_Utf8 = 1,
   1.159 +      //CONSTANT_Unicode = 2,               /* unused */
   1.160 +      CONSTANT_Integer = 3,
   1.161 +      CONSTANT_Float = 4,
   1.162 +      CONSTANT_Long = 5,
   1.163 +      CONSTANT_Double = 6,
   1.164 +      CONSTANT_Class = 7,
   1.165 +      CONSTANT_String = 8,
   1.166 +      CONSTANT_Fieldref = 9,
   1.167 +      CONSTANT_Methodref = 10,
   1.168 +      CONSTANT_InterfaceMethodref = 11,
   1.169 +      CONSTANT_NameAndType = 12;
   1.170 +
   1.171 +    private static String[] TAG_NAMES = {
   1.172 +        "Empty",
   1.173 +        "Utf8",
   1.174 +        null, //"Unicode",
   1.175 +        "Integer",
   1.176 +        "Float",
   1.177 +        "Long",
   1.178 +        "Double",
   1.179 +        "Class",
   1.180 +        "String",
   1.181 +        "Fieldref",
   1.182 +        "Methodref",
   1.183 +        "InterfaceMethodref",
   1.184 +        "NameAndType"
   1.185 +    };
   1.186 +
   1.187 +    public static String tagName(byte tag) {
   1.188 +        String name = null;
   1.189 +        if ((tag & 0xFF) < TAG_NAMES.length)
   1.190 +            name = TAG_NAMES[tag];
   1.191 +        if (name == null)
   1.192 +            name = "Unknown#"+(tag&0xFF);
   1.193 +        return name;
   1.194 +    }
   1.195 +}