Annotations on variables declared in try-with-resources?

Viewed 1372

Just wondering what annotations could be used with variables declared in try-with-resources statements, which is allowed as per its grammar. The section 14.20.3 from the language specification (Java 7) reads,

TryWithResourcesStatement:
    try ResourceSpecification Block Catchesopt Finallyopt

ResourceSpecification:
    ( Resources ;opt )

Resources:
    Resource Resource ; Resources

Resource:
    VariableModifiersopt Type VariableDeclaratorId = Expression

And VariableModifiers expands as (section 14.4),

VariableModifiers:
    VariableModifier
    VariableModifiers VariableModifier

VariableModifier: one of
    Annotation final

There you go: VariableModifier can have Annotation. Well, that basically means, we can write something like this:

try( @SomeAnnotation SomeType obj = createSomeType() ) { 
  //some code
}

So my question is: how and what kind of annotations could possibly be used in try-with-resources and to achieve what kind of behaviors? Any innovative idea? Have anybody used them?

2 Answers
Related