Advertisement
Guest User

Untitled

a guest
Jan 16th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.ArrayList;
  3. import java.lang.reflect.Method;
  4. import java.lang.reflect.Constructor;
  5. import java.lang.reflect.InvocationTargetException;
  6.  
  7. public class Starter implements java.util.function.Consumer<String> {
  8.  
  9. public void accept(String nazwaKlasy) {
  10. int i;
  11.  
  12. Class<?> reflectClass = null;
  13. try {
  14. reflectClass = Class.forName(nazwaKlasy);
  15. } catch (ClassNotFoundException e) {}
  16.  
  17. Constructor konstruktorKlasy = null;
  18. try {
  19. konstruktorKlasy = reflectClass.getConstructor();
  20. } catch (NoSuchMethodException e) {}
  21.  
  22. Object obiektKlasy = null;
  23. try {
  24. obiektKlasy = konstruktorKlasy.newInstance();
  25. } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {}
  26.  
  27.  
  28.  
  29. ArrayList<Method> listaMetod = new ArrayList<Method>(Arrays.asList(reflectClass.getMethods()));
  30.  
  31. for (Method method : listaMetod) {
  32. System.out.println("Nazwy metod: " + method.getName() + "\n");
  33. ArrayList<Class> parametry = new ArrayList<>(Arrays.asList(method.getParameterTypes()));
  34.  
  35. MethodToStart methodToStart = method.getAnnotation(MethodToStart.class);
  36. MethodDisabled methodDisabled = method.getAnnotation(MethodDisabled.class);
  37.  
  38. try {
  39. if (methodToStart != null && methodDisabled == null) {
  40. int liczbaUruchomien = methodToStart.value();
  41.  
  42. if (parametry.size() == 0) {
  43. for (i = 0; i < liczbaUruchomien; i++) {
  44. method.invoke(obiektKlasy);
  45. }
  46. } else if (parametry.size() == 1) {
  47. StringParameter parametrString = method.getAnnotation(StringParameter.class);
  48. String string = parametrString.value();
  49. for (i = 0; i < liczbaUruchomien; i++) {
  50. method.invoke(obiektKlasy, string);
  51. }
  52. }
  53.  
  54. }
  55. } catch (IllegalAccessException | InvocationTargetException e) {}
  56. }
  57.  
  58. }
  59.  
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement