langtools/src/share/classes/com/sun/tools/javap/TypeAnnotationWriter.java
author jjg
Thu, 10 Jun 2010 16:08:01 -0700
changeset 5847 1908176fd6e3
parent 5520 86e4b9a9da40
permissions -rw-r--r--
6944312: Potential rebranding issues in openjdk/langtools repository sources Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3536
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
     1
/*
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3536
diff changeset
     2
 * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
3536
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
     4
 *
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
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: 3536
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
3536
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3536
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
3536
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    10
 *
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    15
 * accompanied this code).
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    16
 *
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3536
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3536
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3536
diff changeset
    23
 * questions.
3536
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    24
 */
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    25
package com.sun.tools.javap;
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    26
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    27
import com.sun.tools.classfile.Attribute;
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    28
import com.sun.tools.classfile.Code_attribute;
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    29
import com.sun.tools.classfile.ExtendedAnnotation;
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    30
import com.sun.tools.classfile.ExtendedAnnotation.Position;
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    31
import com.sun.tools.classfile.Instruction;
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    32
import com.sun.tools.classfile.Method;
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    33
import com.sun.tools.classfile.RuntimeInvisibleTypeAnnotations_attribute;
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    34
import com.sun.tools.classfile.RuntimeTypeAnnotations_attribute;
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    35
import com.sun.tools.classfile.RuntimeVisibleTypeAnnotations_attribute;
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    36
import java.util.ArrayList;
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    37
import java.util.HashMap;
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    38
import java.util.List;
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    39
import java.util.Map;
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    40
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    41
/**
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    42
 * Annotate instructions with details about type annotations.
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    43
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    44
 *  <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
    45
 *  If you write code that depends on this, you do so at your own risk.
3536
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    46
 *  This code and its internal interfaces are subject to change or
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    47
 *  deletion without notice.</b>
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    48
 */
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    49
public class TypeAnnotationWriter extends InstructionDetailWriter {
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    50
    public enum NoteKind { VISIBLE, INVISIBLE };
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    51
    public static class Note {
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    52
        Note(NoteKind kind, ExtendedAnnotation anno) {
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    53
            this.kind = kind;
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    54
            this.anno = anno;
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    55
        }
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    56
        public final NoteKind kind;
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    57
        public final ExtendedAnnotation anno;
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    58
    }
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    59
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    60
    static TypeAnnotationWriter instance(Context context) {
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    61
        TypeAnnotationWriter instance = context.get(TypeAnnotationWriter.class);
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    62
        if (instance == null)
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    63
            instance = new TypeAnnotationWriter(context);
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    64
        return instance;
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    65
    }
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    66
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    67
    protected TypeAnnotationWriter(Context context) {
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    68
        super(context);
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    69
        context.put(TypeAnnotationWriter.class, this);
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    70
        annotationWriter = AnnotationWriter.instance(context);
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    71
        classWriter = ClassWriter.instance(context);
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    72
    }
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    73
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    74
    public void reset(Code_attribute attr) {
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    75
        Method m = classWriter.getMethod();
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    76
        pcMap = new HashMap<Integer, List<Note>>();
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    77
        check(NoteKind.VISIBLE, (RuntimeVisibleTypeAnnotations_attribute) m.attributes.get(Attribute.RuntimeVisibleTypeAnnotations));
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    78
        check(NoteKind.INVISIBLE, (RuntimeInvisibleTypeAnnotations_attribute) m.attributes.get(Attribute.RuntimeInvisibleTypeAnnotations));
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    79
    }
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    80
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    81
    private void check(NoteKind kind, RuntimeTypeAnnotations_attribute attr) {
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    82
        if (attr == null)
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    83
            return;
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    84
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    85
        for (ExtendedAnnotation anno: attr.annotations) {
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    86
            Position p = anno.position;
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    87
            Note note = null;
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    88
            if (p.offset != -1)
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    89
                addNote(p.offset, note = new Note(kind, anno));
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    90
            if (p.lvarOffset != null) {
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    91
                for (int i = 0; i < p.lvarOffset.length; i++) {
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    92
                    if (note == null)
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    93
                        note = new Note(kind, anno);
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    94
                    addNote(p.lvarOffset[i], note);
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    95
                }
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    96
            }
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    97
        }
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    98
    }
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
    99
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
   100
    private void addNote(int pc, Note note) {
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
   101
        List<Note> list = pcMap.get(pc);
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
   102
        if (list == null)
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
   103
            pcMap.put(pc, list = new ArrayList<Note>());
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
   104
        list.add(note);
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
   105
    }
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
   106
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
   107
    @Override
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
   108
    void writeDetails(Instruction instr) {
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
   109
        String indent = space(2); // get from Options?
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
   110
        int pc = instr.getPC();
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
   111
        List<Note> notes = pcMap.get(pc);
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
   112
        if (notes != null) {
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
   113
            for (Note n: notes) {
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
   114
                print(indent);
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
   115
                print("@");
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
   116
                annotationWriter.write(n.anno, false, true);
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
   117
                print(", ");
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
   118
                println(n.kind.toString().toLowerCase());
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
   119
            }
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
   120
        }
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
   121
    }
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
   122
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
   123
    private AnnotationWriter annotationWriter;
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
   124
    private ClassWriter classWriter;
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
   125
    private Map<Integer, List<Note>> pcMap;
dee1b5833af7 6855990: javap InstructionDetailWriter should support new 308 annotations attribute
jjg
parents:
diff changeset
   126
}