2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
6 * The Java Pathfinder core (jpf-core) platform is licensed under the
7 * Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0.
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package gov.nasa.jpf.jvm.bytecode;
20 import gov.nasa.jpf.vm.Instruction;
21 import gov.nasa.jpf.vm.ClassInfo;
22 import gov.nasa.jpf.vm.ElementInfo;
23 import gov.nasa.jpf.vm.LoadOnJPFRequired;
24 import gov.nasa.jpf.vm.MJIEnv;
25 import gov.nasa.jpf.vm.StackFrame;
26 import gov.nasa.jpf.vm.ThreadInfo;
27 import gov.nasa.jpf.vm.Types;
31 * Check whether object is of given type
32 * ..., objectref => ..., objectref
34 public class CHECKCAST extends Instruction implements JVMInstruction {
37 public CHECKCAST() {} // this is going away
39 public CHECKCAST(String typeName){
40 type = Types.getClassNameFromTypeName(typeName);
43 public String getTypeName() {
48 public Instruction execute (ThreadInfo ti) {
49 StackFrame frame = ti.getTopFrame();
50 int objref = frame.peek();
52 if (objref == MJIEnv.NULL) {
53 // we can cast 'null' to anything
56 boolean isValid = false;
58 if(Types.isReferenceSignature(type)) {
60 if(Types.isArray(type)) {
61 // retrieve the component terminal
62 t = Types.getComponentTerminal(type);
67 // resolve the referenced class
69 ti.resolveReferencedClass(t);
70 } catch(LoadOnJPFRequired lre) {
75 ElementInfo e = ti.getElementInfo(objref);
76 ClassInfo eci = e.getClassInfo();
78 if (type.charAt(0) == '['){ // cast between array types
80 // check if the element types are compatible
81 ClassInfo cci = eci.getComponentClassInfo();
82 isValid = cci.isInstanceOf(type.substring(1));
85 } else { // non-array types
86 isValid = e.getClassInfo().isInstanceOf(type);
90 return ti.createAndThrowException("java.lang.ClassCastException",
91 e.getClassInfo().getName() + " cannot be cast to " + type);
100 public int getLength() {
101 return 3; // opcode, index1, index2
105 public int getByteCode () {
110 public void accept(JVMInstructionVisitor insVisitor) {
111 insVisitor.visit(this);