1/*
2 * Javassist, a Java-bytecode translator toolkit.
3 * Copyright (C) 1999-2007 Shigeru Chiba. All Rights Reserved.
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License.  Alternatively, the contents of this file may be used under
8 * the terms of the GNU Lesser General Public License Version 2.1 or later.
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 */
15
16package javassist.bytecode;
17
18import java.io.DataInputStream;
19import java.io.IOException;
20import java.util.Map;
21
22/**
23 * <code>EnclosingMethod_attribute</code>.
24 */
25public class EnclosingMethodAttribute extends AttributeInfo {
26    /**
27     * The name of this attribute <code>"EnclosingMethod"</code>.
28     */
29    public static final String tag = "EnclosingMethod";
30
31    EnclosingMethodAttribute(ConstPool cp, int n, DataInputStream in)
32        throws IOException
33    {
34        super(cp, n, in);
35    }
36
37    /**
38     * Constructs an EnclosingMethod attribute.
39     *
40     * @param cp                a constant pool table.
41     * @param className         the name of the innermost enclosing class.
42     * @param methodName        the name of the enclosing method.
43     * @param methodDesc        the descriptor of the enclosing method.
44     */
45    public EnclosingMethodAttribute(ConstPool cp, String className,
46                                    String methodName, String methodDesc) {
47        super(cp, tag);
48        int ci = cp.addClassInfo(className);
49        int ni = cp.addNameAndTypeInfo(methodName, methodDesc);
50        byte[] bvalue = new byte[4];
51        bvalue[0] = (byte)(ci >>> 8);
52        bvalue[1] = (byte)ci;
53        bvalue[2] = (byte)(ni >>> 8);
54        bvalue[3] = (byte)ni;
55        set(bvalue);
56    }
57
58    /**
59     * Constructs an EnclosingMethod attribute.
60     * The value of <code>method_index</code> is set to 0.
61     *
62     * @param cp                a constant pool table.
63     * @param className         the name of the innermost enclosing class.
64     */
65    public EnclosingMethodAttribute(ConstPool cp, String className) {
66        super(cp, tag);
67        int ci = cp.addClassInfo(className);
68        int ni = 0;
69        byte[] bvalue = new byte[4];
70        bvalue[0] = (byte)(ci >>> 8);
71        bvalue[1] = (byte)ci;
72        bvalue[2] = (byte)(ni >>> 8);
73        bvalue[3] = (byte)ni;
74        set(bvalue);
75    }
76
77    /**
78     * Returns the value of <code>class_index</code>.
79     */
80    public int classIndex() {
81        return ByteArray.readU16bit(get(), 0);
82    }
83
84    /**
85     * Returns the value of <code>method_index</code>.
86     */
87    public int methodIndex() {
88        return ByteArray.readU16bit(get(), 2);
89    }
90
91    /**
92     * Returns the name of the class specified by <code>class_index</code>.
93     */
94    public String className() {
95        return getConstPool().getClassInfo(classIndex());
96    }
97
98    /**
99     * Returns the method name specified by <code>method_index</code>.
100     */
101    public String methodName() {
102        ConstPool cp = getConstPool();
103        int mi = methodIndex();
104        int ni = cp.getNameAndTypeName(mi);
105        return cp.getUtf8Info(ni);
106    }
107
108    /**
109     * Returns the method descriptor specified by <code>method_index</code>.
110     */
111    public String methodDescriptor() {
112        ConstPool cp = getConstPool();
113        int mi = methodIndex();
114        int ti = cp.getNameAndTypeDescriptor(mi);
115        return cp.getUtf8Info(ti);
116    }
117
118    /**
119     * Makes a copy.  Class names are replaced according to the
120     * given <code>Map</code> object.
121     *
122     * @param newCp     the constant pool table used by the new copy.
123     * @param classnames        pairs of replaced and substituted
124     *                          class names.
125     */
126    public AttributeInfo copy(ConstPool newCp, Map classnames) {
127        if (methodIndex() == 0)
128            return new EnclosingMethodAttribute(newCp, className());
129        else
130            return new EnclosingMethodAttribute(newCp, className(),
131                                            methodName(), methodDescriptor());
132    }
133}
134