Site Tools


Sidebar

Smart Card Solution

User Manual

JavaCard API Samples

Java Card Specification

Knowledge Sharing

javacard:jcvm:7.5.16_checkcast

7.5.16 checkcast

Check whether object is of given type

Format

checkcast

atype

indexbyte1

indexbyte2

Forms

checkcast = 148 (0x94)

Stack

…, objectref → …, objectref

Description

The unsigned byte atype is a code that indicates if the type against which the object is being checked is an array type or a class type. It must take one of the following values or zero:

Array Type

atype

T_BOOLEAN

10

T_BYTE

11

T_SHORT

12

T_INT

13

T_REFERENCE

14

If the value of atype is 10, 11, 12, or 13, the values of the indexbyte1 and indexbyte2 must be zero, and the value of atype indicates the array type against which to check the object. Otherwise the unsigned indexbyte1 and indexbyte2 are used to construct an index into the constant pool of the current package ( Section 3.5, Frames), where the value of the index is (indexbyte1 « 8) | indexbyte2. The item at that index in the constant pool must be of type CONSTANT_Classref ( Section 6.7.1, CONSTANT_Classref), a reference to a class or interface type. The reference is resolved. If the value of atype is 14, the object is checked against an array type that is an array of object references of the type of the resolved class. If the value of atype is zero, the object is checked against a class or interface type that is the resolved class.

The objectref must be of type reference. If objectref is null or can be cast to the specified array type or the resolved class or interface type, the operand stack is unchanged; otherwise the checkcast instruction throws a ClassCastException.

The following rules are used to determine whether an objectref that is not null can be cast to the resolved type: if S is the class of the object referred to by objectref and T is the resolved class, array or interface type, checkcast determines whether objectref can be cast to type T as follows:

  • If S is a class type, then:
  • If T is a class type, then S must be the same class as T, or S must be a subclass of T;
  • If T is an interface type, then S must implement interface T.
  • If T is an interface type, T must be the same interface as S or a superinterface of S.
  • If T is a class type, then T must be Object.
  • If T is an array type, namely the type TC[], an array of components of type TC, then one of the following must be true:

TC and SC are the same primitive type ( Section 3.1, Data Types and Values).

TC and SC are reference types Section 3.1, Data Types and Values) with type SC assignable to TC, by these rules.

  • If T is an interface type, T must be one of the interfaces implemented by arrays.

Runtime Exception

If objectref cannot be cast to the resolved class, array, or interface type, the checkcast instruction throws a ClassCastException.

Notes

The checkcast instruction is fundamentally very similar to the instanceof instruction. It differs in its treatment of null, its behavior when its test fails (checkcast throws an exception, instanceof pushes a result code), and its effect on the operand stack.

In some circumstances, the checkcast instruction may throw a SecurityException if the current context ( Section 3.4, Contexts) is not the owning context ( Section 3.4, Contexts) of the object referenced by objectref. The exact circumstances when the exception will be thrown are specified in Chapter 6 of the Runtime Environment Specification, Java Card Platform, Version 2.2.2.

If a virtual machine does not support the int data type, the value of atype may not be 13 (array type = T_INT).

javacard/jcvm/7.5.16_checkcast.txt · Last modified: 2017/05/13 04:09 (external edit)