001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License. 
016     *
017     */
018    package org.apache.bcel.generic;
019    
020    import org.apache.bcel.Constants;
021    import org.apache.bcel.ExceptionConstants;
022    
023    /** 
024     * PUTFIELD - Put field in object
025     * <PRE>Stack: ..., objectref, value -&gt; ...</PRE>
026     * OR
027     * <PRE>Stack: ..., objectref, value.word1, value.word2 -&gt; ...</PRE>
028     *
029     * @version $Id: PUTFIELD.java 1152072 2011-07-29 01:54:05Z dbrosius $
030     * @author  <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
031     */
032    public class PUTFIELD extends FieldInstruction implements PopInstruction, ExceptionThrower {
033    
034        private static final long serialVersionUID = -3931392044558815011L;
035    
036    
037        /**
038         * Empty constructor needed for the Class.newInstance() statement in
039         * Instruction.readInstruction(). Not to be used otherwise.
040         */
041        PUTFIELD() {
042        }
043    
044    
045        public PUTFIELD(int index) {
046            super(Constants.PUTFIELD, index);
047        }
048    
049    
050        @Override
051        public int consumeStack( ConstantPoolGen cpg ) {
052            return getFieldSize(cpg) + 1;
053        }
054    
055    
056        public Class<?>[] getExceptions() {
057            Class<?>[] cs = new Class[2 + ExceptionConstants.EXCS_FIELD_AND_METHOD_RESOLUTION.length];
058            System.arraycopy(ExceptionConstants.EXCS_FIELD_AND_METHOD_RESOLUTION, 0, cs, 0,
059                    ExceptionConstants.EXCS_FIELD_AND_METHOD_RESOLUTION.length);
060            cs[ExceptionConstants.EXCS_FIELD_AND_METHOD_RESOLUTION.length + 1] = ExceptionConstants.INCOMPATIBLE_CLASS_CHANGE_ERROR;
061            cs[ExceptionConstants.EXCS_FIELD_AND_METHOD_RESOLUTION.length] = ExceptionConstants.NULL_POINTER_EXCEPTION;
062            return cs;
063        }
064    
065    
066        /**
067         * Call corresponding visitor method(s). The order is:
068         * Call visitor methods of implemented interfaces first, then
069         * call methods according to the class hierarchy in descending order,
070         * i.e., the most specific visitXXX() call comes last.
071         *
072         * @param v Visitor object
073         */
074        @Override
075        public void accept( Visitor v ) {
076            v.visitExceptionThrower(this);
077            v.visitStackConsumer(this);
078            v.visitPopInstruction(this);
079            v.visitTypedInstruction(this);
080            v.visitLoadClass(this);
081            v.visitCPInstruction(this);
082            v.visitFieldOrMethod(this);
083            v.visitFieldInstruction(this);
084            v.visitPUTFIELD(this);
085        }
086    }