---
spec: JVMS
title: "The Java Virtual Machine Specification"
edition: "Java SE 27 Edition"
java_version: 27
chapter: "Chapter 4. The class File Format"
section: "4.10.1.9.invokespecial"
heading: "invokespecial"
parent: "4.10.1.9. Type Checking Instructions"
source: https://docs.oracle.com/javase/specs/jvms/se27/html/jvms-4.html#jvms-4.10.1.9.invokespecial
id: "4.10.1.9.invokespecial"
url: https://jvm.obfuscated.software/jvms/se27/4.10.1.9.invokespecial.html
raw: https://jvm.obfuscated.software/jvms/se27/4.10.1.9.invokespecial.md
---

# invokespecial

An *invokespecial* instruction is type safe iff all of the following are true:

- Its first operand, `CP`, refers to a constant pool entry denoting a method named `MethodName` with descriptor `Descriptor` that is a member of a type `MethodClassType`.

- Either:

  - `MethodName` is not `<init>`.
  - `MethodName` is not `<clinit>`.
  - `MethodClassType` is the current class, a superclass of the current class, or a direct superinterface of the current class.
  - One can validly replace types matching the current class and the argument types given in `Descriptor` on the incoming operand stack with the return type given in `Descriptor`, yielding the outgoing type state.

```
instructionIsTypeSafe(invokespecial(CP), Environment, _Offset, StackFrame,
                      NextStackFrame, ExceptionStackFrame) :-
    (CP = method(MethodClassType, MethodName, Descriptor) ;
     CP = imethod(MethodClassType, MethodName, Descriptor)),
    MethodName \= '<init>',
    MethodName \= '<clinit>',
    validSpecialMethodClassType(Environment, MethodClassType),
    parseMethodDescriptor(Descriptor, OperandArgList, ReturnType),
    thisType(Environment, ThisType),
    reverse([ThisType | OperandArgList], StackArgList),
    validTypeTransition(Environment, StackArgList, ReturnType,
                        StackFrame, NextStackFrame),
    exceptionStackFrame(StackFrame, ExceptionStackFrame).

validSpecialMethodClassType(Environment, class(MethodClassName, _)) :-
    thisClass(Environment, ThisClass),
    classClassName(ThisClass, MethodClassName).

validSpecialMethodClassType(Environment, class(MethodClassName, L)) :-
    thisClass(Environment, ThisClass),
    loadedSuperclasses(ThisClass, Supers),
    member(Super, Supers),
    classClassName(Super, MethodClassName),
    loadedClass(MethodClassName, L, Super).

validSpecialMethodClassType(Environment, class(MethodClassName, _)) :-
    thisClass(Environment, ThisClass),
    classInterfaceNames(ThisClass, InterfaceNames),
    member(MethodClassName, InterfaceNames).
```

The `validSpecialMethodClassType` clause enforces the structural constraint that *invokespecial*, for other than an instance initialization method, must name a method in the current class/interface or a superclass/superinterface.

The `validTypeTransition` clause enforces the structural constraint that *invokespecial*, for other than an instance initialization method, targets a receiver object of the current class or deeper. To see why, consider that `StackArgList` simulates the list of types on the operand stack expected by the method, starting with the current class (the class performing *invokespecial*). The actual types on the operand stack are in `StackFrame`. The effect of `validTypeTransition` is to pop the first type from the operand stack in `StackFrame` and check it is a subtype of the first term of `StackArgList`, namely the current class. Thus, the actual receiver type is compatible with the current class.

A sharp-eyed reader might notice that enforcing this structural constraint supercedes the structural constraint pertaining to *invokespecial* of a `protected` method. Thus, the Prolog code above makes no reference to `passesProtectedCheck` ([§4.10.1.8](4.10.1.8.md)), whereas the Prolog code for *invokespecial* of an instance initialization method uses `passesProtectedCheck` to ensure the actual receiver type is compatible with the current class when certain `protected` instance initialization methods are named.

- Or:

  - MethodName is `<init>`.
  - `Descriptor` specifies a `void` return type.
  - One can validly pop types matching the argument types given in `Descriptor` and `uninitializedThis` off the incoming operand stack, yielding `OperandStack`.
  - `MethodClassType` is the current class or the direct superclass of the current class.
  - The outgoing type state is derived from the incoming type state by first replacing the incoming operand stack with `OperandStack` and then replacing all instances of `uninitializedThis` with the type of the current class.

```
instructionIsTypeSafe(invokespecial(CP), Environment, _Offset, StackFrame,
                      NextStackFrame, ExceptionStackFrame) :-
    (CP = method(MethodClassType, '<init>', Descriptor) ;
     CP = imethod(MethodClassType, '<init>', Descriptor)),
    parseMethodDescriptor(Descriptor, OperandArgList, void),
    reverse([uninitializedThis | OperandArgList], StackArgList),
    canPop(StackFrame, StackArgList, frame(Locals, OperandStack, Flags)),
    validThisInitClassType(Environment, MethodClassType),
    thisType(Environment, This),
    substitute(uninitializedThis, This, OperandStack, NextOperandStack),
    substitute(uninitializedThis, This, Locals, NextLocals),
    substitute(uninitializedThis, top, Locals, ExceptionLocals),
    NextStackFrame = frame(NextLocals, NextOperandStack, []),
    ExceptionStackFrame = frame(ExceptionLocals, [], Flags).

validThisInitClassType(Environment, class(MethodClassName, _)) :-
    thisClass(Environment, ThisClass),
    classClassName(ThisClass, MethodClassName).

validThisInitClassType(Environment, class(MethodClassName, _)) :-
    thisClass(Environment, ThisClass),
    classSuperClassName(ThisClass, MethodClassName).

substitute(_Old, _New, [], []).
substitute(Old, New, [Old | FromRest], [New | ToRest]) :-
    substitute(Old, New, FromRest, ToRest).
substitute(Old, New, [From1 | FromRest], [From1 | ToRest]) :-
    From1 \= Old,
    substitute(Old, New, FromRest, ToRest).
```

- Or:

  - MethodName is `<init>`.
  - `Descriptor` specifies a `void` return type.
  - One can validly pop types matching the argument types given in `Descriptor` and an uninitialized type, `uninitialized(Address)`, off the incoming operand stack, yielding `OperandStack`.
  - The instruction at `Address` created a new `MethodClassType`.
  - The outgoing type state is derived from the incoming type state by first replacing the incoming operand stack with `OperandStack` and then replacing all instances of `uninitialized(Address)` with `MethodClassType`.
  - If the method is `protected`, the usage conforms to the special rules governing access to `protected` members ([§4.10.1.8](4.10.1.8.md)).

```
instructionIsTypeSafe(invokespecial(CP), Environment, _Offset, StackFrame,
                      NextStackFrame, ExceptionStackFrame) :-
    (CP = method(MethodClassType, '<init>', Descriptor) ;
     CP = imethod(MethodClassType, '<init>', Descriptor)),
    parseMethodDescriptor(Descriptor, OperandArgList, void),
    reverse([uninitialized(Address) | OperandArgList], StackArgList),
    canPop(StackFrame, StackArgList, frame(Locals, OperandStack, Flags)),
    allInstructions(Environment, Instructions),
    member(instruction(Address, new(MethodClassType)), Instructions),
    substitute(uninitialized(Address), MethodClassType,
               OperandStack, NextOperandStack),
    substitute(uninitialized(Address), MethodClassType, Locals, NextLocals),
    substitute(uninitialized(Address), top, Locals, ExceptionLocals),
    NextStackFrame = frame(NextLocals, NextOperandStack, Flags),
    ExceptionStackFrame = frame(ExceptionLocals, [], Flags),
    passesProtectedCheck(Environment, MethodClassType, '<init>',
                         Descriptor, NextStackFrame).
```

The rule for *invokespecial* of an `<init>` method is the sole motivation for passing back a distinct exception stack frame. The concern is that when initializing an object, the *invokespecial* invocation could fail, leaving the object in a partially initialized, permanently unusable state. To prevent repeated initialization attempts after an object fails to initialize the first time, an exception handler must consider any references to the object stored in local variables to have type `top` rather than `uninitializedThis` or `uninitialized(Offset)`.

In the special case of initializing the current object (that is, when invoking `<init>` for type `uninitializedThis`), the original frame typically holds an uninitialized object in local variable 0 and has flag `flagThisUninit`. Normal termination of *invokespecial* initializes the uninitialized object and turns off the `flagThisUninit` flag. But if the *invokespecial* invocation throws an exception, the exception frame contains the broken object (with type `top`) and the `flagThisUninit` flag (the old flag). There is no way to perform a return instruction given that type state, so the handler would have to throw another exception (or loop forever). In fact, it's not even possible to express a handler with that type state, because there is no way for a stack frame, as expressed by the `StackMapTable` attribute ([§4.7.4](4.7.4.md)), to carry `flagThisUninit` without any accompanying use of type `uninitializedThis`.

If not for these special constraints on object initialization, the local variable types and flags of the exception stack frame would always be the same as the local variable types and flags of the input stack frame.
