This page is no longer maintained — Please continue to the home page at www.scala-lang.org

IType.getAnnotations() doesn't seem to work for scala elements

7 replies
CadErik
Joined: 2008-12-19,
User offline. Last seen 3 years 43 weeks ago.
st1\:*{behavior:url(#default#ieooui) }

I’m trying to get the testng plugin to recognize scala testng tests, I fixed this on the testng plugin side but when I open a scala element and call in eclipse IType.getAnnotations, this return an empty array, so I cannot see the @Test annotation.

Are there any other ways to see the annotations in Eclipse?

 

Erik Putrycz, PhD - Research Associate
Institute for Information Technology - Software Engineering Group
National Research Council, Canada - Building M-50, 1200 Montreal Road
Ottawa, Ontario, CANADA K1A 0R6

 

milessabin
Joined: 2008-08-11,
User offline. Last seen 33 weeks 3 days ago.
Re: IType.getAnnotations() doesn't seem to work for scala eleme

On Thu, Jan 8, 2009 at 7:25 PM, Putrycz, Erik
wrote:
> I'm trying to get the testng plugin to recognize scala testng tests, I fixed
> this on the testng plugin side but when I open a scala element and call in
> eclipse IType.getAnnotations, this return an empty array, so I cannot see
> the @Test annotation.

Yes, I'd expect that ... indexing of annotations isn't yet done in the
Scala Eclipse plugin. Could you open an enhancement ticket in Trac for
this against the plugin.

> Are there any other ways to see the annotations in Eclipse?

Not that I know of.

But is this really necessary? I was under the impression that TestNG
was quite usable in conjunction with the Eclipse plugin as is?

I'd be very keen to get input from the users of that the unit testing
frameworks on what's wanted in the way of Eclipse integration ... and
help and patches would be very welcome as well!

Cheers,

Miles

CadErik
Joined: 2008-12-19,
User offline. Last seen 3 years 43 weeks ago.
RE: IType.getAnnotations() doesn't seem to work for scala eleme

> Yes, I'd expect that ... indexing of annotations isn't yet done in the
> Scala Eclipse plugin. Could you open an enhancement ticket in Trac for
> this against the plugin.

Will do!

> But is this really necessary? I was under the impression that TestNG
> was quite usable in conjunction with the Eclipse plugin as is?

TestNG is "usable", i.e. the test runner works but all the context
features of the testng eclipse plugin are missing. Most of the eclipse
plugin feature are useless unless you run the tests manually (Run,
create new configuration, etc...). This is probably the same for JUnit
4; I don't think Eclipse will recognise unit tests unless the
annotations are indexed.

> I'd be very keen to get input from the users of that the unit testing
> frameworks on what's wanted in the way of Eclipse integration ... and
> help and patches would be very welcome as well!

I'm willing to help, the context features are very important for me. I
don't mind having a look at the scala plugin code if you can point me
where the annotations would be gathered. I would expect that there is
only missing a bridge between the AST and IAnnotations, right?

Cheers,

Erik.

milessabin
Joined: 2008-08-11,
User offline. Last seen 33 weeks 3 days ago.
Re: IType.getAnnotations() doesn't seem to work for scala eleme

On Thu, Jan 8, 2009 at 7:56 PM, Putrycz, Erik
wrote:
> TestNG is "usable", i.e. the test runner works but all the context
> features of the testng eclipse plugin are missing. Most of the eclipse
> plugin feature are useless unless you run the tests manually (Run,
> create new configuration, etc...). This is probably the same for JUnit
> 4; I don't think Eclipse will recognise unit tests unless the
> annotations are indexed.

OK, gotcha ...

>> I'd be very keen to get input from the users of that the unit testing
>> frameworks on what's wanted in the way of Eclipse integration ... and
>> help and patches would be very welcome as well!
>
> I'm willing to help, the context features are very important for me. I
> don't mind having a look at the scala plugin code if you can point me
> where the annotations would be gathered. I would expect that there is
> only missing a bridge between the AST and IAnnotations, right?

Fantastic! Thanks!

To check out and build the plugin you should follow the instructions here,

http://lampsvn.epfl.ch/trac/scala/wiki/EclipsePlugin

The place you need to start digging is
scala.tools.eclipse.javaelements.ScalaStructureBuilder in the
scala-plugin project. Feel free to come and chase me on #scala (I keep
UK hours).

Cheers,

Miles

CadErik
Joined: 2008-12-19,
User offline. Last seen 3 years 43 weeks ago.
RE: IType.getAnnotations() doesn't seem to work for scala eleme

I've been looking again at the annotation processing, and the real
complexity is in the annotation arguments.
What eclipse seems to needs is:
- java objects (integer, arrays, etc) for simple types
- strings for classes (which don't need to be resolved)

Is there any way to reuse some of the compiler classes for extracting
annotation arguments?

The compiler has a class "AnnotationInfos" which seem to be close to
what is required but I don't have a clue how to use it.

Btw: I'm now a commiter on the testng plugin so I'll make sure that
scala works flawlessy once annotations are processed.

> -----Original Message-----
> From: Miles Sabin [mailto:miles [at] milessabin [dot] com]
> Sent: January-08-09 3:05 PM
> To: Putrycz, Erik
> Cc: scala-tools [at] listes [dot] epfl [dot] ch
> Subject: Re: [scala-tools] IType.getAnnotations() doesn't seem to work
for
> scala elements
>
> On Thu, Jan 8, 2009 at 7:56 PM, Putrycz, Erik
> wrote:
> > TestNG is "usable", i.e. the test runner works but all the context
> > features of the testng eclipse plugin are missing. Most of the
eclipse
> > plugin feature are useless unless you run the tests manually (Run,
> > create new configuration, etc...). This is probably the same for
JUnit
> > 4; I don't think Eclipse will recognise unit tests unless the
> > annotations are indexed.
>
> OK, gotcha ...
>
> >> I'd be very keen to get input from the users of that the unit
testing
> >> frameworks on what's wanted in the way of Eclipse integration ...
and
> >> help and patches would be very welcome as well!
> >
> > I'm willing to help, the context features are very important for me.
I
> > don't mind having a look at the scala plugin code if you can point
me
> > where the annotations would be gathered. I would expect that there
is
> > only missing a bridge between the AST and IAnnotations, right?
>
> Fantastic! Thanks!
>
> To check out and build the plugin you should follow the instructions
here,
>
> http://lampsvn.epfl.ch/trac/scala/wiki/EclipsePlugin
>
> The place you need to start digging is
> scala.tools.eclipse.javaelements.ScalaStructureBuilder in the
> scala-plugin project. Feel free to come and chase me on #scala (I keep
> UK hours).
>
> Cheers,
>
>
> Miles
>
> --
> Miles Sabin
> tel: +44 (0)1273 720 779
> mobile: +44 (0)7813 944 528
> skype: milessabin

rytz
Joined: 2008-07-01,
User offline. Last seen 45 weeks 5 days ago.
Re: IType.getAnnotations() doesn't seem to work for scala eleme
Hi Erik

I'm not entirely sure to understand your question.. You attached an annotation,
which is itself defined in Java (@interface), to a Scala definition (e.g. a method),
and you'd like to know how the annotation parameters are represented in the
compiler's AST, right?

A method definition is a 'DefDef' AST node, which has a parameter of type
'Modifiers'. Modifiers have a field called 'annotations', which is a list of AST
nodes of type 'Annotation'. This is where annotations end up after parsing.

As you guessed, the 'AnnotationInfo' is more useful to you. Actually, the namer
phase of the compiler transforms the 'Annotation'-trees into AnnotationInfo
objects. For definitions (e.g. DefDef), these object are stored in a field called
'attributes' of the definition's symbol.

In other words, the given a tree: DefDef, you can access the AnnotationInfos
using tree.symbol.attributes (which is a List[AnnotationInfo]).


Let me know if you need more help.

Cheers: Lukas



On Wed, Jan 14, 2009 at 18:18, Putrycz, Erik <Erik [dot] Putrycz [at] nrc-cnrc [dot] gc [dot] ca> wrote:
Hi Lukas,

Miles gave me your contact; he said that you should be knowledgeable on
annotation processing.

I've been looking at implementing annotation processing in the current
scala Eclipse plugin, and the real complexity is in the annotation
arguments.
What eclipse seems to needs for arguments are:
- java objects (integer, arrays, etc) for simple types
- strings for classes (which don't need to be resolved)

Is there any way to reuse some of the compiler classes for extracting
annotation arguments?

The compiler has a class "AnnotationInfos" which seem to be close to
what is required but I don't have a clue how to use it.

Erik Putrycz, PhD - Research Associate / erik [dot] putrycz [at] nrc-cnrc [dot] gc [dot] ca /
(613) 990 0681
Institute for Information Technology - Software Engineering Group
National Research Council, Canada - Building M-50, 1200 Montreal Road
Ottawa, Ontario, CANADA K1A 0R6

> -----Original Message-----
> From: Miles Sabin [mailto:miles [at] milessabin [dot] com]
> Sent: January-08-09 3:05 PM
> To: Putrycz, Erik
> Cc: scala-tools [at] listes [dot] epfl [dot] ch
> Subject: Re: [scala-tools] IType.getAnnotations() doesn't seem to work

> for scala elements
>
> On Thu, Jan 8, 2009 at 7:56 PM, Putrycz, Erik
> <Erik [dot] Putrycz [at] nrc-cnrc [dot] gc [dot] ca> wrote:
> > TestNG is "usable", i.e. the test runner works but all the context
> > features of the testng eclipse plugin are missing. Most of the
> > eclipse plugin feature are useless unless you run the tests manually

> > (Run, create new configuration, etc...). This is probably the same
> > for JUnit 4; I don't think Eclipse will recognise unit tests unless
> > the annotations are indexed.
>
> OK, gotcha ...
>
> >> I'd be very keen to get input from the users of that the unit
> >> testing frameworks on what's wanted in the way of Eclipse
> >> integration ... and help and patches would be very welcome as well!
> >
> > I'm willing to help, the context features are very important for me.

> > I don't mind having a look at the scala plugin code if you can point

> > me where the annotations would be gathered. I would expect that
> > there is only missing a bridge between the AST and IAnnotations,
right?
>
> Fantastic! Thanks!
>
> To check out and build the plugin you should follow the instructions
> here,
>
>   http://lampsvn.epfl.ch/trac/scala/wiki/EclipsePlugin
>
> The place you need to start digging is
> scala.tools.eclipse.javaelements.ScalaStructureBuilder in the
> scala-plugin project. Feel free to come and chase me on #scala (I keep

> UK hours).
>
> Cheers,
>
>
> Miles
>
> --
> Miles Sabin
> tel:    +44 (0)1273 720 779
> mobile: +44 (0)7813 944 528
> skype:  milessabin

CadErik
Joined: 2008-12-19,
User offline. Last seen 3 years 43 weeks ago.
RE: IType.getAnnotations() doesn't seem to work for scala eleme
v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VML);} .shape {behavior:url(#default#VML);} ..shape {behavior:url(#default#VML);} st1\:*{behavior:url(#default#ieooui) }

Hi Lukas,

 

Accessing the field “annotations” from the modifiers is not difficult, Miles did all that work already in the existing plugin.

What I found out to be complex are all the parameters of the annotation itself once you start parsing the AST of the Annotation.

My example is

@Test{ val groups = Array("testgroup"), val retryAnalyzer = classOf[RetryClass]}

 

In the AST, @Test ends up in a sum of Apply, Select, New, Select, Select, Select and Ident.

Then the arguments end up in ValDef, TypeTree and Literal.

Is there something more generic in the compiler that processes these arguments?

Otherwise, I’m afraid of the number of possible cases for the different types of argument ASTs.

 

Cheers,

 

Erik.

 

 

From: rytz [dot] epfl [at] gmail [dot] com [mailto:rytz [dot] epfl [at] gmail [dot] com] On Behalf Of Lukas Rytz
Sent: January-15-09 4:20 AM
To: Putrycz, Erik
Cc: scala-tools [at] listes [dot] epfl [dot] ch
Subject: Re: [scala-tools] IType.getAnnotations() doesn't seem to work for scala elements

 

Hi Erik

I'm not entirely sure to understand your question.. You attached an annotation,
which is itself defined in Java (@interface), to a Scala definition (e.g. a method),
and you'd like to know how the annotation parameters are represented in the
compiler's AST, right?

A method definition is a 'DefDef' AST node, which has a parameter of type
'Modifiers'. Modifiers have a field called 'annotations', which is a list of AST
nodes of type 'Annotation'. This is where annotations end up after parsing.

As you guessed, the 'AnnotationInfo' is more useful to you. Actually, the namer
phase of the compiler transforms the 'Annotation'-trees into AnnotationInfo
objects. For definitions (e.g. DefDef), these object are stored in a field called
'attributes' of the definition's symbol.

In other words, the given a tree: DefDef, you can access the AnnotationInfos
using tree.symbol.attributes (which is a List[AnnotationInfo]).


Let me know if you need more help.

Cheers: Lukas


On Wed, Jan 14, 2009 at 18:18, Putrycz, Erik <Erik [dot] Putrycz [at] nrc-cnrc [dot] gc [dot] ca> wrote:

Hi Lukas,

Miles gave me your contact; he said that you should be knowledgeable on
annotation processing.

I've been looking at implementing annotation processing in the current
scala Eclipse plugin, and the real complexity is in the annotation
arguments.
What eclipse seems to needs for arguments are:
- java objects (integer, arrays, etc) for simple types
- strings for classes (which don't need to be resolved)

Is there any way to reuse some of the compiler classes for extracting
annotation arguments?

The compiler has a class "AnnotationInfos" which seem to be close to
what is required but I don't have a clue how to use it.

Erik Putrycz, PhD - Research Associate / erik [dot] putrycz [at] nrc-cnrc [dot] gc [dot] ca /
(613) 990 0681
Institute for Information Technology - Software Engineering Group
National Research Council, Canada - Building M-50, 1200 Montreal Road
Ottawa, Ontario, CANADA K1A 0R6

> -----Original Message-----
> From: Miles Sabin [mailto:miles [at] milessabin [dot] com]
> Sent: January-08-09 3:05 PM
> To: Putrycz, Erik
> Cc: scala-tools [at] listes [dot] epfl [dot] ch
> Subject: Re: [scala-tools] IType.getAnnotations() doesn't seem to work

> for scala elements
>
> On Thu, Jan 8, 2009 at 7:56 PM, Putrycz, Erik
> <Erik [dot] Putrycz [at] nrc-cnrc [dot] gc [dot] ca> wrote:
> > TestNG is "usable", i.e. the test runner works but all the context
> > features of the testng eclipse plugin are missing. Most of the
> > eclipse plugin feature are useless unless you run the tests manually

> > (Run, create new configuration, etc...). This is probably the same
> > for JUnit 4; I don't think Eclipse will recognise unit tests unless
> > the annotations are indexed.
>
> OK, gotcha ...
>
> >> I'd be very keen to get input from the users of that the unit
> >> testing frameworks on what's wanted in the way of Eclipse
> >> integration ... and help and patches would be very welcome as well!
> >
> > I'm willing to help, the context features are very important for me.

> > I don't mind having a look at the scala plugin code if you can point

> > me where the annotations would be gathered. I would expect that
> > there is only missing a bridge between the AST and IAnnotations,
right?
>
> Fantastic! Thanks!
>
> To check out and build the plugin you should follow the instructions
> here,
>
>   http://lampsvn.epfl.ch/trac/scala/wiki/EclipsePlugin
>
> The place you need to start digging is
> scala.tools.eclipse.javaelements.ScalaStructureBuilder in the
> scala-plugin project. Feel free to come and chase me on #scala (I keep

> UK hours).
>
> Cheers,
>
>
> Miles
>
> --
> Miles Sabin
> tel:    +44 (0)1273 720 779
> mobile: +44 (0)7813 944 528
> skype:  milessabin

 

rytz
Joined: 2008-07-01,
User offline. Last seen 45 weeks 5 days ago.
Re: IType.getAnnotations() doesn't seem to work for scala eleme
Instead of looking at
  defTree.annotations: List[Annotation]

look at
  defTree.symbol.attributes: List[AnnotationInfo]

An AnnotationInfo has
 - atp: Type - the type of the annotation (Test in your case)
 - args: List[AnnotationArgument] - the constructor arguments given to the annotation (empty in your case)
 - assocs: List[(Name, AnnotationArgument)] - the name-value pairs specified when using a java-annotation

annotationArgument.constant gives you the Constant you're looking for. For "groups", you'll get the string with "constant.stringValue", for "retryAnalyzer", you'll get the class with "constant.typeValue".

Lukas



On Thu, Jan 15, 2009 at 16:41, Putrycz, Erik <Erik [dot] Putrycz [at] nrc-cnrc [dot] gc [dot] ca> wrote:

Hi Lukas,

 

Accessing the field "annotations" from the modifiers is not difficult, Miles did all that work already in the existing plugin.

What I found out to be complex are all the parameters of the annotation itself once you start parsing the AST of the Annotation.

My example is

@Test{ val groups = Array("testgroup"), val retryAnalyzer = classOf[RetryClass]}

 

In the AST, @Test ends up in a sum of Apply, Select, New, Select, Select, Select and Ident.

Then the arguments end up in ValDef, TypeTree and Literal.

Is there something more generic in the compiler that processes these arguments?

Otherwise, I'm afraid of the number of possible cases for the different types of argument ASTs.

 

Cheers,

 

Erik.

 

 

From: rytz [dot] epfl [at] gmail [dot] com [mailto:rytz [dot] epfl [at] gmail [dot] com] On Behalf Of Lukas Rytz
Sent: January-15-09 4:20 AM
To: Putrycz, Erik
Cc: scala-tools [at] listes [dot] epfl [dot] ch
Subject: Re: [scala-tools] IType.getAnnotations() doesn't seem to work for scala elements

 

Hi Erik

I'm not entirely sure to understand your question.. You attached an annotation,
which is itself defined in Java (@interface), to a Scala definition (e.g. a method),
and you'd like to know how the annotation parameters are represented in the
compiler's AST, right?

A method definition is a 'DefDef' AST node, which has a parameter of type
'Modifiers'. Modifiers have a field called 'annotations', which is a list of AST
nodes of type 'Annotation'. This is where annotations end up after parsing.

As you guessed, the 'AnnotationInfo' is more useful to you. Actually, the namer
phase of the compiler transforms the 'Annotation'-trees into AnnotationInfo
objects. For definitions (e.g. DefDef), these object are stored in a field called
'attributes' of the definition's symbol.

In other words, the given a tree: DefDef, you can access the AnnotationInfos
using tree.symbol.attributes (which is a List[AnnotationInfo]).


Let me know if you need more help.

Cheers: Lukas


On Wed, Jan 14, 2009 at 18:18, Putrycz, Erik <Erik [dot] Putrycz [at] nrc-cnrc [dot] gc [dot] ca> wrote:

Hi Lukas,

Miles gave me your contact; he said that you should be knowledgeable on
annotation processing.

I've been looking at implementing annotation processing in the current
scala Eclipse plugin, and the real complexity is in the annotation
arguments.
What eclipse seems to needs for arguments are:
- java objects (integer, arrays, etc) for simple types
- strings for classes (which don't need to be resolved)

Is there any way to reuse some of the compiler classes for extracting
annotation arguments?

The compiler has a class "AnnotationInfos" which seem to be close to
what is required but I don't have a clue how to use it.

Erik Putrycz, PhD - Research Associate / erik [dot] putrycz [at] nrc-cnrc [dot] gc [dot] ca /
(613) 990 0681
Institute for Information Technology - Software Engineering Group
National Research Council, Canada - Building M-50, 1200 Montreal Road
Ottawa, Ontario, CANADA K1A 0R6

> -----Original Message-----
> From: Miles Sabin [mailto:miles [at] milessabin [dot] com]
> Sent: January-08-09 3:05 PM
> To: Putrycz, Erik
> Cc: scala-tools [at] listes [dot] epfl [dot] ch
> Subject: Re: [scala-tools] IType.getAnnotations() doesn't seem to work

> for scala elements
>
> On Thu, Jan 8, 2009 at 7:56 PM, Putrycz, Erik
> <Erik [dot] Putrycz [at] nrc-cnrc [dot] gc [dot] ca> wrote:
> > TestNG is "usable", i.e. the test runner works but all the context
> > features of the testng eclipse plugin are missing. Most of the
> > eclipse plugin feature are useless unless you run the tests manually

> > (Run, create new configuration, etc...). This is probably the same
> > for JUnit 4; I don't think Eclipse will recognise unit tests unless
> > the annotations are indexed.
>
> OK, gotcha ...
>
> >> I'd be very keen to get input from the users of that the unit
> >> testing frameworks on what's wanted in the way of Eclipse
> >> integration ... and help and patches would be very welcome as well!
> >
> > I'm willing to help, the context features are very important for me.

> > I don't mind having a look at the scala plugin code if you can point

> > me where the annotations would be gathered. I would expect that
> > there is only missing a bridge between the AST and IAnnotations,
right?
>
> Fantastic! Thanks!
>
> To check out and build the plugin you should follow the instructions
> here,
>
>   http://lampsvn.epfl.ch/trac/scala/wiki/EclipsePlugin
>
> The place you need to start digging is
> scala.tools.eclipse.javaelements.ScalaStructureBuilder in the
> scala-plugin project. Feel free to come and chase me on #scala (I keep

> UK hours).
>
> Cheers,
>
>
> Miles
>
> --
> Miles Sabin
> tel:    +44 (0)1273 720 779
> mobile: +44 (0)7813 944 528
> skype:  milessabin

 


Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland