Site Tools


Sidebar

Smart Card Solution

User Manual

JavaCard API Samples

Java Card Specification

Knowledge Sharing

javacard:jcvm:7.5.53_instanceof

7.5.53 instanceof

Determine if object is of given type

Format

instanceof

atype

indexbyte1

indexbyte2

Forms

instanceof = 149 (0x95)

Stack

…, objectref → …, result

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. It is popped from the operand stack. If objectref is not null and is an instance of the resolved class, array or interface, the instanceof instruction pushes a short result of 1 on the operand stack. Otherwise it pushes a short result of 0.

The following rules are used to determine whether an objectref that is not null is an instance of 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, instanceof determines whether objectref is an instance of 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.

Notes

The instanceof instruction is fundamentally very similar to the checkcast 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 instanceof 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.53_instanceof.txt · Last modified: 2017/05/13 04:09 (external edit)