1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/classes/com/sun/tools/javap/TypeAnnotationWriter.java Tue Jul 28 10:36:25 2009 -0700 1.3 @@ -0,0 +1,126 @@ 1.4 +/* 1.5 + * Copyright 2009 Sun Microsystems, Inc. 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. Sun designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.25 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.26 + * have any questions. 1.27 + */ 1.28 +package com.sun.tools.javap; 1.29 + 1.30 +import com.sun.tools.classfile.Attribute; 1.31 +import com.sun.tools.classfile.Code_attribute; 1.32 +import com.sun.tools.classfile.ExtendedAnnotation; 1.33 +import com.sun.tools.classfile.ExtendedAnnotation.Position; 1.34 +import com.sun.tools.classfile.Instruction; 1.35 +import com.sun.tools.classfile.Method; 1.36 +import com.sun.tools.classfile.RuntimeInvisibleTypeAnnotations_attribute; 1.37 +import com.sun.tools.classfile.RuntimeTypeAnnotations_attribute; 1.38 +import com.sun.tools.classfile.RuntimeVisibleTypeAnnotations_attribute; 1.39 +import java.util.ArrayList; 1.40 +import java.util.HashMap; 1.41 +import java.util.List; 1.42 +import java.util.Map; 1.43 + 1.44 +/** 1.45 + * Annotate instructions with details about type annotations. 1.46 + * 1.47 + * <p><b>This is NOT part of any API supported by Sun Microsystems. If 1.48 + * you write code that depends on this, you do so at your own risk. 1.49 + * This code and its internal interfaces are subject to change or 1.50 + * deletion without notice.</b> 1.51 + */ 1.52 +public class TypeAnnotationWriter extends InstructionDetailWriter { 1.53 + public enum NoteKind { VISIBLE, INVISIBLE }; 1.54 + public static class Note { 1.55 + Note(NoteKind kind, ExtendedAnnotation anno) { 1.56 + this.kind = kind; 1.57 + this.anno = anno; 1.58 + } 1.59 + public final NoteKind kind; 1.60 + public final ExtendedAnnotation anno; 1.61 + } 1.62 + 1.63 + static TypeAnnotationWriter instance(Context context) { 1.64 + TypeAnnotationWriter instance = context.get(TypeAnnotationWriter.class); 1.65 + if (instance == null) 1.66 + instance = new TypeAnnotationWriter(context); 1.67 + return instance; 1.68 + } 1.69 + 1.70 + protected TypeAnnotationWriter(Context context) { 1.71 + super(context); 1.72 + context.put(TypeAnnotationWriter.class, this); 1.73 + annotationWriter = AnnotationWriter.instance(context); 1.74 + classWriter = ClassWriter.instance(context); 1.75 + } 1.76 + 1.77 + public void reset(Code_attribute attr) { 1.78 + Method m = classWriter.getMethod(); 1.79 + pcMap = new HashMap<Integer, List<Note>>(); 1.80 + check(NoteKind.VISIBLE, (RuntimeVisibleTypeAnnotations_attribute) m.attributes.get(Attribute.RuntimeVisibleTypeAnnotations)); 1.81 + check(NoteKind.INVISIBLE, (RuntimeInvisibleTypeAnnotations_attribute) m.attributes.get(Attribute.RuntimeInvisibleTypeAnnotations)); 1.82 + } 1.83 + 1.84 + private void check(NoteKind kind, RuntimeTypeAnnotations_attribute attr) { 1.85 + if (attr == null) 1.86 + return; 1.87 + 1.88 + for (ExtendedAnnotation anno: attr.annotations) { 1.89 + Position p = anno.position; 1.90 + Note note = null; 1.91 + if (p.offset != -1) 1.92 + addNote(p.offset, note = new Note(kind, anno)); 1.93 + if (p.lvarOffset != null) { 1.94 + for (int i = 0; i < p.lvarOffset.length; i++) { 1.95 + if (note == null) 1.96 + note = new Note(kind, anno); 1.97 + addNote(p.lvarOffset[i], note); 1.98 + } 1.99 + } 1.100 + } 1.101 + } 1.102 + 1.103 + private void addNote(int pc, Note note) { 1.104 + List<Note> list = pcMap.get(pc); 1.105 + if (list == null) 1.106 + pcMap.put(pc, list = new ArrayList<Note>()); 1.107 + list.add(note); 1.108 + } 1.109 + 1.110 + @Override 1.111 + void writeDetails(Instruction instr) { 1.112 + String indent = space(2); // get from Options? 1.113 + int pc = instr.getPC(); 1.114 + List<Note> notes = pcMap.get(pc); 1.115 + if (notes != null) { 1.116 + for (Note n: notes) { 1.117 + print(indent); 1.118 + print("@"); 1.119 + annotationWriter.write(n.anno, false, true); 1.120 + print(", "); 1.121 + println(n.kind.toString().toLowerCase()); 1.122 + } 1.123 + } 1.124 + } 1.125 + 1.126 + private AnnotationWriter annotationWriter; 1.127 + private ClassWriter classWriter; 1.128 + private Map<Integer, List<Note>> pcMap; 1.129 +}