Skip to main content

Annotations with dynanic values

4 replies [Last post]
betty6
Offline
Joined: 2010-08-20

I would like to have a value of an annotation to be retrieved from a method call. I understand that this is not possible, as we need to pass in compile time constants. However, I am hoping someone is able to look at my example and offer an alternative solution?

The example below explains what i want to achieve.
I have a java annotation:
public @interface MyAnnotation {
String path();
String url();
}

To use this, I have an object:
public class MyObject {
private String name;
private String description;
private String url;

MyObject (String url) {
this.url=url;
}
public String getUrl() {
return url;

public String getName() {
return name;
}
@MyAnnotation (path="/some/path", url=getUrl())
public String setName(String name) {
this.name=name;
}
@MyAnnotation (path="/someother/path", url=getUrl())
public String setDescription(String description) {
this.description=description;
}
}

So from this, I want to populate the name value with a specified path but from the source of one url- and I want to populate the description with a specified path with the source of a different url.

Another method would then inspect this class and automatically know where to populate the values from based on the urls.

Any help, advice is appreciated.

Thanks

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
betty6
Offline
Joined: 2010-08-20

Thank you very much for your suggestions! I implemented something along the lines of what you suggested Rivas which worked!
Thanks again!!

kennardconsulting
Offline
Joined: 2005-10-25

When developing Metawidget (http://metawidget.org ), I found embedding EL expressions in annotations an interesting approach. You can embed the EL as a static String, and invoke something like Commons JEXL or the JSP/JSF EL to evaluate them, allowing you to do...

@UiJexlAttribute( value = "hidden", expression = "this.shouldBeHidden" )

...or...

@UiFacesAttribute( value = "hidden", expression = "#{someController.shouldBeHidden}" )

Hope that helps,

Richard.

Message was edited by: kennardconsulting

rivasdiaz
Offline
Joined: 2003-06-11

I think you should evaluate if you really need an annotation for your requirement, but based only on your post, I suggest you one way of achieving what you need:

public enum MyAnnotationURLType { STATIC, DYNAMIC }
public interface MyAnnotationURLProvider { String url(); }
public @interface MyAnnotation {
String path();
String url();
MyAnnotationURLType urltype() default MyAnnotationURLType.STATIC;
}

You can check your annotated objects, if they define urltype as DYNAMIC, then they should implement MyAnnotationURLProvider and you should invoke the url() method on the instance when you need to get the url.

you could have:

public class MyObject implements MyAnnotationURLProvider {
// ...
@MyAnnotation( path="/somepath", urltype=DYNAMIC)
public void setName(String name) { this.name = name; }
}

IF you need different URLs in the same class, then you could introduce an extra attribute that could work as a selector

Hope that helps,
rivas

rivasdiaz
Offline
Joined: 2003-06-11

Similar to that solution, you could have an attribute on the annotation interface named urlmethod, and if the url is dynamic, invoke the specified method via reflection.