1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.bcel.classfile;
20
21 import java.io.DataInput;
22 import java.io.DataOutputStream;
23 import java.io.IOException;
24
25 import org.apache.bcel.Const;
26 import org.apache.bcel.util.Args;
27
28
29
30
31
32
33 public class EnclosingMethod extends Attribute {
34
35
36
37 private int classIndex;
38
39
40
41
42
43
44
45
46
47 private int methodIndex;
48
49
50 EnclosingMethod(final int nameIndex, final int len, final DataInput input, final ConstantPool cpool) throws IOException {
51 this(nameIndex, len, input.readUnsignedShort(), input.readUnsignedShort(), cpool);
52 }
53
54 private EnclosingMethod(final int nameIndex, final int len, final int classIndex, final int methodIndex, final ConstantPool cpool) {
55 super(Const.ATTR_ENCLOSING_METHOD, nameIndex, Args.require(len, 4, "EnclosingMethod attribute length"), cpool);
56 this.classIndex = Args.requireU2(classIndex, 0, cpool.getLength(), "EnclosingMethod class index");
57 this.methodIndex = Args.requireU2(methodIndex, "EnclosingMethod method index");
58 }
59
60 @Override
61 public void accept(final Visitor v) {
62 v.visitEnclosingMethod(this);
63 }
64
65 @Override
66 public Attribute copy(final ConstantPool constantPool) {
67 return (Attribute) clone();
68 }
69
70 @Override
71 public final void dump(final DataOutputStream file) throws IOException {
72 super.dump(file);
73 file.writeShort(classIndex);
74 file.writeShort(methodIndex);
75 }
76
77 public final ConstantClass getEnclosingClass() {
78 return super.getConstantPool().getConstant(classIndex, Const.CONSTANT_Class, ConstantClass.class);
79 }
80
81
82 public final int getEnclosingClassIndex() {
83 return classIndex;
84 }
85
86 public final ConstantNameAndType getEnclosingMethod() {
87 if (methodIndex == 0) {
88 return null;
89 }
90 return super.getConstantPool().getConstant(methodIndex, Const.CONSTANT_NameAndType, ConstantNameAndType.class);
91 }
92
93 public final int getEnclosingMethodIndex() {
94 return methodIndex;
95 }
96
97 public final void setEnclosingClassIndex(final int idx) {
98 classIndex = idx;
99 }
100
101 public final void setEnclosingMethodIndex(final int idx) {
102 methodIndex = idx;
103 }
104 }