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