001    package serp.bytecode;
002    
003    import java.io.*;
004    
005    import serp.bytecode.visitor.*;
006    
007    /**
008     * The <code>multianewarray</code> instruction, which creates a new
009     * multi-dimensional array.
010     *
011     * @author Abe White
012     */
013    public class MultiANewArrayInstruction extends ClassInstruction {
014        private int _dims = -1;
015    
016        MultiANewArrayInstruction(Code owner) {
017            super(owner, Constants.MULTIANEWARRAY);
018        }
019    
020        int getLength() {
021            return super.getLength() + 1;
022        }
023    
024        public int getStackChange() {
025            return -_dims + 1;
026        }
027    
028        /**
029         * Return the dimensions of the array, or -1 if not set.
030         */
031        public int getDimensions() {
032            return _dims;
033        }
034    
035        /**
036         * Set the dimensions of the array.
037         *
038         * @return this instruction, for method chaining
039         */
040        public MultiANewArrayInstruction setDimensions(int dims) {
041            _dims = dims;
042            return this;
043        }
044    
045        /**
046         * Two MultiANewArray instructions are equal if they have the same
047         * type and dimensions, or if the type and dimensions of either is unset.
048         */
049        public boolean equalsInstruction(Instruction other) {
050            if (other == this)
051                return true;
052            if (!(other instanceof MultiANewArrayInstruction))
053                return false;
054            if (!super.equalsInstruction(other))
055                return false;
056    
057            MultiANewArrayInstruction ins = (MultiANewArrayInstruction) other;
058            int dims = getDimensions();
059            int otherDims = ins.getDimensions();
060            return dims == -1 || otherDims == -1 || dims == otherDims;
061        }
062    
063        public void acceptVisit(BCVisitor visit) {
064            visit.enterMultiANewArrayInstruction(this);
065            visit.exitMultiANewArrayInstruction(this);
066        }
067    
068        void read(Instruction orig) {
069            super.read(orig);
070            _dims = ((MultiANewArrayInstruction) orig).getDimensions();
071        }
072    
073        void read(DataInput in) throws IOException {
074            super.read(in);
075            _dims = in.readUnsignedByte();
076        }
077    
078        void write(DataOutput out) throws IOException {
079            super.write(out);
080            out.writeByte(_dims);
081        }
082    }