So Where Were We?

I haven’t written in a while. I also haven’t really worked in a while. I did some work Friday until the early afternoon, then went home, and after a week of not being able to sleep for more than maybe two or three hours every night, the sleep came. And it stayed for a while. With it, unfortunately, came dehydration and congestion, as I didn’t take my allergy medication, and therefore, a headache.

Tonight (or rather: during this night), I was able to put the pre-finishing touches on LAPT. It works in many simple cases already. The biggest hurdle that I didn’t expect was that I had to transfer constant pool items from the annotated class file to the original class file. Since annotations on local variables usually completely get dropped, their class names, constants, classes, and annotations they contain also get dropped from the constant pool.

I should probably define the format of the LocalVariableAnnotationsTable I settled on. It’s a mixture of the LocalVariableTypeTable and the RuntimeInvisibleAnnotations attributes:

LocalVariableAnnotationsTable\_attribute {
u2 attribute\_name\_index;
u4 attribute\_length;
u1 local\_variable\_annotations\_table\_length;
{
u2 start\_pc;
u2 length;
u2 name\_index;
u2 index;
u2 num\_annotations;
annotation annotations[num\_annotations];
} local\_variable\_annotation\_table[local\_variable\_annotations\_table\_length];
}

So it really still is an array of arrays of annotations. I’ve just decided to separate it from the LocalVariableTable and let it contain information about the variable, like the PC range it’s valid and especially the name, itself. This is important since the LocalVariableTable might not even exist if a program is compiled without debug information.

Another thing to think about, of course, is reflection support. Right now, at least the annotations make it into the class file, but they’re not available at runtime. What I would need for that is an additional method in Constructor and Method, one that mirrors getParameterAnnotations(). In principle, this isn’t too hard to do.

Now I’m still having some problems with annotations and array initializers, and annotations containing annotations themselves, and there is a big caveat: Annotations that a programmer wants to place on a local variable also have to be appropriate for parameters. That means the following very narrow annotation definition will not work:

@Target({
ElementType.LOCAL_VARIABLE})
public @interface Bogus {
...
}

To use LAPT, the programmer will have to expand it to

@Target({
ElementType.LOCAL_VARIABLE,
ElementType.PARAMETER})
public @interface Bogus {
...
}

I’m going to start making this tool available and known. I would even like Sun to incorporate this attribute. I just keep thinking how much easier it would have been for Sun to do this directly within the compiler. Ideally, this would end up in a JSR and then in a future version of Java. I’ll have to come up with examples that demonstrate the use of annotations on local variables, even though it should be quite obvious.

I think I’ll have to put working on this tool aside for a while. There are other things that I absolutely need to work on.

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 xajavac. Bookmark the permalink.

Leave a Reply