0

Maybe I'm not seeing the forest for the trees but is there a way to use a String resource to provide a value to a annotation field. What I would like is something like so

@Rest(rootUrl=@StringRes(R.string.root_url), converters=MappingJackson2HttpMessageConverter.class) 
public interface MyRestService extends RestClientErrorHandling {...

The rootUrl value in the @Rest annotation should get the value from the String resource R.string.root_url.

Any direction is much appreciated.

Mill3r
  • 78
  • 1
  • 8

3 Answers3

0

Few minutes after I posted the question, I found Why The value for annotation attribute Rest.rootUrl must be a constant expression? which pretty much answers the question.

Basically, I can use a setRootUrl(String rootUrl) method in the rest service interface to inject the rootUrl value during runtime. Anyone wanting more information, please refer to the answer in the referenced SO question for more details.

Community
  • 1
  • 1
Mill3r
  • 78
  • 1
  • 8
0

I am also interesting in this problem. But after thinking, I think this is almost impossible.

  1. Java provides three types of RetentionPolicy, SOURCE, CLASS, RUNTIME. It defines the life of this annotation. Only RUNTIME annotation will be kept during runtime. But how could a compiler knows if the annotation will be kept?

  2. The annotation is handled by APT(Annotation Processing Tool) which is totally different with compiler for source code. So how could an APT has the knowledge of compiler and even JVM?

Based on the above two points, it is impossible to use variables defined in the code in annotation.

peacepassion
  • 1,918
  • 2
  • 15
  • 19
0

i use the string resource name as the value in the annotation, later in the code i get that value using this method

private String getStringResourceByName(String aString) {
  String packageName = getPackageName();
  int resId = getResources().getIdentifier(aString, "string", packageName);
  return getString(resId);
}

its not exactly what was asked here but its how i got the resource value based on the annotation value

Gil
  • 11
  • 2