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.ExceptionConstants;
021    
022    /** 
023     * NEW - Create new object
024     * <PRE>Stack: ... -&gt; ..., objectref</PRE>
025     *
026     * @version $Id: NEW.java 1152072 2011-07-29 01:54:05Z dbrosius $
027     * @author  <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
028     */
029    public class NEW extends CPInstruction implements LoadClass, AllocationInstruction,
030            ExceptionThrower, StackProducer {
031    
032        private static final long serialVersionUID = 5773167897857305796L;
033    
034    
035        /**
036         * Empty constructor needed for the Class.newInstance() statement in
037         * Instruction.readInstruction(). Not to be used otherwise.
038         */
039        NEW() {
040        }
041    
042    
043        public NEW(int index) {
044            super(org.apache.bcel.Constants.NEW, index);
045        }
046    
047    
048        public Class<?>[] getExceptions() {
049            Class<?>[] cs = new Class[2 + ExceptionConstants.EXCS_CLASS_AND_INTERFACE_RESOLUTION.length];
050            System.arraycopy(ExceptionConstants.EXCS_CLASS_AND_INTERFACE_RESOLUTION, 0, cs, 0,
051                    ExceptionConstants.EXCS_CLASS_AND_INTERFACE_RESOLUTION.length);
052            cs[ExceptionConstants.EXCS_CLASS_AND_INTERFACE_RESOLUTION.length + 1] = ExceptionConstants.INSTANTIATION_ERROR;
053            cs[ExceptionConstants.EXCS_CLASS_AND_INTERFACE_RESOLUTION.length] = ExceptionConstants.ILLEGAL_ACCESS_ERROR;
054            return cs;
055        }
056    
057    
058        public ObjectType getLoadClassType( ConstantPoolGen cpg ) {
059            return (ObjectType) getType(cpg);
060        }
061    
062    
063        /**
064         * Call corresponding visitor method(s). The order is:
065         * Call visitor methods of implemented interfaces first, then
066         * call methods according to the class hierarchy in descending order,
067         * i.e., the most specific visitXXX() call comes last.
068         *
069         * @param v Visitor object
070         */
071        @Override
072        public void accept( Visitor v ) {
073            v.visitLoadClass(this);
074            v.visitAllocationInstruction(this);
075            v.visitExceptionThrower(this);
076            v.visitStackProducer(this);
077            v.visitTypedInstruction(this);
078            v.visitCPInstruction(this);
079            v.visitNEW(this);
080        }
081    }