Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 344314 - [Xtend] Add support for annotations
Summary: [Xtend] Add support for annotations
Status: CLOSED FIXED
Alias: None
Product: TMF
Classification: Modeling
Component: Xtext (show other bugs)
Version: 2.0.0   Edit
Hardware: PC Mac OS X - Carbon (unsup.)
: P3 enhancement (vote)
Target Milestone: RC1   Edit
Assignee: Project Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-04-29 12:48 EDT by Sebastian Zarnekow CLA
Modified: 2017-09-19 17:39 EDT (History)
1 user (show)

See Also:
sven.efftinge: indigo+


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Sebastian Zarnekow CLA 2011-04-29 12:48:27 EDT
There are several interesting use cases for annotations, e.g. @Singleton, binding annotations or @Test to define Junit4 test methods.

However, full support for annotations touches many aspects of the language, some of them seem to be hard to tackle or require considerable efforts (assuming we use the Java like syntax where annotations precede the annotated element):

- The parser would need larger lookahead to decide which path to go: declared dependency or operation. This may effect content assist.
- Validation is required to ensure the annotations are not misplaced
- Java7 will likely support annotations for type parameters JSR308 - either we do not support that or we need to be aware of the target platform, e.g. the used Java version
- Annotations may take arguments which may be of any primitive type, Class, String, other annotations, or a one dimensional array of one of these things. Java allows to write expressions there as long as the compiler can evaluate them statically. Especially problematic seems the support for arrays.
- we already decided to use the override keyword instead of @Override which may be counter intuitive as soon as annotations are allowed
- Validation is required for annotations that are inherited

However, since annotation support has many advantages, we could think about a limited support for them as a first shot and postpone many of the tricky parts, e.g. we could only allow literals instead of full expressions.
Comment 1 Sebastian Zarnekow CLA 2011-04-29 16:42:52 EDT
(In reply to comment #0)
> - Java7 will likely support annotations for type parameters JSR308 - either we
> do not support that or we need to be aware of the target platform, e.g. the
> used Java version

Correction: JSR308 was postponed to Java8 so annotated type parameters should not affect the complexity of Xtend's annotation support for now
Comment 2 Sven Efftinge CLA 2011-04-30 16:33:38 EDT
(In reply to comment #0)
> There are several interesting use cases for annotations, e.g. @Singleton,
> binding annotations or @Test to define Junit4 test methods.

Not to mention all the other Junit4 and Guice annotation as well as our own Annotation based APIs (@Check). Just to make clear that hard coding this stuff into the language as we started with @Inject is a dead-end.

> - The parser would need larger lookahead to decide which path to go: declared
> dependency or operation. This may effect content assist.

You are implying that we also switch our dependency declaration to real fields.
We could still syntactically require '@Inject' at that position. But that doesn't seem sound, does it?

The lookahead problem happens mostly because of the optional name in fields and the optional return type in functions. If we make the name of a field mandatary, things should work out better.

> - Java7 will likely support annotations for type parameters JSR308 - either we
> do not support that or we need to be aware of the target platform, e.g. the
> used Java version

That's a problem of the future.

> - Annotations may take arguments which may be of any primitive type, Class,
> String, other annotations, or a one dimensional array of one of these things.
> Java allows to write expressions there as long as the compiler can evaluate
> them statically. Especially problematic seems the support for arrays.

We could support the literals as well as references to static literals (enums, constants).
Java has this sugar for Array initializers in the context of annotations. We could support just that.
Maybe even not only for single annotation values, but also for multiple. 

> - we already decided to use the override keyword instead of @Override which may
> be counter intuitive as soon as annotations are allowed

If we make the decision soon enough I'd want to change that.
Comment 3 Sven Efftinge CLA 2011-04-30 16:34:31 EDT
I think this should be a reusable layer between Xbase and Xtend. Project-wise it should go into Xbase.
Comment 4 Sven Efftinge CLA 2011-05-01 01:12:36 EDT
After having slept over this, I don't think override should become a keyword. In that case we would need to discuss all other modifiers as well which would open up a whole new can of worms.

I think we should use keywords for modifiers used by the language implementation.

Regarding the field and the optional name:
We could allow omitting the name only if a field is declared using the 'extension' keyword.
Comment 5 Sebastian Zarnekow CLA 2011-05-01 03:47:39 EDT
(In reply to comment #2)
> Just to make clear that hard coding this stuff
> into the language as we started with @Inject is a dead-end.
> 

It is, indeed no option.

> > - The parser would need larger lookahead to decide which path to go: declared
> > dependency or operation. This may effect content assist.
> 
> You are implying that we also switch our dependency declaration to real fields.
> We could still syntactically require '@Inject' at that position. But that
> doesn't seem sound, does it?

The @Inject keyword would only work as long as we don't allow to define 'normal' fields in Xtend which is worth to discuss.
@MyAnnotation(types={., .}, other={@OtherAnnotation(..), ..) methodName / FieldType

is actually infinite lookahead for the parser. We could try to make compromises with the AST and introduce something like an AnnotatedXtendMember which wraps the actual function or dependency. But that's more or less a technical problem which should be discussed in-depth offline.


> > - we already decided to use the override keyword instead of @Override which may
> > be counter intuitive as soon as annotations are allowed
> 
> If we make the decision soon enough I'd want to change that.

I'm thinking about a quickfix that converts @Override to the override since I prefer the keyword as it affects validation rules and things like that. But I'd be ok with java.lang.Override, too.
Comment 6 Sven Efftinge CLA 2011-05-04 10:31:09 EDT
Annotations should be allowed before classes, fields (we should rename the concept 'declared dependency' to field) and functions.
Comment 7 Sven Efftinge CLA 2011-05-06 11:14:42 EDT
I've pushed a first draft of grammar,  scopeprovider and compiler.

Todos: 
 - validation
 - tests
 - update docs
Comment 8 Sven Efftinge CLA 2011-05-11 10:19:47 EDT
pushed to master
Comment 9 Karsten Thoms CLA 2017-09-19 17:28:03 EDT
Closing all bugs that were set to RESOLVED before Neon.0
Comment 10 Karsten Thoms CLA 2017-09-19 17:39:13 EDT
Closing all bugs that were set to RESOLVED before Neon.0