View Javadoc

1   /*
2    * Copyright  2000-2004 The Apache Software Foundation
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License"); 
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License. 
15   *
16   */
17  package org.apache.bcel.generic;
18  
19  import org.apache.bcel.ExceptionConstants;
20  
21  /*** 
22   * CHECKCAST - Check whether object is of given type
23   * <PRE>Stack: ..., objectref -&gt; ..., objectref</PRE>
24   *
25   * @version $Id: CHECKCAST.java 386056 2006-03-15 11:31:56Z tcurdt $
26   * @author  <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
27   */
28  public class CHECKCAST extends CPInstruction implements LoadClass, ExceptionThrower, StackProducer,
29          StackConsumer {
30  
31      /***
32       * Empty constructor needed for the Class.newInstance() statement in
33       * Instruction.readInstruction(). Not to be used otherwise.
34       */
35      CHECKCAST() {
36      }
37  
38  
39      /*** Check whether object is of given type
40       * @param index index to class in constant pool
41       */
42      public CHECKCAST(int index) {
43          super(org.apache.bcel.Constants.CHECKCAST, index);
44      }
45  
46  
47      /*** @return exceptions this instruction may cause
48       */
49      public Class[] getExceptions() {
50          Class[] cs = new Class[1 + ExceptionConstants.EXCS_CLASS_AND_INTERFACE_RESOLUTION.length];
51          System.arraycopy(ExceptionConstants.EXCS_CLASS_AND_INTERFACE_RESOLUTION, 0, cs, 0,
52                  ExceptionConstants.EXCS_CLASS_AND_INTERFACE_RESOLUTION.length);
53          cs[ExceptionConstants.EXCS_CLASS_AND_INTERFACE_RESOLUTION.length] = ExceptionConstants.CLASS_CAST_EXCEPTION;
54          return cs;
55      }
56  
57  
58      public ObjectType getLoadClassType( ConstantPoolGen cpg ) {
59          Type t = getType(cpg);
60          if (t instanceof ArrayType) {
61              t = ((ArrayType) t).getBasicType();
62          }
63          return (t instanceof ObjectType) ? (ObjectType) t : null;
64      }
65  
66  
67      /***
68       * Call corresponding visitor method(s). The order is:
69       * Call visitor methods of implemented interfaces first, then
70       * call methods according to the class hierarchy in descending order,
71       * i.e., the most specific visitXXX() call comes last.
72       *
73       * @param v Visitor object
74       */
75      public void accept( Visitor v ) {
76          v.visitLoadClass(this);
77          v.visitExceptionThrower(this);
78          v.visitStackProducer(this);
79          v.visitStackConsumer(this);
80          v.visitTypedInstruction(this);
81          v.visitCPInstruction(this);
82          v.visitCHECKCAST(this);
83      }
84  }