Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Override annotation member variable at runtime

Override annotation member variable at runtime

Scheduled Pinned Locked Moved Java
questionjavatutorial
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lash20
    wrote on last edited by
    #1

    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;
    }
    

    }

    L J 2 Replies Last reply
    0
    • L Lash20

      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;
      }
      

      }

      L Offline
      L Offline
      lrinish
      wrote on last edited by
      #2

      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?

      L 1 Reply Last reply
      0
      • L lrinish

        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?

        L Offline
        L Offline
        Lash20
        wrote on last edited by
        #3

        Thanks, I'll take a look at the link. Per your question, I'm using annotations to for custom validation.

        1 Reply Last reply
        0
        • L Lash20

          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;
          }
          

          }

          J Offline
          J Offline
          jschell
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups