1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * https://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
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 * This attribute exists for local or anonymous classes and ... there can be only one.
30 *
31 * @since 6.0
32 */
33 public class EnclosingMethod extends Attribute {
34
35 // Pointer to the CONSTANT_Class_info structure representing the
36 // innermost class that encloses the declaration of the current class.
37 private int classIndex;
38
39 // If the current class is not immediately enclosed by a method or
40 // constructor, then the value of the method_index item must be zero.
41 // Otherwise, the value of the method_index item must point to a
42 // CONSTANT_NameAndType_info structure representing the name and the
43 // type of a method in the class referenced by the class we point
44 // to in the class_index. *It is the compiler responsibility* to
45 // ensure that the method identified by this index is the closest
46 // lexically enclosing method that includes the local/anonymous class.
47 private int methodIndex;
48
49 // Ctors - and code to read an attribute in.
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 /**
78 * Gets the enclosing class.
79 *
80 * @return the enclosing class.
81 */
82 public final ConstantClass getEnclosingClass() {
83 return super.getConstantPool().getConstant(classIndex, Const.CONSTANT_Class, ConstantClass.class);
84 }
85
86 /**
87 * Gets the enclosing class index.
88 *
89 * @return the enclosing class index.
90 */
91 public final int getEnclosingClassIndex() {
92 return classIndex;
93 }
94
95 /**
96 * Gets the enclosing method.
97 *
98 * @return the enclosing method.
99 */
100 public final ConstantNameAndType getEnclosingMethod() {
101 if (methodIndex == 0) {
102 return null;
103 }
104 return super.getConstantPool().getConstant(methodIndex, Const.CONSTANT_NameAndType, ConstantNameAndType.class);
105 }
106
107 /**
108 * Gets the enclosing method index.
109 *
110 * @return the enclosing method index.
111 */
112 public final int getEnclosingMethodIndex() {
113 return methodIndex;
114 }
115
116 /**
117 * Sets the enclosing class index.
118 *
119 * @param idx the index.
120 */
121 public final void setEnclosingClassIndex(final int idx) {
122 classIndex = idx;
123 }
124
125 /**
126 * Sets the enclosing method index.
127 *
128 * @param idx the index.
129 */
130 public final void setEnclosingMethodIndex(final int idx) {
131 methodIndex = idx;
132 }
133 }