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.*;
006    import edu.rice.cs.cunit.classFile.constantPool.visitors.ADefaultPoolInfoVisitor;
007    import edu.rice.cs.cunit.util.Types;
008    
009    import java.util.ArrayList;
010    
011    /**
012     * Represents the SourceFile attribute in a class file.
013     *
014     * @author Mathias Ricken
015     */
016    public class ConstantValueAttributeInfo extends AAttributeInfo {
017        /**
018         * Constructor.
019         *
020         * @param name attribute name
021         * @param data attribute data
022         * @param cp   constant pool
023         *
024         * @throws ClassFormatError
025         */
026        public ConstantValueAttributeInfo(AUTFPoolInfo name, byte[] data, ConstantPool cp) throws ClassFormatError {
027            super(name, data, cp);
028        }
029    
030        /**
031         * Return the constant value information.
032         *
033         * @return constant value information
034         *
035         * @throws ClassFormatError
036         */
037        public APoolInfo getConstantValue() throws ClassFormatError {
038            return _constantPool.get(Types.ushortFromBytes(_data, 0)).execute(new ADefaultPoolInfoVisitor<APoolInfo, Object>() {
039                public APoolInfo defaultCase(APoolInfo host, Object o) {
040                    throw new ClassFormatError("Variable type item expected for ConstantValue attribute.");
041                }
042    
043                public APoolInfo stringCase(StringPoolInfo host, Object o) {
044                    return host;
045                }
046    
047                public APoolInfo intCase(IntegerPoolInfo host, Object o) {
048                    return host;
049                }
050    
051                public APoolInfo floatCase(FloatPoolInfo host, Object o) {
052                    return host;
053                }
054    
055                public APoolInfo longCase(LongPoolInfo host, Object o) {
056                    return host;
057                }
058    
059                public APoolInfo doubleCase(DoublePoolInfo host, Object o) {
060                    return host;
061                }
062            }, null);
063        }
064    
065        /**
066         * Set the constant value information.
067         *
068         * @param newConst new constant value information.
069         *
070         * @throws IllegalArgumentException
071         */
072        public void setConstantValue(APoolInfo newConst) throws IllegalArgumentException {
073            Types.bytesFromShort(_constantPool.indexOf(newConst.execute(new ADefaultPoolInfoVisitor<APoolInfo, Object>() {
074                public APoolInfo defaultCase(APoolInfo host, Object o) {
075                    throw new IllegalArgumentException("Variable type item expected for ConstantValue attribute.");
076                }
077    
078                public APoolInfo stringCase(StringPoolInfo host, Object o) {
079                    return host;
080                }
081    
082                public APoolInfo intCase(IntegerPoolInfo host, Object o) {
083                    return host;
084                }
085    
086                public APoolInfo floatCase(FloatPoolInfo host, Object o) {
087                    return host;
088                }
089    
090                public APoolInfo longCase(LongPoolInfo host, Object o) {
091                    return host;
092                }
093    
094                public APoolInfo doubleCase(DoublePoolInfo host, Object o) {
095                    return host;
096                }
097            }, null)), _data, 0);
098        }
099    
100        /**
101         * Execute a visitor on this attribute.
102         *
103         * @param visitor visitor
104         * @param param   visitor-specific parameter
105         *
106         * @return visitor-specific return value
107         */
108        public <R, D> R execute(IAttributeVisitor<R, D> visitor, D param) {
109            return visitor.constantValueCase(this, param);
110        }
111    
112        /**
113         * Adjust program counter values contained in this attribute, starting at startPC, by adding deltaPC to them.
114         *
115         * @param startPC program counter to start at
116         * @param deltaPC change in program counter values
117         */
118        public void adjustPC(int startPC, int deltaPC) {
119            // nothing to do
120        }
121    
122        /**
123         * Translate the program counter values contained in this attribute from an old line number table to a new one.
124         *
125         * @param index      critical point (insertion or deletion point)
126         * @param deltaIndex delta value to add to all old line numbers greater than the critical point
127         * @param oldLnt     old line number table
128         * @param newLnt     new line number table
129         */
130        public void translatePC(int index, int deltaIndex, LineNumberTable oldLnt, LineNumberTable newLnt) {
131            // nothing to do
132        }
133    
134        /**
135         * Creates and returns a copy of this object.
136         */
137        public Object clone() throws CloneNotSupportedException {
138            return super.clone();
139        }
140    
141        /**
142         * Returns the name of the attribute as it appears in the class file.
143         *
144         * @return name of the attribute.
145         */
146        public static String getAttributeName() {
147            return "ConstantValue";
148        }
149    }