Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Accessing Class annotation values from JvmDeclaredType?
Accessing Class annotation values from JvmDeclaredType? [message #1730848] Fri, 29 April 2016 05:09 Go to next message
Eclipse UserFriend
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 05:24 Go to previous messageGo to next message
Eclipse UserFriend
the values of JvmAnnotationReference give you nothing ?!?
Re: Accessing Class annotation values from JvmDeclaredType? [message #1730854 is a reply to message #1730851] Fri, 29 April 2016 05:47 Go to previous messageGo to next message
Eclipse UserFriend
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 05:48] by Moderator

Re: Accessing Class annotation values from JvmDeclaredType? [message #1730855 is a reply to message #1730854] Fri, 29 April 2016 05:48 Go to previous messageGo to next message
Eclipse UserFriend
maybe you havr to look at sublclasses of the inferface (downcast)
Re: Accessing Class annotation values from JvmDeclaredType? [message #1730908 is a reply to message #1730855] Fri, 29 April 2016 12:27 Go to previous messageGo to next message
Eclipse UserFriend
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 13:08 Go to previous messageGo to next message
Eclipse UserFriend
JvmAnnotationReference has JvmAnnotationValue that might be actually instances of JvmStringAnnotationValue and other onse
Re: Accessing Class annotation values from JvmDeclaredType? [message #1730930 is a reply to message #1730909] Sat, 30 April 2016 00:48 Go to previous message
Eclipse UserFriend
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 Jul 05 04:28:57 EDT 2025

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

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

Back to the top