001    package edu.rice.cs.cunit.classFile.attributes;
002    
003    import edu.rice.cs.cunit.classFile.attributes.visitors.IAttributeVisitor;
004    import edu.rice.cs.cunit.classFile.code.instructions.LineNumberTable;
005    import edu.rice.cs.cunit.classFile.constantPool.APoolInfo;
006    import edu.rice.cs.cunit.classFile.constantPool.AUTFPoolInfo;
007    import edu.rice.cs.cunit.classFile.constantPool.ConstantPool;
008    import edu.rice.cs.cunit.classFile.constantPool.visitors.CheckUTFVisitor;
009    import edu.rice.cs.cunit.classFile.ClassFile;
010    import edu.rice.cs.cunit.util.Types;
011    
012    import java.util.ArrayList;
013    
014    /**
015     * Represents the SourceFile attribute in a class file.
016     *
017     * @author Mathias Ricken
018     */
019    public class SourceFileAttributeInfo extends AAttributeInfo {
020        /**
021         * Constructor.
022         *
023         * @param name attribute name
024         * @param data attribute data
025         * @param cp   constant pool
026         *
027         * @throws ClassFormatError
028         */
029        public SourceFileAttributeInfo(AUTFPoolInfo name, byte[] data, ConstantPool cp) throws ClassFormatError {
030            super(name, data, cp);
031        }
032    
033        /**
034         * Return the source file name information.
035         *
036         * @return source file name information
037         *
038         * @throws ClassFormatError
039         */
040        public AUTFPoolInfo getSourceFileName() throws ClassFormatError {
041            return _constantPool.get(Types.ushortFromBytes(_data, 0)).execute(CheckUTFVisitor.singleton(), null);
042        }
043    
044        /**
045         * Set the source file name information.
046         *
047         * @param newFileName constant pool
048         */
049        public void setSourceFileName(AUTFPoolInfo newFileName) {
050            _data = new byte[2];
051            Types.bytesFromShort(_constantPool.indexOf(newFileName), _data, 0);
052        }
053    
054        /**
055         * Execute a visitor on this attribute.
056         *
057         * @param visitor visitor
058         * @param param   visitor-specific parameter
059         *
060         * @return visitor-specific return value
061         */
062        public <R, D> R execute(IAttributeVisitor<R, D> visitor, D param) {
063            return visitor.sourceFileCase(this, param);
064        }
065    
066        /**
067         * Adjust program counter values contained in this attribute, starting at startPC, by adding deltaPC to them.
068         *
069         * @param startPC program counter to start at
070         * @param deltaPC change in program counter values
071         */
072        public void adjustPC(int startPC, int deltaPC) {
073            // nothing to do
074        }
075    
076        /**
077         * Translate the program counter values contained in this attribute from an old line number table to a new one.
078         *
079         * @param index      critical point (insertion or deletion point)
080         * @param deltaIndex delta value to add to all old line numbers greater than the critical point
081         * @param oldLnt     old line number table
082         * @param newLnt     new line number table
083         */
084        public void translatePC(int index, int deltaIndex, LineNumberTable oldLnt, LineNumberTable newLnt) {
085            // nothing to do
086        }
087    
088        /**
089         * Creates and returns a copy of this object.
090         */
091        public Object clone() throws CloneNotSupportedException {
092            return super.clone();
093        }
094    
095        /**
096         * Return a human-readable version of this attribute.
097         *
098         * @return string
099         */
100        public String toString() {
101            return "Source File = "+getSourceFileName().toString();
102        }
103    
104        /**
105         * Returns the name of the attribute as it appears in the class file.
106         *
107         * @return name of the attribute.
108         */
109        public static String getAttributeName() {
110            return "SourceFile";
111        }
112    }