• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Javassist, a Java-bytecode translator toolkit.
3  * Copyright (C) 1999- 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  * or the Apache License Version 2.0.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  */
16 
17 package javassist.bytecode;
18 
19 import java.io.DataInputStream;
20 import java.io.IOException;
21 import java.util.Map;
22 
23 /**
24  * <code>SourceFile_attribute</code>.
25  */
26 public class SourceFileAttribute extends AttributeInfo {
27     /**
28      * The name of this attribute <code>"SourceFile"</code>.
29      */
30     public static final String tag = "SourceFile";
31 
SourceFileAttribute(ConstPool cp, int n, DataInputStream in)32     SourceFileAttribute(ConstPool cp, int n, DataInputStream in)
33         throws IOException
34     {
35         super(cp, n, in);
36     }
37 
38     /**
39      * Constructs a SourceFile attribute.
40      *
41      * @param cp                a constant pool table.
42      * @param filename          the name of the source file.
43      */
SourceFileAttribute(ConstPool cp, String filename)44     public SourceFileAttribute(ConstPool cp, String filename) {
45         super(cp, tag);
46         int index = cp.addUtf8Info(filename);
47         byte[] bvalue = new byte[2];
48         bvalue[0] = (byte)(index >>> 8);
49         bvalue[1] = (byte)index;
50         set(bvalue);
51     }
52 
53     /**
54      * Returns the file name indicated by <code>sourcefile_index</code>.
55      */
getFileName()56     public String getFileName() {
57         return getConstPool().getUtf8Info(ByteArray.readU16bit(get(), 0));
58     }
59 
60     /**
61      * Makes a copy.  Class names are replaced according to the
62      * given <code>Map</code> object.
63      *
64      * @param newCp     the constant pool table used by the new copy.
65      * @param classnames        pairs of replaced and substituted
66      *                          class names.
67      */
68     @Override
copy(ConstPool newCp, Map<String,String> classnames)69     public AttributeInfo copy(ConstPool newCp, Map<String,String> classnames) {
70         return new SourceFileAttribute(newCp, getFileName());
71     }
72 }
73