1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.bcel.classfile;
18
19 import java.io.DataInput;
20 import java.io.DataOutputStream;
21 import java.io.IOException;
22
23 import org.apache.bcel.Const;
24
25
26
27
28
29
30
31
32 public final class StackMapType implements Node, Cloneable {
33
34 public static final StackMapType[] EMPTY_ARRAY = {};
35
36 private byte type;
37 private int index = -1;
38 private ConstantPool constantPool;
39
40
41
42
43
44 public StackMapType(final byte type, final int index, final ConstantPool constantPool) {
45 this.type = checkType(type);
46 this.index = index;
47 this.constantPool = constantPool;
48 }
49
50
51
52
53
54
55
56 StackMapType(final DataInput file, final ConstantPool constantPool) throws IOException {
57 this(file.readByte(), -1, constantPool);
58 if (hasIndex()) {
59 this.index = file.readUnsignedShort();
60 }
61 this.constantPool = constantPool;
62 }
63
64
65
66
67
68
69
70
71 @Override
72 public void accept(final Visitor v) {
73 v.visitStackMapType(this);
74 }
75
76 private byte checkType(final byte type) {
77 if (type < Const.ITEM_Bogus || type > Const.ITEM_NewObject) {
78 throw new ClassFormatException("Illegal type for StackMapType: " + type);
79 }
80 return type;
81 }
82
83
84
85
86 public StackMapType copy() {
87 try {
88 return (StackMapType) clone();
89 } catch (final CloneNotSupportedException e) {
90
91 }
92 return null;
93 }
94
95
96
97
98
99
100
101 public void dump(final DataOutputStream file) throws IOException {
102 file.writeByte(type);
103 if (hasIndex()) {
104 file.writeShort(getIndex());
105 }
106 }
107
108
109
110
111
112
113 public String getClassName() {
114 return constantPool.constantToString(index, Const.CONSTANT_Class);
115 }
116
117
118
119
120 public ConstantPool getConstantPool() {
121 return constantPool;
122 }
123
124
125
126
127
128 public int getIndex() {
129 return index;
130 }
131
132 public byte getType() {
133 return type;
134 }
135
136
137
138
139 public boolean hasIndex() {
140 return type == Const.ITEM_Object || type == Const.ITEM_NewObject;
141 }
142
143 private String printIndex() {
144 if (type == Const.ITEM_Object) {
145 if (index < 0) {
146 return ", class=<unknown>";
147 }
148 return ", class=" + getClassName();
149 }
150 if (type == Const.ITEM_NewObject) {
151 return ", offset=" + index;
152 }
153 return "";
154 }
155
156
157
158
159 public void setConstantPool(final ConstantPool constantPool) {
160 this.constantPool = constantPool;
161 }
162
163 public void setIndex(final int index) {
164 this.index = index;
165 }
166
167 public void setType(final byte type) {
168 this.type = checkType(type);
169 }
170
171
172
173
174 @Override
175 public String toString() {
176 return "(type=" + Const.getItemName(type) + printIndex() + ")";
177 }
178 }