Created
February 13, 2016 17:00
-
-
Save AnnaBoro/3dda8d5beba1caa27373 to your computer and use it in GitHub Desktop.
@initService
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lesson7_10.reflection; | |
import java.lang.annotation.Annotation; | |
import java.lang.reflect.InvocationTargetException; | |
import java.lang.reflect.Method; | |
public class ApplicationManager { | |
public void runManager(Class sclass) throws IllegalAccessException, InstantiationException, InvocationTargetException { | |
Annotation[] annotations = sclass.getAnnotations(); | |
for (Annotation annotation : annotations) { | |
if (annotation.annotationType().getSimpleName().equalsIgnoreCase("service")) { | |
System.out.println("Class " + sclass.getSimpleName() + " has @Service"); | |
Object o = sclass.newInstance(); | |
Method[] methods = o.getClass().getDeclaredMethods(); | |
for (Method method : methods) { | |
Annotation[] anns = method.getDeclaredAnnotations(); | |
for (Annotation ann : anns) { | |
if (checkMethodAnnotation(ann)) { | |
method.invoke(o, null); | |
} | |
} | |
} | |
} | |
} | |
} | |
public boolean checkMethodAnnotation(Annotation annotation) { | |
if (annotation.toString().equalsIgnoreCase("initService")) { | |
return true; | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment