User loginNavigation |
Modified javac That Processes Annotations on Local VariablesHi everyone! This is my first post here, so if I'm doing something wrong, please let me know and have mercy with me. But I do think I have something to share that you may find interesting: Using material available from Sun under the Java Research License (JRL), I have created a modified version of the javac compiler (javac 1.5.0_06), which I call LAPT-javac, that encodes annotations on local variables into the class files it compiles. Pre-compiled binaries, the source code, and other useful information is available from the project website at http://www.cs.rice.edu/~mgricken/research/laptjavac Please note that you have to accept the terms of the JRL before you can use the files I provide. Some background information may be useful: Java 5.0 introduced annotations, meta-data that can be attached to other parts of the program. Annotations may target other annotation types, packages, types, constructors, methods, fields (including enum constants), parameters, local variables, or any combination of the above. Depending on their retention policy, annotations may be accessible in the source code only using the apt Annotation Processing Tool (SOURCE retention); they may be encoded in the class file but remain invisible at runtime (CLASS retention); or they may be accessible at runtime using reflection (RUNTIME retention). Unfortunately, both javac and apt completely ignore local annotations on local variables. In accordance with the grammar, they do not signal a syntax error, but annotations on local variables are neither accessible in the source or class files nor at runtime! In effect, an annotation on a local variable void someMethod() { // ... @SomeAnnotation int myLocal = 123; // ... } behaves exactly the same as if it were written like this: void someMethod() { // ... /* @SomeAnnotation */ int myLocal = 123; // ... } Annotations on local variables are comments, and comments only. A while ago, I realized that another project I am working on could tremendously benefit if I could at least access annotations on local variables in the class files. So I decided to take a look at the javac source code released under the JRL. To my delight, I found that annotations on local variables were being parsed correctly, and all the information was available all through the very last stage, writing the class files. My change involved only four files (ClassWriter.java, ClassReader.java, Code.java and Name.java) and about 100 lines. On the project website linked above, I am making available a modified version of the javac compiler (javac 1.5.0_06), LAPT-javac, that encodes annotations on local variables in the class file. I also specify the format for two new, non-standard attributes, RuntimeVisibleLocalVariableAnnotations and RuntimeInvisibleLocalVariableAnnotations, on methods and constructors that closely mirror the format of the RuntimeVisibleParameterAnnotations and RuntimeInvisibleParameterAnnotations attributes that Sun introduced with Java 5.0. To use LAPT-javac, please download the LAPT-javac binaries file and execute it using Java 5.0: java -jar laptjavac.jar <parameters> where <parameters> matches the parameters on the javac command line you would normally use. I have to admit that what I currently provide may not be very useful to most of you. Even though I encode annotations on local variables with RUNTIME retention in the class files, the original Java 5.0 reflection API is not be able to process these annotations, and I do not currently provide a modified reflection API. So unless you are directly processing class files on a binary/bytecode level like I am for my other project, you are probably not going to get anything out of LAPT-javac. In effect, right now only CLASS retention works as it should. However, I have some experience with writing libraries that can be put on the Java bootclasspath, and I think I will be able to provide an enhanced reflection library that allows programmers to query annotations on local variables at runtime, just like it can already be done for method parameters. From what I know, apt uses javac's parser, and I know that parser reads annotations on local variables just fine, so an enhanced version of apt is definitely possible too (though less interesting to me personally). My hope is that eventually Sun will incorporate my changes or something similar into standard Java. I truly believe annotations on local variables can be very useful, as it really opens the door for usage-site annotations as opposed to definition-site ones. I am very much looking forward to hearing your comments. Thank you for your time and interest. Mathias Ricken By mgricken at 2006-10-10 10:05 | LtU Forum | previous forum topic | next forum topic | other blogs | 7210 reads
|
Browse archives
Active forum topics |
Recent comments
22 weeks 6 days ago
22 weeks 6 days ago
22 weeks 6 days ago
45 weeks 19 hours ago
49 weeks 2 days ago
50 weeks 6 days ago
50 weeks 6 days ago
1 year 1 week ago
1 year 6 weeks ago
1 year 6 weeks ago