View Javadoc
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      public final ConstantClass getEnclosingClass() {
78          return super.getConstantPool().getConstant(classIndex, Const.CONSTANT_Class, ConstantClass.class);
79      }
80  
81      // Accessors
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 }