Skip to content

Instantly share code, notes, and snippets.

@AnnaBoro
Created February 13, 2016 17:00
Show Gist options
  • Save AnnaBoro/3dda8d5beba1caa27373 to your computer and use it in GitHub Desktop.
Save AnnaBoro/3dda8d5beba1caa27373 to your computer and use it in GitHub Desktop.
@initService
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