View Javadoc
1   /**
2    *  Licensed to the Apache Software Foundation (ASF) under one or more
3    *  contributor license agreements.  See the NOTICE file distributed with
4    *  this work for additional information regarding copyright ownership.
5    *  The ASF licenses this file to You under the Apache License, Version 2.0
6    *  (the "License"); you may not use this file except in compliance with
7    *  the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
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   * This attribute exists for local or anonymous classes and ... there can be only one.
28   *
29   * @since 6.0
30   */
31  public class EnclosingMethod extends Attribute {
32  
33      // Pointer to the CONSTANT_Class_info structure representing the
34      // innermost class that encloses the declaration of the current class.
35      private int classIndex;
36  
37      // If the current class is not immediately enclosed by a method or
38      // constructor, then the value of the method_index item must be zero.
39      // Otherwise, the value of the method_index item must point to a
40      // CONSTANT_NameAndType_info structure representing the name and the
41      // type of a method in the class referenced by the class we point
42      // to in the class_index. *It is the compiler responsibility* to
43      // ensure that the method identified by this index is the closest
44      // lexically enclosing method that includes the local/anonymous class.
45      private int methodIndex;
46  
47      // Ctors - and code to read an attribute in.
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      // Accessors
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 }