1. A non-transitory computer-readable storage medium storing program instructions executable by a computer to implement a tool configured to: receive source code for a computer program, written in a programming language that supports primitive types and reference types; determine, for an operation in the source code for which type conversion is permitted on at least one operand, a source type of the operand and a target type to which the operand is to be converted; in response to determining that the source type is a reference type corresponding to a source primitive type and the target type is a primitive type different than the source primitive type, generate a set of instructions to: determine whether, at run time, in accordance with a class definition of the reference type, a value of the target primitive type can be obtained via one or more method invocations on the operand; in response to determining that a value of the target primitive type can be obtained, obtain the value and assign the value to a result of the operation; and in response to determining that a value of the target primitive type cannot be obtained, generate an error indication.