169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal/*
269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Javassist, a Java-bytecode translator toolkit.
369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Copyright (C) 1999-2007 Shigeru Chiba. All Rights Reserved.
469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal *
569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * The contents of this file are subject to the Mozilla Public License Version
669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 1.1 (the "License"); you may not use this file except in compliance with
769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * the License.  Alternatively, the contents of this file may be used under
869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * the terms of the GNU Lesser General Public License Version 2.1 or later.
969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal *
1069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Software distributed under the License is distributed on an "AS IS" basis,
1169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
1269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * for the specific language governing rights and limitations under the
1369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * License.
1469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */
1569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
1669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalpackage javassist.bytecode;
1769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
1869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport java.io.DataInputStream;
1969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport java.io.IOException;
2069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport java.util.Map;
2169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
2269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal/**
2369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * <code>EnclosingMethod_attribute</code>.
2469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */
2569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalpublic class EnclosingMethodAttribute extends AttributeInfo {
2669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
2769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * The name of this attribute <code>"EnclosingMethod"</code>.
2869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
2969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public static final String tag = "EnclosingMethod";
3069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
3169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    EnclosingMethodAttribute(ConstPool cp, int n, DataInputStream in)
3269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        throws IOException
3369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    {
3469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        super(cp, n, in);
3569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
3669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
3769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
3869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Constructs an EnclosingMethod attribute.
3969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
4069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @param cp                a constant pool table.
4169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @param className         the name of the innermost enclosing class.
4269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @param methodName        the name of the enclosing method.
4369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @param methodDesc        the descriptor of the enclosing method.
4469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
4569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public EnclosingMethodAttribute(ConstPool cp, String className,
4669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                                    String methodName, String methodDesc) {
4769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        super(cp, tag);
4869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int ci = cp.addClassInfo(className);
4969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int ni = cp.addNameAndTypeInfo(methodName, methodDesc);
5069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        byte[] bvalue = new byte[4];
5169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        bvalue[0] = (byte)(ci >>> 8);
5269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        bvalue[1] = (byte)ci;
5369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        bvalue[2] = (byte)(ni >>> 8);
5469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        bvalue[3] = (byte)ni;
5569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        set(bvalue);
5669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
5769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
5869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
5969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Constructs an EnclosingMethod attribute.
6069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * The value of <code>method_index</code> is set to 0.
6169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
6269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @param cp                a constant pool table.
6369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @param className         the name of the innermost enclosing class.
6469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
6569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public EnclosingMethodAttribute(ConstPool cp, String className) {
6669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        super(cp, tag);
6769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int ci = cp.addClassInfo(className);
6869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int ni = 0;
6969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        byte[] bvalue = new byte[4];
7069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        bvalue[0] = (byte)(ci >>> 8);
7169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        bvalue[1] = (byte)ci;
7269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        bvalue[2] = (byte)(ni >>> 8);
7369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        bvalue[3] = (byte)ni;
7469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        set(bvalue);
7569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
7669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
7769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
7869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Returns the value of <code>class_index</code>.
7969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
8069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public int classIndex() {
8169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return ByteArray.readU16bit(get(), 0);
8269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
8369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
8469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
8569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Returns the value of <code>method_index</code>.
8669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
8769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public int methodIndex() {
8869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return ByteArray.readU16bit(get(), 2);
8969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
9069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
9169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
9269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Returns the name of the class specified by <code>class_index</code>.
9369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
9469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public String className() {
9569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return getConstPool().getClassInfo(classIndex());
9669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
9769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
9869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
9969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Returns the method name specified by <code>method_index</code>.
10069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
10169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public String methodName() {
10269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        ConstPool cp = getConstPool();
10369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int mi = methodIndex();
10469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int ni = cp.getNameAndTypeName(mi);
10569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return cp.getUtf8Info(ni);
10669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
10769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
10869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
10969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Returns the method descriptor specified by <code>method_index</code>.
11069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
11169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public String methodDescriptor() {
11269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        ConstPool cp = getConstPool();
11369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int mi = methodIndex();
11469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int ti = cp.getNameAndTypeDescriptor(mi);
11569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return cp.getUtf8Info(ti);
11669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
11769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
11869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
11969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Makes a copy.  Class names are replaced according to the
12069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * given <code>Map</code> object.
12169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
12269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @param newCp     the constant pool table used by the new copy.
12369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @param classnames        pairs of replaced and substituted
12469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *                          class names.
12569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
12669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public AttributeInfo copy(ConstPool newCp, Map classnames) {
12769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (methodIndex() == 0)
12869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return new EnclosingMethodAttribute(newCp, className());
12969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        else
13069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return new EnclosingMethodAttribute(newCp, className(),
13169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                                            methodName(), methodDescriptor());
13269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
13369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal}
134