EnclosingMethod.java

  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. import java.io.DataInput;
  19. import java.io.DataOutputStream;
  20. import java.io.IOException;

  21. import org.apache.bcel.Const;
  22. import org.apache.bcel.util.Args;

  23. /**
  24.  * This attribute exists for local or anonymous classes and ... there can be only one.
  25.  *
  26.  * @since 6.0
  27.  */
  28. public class EnclosingMethod extends Attribute {

  29.     // Pointer to the CONSTANT_Class_info structure representing the
  30.     // innermost class that encloses the declaration of the current class.
  31.     private int classIndex;

  32.     // If the current class is not immediately enclosed by a method or
  33.     // constructor, then the value of the method_index item must be zero.
  34.     // Otherwise, the value of the method_index item must point to a
  35.     // CONSTANT_NameAndType_info structure representing the name and the
  36.     // type of a method in the class referenced by the class we point
  37.     // to in the class_index. *It is the compiler responsibility* to
  38.     // ensure that the method identified by this index is the closest
  39.     // lexically enclosing method that includes the local/anonymous class.
  40.     private int methodIndex;

  41.     // Ctors - and code to read an attribute in.
  42.     EnclosingMethod(final int nameIndex, final int len, final DataInput input, final ConstantPool cpool) throws IOException {
  43.         this(nameIndex, len, input.readUnsignedShort(), input.readUnsignedShort(), cpool);
  44.     }

  45.     private EnclosingMethod(final int nameIndex, final int len, final int classIndex, final int methodIndex, final ConstantPool cpool) {
  46.         super(Const.ATTR_ENCLOSING_METHOD, nameIndex, Args.require(len, 4, "EnclosingMethod attribute length"), cpool);
  47.         this.classIndex = Args.requireU2(classIndex, 0, cpool.getLength(), "EnclosingMethod class index");
  48.         this.methodIndex = Args.requireU2(methodIndex, "EnclosingMethod method index");
  49.     }

  50.     @Override
  51.     public void accept(final Visitor v) {
  52.         v.visitEnclosingMethod(this);
  53.     }

  54.     @Override
  55.     public Attribute copy(final ConstantPool constantPool) {
  56.         return (Attribute) clone();
  57.     }

  58.     @Override
  59.     public final void dump(final DataOutputStream file) throws IOException {
  60.         super.dump(file);
  61.         file.writeShort(classIndex);
  62.         file.writeShort(methodIndex);
  63.     }

  64.     public final ConstantClass getEnclosingClass() {
  65.         return super.getConstantPool().getConstant(classIndex, Const.CONSTANT_Class, ConstantClass.class);
  66.     }

  67.     // Accessors
  68.     public final int getEnclosingClassIndex() {
  69.         return classIndex;
  70.     }

  71.     public final ConstantNameAndType getEnclosingMethod() {
  72.         if (methodIndex == 0) {
  73.             return null;
  74.         }
  75.         return super.getConstantPool().getConstant(methodIndex, Const.CONSTANT_NameAndType, ConstantNameAndType.class);
  76.     }

  77.     public final int getEnclosingMethodIndex() {
  78.         return methodIndex;
  79.     }

  80.     public final void setEnclosingClassIndex(final int idx) {
  81.         classIndex = idx;
  82.     }

  83.     public final void setEnclosingMethodIndex(final int idx) {
  84.         methodIndex = idx;
  85.     }
  86. }