15385
|
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 |
|
|
41 |
/**
|
|
42 |
* Annotate instructions with details about type annotations.
|
|
43 |
*
|
|
44 |
* <p><b>This is NOT part of any supported API.
|
|
45 |
* If you write code that depends on this, you do so at your own risk.
|
|
46 |
* This code and its internal interfaces are subject to change or
|
|
47 |
* deletion without notice.</b>
|
|
48 |
*/
|
|
49 |
public class TypeAnnotationWriter extends InstructionDetailWriter {
|
|
50 |
public enum NoteKind { VISIBLE, INVISIBLE };
|
|
51 |
public static class Note {
|
|
52 |
Note(NoteKind kind, TypeAnnotation anno) {
|
|
53 |
this.kind = kind;
|
|
54 |
this.anno = anno;
|
|
55 |
}
|
|
56 |
public final NoteKind kind;
|
|
57 |
public final TypeAnnotation anno;
|
|
58 |
}
|
|
59 |
|
|
60 |
static TypeAnnotationWriter instance(Context context) {
|
|
61 |
TypeAnnotationWriter instance = context.get(TypeAnnotationWriter.class);
|
|
62 |
if (instance == null)
|
|
63 |
instance = new TypeAnnotationWriter(context);
|
|
64 |
return instance;
|
|
65 |
}
|
|
66 |
|
|
67 |
protected TypeAnnotationWriter(Context context) {
|
|
68 |
super(context);
|
|
69 |
context.put(TypeAnnotationWriter.class, this);
|
|
70 |
annotationWriter = AnnotationWriter.instance(context);
|
|
71 |
classWriter = ClassWriter.instance(context);
|
|
72 |
}
|
|
73 |
|
|
74 |
public void reset(Code_attribute attr) {
|
|
75 |
Method m = classWriter.getMethod();
|
|
76 |
pcMap = new HashMap<Integer, List<Note>>();
|
|
77 |
check(NoteKind.VISIBLE, (RuntimeVisibleTypeAnnotations_attribute) m.attributes.get(Attribute.RuntimeVisibleTypeAnnotations));
|
|
78 |
check(NoteKind.INVISIBLE, (RuntimeInvisibleTypeAnnotations_attribute) m.attributes.get(Attribute.RuntimeInvisibleTypeAnnotations));
|
|
79 |
}
|
|
80 |
|
|
81 |
private void check(NoteKind kind, RuntimeTypeAnnotations_attribute attr) {
|
|
82 |
if (attr == null)
|
|
83 |
return;
|
|
84 |
|
|
85 |
for (TypeAnnotation anno: attr.annotations) {
|
|
86 |
Position p = anno.position;
|
|
87 |
Note note = null;
|
|
88 |
if (p.offset != -1)
|
|
89 |
addNote(p.offset, note = new Note(kind, anno));
|
|
90 |
if (p.lvarOffset != null) {
|
|
91 |
for (int i = 0; i < p.lvarOffset.length; i++) {
|
|
92 |
if (note == null)
|
|
93 |
note = new Note(kind, anno);
|
|
94 |
addNote(p.lvarOffset[i], note);
|
|
95 |
}
|
|
96 |
}
|
|
97 |
}
|
|
98 |
}
|
|
99 |
|
|
100 |
private void addNote(int pc, Note note) {
|
|
101 |
List<Note> list = pcMap.get(pc);
|
|
102 |
if (list == null)
|
|
103 |
pcMap.put(pc, list = new ArrayList<Note>());
|
|
104 |
list.add(note);
|
|
105 |
}
|
|
106 |
|
|
107 |
@Override
|
|
108 |
void writeDetails(Instruction instr) {
|
|
109 |
String indent = space(2); // get from Options?
|
|
110 |
int pc = instr.getPC();
|
|
111 |
List<Note> notes = pcMap.get(pc);
|
|
112 |
if (notes != null) {
|
|
113 |
for (Note n: notes) {
|
|
114 |
print(indent);
|
|
115 |
print("@");
|
|
116 |
annotationWriter.write(n.anno, false, true);
|
|
117 |
print(", ");
|
|
118 |
println(n.kind.toString().toLowerCase());
|
|
119 |
}
|
|
120 |
}
|
|
121 |
}
|
|
122 |
|
|
123 |
private AnnotationWriter annotationWriter;
|
|
124 |
private ClassWriter classWriter;
|
|
125 |
private Map<Integer, List<Note>> pcMap;
|
|
126 |
}
|