• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Tough Question: Creating modifiers

 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I'm using reflection to save objects to XML and read them back from XML.

Right now I'm using the 'public' modifier to tell which variables get saved. This is workable.

But it would be nice if I could create my own Modifier, such as xmlSaveable, and use it like:
public xmlSaveable int gameRound = 0;

What objects would I have to extend to make my own Modifiers? Would I have to fool with the class loader?

Thanks!
Skip
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could this be a job for Annotations?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeff means: This is exactly what annotations were meant for!
 
Skip Cole
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

This does look like it is exactly what I need, but I haven't gotten it to work. Anyone know if there are any bugs associated with this?

I have found examples for methods and classes, but not for annotated fields. That would help too.

Once I do figure this out, I will post my code here.

Skip
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It would help if you ask something specific about annotations.
What do you mean you have been able to make it work with fields? Perhaps the ElementType annotation is of interest to you?

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/annotation/ElementType.html
 
Skip Cole
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that annotations was a false start, since as explicitly stated in the docs "Annotations provide data about a program that is not part of the program."

I want to create my own modifiers, so I can tag fields I want to be saved at particular times. Note, this could be done with reflection and naming conventions, but naming conventions are lame.

In response to the posting above, I'll post the code tonight showing this doesn't work. Really all I want to do is create my own modifier that can be picked up AT RUN TIME by reflection, and used to treat certain variables differently (such as in writing them out to XML.)

Make sense?

Thanks,
Skip
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Annotations is definitely what you want. Avoid Sun marketing material as a definition of truth. Annotations are metadata (no invokable operations) upon contracts and their operations i.e. the equivalent of modifiers.

If you want to obtain your metadata at runtime, you simply use the correct RetentionPolicy. You have been pointed to the appropriate documentation a number of times, but seem to have ignored it (besides the marketing material, which I can sympathise with). I refer you to JTiger http://jtiger.org/ which makes heavy use of runtime reflection upon annotations as they appear on the various available source constructs (such as fields).

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/annotation/RetentionPolicy.html
 
Skip Cole
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The uh, rather tactless, posting above was correct.

Here is a way to show it. Just create the two files below and run the main method of TestMyModifiers.

---------Begin contents of XMLSaveable.java-------------------
import java.lang.annotation.*;

@Target(ElementType.FIELD)
@Retention(value=RetentionPolicy.RUNTIME)

public @interface XMLSaveable {

}
---------------------End Contents------------------------------


--------------- Begin contents of TestMyModifiers.java ----------
import java.lang.reflect.*;

public class TestMyModifiers{

public @XMLSaveable int myStuff = 0;
public int yourStuff = 0;


public static void main(String args[]){

TestMyModifiers tmm = new TestMyModifiers();

tmm.myStuff = 1;

Class c = tmm.getClass();

for (Field f : c.getFields()) {
if (f.isAnnotationPresent(XMLSaveable.class)) {
System.out.println(f.getName() + " is XMLSaveable");
} else {
System.out.println(f.getName() + " is not XMLSaveable");
}
}

}
}
-------------------End Contents-------------------------

It gives the output:
myStuff is XMLSaveable
yourStuff is not XMLSaveable


And this truly does rock.

Thanks to all of the people who helped me with this!
[ January 20, 2006: Message edited by: Skip Cole ]
 
You get good luck from rubbing the belly of a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic