wood burning stoves 2.0*
The moose likes Other JSE/JEE APIs and the fly likes annotation internal funcationality Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "annotation internal funcationality" Watch "annotation internal funcationality" New topic
Author

annotation internal funcationality

Thamayanthi Guhan
Greenhorn

Joined: Feb 09, 2009
Posts: 8

I have written simple annotation which returns String value. But I couldn't understand how it is working by running the program. How the value will used in below example?

Could any one please provide me internal functional details of the annotation?

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@interface CustomAnnotation {
String value();
}

public class AnnotationExample1 {

@CustomAnnotation(value="Test Annotation")
private void printName(String name) {
System.out.println("Name = " + name);
}

public static void main(String[] string) {
System.out.println("Test");
AnnotationExample1 exm = new AnnotationExample1();
exm.printName("Guhan");
}

}

Please help me to understand how to write our annotation effectively?

I am sure @deprecated and all working some how?. But I couldn't capture how?

Please help me to understand the internal functionality of the concept?

Thanks
Thamayanthi
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42601
    
  65
Here's an example of how to use annotation programmatically: http://faq.javaranch.com/java/AnnotationsExample

In most cases annotations are used by tools and servers, though, not by code you'd be writing.

Here's what the documentation has to say about them: http://java.sun.com/docs/books/tutorial/java/javaOO/annotations.html and http://java.sun.com/j2se/1.5.0/docs/guide/language/annotations.html


Ping & DNS - my free Android networking tools app
Thamayanthi Guhan
Greenhorn

Joined: Feb 09, 2009
Posts: 8
so, if we define own annotation, just to understand.

It is not possible to see the functionality if the compiler/runtime didn't understand what to do with own annotation?

if we take @deprecated, will it compiler should support this?. ie some code written in compiler when it identifies the method which has @deprecated should throw warning message.

Thanks
Thamayanthi


Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42601
    
  65
The Java compiler and runtime obviously can't know the annotations you've defined. Only your code can know what to do with those.

You can use @Deprecated in your code, and the compiler should generate the appropriate warnings.
 
Consider Paul's rocket mass heater.
 
subject: annotation internal funcationality