Adding Object IDs to All Direct Subclasses of Object

I’ve begun working on my alternative approach. If I can’t add fields to java.lang.Object, I’ll just add them to all direct subclasses. Once this is done, all objects will have the fields since nothing is ever just an Object.

First I scan through all files, and if they are subclassing java.lang.Object, I add the field for the object ID. This ID has to be initialized once and only once, though, which has to happen in the constructors. This is complicated by the fact that one constructor may call another:

1
2
3
4
5
6
7
8
9
public class ThisCtorTest {
    public ThisCtorTest() {
        System.out.println("ThisCtorTest()");
    }
    public ThisCtorTest(int i) {
        this();
        System.out.println("ThisCtorTest("+i+")");
    }
}

Here, a call to ThisCtorTest(int i) will also invoke ThisCtorTest(). If the initialization code is added to both constructors, it would be run twice.

Conceptually, the constructors (their name in the class files is <init>) form a call graph:

<br />
\Large\begin{array}{ccc}<br />
<init>\_0 &#038; \longl[75]^{calls} &#038; <init>\_1 \\\\<br />
&#038; &#038; \longu[40]^{calls} \\\\<br />
<init>\_2 &#038; &#038; <init>\_3 \\\\<br />
\end{array}<br />

I only add the initialization code to constructors that are sinks, that don’t call any other constructors. In this example, these constructors are <init>\_0 and <init>\_2. Calls to <init>\_1 and <init>\_3 both invoke <init>\_0 and thus execute the initialization code.

If I decide to initialize the field, this code will be inserted:


GeSHi Error: GeSHi could not find the language jvm (using path /homepages/42/d86928234/htdocs/blog/wp-content/plugins/codecolorer/lib/geshi/) (code 2)

which corresponds to the Java code

1
objectID = nextObjectID++;

I may have to make this section synchronized.

Of course, nothing is so simple with the JVM that it could work right away, now is it? :(

Share

About Mathias

Software development engineer. Principal developer of DrJava. Recent Ph.D. graduate from the Department of Computer Science at Rice University.
This entry was posted in Concurrent Unit Testing. Bookmark the permalink.

Leave a Reply