Advertisement
Guest User

Untitled

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