Created
February 15, 2016 15:21
-
-
Save AnnaBoro/6dd927f4c3cc9419162e to your computer and use it in GitHub Desktop.
+ getService
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<T> { | |
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; | |
} | |
public T getService(Class<T> tClass) throws IllegalAccessException, InstantiationException { | |
return tClass.newInstance(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment