Display the method annotations available in a java class using Java reflection -


i working on java project needs display meta information of java class annotations have been declared methods, parameters, etc.

i have following classes

employee.java

package labsheet;  public class employee {      private string eid = "e001";     string ename = "anura";     public string address = "batticaloa";     protected double salary = 60_000.00;      public string geteid() {         return eid;     }     public void seteid(string eid) {         this.eid = eid;     }     string getename() {         return ename;     }     void setename(string ename) {         this.ename = ename;     }     protected string getaddress() {         return address;     }     protected void setaddress(string address) {         this.address = address;     }     @suppresswarnings("unused")     private double getsalary() {         return salary;     }     @suppresswarnings("unused")     private void setsalary(double salary) {         this.salary = salary;     }      public string display(string eid, string ename, string address, double salary){         system.out.println("method invoked successfully");         return eid+" , "+ename+" , "+address+" , "+salary;     }   } 

in main class task3.java trying display annotations available in employee.java. , hence intend display @suppresswarnings("unused") details methods getsalary() , setsalary() using java reflection.

i have coded task3.java follows.

task3.java

package labsheet; import java.lang.annotation.annotation; import java.lang.reflect.invocationtargetexception; import java.lang.reflect.method; import java.lang.reflect.modifier; import java.lang.reflect.parameter;  public class task3 {     public static void main(string[] args) {         try {             class<?> clazz = class.forname("labsheet.employee");             method[] methods = clazz.getdeclaredmethods();              for(int m =0; m<methods.length;m++){                 system.out.println("modifier=> "+ modifier.tostring(methods[m].getmodifiers()));                 annotation [] annotations = methods[m].getannotations();                 system.out.println("annotation count: "+annotations.length);                 for(int o =0; o<annotations.length;o++){                     system.out.println("annotation "+(o+1)+": "+annotations[o]);                 }                 system.out.print("|| return type=> "+ methods[m].getreturntype());                 system.out.print("|| method name=> "+ methods[m].getname());                 parameter[] parameters = methods[m].getparameters();                 if(parameters.length != 0){                     system.out.print("|| method parameters=> ");                     for(int u = 0; u<parameters.length;u++){                         if(u== parameters.length-1){                             system.out.print(parameters[u].gettype().getsimplename());                         }                         else{                             system.out.print(parameters[u].gettype().getsimplename()+" ");                         }                     }                 }                  system.out.println();                 system.out.println();             }             system.out.println("*******************************");             executemethod();             system.out.println("*******************************");             executemethodwithdefault();         }           catch (classnotfoundexception e) {             e.printstacktrace();         }      }  ... //methods declaration executemethod() , executemethodwithdefault()  } 

but getting output says there no annotations getsalary() , setsalary() methods well, whereas need @suppresswarnings("unused") annotation details displayed here.

output

modifier=> protected annotation count: 0 || return type=> class java.lang.string|| method name=> getaddress  modifier=> public annotation count: 0 || return type=> class java.lang.string|| method name=> display|| method parameters=> string string string double  modifier=> private annotation count: 0 || return type=> void|| method name=> setsalary|| method parameters=> double  modifier=>  annotation count: 0 || return type=> class java.lang.string|| method name=> getename  modifier=> protected annotation count: 0 || return type=> void|| method name=> setaddress|| method parameters=> string  modifier=>  annotation count: 0 || return type=> void|| method name=> setename|| method parameters=> string  modifier=> private annotation count: 0 || return type=> double|| method name=> getsalary  modifier=> public annotation count: 0 || return type=> class java.lang.string|| method name=> geteid  modifier=> public annotation count: 0 || return type=> void|| method name=> seteid|| method parameters=> string 

any suggestions in regard appreciated.

look @ java.lang.suppresswarnings class:

@retention(retentionpolicy.source) public @interface suppresswarnings {   // code... } 

it has source retention policy (see java.lang.annotation.retentionpolicy class):

public enum retentionpolicy {     /**      * annotations discarded compiler.      */     source,     // code... } 

this means impossible information annotation in runtime, compiler doesn't place class files.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -