[JSR308] annotations on expressions and literal values

Eugene Kuleshov eu at javatx.org
Tue Jan 30 21:41:33 EST 2007

  I would like to change a topic and discuss something different then 
array annotations.

  Java 6 introduced scripting support in the JRE. It is really handy, 
but it is easy to notice that we'll be soon dealing with some "alien" 
code or micro-languages in string literals inside Java code.

  From the tooling point of view these literals are hard to identify and 
hence difficult to help user with code completion and validation.

  Consider the following example borrowed from one of the blog posts:

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
Bindings bindings = engine.createBindings();
bindings.put("num", "20");
Object result = engine.eval(
    "fib(num);" +
    "function fib(n) {" +
    "  if(n <= 1) return n; " +
    "  return fib(n-1) + fib(n-2); " +
    "};", bindings);

  The string literal passed to eval(..) method is in fact a JavaScript code.
  I think it would be really useful be able to annotate method 
parameters on the call. For example in this case we can tell specify 
that expression result is in fact JavaScript code:

Object result = engine.eval(
    "fib(num);" +
    "function fib(n) {" +
    "  if(n <= 1) return n; " +
    "  return fib(n-1) + fib(n-2); " +
    "};" @Script("JavaScript"),

  Seems like postfix variant works better then prefix one and helps to 
remove ambiguity.

  Interestingly, the whole expression will be a single string literal in 
the bytecode represented as a single entry in a class constant pool.

  Open question here is if we need to differentiate between annotations 
on expressions and annotation on the parameters of the method calls.

  I also would like to mention recent presentations by Danny Coward 
about features considered for Java 7 and especially the part about 
support for XML literals. I have to admit that I believe it will be a 
bad idea to add such XML support into Java language, because it is too 
specific for XML only. It would be more interesting to have more generic 
support for the "alien" languages and allow inlining and scripting code 
in the same way as it is being proposed for XML... From compiler point 
of view it still could go into sting literal,
but user won't have to wrap it into quotes or escape special chars. 
Though this kind of stuff is out of scope of JSR 308.


More information about the JSR308 mailing list