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