Advertisement
Guest User

Untitled

a guest
Jan 16th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 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 e) {
  26. } catch (IllegalAccessException e) {
  27. } catch (InvocationTargetException e) {}
  28.  
  29.  
  30. ArrayList<Method> listaMetod = new ArrayList<Method>(Arrays.asList(reflectClass.getMethods()));
  31.  
  32. for (Method method : listaMetod) {
  33. System.out.println("Nazwy metod: " + method.getName() + "\n");
  34. ArrayList<Class> parametry = new ArrayList<>(Arrays.asList(method.getParameterTypes()));
  35.  
  36. MethodToStart methodToStart = method.getAnnotation(MethodToStart.class);
  37. MethodDisabled methodDisabled = method.getAnnotation(MethodDisabled.class);
  38.  
  39. try {
  40. if (methodToStart != null && methodDisabled == null) {
  41. int liczbaUruchomien = methodToStart.value();
  42.  
  43. if (parametry.size() == 0) {
  44. for (i = 0; i < liczbaUruchomien; i++) {
  45. method.invoke(obiektKlasy);
  46. }
  47. } else if (parametry.size() == 1) {
  48. StringParameter parametrString = method.getAnnotation(StringParameter.class);
  49. String string = parametrString.value();
  50. for (i = 0; i < liczbaUruchomien; i++) {
  51. method.invoke(obiektKlasy, string);
  52. }
  53. }
  54.  
  55. }
  56. } catch (IllegalAccessException | InvocationTargetException e) {}
  57. }
  58.  
  59. }
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement