Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Accessing Class annotation values from JvmDeclaredType?
Accessing Class annotation values from JvmDeclaredType? [message #1730848] Fri, 29 April 2016 09:09 Go to next message
Larry LeBron is currently offline Larry LeBronFriend
Messages: 124
Registered: October 2015
Senior Member
Is it possible to access the declared value of a class's annotation via JvmDeclaredType?

e.g. Take this class:

@ExampleAnnotation(exampleVal = "hi")
Class Foo {}


I'm using XBase, and would like to be able to get this value, "hi" from a JvmDeclaredType that's referencing Foo.

I've been able to access the JvmAnnotationReference, which gives me information about the annotation type itself, but doesn't seem to grant access to the specific class's defined value.

Is this possible, or do I need to create an instance of the Class<Foo> object itself to do this?

Thanks!
Re: Accessing Class annotation values from JvmDeclaredType? [message #1730851 is a reply to message #1730848] Fri, 29 April 2016 09:24 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
the values of JvmAnnotationReference give you nothing ?!?

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Accessing Class annotation values from JvmDeclaredType? [message #1730854 is a reply to message #1730851] Fri, 29 April 2016 09:47 Go to previous messageGo to next message
Larry LeBron is currently offline Larry LeBronFriend
Messages: 124
Registered: October 2015
Senior Member
Right, JvmAnnotationReference lets me access the list of JvmAnnotationValue for the annotation, but this just gives me the name of the value and the operation it supports. I don't see a way to get the actual defined value, as specified by the annotated class.

For my example, I can get information like:

name: exampleVal
operation: JvmOperation: packageName.exampleVal() (visibility: PUBLIC, simpleName: exampleVal, identifier: packageName.exampleVal(), deprecated: false) (varArgs: false) (static: false, final: false, abstract: true, synchronized: false, default: false, native: false, strictFloatingPoint: false)

What I'm looking for is the equivalent of what I can invoke on the Foo.class object itself:

val fooAnnotation = Foo.class.getAnnotation(ExampleAnnotation.class)
System.out.println(fooAnnotation.exampleVal) // prints "hi"


I can get this class instance by calling: Class.forName(jvmDeclaredType.qualifiedName), but not sure this is a fully safe operation during inferral.

[Updated on: Fri, 29 April 2016 09:48]

Report message to a moderator

Re: Accessing Class annotation values from JvmDeclaredType? [message #1730855 is a reply to message #1730854] Fri, 29 April 2016 09:48 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
maybe you havr to look at sublclasses of the inferface (downcast)

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Accessing Class annotation values from JvmDeclaredType? [message #1730908 is a reply to message #1730855] Fri, 29 April 2016 16:27 Go to previous messageGo to next message
Larry LeBron is currently offline Larry LeBronFriend
Messages: 124
Registered: October 2015
Senior Member
Hmm, I'm not sure how I would go about down-casting from a JvmAnnotationReference, if that's what you mean.

Can you give an example?

Is there actually a problem with creating the class instance using Class.forName() ?
Re: Accessing Class annotation values from JvmDeclaredType? [message #1730909 is a reply to message #1730908] Fri, 29 April 2016 17:08 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
JvmAnnotationReference has JvmAnnotationValue that might be actually instances of JvmStringAnnotationValue and other onse

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Accessing Class annotation values from JvmDeclaredType? [message #1730930 is a reply to message #1730909] Sat, 30 April 2016 04:48 Go to previous message
Larry LeBron is currently offline Larry LeBronFriend
Messages: 124
Registered: October 2015
Senior Member
Yes, this worked like a charm.

Casting as the more specific sub-interface of JvmAnnotationValue gives access to the corresponding defined value.

Thanks!
Previous Topic: Problem by reference in Xtext
Next Topic:JVM model inferrer comments out type reference
Goto Forum:
  


Current Time: Sat Apr 20 00:09:46 GMT 2024

Powered by FUDForum. Page generated in 0.03726 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top