IP Library › Granted Patent US 8,863,079
Granted Patent B2
US 8,863,079 · App. 13/563,587 · Granted Oct 14, 2014

Efficient and expansive conversions between reference and primitive

Inventors: Joseph D. Darcy (Redwood City, CA); Maurizio Cimadamore (Dublin, IE); Alexander R. Buckley (Cupertino, CA)
Assignee: Oracle International Corporation
View Patent ↗
Loading inventors, assignments & file history…
Monitor This Case
Get email alerts when status or documents change.
Order Certified Copies
Most orders are placed with the USPTO same day — all within 24 business hours.
Order via The Patent Place →
Pre-filled with this patent's details
Quick Facts
Patent No.
US 8,863,079
App. No.
13/563,587
Granted
Oct 14, 2014
Kind
B2
Abstract

A tool, such as a compiler or an interpreter, receives program source code and determines that the code includes an operation for which type conversion is permitted on an operand. The tool determines 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 and the target type is a primitive type, the tool generates a set of instructions to (a) 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 (b) if such a value can be obtained, obtain the value and assign the value to a result of the operation and (c) if such a value cannot be obtained, generate an error indication.

Claims (63)

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.

2. The storage medium as recited in claim 1 , wherein the tool comprises at least one of: a compiler or an interpreter.

3. The storage medium as recited in claim 1 , wherein, to determine whether a value of the target primitive type can be obtained, the tool is configured to generate instructions to:

determine whether the run-time class of the operand is a wrapper class designed to encapsulate a primitive value.

4. The storage medium as recited in claim 3 , wherein, to determine whether a value of the target primitive type can be obtained, the tool is configured to generate instructions to:

determine, from the kind of wrapper class of which the operand is an instance, a method able to extract the primitive value encapsulated by the operand.

5. The storage medium as recited in claim 4 , wherein, to obtain the value assigned to the result, the tool is further operable to generate instructions to:

perform at least one narrowing conversion operation by invoking, on the operand object, the method able to extract the primitive value, wherein a result value of the narrowing conversion requires fewer bits of memory storage than the operand requires.

6. The storage medium as recited in claim 1 , wherein, to obtain the value assigned to the result, the tool is further operable to generate instructions to:

perform at least one widening conversion operation, wherein a result value of the widening conversion requires more bits of memory storage than the operand requires.

7. The storage medium as recited in claim 1 , wherein the set of instructions generated comprises a bytecode instruction for type conversion.

8. The storage medium as recited in claim 1 , wherein the tool is configured to:

in response to determining that the source type is a primitive type and the target type is a reference type, generate a second set of instructions to:

obtain at run time, using the operand as a parameter of a boxing operation, an object of a wrapper class corresponding to the primitive type;

perform a numeric conversion operation supported by the wrapper class to obtain an intermediate primitive value of a different primitive type encapsulated by the target type; and

perform another boxing operation to obtain an object of the target type from the intermediate primitive value.

9. The storage medium as recited in claim 1 , wherein the tool is configured to:

in response to determining that the source type is one of (a) a first reference type with a corresponding primitive type or (b) a supertype of all other reference types defined in the programming language, and the target type is a different reference type with a corresponding primitive type, generate a second set of instructions to implement a sequence of operations to obtain an object of the target type from the operand, wherein the sequence comprises one or more of: a reference-to-primitive conversion, an unboxing operation, a primitive-to-reference conversion, a boxing operation, or a numeric value extraction operation.

10. A method, comprising:

receiving, as input for a tool associated with a programming language that supports primitive types and reference types, source code for a computer program;

determining by the tool, 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 , generating by the tool 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.

11. The method as recited in claim 10 , wherein said generating a set of instructions comprises:

generating, to determine whether a value of the target primitive type can be obtained, instructions to determine whether the run-time class of the operand is a wrapper class designed to encapsulate a primitive value.

12. The method as recited in claim 11 , wherein said generating a set of instructions comprises:

generating instructions to determine, from the kind of wrapper class of which the operand is an instance, a method able to extract the primitive value encapsulated by the operand.

13. The method as recited in claim 10 , further comprising:

in response to determining that the source type is a primitive type and the target type is a reference type, generating a second set of instructions to:

obtain at run time, using the operand as a parameter, an object of a wrapper class corresponding to the primitive type;

perform a numeric conversion operation supported by the wrapper class to obtain an intermediate primitive value of a different primitive type encapsulated by the target type; and

obtain an object of the target type from the intermediate primitive value.

14. The method as recited in claim 10 , further comprising:

in response to determining that the source type is one of (a) a first reference type with a corresponding primitive type or (b) a supertype of all other reference types defined in the programming language, and the target type is a different reference type with a corresponding primitive type, generating, a set of instructions to implement a sequence of operations to obtain an object of the target type from the operand, wherein the sequence comprises one or more of: a reference-to-primitive conversion, an unboxing operation, a primitive-to-reference conversion, a boxing operation, or a numeric value extraction operation.

15. The method as recited in claim 10 , wherein the set of instructions comprises virtual machine bytecode instructions.

16. A system, comprising:

one or more processors;

a memory coupled to the one or more processors, wherein the memory stores program instructions executable by the processors to implement a tool for a programming language supporting reference types and primitive types, wherein the tool is configured to:

receive source code for a computer program;

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, 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.

17. The system as recited in claim 16 , wherein, to determine whether a value of the target primitive type can be obtained, the tool is configured to generate instructions to:

determine whether the run-time class of the operand is a wrapper class designed to encapsulate a primitive value.

18. The system as recited in claim 17 , wherein, to determine whether a value of the target primitive type can be obtained, the tool is configured to generate instructions to:

determine, from the kind of wrapper class of which the operand is an instance, a method able to extract the primitive value encapsulated by the operand.

19. The system as recited in claim 16 , wherein the tool is configured to:

in response to determining that the source type is a primitive type and the target type is a reference type, generate a second set of instructions to:

obtain at run time, using a boxing operation, an object of a wrapper class corresponding to the primitive type;

perform a numeric conversion operation supported by the wrapper class to obtain an intermediate primitive value of a different primitive type encapsulated by the target type; and

perform another boxing operation to obtain an object of the target type from the intermediate primitive value.

20. The system as recited in claim 16 , wherein the tool is configured to:

in response to determining that the source type is one of (a) a first reference type with a corresponding primitive type or (b) a supertype of all other reference types defined in the programming language, and the target type is a different reference type with a corresponding primitive type, generate a second set of instructions to implement a sequence of operations to obtain an object of the target type from the operand, wherein the sequence comprises one or more of: a reference-to-primitive conversion, an unboxing operation, a primitive-to-reference conversion, a boxing operation, or a numeric value extraction operation.

Assignments (2)
CORRECTIVE ASSIGNMENT TO CORRECT THE PLEASE CHANGE THE APPLICATION NUMBER FROM 13562635 TO 13563587 PREVIOUSLY RECORDED ON REEL 028708 FRAME 0735. ASSIGNOR(S) HEREBY CONFIRMS THE ASSIGNMENT. Recorded Aug 6, 2012
From: DARCY, JOSEPH D.; CIMADAMORE, MAURIZIO; BUCKLEY, ALEXANDER R.
To: ORACLE INTERNATIONAL CORPORATION
Reel/Frame 028734/0524 →
ASSIGNMENT OF ASSIGNOR'S INTEREST Recorded Aug 2, 2012
From: DARCY, JOSEPH D.; CIMADAMORE, MAURIZIO; BUCKLEY, ALEXANDER R.
To: ORACLE INTERNATIONAL CORPORATION
Reel/Frame 028708/0735 →
Continuity (1)
Related Publication 20140040860A1 · Feb 6, 2014