Package com.tonic.analysis.frame
Class TypeState
- java.lang.Object
-
- com.tonic.analysis.frame.TypeState
-
public final class TypeState extends Object
Immutable representation of the type state at a specific point in bytecode execution.
-
-
Constructor Summary
Constructors Constructor Description TypeState(List<VerificationType> locals, List<VerificationType> stack)Constructs a TypeState with the given locals and stack.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description TypeStateclearStack()Clears the stack.static TypeStateempty()Creates an empty TypeState with no locals and empty stack.booleanequals(Object o)static TypeStatefromMethodEntry(MethodEntry method, ConstPool constPool)Creates the initial TypeState from a method descriptor and access flags.VerificationTypegetLocal(int index)Gets the local variable type at the given index.List<VerificationType>getLocals()intgetLocalsCount()Gets the number of local variable slots used.static VerificationTypegetReturnType(String descriptor, ConstPool constPool)Returns the return type from a method descriptor.List<VerificationType>getStack()intgetStackSize()Gets the current stack depth.inthashCode()booleanisStackEmpty()Returns true if the stack is empty.List<VerificationTypeInfo>localsToVerificationTypeInfo()Converts locals to VerificationTypeInfo list for writing to class file.TypeStatemerge(TypeState other)Merges this state with another without a constant pool, so unequal reference types unify to TOP.TypeStatemerge(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.VerificationTypepeek()Gets the type at the top of the stack without popping.VerificationTypepeek(int fromTop)Gets the type at position from top.TypeStatepop()Pops a single type from the stack.TypeStatepop(int n)Pops n types from the stack.TypeStatepush(VerificationType type)Pushes a type onto the stack.TypeStatepushRaw(VerificationType type)Pushes a single raw stack entry, WITHOUT the category-2TOPcompanion thatpush(com.tonic.analysis.frame.VerificationType)appends for a long/double.TypeStatereplaceType(VerificationType oldType, VerificationType newType)Substitutes every occurrence of a type in both locals and stack.TypeStatesetLocal(int index, VerificationType type)Sets a local variable at the given index.static voidsetSuperclassResolver(Function<String,String> resolver)Installs the global resolver used to walk superclass chains when merging reference types.intstackSlots()The operand-stack depth in slots (category-2long/doublecount as 2, since they are stored with aTOPfiller) - i.e.List<VerificationTypeInfo>stackToVerificationTypeInfo()Converts stack to VerificationTypeInfo list for writing to class file.StringtoString()TypeStatewithLocals(List<VerificationType> newLocals)Creates a copy of this state with the given locals replaced.TypeStatewithStack(List<VerificationType> newStack)Creates a copy of this state with the given stack replaced.
-
-
-
Constructor Detail
-
TypeState
public TypeState(List<VerificationType> locals, List<VerificationType> stack)
Constructs a TypeState with the given locals and stack.- Parameters:
locals- local variable typesstack- 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
-
getLocals
public List<VerificationType> getLocals()
- Returns:
- the locals
-
getStack
public List<VerificationType> getStack()
- Returns:
- the stack
-
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 entryconstPool- 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 descriptorconstPool- the constant pool- Returns:
- return type or null for void
-
stackSlots
public int stackSlots()
The operand-stack depth in slots (category-2long/doublecount as 2, since they are stored with aTOPfiller) - i.e. themax_stackcontribution 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
-
pushRaw
public TypeState pushRaw(VerificationType type)
Pushes a single raw stack entry, WITHOUT the category-2TOPcompanion thatpush(com.tonic.analysis.frame.VerificationType)appends for a long/double.- Parameters:
type- the raw slot entry to push- Returns:
- new state with the entry 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 replacenewType- 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 indextype- 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 pathconstPool- pool used to intern merged reference types, or null to merge without one- Returns:
- the merged state
-
-