Override annotation member variable at runtime
-
Hi all, Java newbie here.. Is it possible to override/set the value of a member variable at runtime? So for example, in the snippet below, how do I set fieldValue with the value of the data that is stored in fieldName?
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyField{String fieldName() default ""; String fieldValue() default "";
}
@MyField(fieldName = "pageTitle")
public class Foobar {
private String pageTitle;public String getPageTitle() { return pageTitle; } public void getPageTitle(String pageTitle) { this.pageTitle = pageTitle; }
}
-
Hi all, Java newbie here.. Is it possible to override/set the value of a member variable at runtime? So for example, in the snippet below, how do I set fieldValue with the value of the data that is stored in fieldName?
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyField{String fieldName() default ""; String fieldValue() default "";
}
@MyField(fieldName = "pageTitle")
public class Foobar {
private String pageTitle;public String getPageTitle() { return pageTitle; } public void getPageTitle(String pageTitle) { this.pageTitle = pageTitle; }
}
I think this answers your question, http://docs.oracle.com/javase/1.5.0/docs/guide/language/annotations.html[^] Quote: Once an annotation type is defined, you can use it to annotate declarations. An annotation is a special kind of modifier, and can be used anywhere that other modifiers (such as public, static, or final) can be used. By convention, annotations precede other modifiers. Annotations consist of an at-sign (@) followed by an annotation type and a parenthesized list of element-value pairs. The values must be compile-time constants. Personally never used annotations for anything other than generating Java Docs and I'm not sure what your trying to use it for here? Are you really trying to define an interface?
-
I think this answers your question, http://docs.oracle.com/javase/1.5.0/docs/guide/language/annotations.html[^] Quote: Once an annotation type is defined, you can use it to annotate declarations. An annotation is a special kind of modifier, and can be used anywhere that other modifiers (such as public, static, or final) can be used. By convention, annotations precede other modifiers. Annotations consist of an at-sign (@) followed by an annotation type and a parenthesized list of element-value pairs. The values must be compile-time constants. Personally never used annotations for anything other than generating Java Docs and I'm not sure what your trying to use it for here? Are you really trying to define an interface?
-
Hi all, Java newbie here.. Is it possible to override/set the value of a member variable at runtime? So for example, in the snippet below, how do I set fieldValue with the value of the data that is stored in fieldName?
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyField{String fieldName() default ""; String fieldValue() default "";
}
@MyField(fieldName = "pageTitle")
public class Foobar {
private String pageTitle;public String getPageTitle() { return pageTitle; } public void getPageTitle(String pageTitle) { this.pageTitle = pageTitle; }
}
Annotations can be processed to produce code in various ways. The code of course would exist at run time. If you are using an existing annotation processing system then what ever it does is what you can do, and you would look at the docs for that. If your writing your own then add the functionality to keep track of it.