001/**
002 *  Licensed to the Apache Software Foundation (ASF) under one or more
003 *  contributor license agreements.  See the NOTICE file distributed with
004 *  this work for additional information regarding copyright ownership.
005 *  The ASF licenses this file to You under the Apache License, Version 2.0
006 *  (the "License"); you may not use this file except in compliance with
007 *  the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 *  Unless required by applicable law or agreed to in writing, software
012 *  distributed under the License is distributed on an "AS IS" BASIS,
013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 *  See the License for the specific language governing permissions and
015 *  limitations under the License.
016 */
017package org.apache.bcel.classfile;
018
019import java.io.DataInput;
020import java.io.DataOutputStream;
021import java.io.IOException;
022
023import org.apache.bcel.Const;
024
025/**
026 * This attribute exists for local or
027 * anonymous classes and ... there can be only one.
028 *
029 * @since 6.0
030 */
031public class EnclosingMethod extends Attribute {
032
033    // Pointer to the CONSTANT_Class_info structure representing the
034    // innermost class that encloses the declaration of the current class.
035    private int classIndex;
036
037    // If the current class is not immediately enclosed by a method or
038    // constructor, then the value of the method_index item must be zero.
039    // Otherwise, the value of the  method_index item must point to a
040    // CONSTANT_NameAndType_info structure representing the name and the
041    // type of a method in the class referenced by the class we point
042    // to in the class_index.  *It is the compiler responsibility* to
043    // ensure that the method identified by this index is the closest
044    // lexically enclosing method that includes the local/anonymous class.
045    private int methodIndex;
046
047    // Ctors - and code to read an attribute in.
048    EnclosingMethod(final int nameIndex, final int len, final DataInput input, final ConstantPool cpool) throws IOException {
049        this(nameIndex, len, input.readUnsignedShort(), input.readUnsignedShort(), cpool);
050    }
051
052    private EnclosingMethod(final int nameIndex, final int len, final int classIdx,final int methodIdx, final ConstantPool cpool) {
053        super(Const.ATTR_ENCLOSING_METHOD, nameIndex, len, cpool);
054        classIndex  = classIdx;
055        methodIndex = methodIdx;
056    }
057
058    @Override
059    public void accept(final Visitor v) {
060      v.visitEnclosingMethod(this);
061    }
062
063    @Override
064    public Attribute copy(final ConstantPool constant_pool) {
065        return (Attribute) clone();
066    }
067
068    // Accessors
069    public final int getEnclosingClassIndex() {
070        return classIndex;
071    }
072
073    public final int getEnclosingMethodIndex() {
074        return methodIndex;
075    }
076
077    public final void setEnclosingClassIndex(final int idx) {
078        classIndex = idx;
079    }
080
081    public final void setEnclosingMethodIndex(final int idx) {
082        methodIndex = idx;
083    }
084
085    public final ConstantClass getEnclosingClass() {
086        final ConstantClass c =
087            (ConstantClass)super.getConstantPool().getConstant(classIndex,Const.CONSTANT_Class);
088        return c;
089    }
090
091    public final ConstantNameAndType getEnclosingMethod() {
092        if (methodIndex == 0) {
093            return null;
094        }
095        final ConstantNameAndType nat =
096            (ConstantNameAndType)super.getConstantPool().getConstant(methodIndex,Const.CONSTANT_NameAndType);
097        return nat;
098    }
099
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}