langtools/src/share/classes/com/sun/tools/javap/AnnotationWriter.java
author jjg
Wed, 23 Jan 2013 13:27:24 -0800
changeset 15385 ee1eebe7e210
parent 8031 d5fe2c1cecfc
child 15718 8e54c8e43d38
permissions -rw-r--r--
8006775: JSR 308: Compiler changes in JDK8 Reviewed-by: jjg Contributed-by: mernst@cs.washington.edu, wmdietl@cs.washington.edu, mpapi@csail.mit.edu, mahmood@notnoop.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
727
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
     1
/*
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
     2
 * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.
727
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
     4
 *
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4873
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
727
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4873
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
727
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    10
 *
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    15
 * accompanied this code).
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    16
 *
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4873
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4873
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4873
diff changeset
    23
 * questions.
727
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    24
 */
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    25
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    26
package com.sun.tools.javap;
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    27
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    28
import com.sun.tools.classfile.Annotation;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
    29
import com.sun.tools.classfile.TypeAnnotation;
727
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    30
import com.sun.tools.classfile.Annotation.Annotation_element_value;
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    31
import com.sun.tools.classfile.Annotation.Array_element_value;
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    32
import com.sun.tools.classfile.Annotation.Class_element_value;
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    33
import com.sun.tools.classfile.Annotation.Enum_element_value;
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    34
import com.sun.tools.classfile.Annotation.Primitive_element_value;
3536
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
    35
import com.sun.tools.classfile.ConstantPool;
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
    36
import com.sun.tools.classfile.ConstantPoolException;
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
    37
import com.sun.tools.classfile.Descriptor;
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
    38
import com.sun.tools.classfile.Descriptor.InvalidDescriptor;
727
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    39
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    40
/**
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    41
 *  A writer for writing annotations as text.
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    42
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    43
 *  <p><b>This is NOT part of any supported API.
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    44
 *  If you write code that depends on this, you do so at your own risk.
727
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    45
 *  This code and its internal interfaces are subject to change or
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    46
 *  deletion without notice.</b>
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    47
 */
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    48
public class AnnotationWriter extends BasicWriter {
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    49
    static AnnotationWriter instance(Context context) {
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    50
        AnnotationWriter instance = context.get(AnnotationWriter.class);
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    51
        if (instance == null)
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    52
            instance = new AnnotationWriter(context);
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    53
        return instance;
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    54
    }
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    55
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    56
    protected AnnotationWriter(Context context) {
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    57
        super(context);
3536
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
    58
        classWriter = ClassWriter.instance(context);
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
    59
        constantWriter = ConstantWriter.instance(context);
727
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    60
    }
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    61
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    62
    public void write(Annotation annot) {
3536
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
    63
        write(annot, false);
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
    64
    }
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
    65
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
    66
    public void write(Annotation annot, boolean resolveIndices) {
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
    67
        writeDescriptor(annot.type_index, resolveIndices);
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
    68
        boolean showParens = annot.num_element_value_pairs > 0 || !resolveIndices;
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
    69
        if (showParens)
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
    70
            print("(");
727
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    71
        for (int i = 0; i < annot.num_element_value_pairs; i++) {
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    72
            if (i > 0)
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    73
                print(",");
3536
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
    74
            write(annot.element_value_pairs[i], resolveIndices);
727
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    75
        }
3536
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
    76
        if (showParens)
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
    77
            print(")");
727
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    78
    }
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    79
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
    80
    public void write(TypeAnnotation annot) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
    81
        write(annot, true, false);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
    82
    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
    83
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
    84
    public void write(TypeAnnotation annot, boolean showOffsets, boolean resolveIndices) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
    85
        write(annot.annotation, resolveIndices);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
    86
        print(": ");
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
    87
        write(annot.position, showOffsets);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
    88
    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
    89
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
    90
    public void write(TypeAnnotation.Position pos, boolean showOffsets) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
    91
        print(pos.type);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
    92
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
    93
        switch (pos.type) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
    94
        // type cast
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
    95
        case CAST:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
    96
        // instanceof
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
    97
        case INSTANCEOF:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
    98
        // new expression
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
    99
        case NEW:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   100
            if (showOffsets) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   101
                print(", offset=");
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   102
                print(pos.offset);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   103
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   104
            break;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   105
        // local variable
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   106
        case LOCAL_VARIABLE:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   107
        // resource variable
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   108
        case RESOURCE_VARIABLE:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   109
            if (pos.lvarOffset == null) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   110
                print(", lvarOffset is Null!");
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   111
                break;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   112
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   113
            print(", {");
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   114
            for (int i = 0; i < pos.lvarOffset.length; ++i) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   115
                if (i != 0) print("; ");
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   116
                if (showOffsets) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   117
                    print("start_pc=");
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   118
                    print(pos.lvarOffset[i]);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   119
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   120
                print(", length=");
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   121
                print(pos.lvarLength[i]);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   122
                print(", index=");
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   123
                print(pos.lvarIndex[i]);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   124
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   125
            print("}");
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   126
            break;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   127
        // exception parameter
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   128
        case EXCEPTION_PARAMETER:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   129
            print(", exception_index=");
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   130
            print(pos.exception_index);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   131
            break;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   132
        // method receiver
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   133
        case METHOD_RECEIVER:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   134
            // Do nothing
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   135
            break;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   136
        // type parameter
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   137
        case CLASS_TYPE_PARAMETER:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   138
        case METHOD_TYPE_PARAMETER:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   139
            print(", param_index=");
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   140
            print(pos.parameter_index);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   141
            break;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   142
        // type parameter bound
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   143
        case CLASS_TYPE_PARAMETER_BOUND:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   144
        case METHOD_TYPE_PARAMETER_BOUND:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   145
            print(", param_index=");
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   146
            print(pos.parameter_index);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   147
            print(", bound_index=");
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   148
            print(pos.bound_index);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   149
            break;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   150
        // class extends or implements clause
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   151
        case CLASS_EXTENDS:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   152
            print(", type_index=");
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   153
            print(pos.type_index);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   154
            break;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   155
        // throws
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   156
        case THROWS:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   157
            print(", type_index=");
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   158
            print(pos.type_index);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   159
            break;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   160
        // method parameter
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   161
        case METHOD_FORMAL_PARAMETER:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   162
            print(", param_index=");
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   163
            print(pos.parameter_index);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   164
            break;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   165
        // method/constructor/reference type argument
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   166
        case CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   167
        case METHOD_INVOCATION_TYPE_ARGUMENT:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   168
        case METHOD_REFERENCE_TYPE_ARGUMENT:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   169
            if (showOffsets) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   170
                print(", offset=");
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   171
                print(pos.offset);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   172
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   173
            print(", type_index=");
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   174
            print(pos.type_index);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   175
            break;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   176
        // We don't need to worry about these
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   177
        case METHOD_RETURN:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   178
        case FIELD:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   179
            break;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   180
        // lambda formal parameter
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   181
        case LAMBDA_FORMAL_PARAMETER:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   182
            print(", param_index=");
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   183
            print(pos.parameter_index);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   184
            break;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   185
        case UNKNOWN:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   186
            throw new AssertionError("AnnotationWriter: UNKNOWN target type should never occur!");
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   187
        default:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   188
            throw new AssertionError("AnnotationWriter: Unknown target type for position: " + pos);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   189
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   190
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   191
        // Append location data for generics/arrays.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   192
        if (!pos.location.isEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   193
            print(", location=");
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   194
            print(pos.location);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   195
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   196
    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 8031
diff changeset
   197
727
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   198
    public void write(Annotation.element_value_pair pair) {
3536
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   199
        write(pair, false);
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   200
    }
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   201
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   202
    public void write(Annotation.element_value_pair pair, boolean resolveIndices) {
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   203
        writeIndex(pair.element_name_index, resolveIndices);
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   204
        print("=");
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   205
        write(pair.value, resolveIndices);
727
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   206
    }
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   207
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   208
    public void write(Annotation.element_value value) {
3536
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   209
        write(value, false);
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   210
    }
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   211
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   212
    public void write(Annotation.element_value value, boolean resolveIndices) {
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   213
        ev_writer.write(value, resolveIndices);
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   214
    }
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   215
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   216
    private void writeDescriptor(int index, boolean resolveIndices) {
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   217
        if (resolveIndices) {
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   218
            try {
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   219
                ConstantPool constant_pool = classWriter.getClassFile().constant_pool;
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   220
                Descriptor d = new Descriptor(index);
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   221
                print(d.getFieldType(constant_pool));
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   222
                return;
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   223
            } catch (ConstantPoolException ignore) {
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   224
            } catch (InvalidDescriptor ignore) {
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   225
            }
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   226
        }
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   227
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   228
        print("#" + index);
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   229
    }
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   230
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   231
    private void writeIndex(int index, boolean resolveIndices) {
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   232
        if (resolveIndices) {
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   233
            print(constantWriter.stringValue(index));
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   234
        } else
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   235
            print("#" + index);
727
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   236
    }
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   237
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   238
    element_value_Writer ev_writer = new element_value_Writer();
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   239
3536
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   240
    class element_value_Writer implements Annotation.element_value.Visitor<Void,Boolean> {
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   241
        public void write(Annotation.element_value value, boolean resolveIndices) {
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   242
            value.accept(this, resolveIndices);
727
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   243
        }
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   244
3536
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   245
        public Void visitPrimitive(Primitive_element_value ev, Boolean resolveIndices) {
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   246
            if (resolveIndices)
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   247
                writeIndex(ev.const_value_index, resolveIndices);
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   248
            else
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   249
                print(((char) ev.tag) + "#" + ev.const_value_index);
727
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   250
            return null;
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   251
        }
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   252
3536
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   253
        public Void visitEnum(Enum_element_value ev, Boolean resolveIndices) {
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   254
            if (resolveIndices) {
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   255
                writeIndex(ev.type_name_index, resolveIndices);
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   256
                print(".");
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   257
                writeIndex(ev.const_name_index, resolveIndices);
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   258
            } else
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   259
                print(((char) ev.tag) + "#" + ev.type_name_index + ".#" + ev.const_name_index);
727
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   260
            return null;
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   261
        }
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   262
3536
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   263
        public Void visitClass(Class_element_value ev, Boolean resolveIndices) {
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   264
            if (resolveIndices) {
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   265
                writeIndex(ev.class_info_index, resolveIndices);
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   266
                print(".class");
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   267
            } else
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   268
                print(((char) ev.tag) + "#" + ev.class_info_index);
727
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   269
            return null;
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   270
        }
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   271
3536
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   272
        public Void visitAnnotation(Annotation_element_value ev, Boolean resolveIndices) {
727
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   273
            print((char) ev.tag);
3536
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   274
            AnnotationWriter.this.write(ev.annotation_value, resolveIndices);
727
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   275
            return null;
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   276
        }
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   277
3536
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   278
        public Void visitArray(Array_element_value ev, Boolean resolveIndices) {
727
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   279
            print("[");
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   280
            for (int i = 0; i < ev.num_values; i++) {
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   281
                if (i > 0)
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   282
                    print(",");
3536
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   283
                write(ev.values[i], resolveIndices);
727
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   284
            }
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   285
            print("]");
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   286
            return null;
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   287
        }
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   288
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   289
    }
3536
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   290
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   291
    private ClassWriter classWriter;
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents: 3149
diff changeset
   292
    private ConstantWriter constantWriter;
727
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   293
}