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 anonymous classes and ... there can be only one.
027 *
028 * @since 6.0
029 */
030public class EnclosingMethod extends Attribute {
031
032    // Pointer to the CONSTANT_Class_info structure representing the
033    // innermost class that encloses the declaration of the current class.
034    private int classIndex;
035
036    // If the current class is not immediately enclosed by a method or
037    // constructor, then the value of the method_index item must be zero.
038    // Otherwise, the value of the method_index item must point to a
039    // CONSTANT_NameAndType_info structure representing the name and the
040    // type of a method in the class referenced by the class we point
041    // to in the class_index. *It is the compiler responsibility* to
042    // ensure that the method identified by this index is the closest
043    // lexically enclosing method that includes the local/anonymous class.
044    private int methodIndex;
045
046    // Ctors - and code to read an attribute in.
047    EnclosingMethod(final int nameIndex, final int len, final DataInput input, final ConstantPool cpool) throws IOException {
048        this(nameIndex, len, input.readUnsignedShort(), input.readUnsignedShort(), cpool);
049    }
050
051    private EnclosingMethod(final int nameIndex, final int len, final int classIdx, final int methodIdx, final ConstantPool cpool) {
052        super(Const.ATTR_ENCLOSING_METHOD, nameIndex, len, cpool);
053        classIndex = classIdx;
054        methodIndex = methodIdx;
055    }
056
057    @Override
058    public void accept(final Visitor v) {
059        v.visitEnclosingMethod(this);
060    }
061
062    @Override
063    public Attribute copy(final ConstantPool constantPool) {
064        return (Attribute) clone();
065    }
066
067    @Override
068    public final void dump(final DataOutputStream file) throws IOException {
069        super.dump(file);
070        file.writeShort(classIndex);
071        file.writeShort(methodIndex);
072    }
073
074    public final ConstantClass getEnclosingClass() {
075        return super.getConstantPool().getConstant(classIndex, Const.CONSTANT_Class, ConstantClass.class);
076    }
077
078    // Accessors
079    public final int getEnclosingClassIndex() {
080        return classIndex;
081    }
082
083    public final ConstantNameAndType getEnclosingMethod() {
084        if (methodIndex == 0) {
085            return null;
086        }
087        return super.getConstantPool().getConstant(methodIndex, Const.CONSTANT_NameAndType, ConstantNameAndType.class);
088    }
089
090    public final int getEnclosingMethodIndex() {
091        return methodIndex;
092    }
093
094    public final void setEnclosingClassIndex(final int idx) {
095        classIndex = idx;
096    }
097
098    public final void setEnclosingMethodIndex(final int idx) {
099        methodIndex = idx;
100    }
101}