jjg@338: /* jjg@338: * Copyright 2009 Sun Microsystems, Inc. All Rights Reserved. jjg@338: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@338: * jjg@338: * This code is free software; you can redistribute it and/or modify it jjg@338: * under the terms of the GNU General Public License version 2 only, as jjg@338: * published by the Free Software Foundation. Sun designates this jjg@338: * particular file as subject to the "Classpath" exception as provided jjg@338: * by Sun in the LICENSE file that accompanied this code. jjg@338: * jjg@338: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@338: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@338: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@338: * version 2 for more details (a copy is included in the LICENSE file that jjg@338: * accompanied this code). jjg@338: * jjg@338: * You should have received a copy of the GNU General Public License version jjg@338: * 2 along with this work; if not, write to the Free Software Foundation, jjg@338: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@338: * jjg@338: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, jjg@338: * CA 95054 USA or visit www.sun.com if you need additional information or jjg@338: * have any questions. jjg@338: */ jjg@338: package com.sun.tools.javap; jjg@338: jjg@338: import com.sun.tools.classfile.Attribute; jjg@338: import com.sun.tools.classfile.Code_attribute; jjg@338: import com.sun.tools.classfile.ExtendedAnnotation; jjg@338: import com.sun.tools.classfile.ExtendedAnnotation.Position; jjg@338: import com.sun.tools.classfile.Instruction; jjg@338: import com.sun.tools.classfile.Method; jjg@338: import com.sun.tools.classfile.RuntimeInvisibleTypeAnnotations_attribute; jjg@338: import com.sun.tools.classfile.RuntimeTypeAnnotations_attribute; jjg@338: import com.sun.tools.classfile.RuntimeVisibleTypeAnnotations_attribute; jjg@338: import java.util.ArrayList; jjg@338: import java.util.HashMap; jjg@338: import java.util.List; jjg@338: import java.util.Map; jjg@338: jjg@338: /** jjg@338: * Annotate instructions with details about type annotations. jjg@338: * jjg@338: *

This is NOT part of any API supported by Sun Microsystems. If jjg@338: * you write code that depends on this, you do so at your own risk. jjg@338: * This code and its internal interfaces are subject to change or jjg@338: * deletion without notice. jjg@338: */ jjg@338: public class TypeAnnotationWriter extends InstructionDetailWriter { jjg@338: public enum NoteKind { VISIBLE, INVISIBLE }; jjg@338: public static class Note { jjg@338: Note(NoteKind kind, ExtendedAnnotation anno) { jjg@338: this.kind = kind; jjg@338: this.anno = anno; jjg@338: } jjg@338: public final NoteKind kind; jjg@338: public final ExtendedAnnotation anno; jjg@338: } jjg@338: jjg@338: static TypeAnnotationWriter instance(Context context) { jjg@338: TypeAnnotationWriter instance = context.get(TypeAnnotationWriter.class); jjg@338: if (instance == null) jjg@338: instance = new TypeAnnotationWriter(context); jjg@338: return instance; jjg@338: } jjg@338: jjg@338: protected TypeAnnotationWriter(Context context) { jjg@338: super(context); jjg@338: context.put(TypeAnnotationWriter.class, this); jjg@338: annotationWriter = AnnotationWriter.instance(context); jjg@338: classWriter = ClassWriter.instance(context); jjg@338: } jjg@338: jjg@338: public void reset(Code_attribute attr) { jjg@338: Method m = classWriter.getMethod(); jjg@338: pcMap = new HashMap>(); jjg@338: check(NoteKind.VISIBLE, (RuntimeVisibleTypeAnnotations_attribute) m.attributes.get(Attribute.RuntimeVisibleTypeAnnotations)); jjg@338: check(NoteKind.INVISIBLE, (RuntimeInvisibleTypeAnnotations_attribute) m.attributes.get(Attribute.RuntimeInvisibleTypeAnnotations)); jjg@338: } jjg@338: jjg@338: private void check(NoteKind kind, RuntimeTypeAnnotations_attribute attr) { jjg@338: if (attr == null) jjg@338: return; jjg@338: jjg@338: for (ExtendedAnnotation anno: attr.annotations) { jjg@338: Position p = anno.position; jjg@338: Note note = null; jjg@338: if (p.offset != -1) jjg@338: addNote(p.offset, note = new Note(kind, anno)); jjg@338: if (p.lvarOffset != null) { jjg@338: for (int i = 0; i < p.lvarOffset.length; i++) { jjg@338: if (note == null) jjg@338: note = new Note(kind, anno); jjg@338: addNote(p.lvarOffset[i], note); jjg@338: } jjg@338: } jjg@338: } jjg@338: } jjg@338: jjg@338: private void addNote(int pc, Note note) { jjg@338: List list = pcMap.get(pc); jjg@338: if (list == null) jjg@338: pcMap.put(pc, list = new ArrayList()); jjg@338: list.add(note); jjg@338: } jjg@338: jjg@338: @Override jjg@338: void writeDetails(Instruction instr) { jjg@338: String indent = space(2); // get from Options? jjg@338: int pc = instr.getPC(); jjg@338: List notes = pcMap.get(pc); jjg@338: if (notes != null) { jjg@338: for (Note n: notes) { jjg@338: print(indent); jjg@338: print("@"); jjg@338: annotationWriter.write(n.anno, false, true); jjg@338: print(", "); jjg@338: println(n.kind.toString().toLowerCase()); jjg@338: } jjg@338: } jjg@338: } jjg@338: jjg@338: private AnnotationWriter annotationWriter; jjg@338: private ClassWriter classWriter; jjg@338: private Map> pcMap; jjg@338: }