Class TypeState


  • public final class TypeState
    extends Object
    Immutable representation of the type state at a specific point in bytecode execution.
    • Constructor Detail

      • TypeState

        public TypeState​(List<VerificationType> locals,
                         List<VerificationType> stack)
        Constructs a TypeState with the given locals and stack.
        Parameters:
        locals - local variable types
        stack - operand stack types
    • Method Detail

      • setSuperclassResolver

        public static void setSuperclassResolver​(Function<String,​String> resolver)
        Installs the global resolver used to walk superclass chains when merging reference types.
        Parameters:
        resolver - maps an internal class name to its superclass name, or null if unknown
      • empty

        public static TypeState empty()
        Creates an empty TypeState with no locals and empty stack.
        Returns:
        empty TypeState
      • fromMethodEntry

        public static TypeState fromMethodEntry​(MethodEntry method,
                                                ConstPool constPool)
        Creates the initial TypeState from a method descriptor and access flags.
        Parameters:
        method - the method entry
        constPool - the constant pool for resolving class references
        Returns:
        initial TypeState at method entry
      • getReturnType

        public static VerificationType getReturnType​(String descriptor,
                                                     ConstPool constPool)
        Returns the return type from a method descriptor.
        Parameters:
        descriptor - the method descriptor
        constPool - the constant pool
        Returns:
        return type or null for void
      • stackSlots

        public int stackSlots()
        The operand-stack depth in slots (category-2 long/double count as 2, since they are stored with a TOP filler) - i.e. the max_stack contribution of this state.
        Returns:
        the stack depth in slots
      • push

        public TypeState push​(VerificationType type)
        Pushes a type onto the stack.
        Parameters:
        type - the type to push
        Returns:
        new state with type pushed
      • pop

        public TypeState pop()
        Pops a single type from the stack.
        Returns:
        new state with top type removed
      • pop

        public TypeState pop​(int n)
        Pops n types from the stack.
        Parameters:
        n - number of types to pop
        Returns:
        new state with n types removed
      • peek

        public VerificationType peek()
        Gets the type at the top of the stack without popping.
        Returns:
        top stack type
      • peek

        public VerificationType peek​(int fromTop)
        Gets the type at position from top.
        Parameters:
        fromTop - offset from top (0 = top)
        Returns:
        type at position
      • clearStack

        public TypeState clearStack()
        Clears the stack.
        Returns:
        new state with empty stack
      • replaceType

        public TypeState replaceType​(VerificationType oldType,
                                     VerificationType newType)
        Substitutes every occurrence of a type in both locals and stack.
        Parameters:
        oldType - the type to replace
        newType - the replacement
        Returns:
        a new state with the substitution applied
      • setLocal

        public TypeState setLocal​(int index,
                                  VerificationType type)
        Sets a local variable at the given index.
        Parameters:
        index - the local variable index
        type - the type to set
        Returns:
        new state with local updated
      • getLocal

        public VerificationType getLocal​(int index)
        Gets the local variable type at the given index.
        Parameters:
        index - the local variable index
        Returns:
        type at index or TOP if out of bounds
      • getLocalsCount

        public int getLocalsCount()
        Gets the number of local variable slots used.
        Returns:
        locals count
      • getStackSize

        public int getStackSize()
        Gets the current stack depth.
        Returns:
        stack size
      • isStackEmpty

        public boolean isStackEmpty()
        Returns true if the stack is empty.
        Returns:
        true if stack is empty
      • localsToVerificationTypeInfo

        public List<VerificationTypeInfo> localsToVerificationTypeInfo()
        Converts locals to VerificationTypeInfo list for writing to class file.
        Returns:
        list of VerificationTypeInfo for locals
      • stackToVerificationTypeInfo

        public List<VerificationTypeInfo> stackToVerificationTypeInfo()
        Converts stack to VerificationTypeInfo list for writing to class file.
        Returns:
        list of VerificationTypeInfo for stack
      • withStack

        public TypeState withStack​(List<VerificationType> newStack)
        Creates a copy of this state with the given stack replaced.
        Parameters:
        newStack - the new stack
        Returns:
        new state with replaced stack
      • withLocals

        public TypeState withLocals​(List<VerificationType> newLocals)
        Creates a copy of this state with the given locals replaced.
        Parameters:
        newLocals - the new locals
        Returns:
        new state with replaced locals
      • merge

        public TypeState merge​(TypeState other)
        Merges this state with another without a constant pool, so unequal reference types unify to TOP.
        Parameters:
        other - the state on the joining path
        Returns:
        the merged state
      • merge

        public TypeState merge​(TypeState other,
                               ConstPool constPool)
        Merges this state with another at a control-flow join, unifying locals slot-by-slot and reference stack entries to a common superclass when a pool is available.
        Parameters:
        other - the state on the joining path
        constPool - pool used to intern merged reference types, or null to merge without one
        Returns:
        the merged state
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object