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>SourceFile_attribute</code>.
24 */
25public class SourceFileAttribute extends AttributeInfo {
26    /**
27     * The name of this attribute <code>"SourceFile"</code>.
28     */
29    public static final String tag = "SourceFile";
30
31    SourceFileAttribute(ConstPool cp, int n, DataInputStream in)
32        throws IOException
33    {
34        super(cp, n, in);
35    }
36
37    /**
38     * Constructs a SourceFile attribute.
39     *
40     * @param cp                a constant pool table.
41     * @param filename          the name of the source file.
42     */
43    public SourceFileAttribute(ConstPool cp, String filename) {
44        super(cp, tag);
45        int index = cp.addUtf8Info(filename);
46        byte[] bvalue = new byte[2];
47        bvalue[0] = (byte)(index >>> 8);
48        bvalue[1] = (byte)index;
49        set(bvalue);
50    }
51
52    /**
53     * Returns the file name indicated by <code>sourcefile_index</code>.
54     */
55    public String getFileName() {
56        return getConstPool().getUtf8Info(ByteArray.readU16bit(get(), 0));
57    }
58
59    /**
60     * Makes a copy.  Class names are replaced according to the
61     * given <code>Map</code> object.
62     *
63     * @param newCp     the constant pool table used by the new copy.
64     * @param classnames        pairs of replaced and substituted
65     *                          class names.
66     */
67    public AttributeInfo copy(ConstPool newCp, Map classnames) {
68        return new SourceFileAttribute(newCp, getFileName());
69    }
70}
71