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

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