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  
25  /**
26   * This attribute exists for local or
27   * 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 classIdx,final int methodIdx, final ConstantPool cpool) {
53          super(Const.ATTR_ENCLOSING_METHOD, nameIndex, len, cpool);
54          classIndex  = classIdx;
55          methodIndex = methodIdx;
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 constant_pool) {
65          return (Attribute) clone();
66      }
67  
68      // Accessors
69      public final int getEnclosingClassIndex() {
70          return classIndex;
71      }
72  
73      public final int getEnclosingMethodIndex() {
74          return methodIndex;
75      }
76  
77      public final void setEnclosingClassIndex(final int idx) {
78          classIndex = idx;
79      }
80  
81      public final void setEnclosingMethodIndex(final int idx) {
82          methodIndex = idx;
83      }
84  
85      public final ConstantClass getEnclosingClass() {
86          final ConstantClass c =
87              (ConstantClass)super.getConstantPool().getConstant(classIndex,Const.CONSTANT_Class);
88          return c;
89      }
90  
91      public final ConstantNameAndType getEnclosingMethod() {
92          if (methodIndex == 0) {
93              return null;
94          }
95          final ConstantNameAndType nat =
96              (ConstantNameAndType)super.getConstantPool().getConstant(methodIndex,Const.CONSTANT_NameAndType);
97          return nat;
98      }
99  
100     @Override
101     public final void dump(final DataOutputStream file) throws IOException {
102         super.dump(file);
103         file.writeShort(classIndex);
104         file.writeShort(methodIndex);
105     }
106 }