src/share/classes/com/sun/tools/javap/TypeAnnotationWriter.java

Tue, 28 Jul 2009 10:36:25 -0700

author
jjg
date
Tue, 28 Jul 2009 10:36:25 -0700
changeset 338
777a3efad0d5
child 554
9d9f26857129
permissions
-rw-r--r--

6855990: javap InstructionDetailWriter should support new 308 annotations attribute
Reviewed-by: mcimadamore

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

mercurial