WELCOME Abdennour : Software engineer

Dec 1, 2012

Reflection Java Api Best Practices


Code Source


  1. create a java class nemed TestClass containing 5 methods (m_A(), m_B(), m_C(), m_D(), m_E() ) every method return a String ("A", "B" ...) :

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    
    package slm.abdennour.domain;
    import slm.abdennour.reflection.AnnoteMe;
    /**
    *
    * @author عبد النور التومي Abdennour TOUMI
    * @blog http://abdennour-insat.blogspot.com
    * 28 oct. 2012
    */
    public class TestClass {
    public String m_A(){
    return "A";
    }
    public String m_B(){
    return "B";
    }
    public String m_C(){
    return "C";
    }
    public String m_D(){
    return "D";
    }
    public String m_E(){
    return "E";
    }
    }











  2. create a parameterized annotation for methods with name: annoteMe (parameter name: "returnKey") :
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    
    package slm.abdennour.reflection;
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    /**
    * @author عبد النور التومي Abdennour TOUMI
    * 28 oct. 2012
    */
    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface AnnoteMe {
          String returnKey();
    }
    
    

  3. annotate the methods m_B(), m_C(), m_D() with annoteMe :

    @AnnoteMe(returnKey="antB")
    public String m_B(){
    return "B";
    }
    @AnnoteMe(returnKey="antC")
    public String m_C(){
    return "C";
    }
    @AnnoteMe(returnKey="antD")
    public String m_D(){
    return "D";
    }





  4. create a main-class that search classes via the Reflection API in the same package (TestClass should be found):

    public static Class[] getClasses(String packageName)
    throws ClassNotFoundException, IOException {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    assert classLoader != null;
    String path = packageName.replace('.', '/');
    Enumeration<URL> resources = classLoader.getResources(path);
    List<File> dirs = new ArrayList<File>();
    while (resources.hasMoreElements()) {
    URL resource = resources.nextElement();
    dirs.add(new File(resource.getFile()));
    }
    ArrayList<Class> classes = new ArrayList<Class>();
    for (File directory : dirs) {
    classes.addAll(findClasses(directory, packageName));
    }
    return classes.toArray(new Class[classes.size()]);
    }
    Invoke getClasses:
    Class[] cls=getClasses(Main.class.getPackage().getName());
  5. search the methods annotated with "annoteMe" in the found classes (hier TestClass) via the Reflection API and invoke them :


    public static List<Method> getMethods(Class[] classes,Class annotat){
    List<Method> methodsAnnoted=new ArrayList<Method>();
    for(int i=0;i<classes.length;i++){
    Method[] subMs=classes[i].getMethods();
    System.out.println(subMs.length);
    for(int j=0;j<subMs.length;j++){
    //System.out.println(Calendar.getInstance().ge+"mmm : "+subMs[j].getName());
    Annotation[] ans=Arrays.asList(subMs).get(j).getAnnotations();
    for(Annotation ant:ans) {
    if( ant.annotationType() == annotat ) {
    methodsAnnoted.add(subMs[j]);
    }
    }
    }
    }
    return methodsAnnoted;
    }


    Inv
    oke getMethods:
    List<Method> annotatedMethods=getMethods(cls, AnnoteMe.class);




No comments:

Post a Comment