• Post Reply Bookmark Topic Watch Topic
  • New Topic

To HS: How attributes change design  RSS feed

 
author
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Herbert!

I have dipped my toe into the .NET waters, writing a little of C# with attributes, but I haven't yet done it in a professional setting where I need to solve real problems. My question involves Java attributes: what are (say) three ways in which using attributes has changed the way you design systems?

Thanks for your time.
 
author
Ranch Hand
Posts: 799
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Herb missed the question, J.B.; I suspect it got here before he did.

I did some C# a few years ago. It seems like C# was designed with attributes/annotations in mind. Many of the things we do in Java through code are done in C# using attributes. For example, in Java you implement the Serializable interface; in C#, you mark a class as serializable using an attribute.

As such, I struggled a bit to find relevant uses for annotation types in Java. Many of the good examples from C# already exist in another form in the language. Nonetheless, I'm sure there are plenty of uses. Obviously tool makers will find them useful. The classic example is to derive an exportable web services interface from implementation code.

JUnit is possibly a good place to start with an example. I prefer the NUnit technique of tagging test methods with attributes, rather than having to depend upon adherence to reflective rules (using attributes to tag a class is nice but perhaps dubious). There is a tool out there (TestNG) that demonstrates how this might look.

In learning about annotations in Java, I wrote a 2-part article (that appears at my site, click the little mini-icon by this post) that demonstrates an annotation type named @Ignore. The idea is to allow you to mark test methods as being temporarily ignored. I also retrofitted JUnit to find and display these ignored methods as a reminder that they exist.

-Jeff-

ps -- how's the book going? I just got it; it looks big and promising!
 
Author
Ranch Hand
Posts: 253
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J.B.: There are so many posts, I haven't been able to get to them all yet, sorry.

At any rate: I have two things to say about annotations. First, they are most important for what they imply: a transition to a more declartive style of programming. As tools that recognize annotations become more powerful, so too will the impact of annotations. Second, although useful, they have had little impact on how I code right now. Currently, they offer more convenience than anything else. But that will change.

That said, annotations are still a key feature of Java 2, v5 because they represent fundamentally new functionality that has been added to Java. Also, there are aspects of annotations that programmers will want to use right now. For example, you can use the @deprecated annotation rather than the @deprecated javadoc tag to mark a deprecated method. Annotations can also be queried by reflection.
 
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the great explanation about introductory to annotation...
 
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Herb Schildt:
J.B.: There are so many posts, I haven't been able to get to them all yet, sorry.

At any rate: I have two things to say about annotations. First, they are most important for what they imply: a transition to a more declartive style of programming. As tools that recognize annotations become more powerful, so too will the impact of annotations. Second, although useful, they have had little impact on how I code right now. Currently, they offer more convenience than anything else. But that will change.

That said, annotations are still a key feature of Java 2, v5 because they represent fundamentally new functionality that has been added to Java. Also, there are aspects of annotations that programmers will want to use right now. For example, you can use the @deprecated annotation rather than the @deprecated javadoc tag to mark a deprecated method. Annotations can also be queried by reflection.



Annotation is seem like JavaDoc but DIFFERENT Goal/Purpose or not ?? How ??
 
Jeff Langr
author
Ranch Hand
Posts: 799
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Annotations are compiled. This is a significant benefit over schemes that depend on text parsing or reflection conventions (e.g. getter methods for beans must be in the form "getXxx" where "xxx" is the name of the instance variable).

You might use annotations for many things:
- mark a method as exportable to an interface
- mark a method as "@todo", so that an IDE can list the outstanding work
- mark a method as "@override," indicating that the method overrides a superclass method. The compiler will fail if the method does not properly override a method.
- add formatted modification comments

-Jeff-
 
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeff Langr:
You might use annotations for many things:
...

In general, having annotations extends what you can do with the Reflection API at runtime. You can couple your reflection-based code into a specific annotation type instead of (in addition to) a specific interface, class or naming convention.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!