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