The present invention relates generally to data processing systems and, more particularly, to protecting the identity of objects in a programming language that has built-in synchronization on objects.
In an object-oriented programming language, mathematical values and other literal data types are typically represented as objects. A literal is a value that is expressed as itself rather than as a variable's value or the result of an expression. For example, the number 5 is a literal of primitive data type Integer in the Java™ object-oriented programming language. Other primitive data types in the Java programming language include Boolean, Character, Byte, Short, Long, Float, and Double. Each of the primitive data types has a class wrapper around it that is useful when needed to manipulate literal types as objects. Accordingly, in the example above, the number 5 is an object of class Integer. The aforementioned classes are also immutable, which means that objects of these classes are not subject to changing fields. Therefore, the fields of the object 5 cannot be changed. The Java programming language also includes String as an immutable type, although it is not a primitive data type.
The identity of an object can often be detected in a variety of ways in object-oriented languages. For example, many object-oriented languages include an identity method that is implemented using an operator such as the “=” operator. The “=” operator determines whether two variables are identified with the same object. Another manner in which the identity of objects may be determined is by changing the fields of the objects. Field changing, however, is ineffective to determine the identity of objects when the objects are immutable.
Yet another method of determining the identity of objects exists in some object-oriented programming languages that enable multithreading, such as the Java programming language. Multithreaded languages provide support for multiple threads of execution, each handling a different task. The Java programming language, for example, may determine the identity of objects using the synchronized instruction. In particular, when a thread executes the synchronized statement to acquire an exclusive lock for an object, it does not access the object until it can obtain this lock, ensuring that no other threads can access the object at the same time. If a particular thread uses the synchronized statement to attempt to use one variable of an object, it can determine whether or not the object is locked. If the object is locked, then the variable is from the same object as another variable in use by another thread. If the object is not locked, then it can be determined that the variable in use by the other thread is not from the same object. The identity of the object, therefore, is determinable through use of the synchronized operation.
It is useful to protect the physical identity of objects to permit optimizations that involve aliasing in a manner that is semantically transparent. Accordingly, there is a need for a system and method for protecting the identity of objects in a programming language that has built-in synchronization on objects.
Methods and systems consistent with the principles of the invention enable protection of object identity in an object-oriented programming language. An object from a class for protecting object identity is instantiated in memory. The object is immediately locked in response to the instantiating, so that the identity of the locked object is protected from threads that attempt to synchronize on the locked object.
Other methods and systems consistent with the principles of the invention enable protection of object identity in an object-oriented programming language. An object from a class for protecting object identity is instantiated in memory. The object includes a first method that determines whether two object values are equal, and a second method that overrides an identity method associated with a superclass of the object by invoking the first method, the identity method for determining the identity of two objects. The object is immediately locked in response to the instantiating, so that the identity of the locked object is protected from threads that attempt to synchronize on the locked object.
Other methods and systems consistent with the principles of the invention also enable protection of object identity in an object-oriented programming language. A virtual machine receives code including an identity operator and then invokes an identity method associated with the identity operator. A first method is performed based on a determination that the identity method is associated with an object from a first class, the first method for determining whether two object values are substantially equal. An identity function is performed based on a determination that the identity method is not associated with an object from the first class, the identity function for determining whether two object references refer to the same object.
Other methods and systems consistent with the principles of the invention also enable protection of object identity in an object-oriented programming language. An object from a class for protecting object identity in an object-oriented programming language includes a first method that determines whether two object values are equal. The object also includes a second method that overrides an identity method associated with a superclass of the object by invoking the first method, the identity method for determining the identity of two objects.
Other methods and systems consistent with the principles of the invention also enable protection of object identity in an object-oriented programming language. A class hierarchy for protecting object identity includes three classes. A first class includes a first identity method configured to determine identity of two objects. A second class includes a second identity method and an equals method for determining whether two object values are equal, the second identity method overriding the first identity method by invoking the equals method, wherein the second class is a direct abstract subclass of the first class. A third class inherits the second identity method from the second class and is a subclass of the second class. An object from the third class is locked upon instantiation in memory so that the identity of the locked object is protected from threads that attempt to synchronize on the locked object.
The accompanying drawings are incorporated in and constitute a part of this specification and, together with the description, explain the features and principles of the invention. In the drawings:
The following detailed description of methods and systems consistent with the invention refers to the accompanying drawings. While the description includes exemplary implementations, other implementations are possible, and changes may be made to the implementations described without departing from the spirit and scope of the invention. The following detailed description does not limit the invention. Instead, the scope of the invention is defined by the appended claims and their equivalents.
Overview
Methods and systems consistent with the principles of the invention protect object identity in an object-oriented programming language. When an instance of an object of a particular class that complies with certain aspects of this invention is created, the new object is automatically locked in response to the instantiation. As a result, the identity of the locked object is protected from threads that attempt to synchronize on the object.
The object itself includes a first method that determines whether two object values are equal and a second method that overrides an identity method from a superclass of the object by calling the first method. The identity method determines the identity of two objects (e.g., determines whether two object references refer to the same object). Accordingly, attempts to determine the identity of the object using an identity method result in determining whether two object values are equal, not whether two object references refer to the same object.
Network Environment
Servers 102 and 104a-104n supply clients 106a-106n with programs written in a programming language, such as the Java programming language. For example, a software developer may create one or more programs in the Java programming language and compile them into class files that contain bytecodes executable by a virtual machine, such as the Java™ virtual machine. When a client, such as client 106a, wishes to execute a program written in the Java programming language, it may issue a request to a server, such as server 102, that contains the program. In response, server 102 transmits the corresponding class files to client 106a via a communication channel, such as network 108 (which may comprise a wired or wireless communication network, including the Internet). Client 106a may load the class files into a virtual machine located in client 106a and proceed to execute the program written in the Java programming language. Alternatively, a client may receive a program, such as a program written in the Java programming language, from another client instead of a server.
Server 102 may include CPU 110, secondary storage 112, input device 114, display 116, communications device 118, and memory 120. Memory 120 may include operating system 122, compiler 124, core classes 126, development environment 128, source code 130, class file repository 132, which includes class files 134, and class loader 136. Servers 104a-104n may contain components similar to those in server 102.
Compiler 124 translates source code into class files that contain bytecodes executable by a virtual machine or abstract processor. Core classes 126 may be a set of classes, such as a library of classes written in the Java programming language, that can be referenced by source files, such as source code 130, containing other/new classes written in the Java programming language. Core classes 126 and/or source code 130 may define special value classes and constant classes that make it possible to protect the identity of objects, such as primitive data type objects. Development environment 128 may provide a mechanism, such as an editor, for generating source files, such as source code 130. Source code 130 may be files containing code written in the Java programming language. Class file repository 132 includes class files 134. Class files 134 are bytecodes executable by a virtual machine and contain data representing a particular class, including data structures, method implementations, and references to other classes. Class loader 136 loads classes onto a client.
When a user of client 106a wishes to execute a program stored on a server, such as server 102, the user may use browser 216 to issue a request to server 102, using known client-server communication protocols such as TCP/IP (Transmission Control Protocol/Internet Protocol). In response, server 102 transmits the corresponding class files to client 106a via network 108. For example, class files from server 102 may be stored either temporarily or on a more permanent basis in class file repository 218. Client 106a may load the class files into a virtual machine, such as virtual machine 222, located in client 106a and proceed to execute the program. Class 106a may also receive class files from another client.
Class file repository 218 includes class files 220. Class files 220 may include both preloaded class files and dynamically loaded class files. Preloaded class files are those class files that are loaded onto client 106a prior to runtime. Dynamically loaded class files are those class files that are dynamically loaded at runtime.
Virtual machine 222 includes JIT compiler 224 and interpreter 226 and is operable to execute class files. In one implementation, virtual machine 222 is a Java virtual machine. One of ordinary skill in the art will recognize that other types of virtual machines may be used. Virtual machine 222 may utilize both JIT compiler 224 and interpreter 226 to execute class files. JIT compiler 224 is a compiler running as part of virtual machine 222 that dynamically compiles bytecode to platform-dependent machine code just before a method is first executed. Interpreter 226 interprets class files without compilation to platform-dependent machine code.
Protecting Object Identity
Object class 402 is the root class (e.g., at the top of the class hierarchy) in the Java programming language. Accordingly, all classes are subclasses of Object class 402, either directly or indirectly, and therefore inherit the methods of Object class 402. These methods may include equals ( ), hashcode( ), and =. Equals( ) tests whether two objects two objects have the same value, not whether two references refer to the same object. Hashcode( ) returns a hash code for a specified object within the parentheses “( )”. The default implementation returns a value that is usually unique for different objects. The “=” method tests whether two variables refer to the same object, not whether two objects contain the same values.
Value class 404 is a direct abstract subclass of object class 402. An abstract class is a class that is used to define common behavior for an intended set of subclasses, such as constant classes 406a-406n. In other words, the abstract class includes whatever features each of the intended set of subclasses have in common. Using abstract classes, one can declare classes that define part of an implementation, leaving extended classes to provide specific implementation of some or all of the methods. In one implementation, abstract classes are incomplete and cannot be instantiated in terms of objects. A subclass of an abstract class, however, may be instantiated if it overrides each of the abstract methods of its superclass and provides an implementation (i.e., a method body) for all of them.
Value class 404 includes an equals( ) method, a hashcode( ) method, and a “final” version of a = method that overrides the = method of object class 402. The equals ( ) method and the hashcode( ) method may be abstract methods. The final version of the = method calls the equals( ) method of value class 404. As such, whenever somebody tries to use the = operator in conjunction with an object from a subclass of value class 404, the equals( ) method is called. Because the = method of value class 404 is final, it cannot be overridden. A method in a class is overridden, for example, when a subclass of the class defines a method using the same name, return type, and arguments as the method in the class. When the method is invoked for an object of the subclass, it is the new definition of the method that is called, not the class' old definition.
Constant classes 406a-406n are direct subclasses of value class 404. Accordingly, constant classes 406a-406n inherit the methods of value class 404, including the = method. Also, constant classes 406a-406n override and provide an implementation for each of the abstract methods of value class 404, including equals( ) and hashcode( ). Because constant classes 406a-406n provide a specific implementation of all of their methods, constant classes 406a-406n may be instantiated. Objects whose identities need to remain protected may be instantiated as a constant class object. For example, objects representing mathematical values and other literal data types that need to have their physical identity protected may be designated as type constant class. In one implementation, each constant class 406 may refer to a different literal data type. Accordingly, constant class 406a may be named, for example, “integer_constant” and be used when an integer's identity is to be protected. Constant class 406b may be named “float_constant” and be used when a floating point number's identity is to be protected, and so on. Therefore, each of constant class 406a-406n may have different characteristics as long as each has the aforementioned methods inherited from value class 404.
When a constant class object is instantiated, a special system thread immediately locks the object. This results in all instances of constant classes always being locked. It is legal to try to synchronize on a constant class object, but because the object is always locked, the attempt does not succeed. All instance fields of a constant class are final. Constants classes 410a-410n are indirect subclasses of value class 404 and operate in a manner similar to that of constant classes 406a-406n. Value subclasses 408a-408n demonstrate that constant classes do not necessarily have to be direct subclasses of the value class.
The contents of a value class is not limited to what is shown. Value class 500 may include = method 502, equals ( ) method 504, and hashcode( ) method 506, as previously explained with reference to
The = method 602 is inherited from value class 500 and operates in the same manner. Equals( ) 604 and hashcode( ) 606 override the similarly named methods of value class 500, but they operate in a similar manner. Fields 608 are data belonging to constant class 600. Fields 608 of instances of constant class are final.
First, a determination is made as to whether the particular code being interpreted or compiled by a JIT compiler has the = operator present (step 702). If not, then execution continues as normal. If the = operator is present, then the virtual machine makes a determination as to whether a constant class object is involved (step 704). For example, the virtual machine may invoke the = method of the object instance whose identity is desired. The corresponding = method performs its steps. If the = method is from a constant class object, then the method is a final method that overrides the = method from the object class and invokes the equals( ) method of the constant class object (step 706). Invoking the equals( ) method has the effect of making a determination whether the value of the two objects referenced in conjunction with the = operator are the same. A true indication is returned if the objects have the same value, and a false indication is returned if the objects do not have the same value (step 708). In this manner, when an attempt is made to determine whether a constant class object refers to the same object as another object, an indication of whether the object values are the same is returned, instead of an indication of object identity.
If the = method is not from a constant class object, then the virtual machine performs the identity function (step 710). Execution of the identity function results in the return of an indication whether two variable references refer to the same object (step 712).
A virtual machine executing a program, such as a program written in the Java programming language, may instantiate an object in memory when instructed to do so (step 802). Next, the virtual machine determines whether the newly instantiated object is a constant class object (step 804). If the instantiated object is not a constant class object, then execution continues as normal. If the instantiated object is a constant class object, then the virtual machine proceeds to lock the constant class object using a special system thread (step 806). For example, the virtual machine may cause a special system thread to change one or more bits in the header of the constant class object, indicating that the object is locked. The special system thread is a thread created and controlled by the virtual machine that does not run any user code. Consequently, user code cannot synchronize on the constant class object instance, and it is not possible to determine constant class object identity using a synchronize operation.
While the present invention has been described in connection with various embodiments, many modifications will be readily apparent to those skilled in the art. Although aspects of the present invention are described as being stored in memory, one skilled in the art will appreciate that these aspects can also be stored on or read from other types of computer-readable media, such as secondary storage devices, like hard disks, floppy disks, or CD-ROM; a carrier wave, optical signal or digital signal from a network, such as the Internet; or other forms of RAM or ROM either currently known or later developed. Additionally, although a number of the software components are described as being located on the same machine, one skilled in the art will appreciate that these components may be distributed over a number of machines. The invention, therefore, is not limited to the disclosure herein, but is intended to cover any adaptations or variations thereof.
Number | Name | Date | Kind |
---|---|---|---|
5202672 | Nakamura et al. | Apr 1993 | A |
5247305 | Hirata et al. | Sep 1993 | A |
5525991 | Nagura et al. | Jun 1996 | A |
5615362 | Jensen et al. | Mar 1997 | A |
5631642 | Brockelsby et al. | May 1997 | A |
5706506 | Jensen et al. | Jan 1998 | A |
5717919 | Kodavalla et al. | Feb 1998 | A |
5825299 | Fuentes et al. | Oct 1998 | A |
5826268 | Schaefer et al. | Oct 1998 | A |
5848232 | Lermuzeaux et al. | Dec 1998 | A |
6026401 | Brealey et al. | Feb 2000 | A |
6058209 | Vaidyanathan et al. | May 2000 | A |
6134627 | Bak | Oct 2000 | A |
6167424 | Bak et al. | Dec 2000 | A |
6173404 | Colburn et al. | Jan 2001 | B1 |
6173442 | Agesen et al. | Jan 2001 | B1 |
6195791 | Carlson et al. | Feb 2001 | B1 |
6212608 | Bak | Apr 2001 | B1 |
6247025 | Bacon | Jun 2001 | B1 |
6256783 | Wada et al. | Jul 2001 | B1 |
6262664 | Maloney | Jul 2001 | B1 |
6275979 | Graser et al. | Aug 2001 | B1 |
6295640 | Eidt | Sep 2001 | B1 |
6314563 | Agesen et al. | Nov 2001 | B1 |
6327533 | Chou | Dec 2001 | B1 |
6342837 | Lai et al. | Jan 2002 | B1 |
6345313 | Lindholm | Feb 2002 | B1 |
6349322 | Shaylor | Feb 2002 | B1 |
6362737 | Rodgers et al. | Mar 2002 | B1 |
6405209 | Obendorf | Jun 2002 | B2 |
6408311 | Baisley et al. | Jun 2002 | B1 |
6442570 | Wu | Aug 2002 | B1 |
6442748 | Bowman-Amuah | Aug 2002 | B1 |
6449607 | Tomita et al. | Sep 2002 | B1 |
6457023 | Pinter et al. | Sep 2002 | B1 |
6487607 | Wollrath et al. | Nov 2002 | B1 |
6526455 | Kamimura | Feb 2003 | B1 |
6529948 | Bowman-Amuah | Mar 2003 | B1 |
6539396 | Bowman-Amuah | Mar 2003 | B1 |
6591276 | Dockter et al. | Jul 2003 | B1 |
6601234 | Bowman-Amuah | Jul 2003 | B1 |
6658652 | Alexander et al. | Dec 2003 | B1 |
6662191 | Cherniavsky et al. | Dec 2003 | B1 |
6674904 | McQueen | Jan 2004 | B1 |
6678691 | Kikkers | Jan 2004 | B1 |
6687707 | Shorter | Feb 2004 | B1 |
6706314 | Butland | Mar 2004 | B2 |
6707380 | Maloney | Mar 2004 | B2 |
6727817 | Maloney | Apr 2004 | B2 |
6782532 | Sexton et al. | Aug 2004 | B1 |
6801919 | Hunt et al. | Oct 2004 | B2 |
6802447 | Horng | Oct 2004 | B2 |
6831562 | Rodgers et al. | Dec 2004 | B2 |
6879999 | Elnozahy | Apr 2005 | B2 |
6915954 | Knowles et al. | Jul 2005 | B2 |
6918123 | Shteyn | Jul 2005 | B1 |
6918541 | Knowles et al. | Jul 2005 | B2 |
6925646 | Korenshtein et al. | Aug 2005 | B1 |
6941133 | Jacobs et al. | Sep 2005 | B2 |
6947952 | Welch et al. | Sep 2005 | B1 |
6957775 | Tsikos et al. | Oct 2005 | B2 |
6958698 | Maloney | Oct 2005 | B2 |
6980797 | Tuulos | Dec 2005 | B1 |
6982646 | Rodgers et al. | Jan 2006 | B2 |
7005661 | Yamaguchi et al. | Feb 2006 | B2 |
7024434 | Fuller et al. | Apr 2006 | B2 |
7055134 | Schroeder | May 2006 | B2 |
7069540 | Sievert | Jun 2006 | B1 |
7089561 | Morrison et al. | Aug 2006 | B2 |
7271814 | Anwar et al. | Sep 2007 | B2 |
Number | Date | Country | |
---|---|---|---|
20040039746 A1 | Feb 2004 | US |