001    
002    /*
003     * Copyright 2007-2008 Sun Microsystems, Inc.  All Rights Reserved.
004     * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
005     *
006     * This code is free software; you can redistribute it and/or modify it
007     * under the terms of the GNU General Public License version 2 only, as
008     * published by the Free Software Foundation.  Sun designates this
009     * particular file as subject to the "Classpath" exception as provided
010     * by Sun in the LICENSE file that accompanied this code.
011     *
012     * This code is distributed in the hope that it will be useful, but WITHOUT
013     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014     * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
015     * version 2 for more details (a copy is included in the LICENSE file that
016     * accompanied this code).
017     *
018     * You should have received a copy of the GNU General Public License version
019     * 2 along with this work; if not, write to the Free Software Foundation,
020     * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
021     *
022     * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
023     * CA 95054 USA or visit www.sun.com if you need additional information or
024     * have any questions.
025     */
026    
027    package com.sun.tools.classfile;
028    
029    import java.io.IOException;
030    
031    /**
032     * See JVMS3, section 4.8.7.
033     *
034     *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
035     *  you write code that depends on this, you do so at your own risk.
036     *  This code and its internal interfaces are subject to change or
037     *  deletion without notice.</b>
038     */
039    public class EnclosingMethod_attribute extends Attribute {
040        EnclosingMethod_attribute(ClassReader cr, int name_index, int length) throws IOException {
041            super(name_index, length);
042            class_index = cr.readUnsignedShort();
043            method_index = cr.readUnsignedShort();
044        }
045    
046        public EnclosingMethod_attribute(ConstantPool constant_pool, int class_index, int method_index)
047                throws ConstantPoolException {
048            this(constant_pool.getUTF8Index(Attribute.EnclosingMethod), class_index, method_index);
049        }
050    
051        public EnclosingMethod_attribute(int name_index, int class_index, int method_index) {
052            super(name_index, 4);
053            this.class_index = class_index;
054            this.method_index = method_index;
055        }
056    
057        public String getClassName(ConstantPool constant_pool) throws ConstantPoolException {
058            return constant_pool.getClassInfo(class_index).getName();
059        }
060    
061        public String getMethodName(ConstantPool constant_pool) throws ConstantPoolException {
062            if (method_index == 0)
063                return "";
064            return constant_pool.getNameAndTypeInfo(method_index).getName();
065        }
066    
067        public <R, D> R accept(Visitor<R, D> visitor, D data) {
068            return visitor.visitEnclosingMethod(this, data);
069        }
070    
071        public final int class_index;
072        public final int method_index;
073    }