Hi Anthony
Your annotation doesn't modify the method behavior but may let another
program to get some more information about your class (of your method
for instance). Let's say your class describes an object related to the
database and your annotations describe the way the database fields are
mapped to the class properties/methods. A specialized container can read
at runtime (as long as RetentionPolicy.RUNTIME) the annotations
associated to the methods of your class and automatically map data
from/to the database (without supplementary code written by you - unless
you create the specialized container).
In your case, you can do something in the main function like this:
public static void main(String[] args) {
// TODO code application logic here
MutatorAnnotation mutAnnot = new MutatorAnnotation(); // code I added
System.out.println("Name is : "+mutAnnot.getName());
* Class c = mutAnnot.getClass(); // Get the class of the object
Method[] ms = c.getMethods(); // Get the list of the methods of
the class
for ( Method m : ms ){
Annotation[] annos = m.getAnnotations(); // Get method
annotations if any
for ( Annotation a : annos )
System.out.println( m.getName() + " -> " + a ); // Print
method and annotation
}
* *...*
A code like this allows, thus, to discover at runtime features about the
class and its methods if annotated.
Hope it helps
mihai
Anthony Lam a écrit :
Hi,
I am working on the Java Annotation lab MutatorAnnotation.java
exercises. I have no clue of how the code in red could have any
affect to setName method :
public class MutatorAnnotation {
private String name;
private int id;
/**
* Constructor
**/
public MutatorAnnotation() {
name = "Java Passion!";
}
public String getName() {
return name;
}
@Mutator(variable = "name")
public void setName(String name) {
this.name = name;
}
@Retention(RetentionPolicy.RUNTIME)
public @interface Mutator { // annotation definition
String variable();
}
public static void main(String[] args) {
// TODO code application logic here
*MutatorAnnotation mutAnnot = new
MutatorAnnotation(); // code I added
System.out.println("Name is : "+mutAnnot.getName());*
Run result:
run:
Name is : Java Passion!
BUILD SUCCESSFUL (total time: 0 seconds)
The result display "Java Passion". In this case the Mutator annotation
has no impact to the setName method. Then what is the usage of have
this method annotated by the @Mutator annotation. Would like to see
some code how @Mutator annotation could apply to practical usage?
Thank you!
Regards,
Anthony
------------------------------------------------------------------------
Hotmail: Powerful Free email with security by Microsoft. Get it now.
<https://signup.live.com/signup.aspx?id=60969> --
To post to this group, send email to
[email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en