Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.ArrayList;
- import java.lang.reflect.Method;
- import java.lang.reflect.Constructor;
- import java.lang.reflect.InvocationTargetException;
- public class Starter implements java.util.function.Consumer<String> {
- public void accept(String nazwaKlasy) {
- int i;
- Class<?> reflectClass = null;
- try {
- reflectClass = Class.forName(nazwaKlasy);
- } catch (ClassNotFoundException e) {}
- Constructor konstruktorKlasy = null;
- try {
- konstruktorKlasy = reflectClass.getConstructor();
- } catch (NoSuchMethodException e) {}
- Object obiektKlasy = null;
- try {
- obiektKlasy = konstruktorKlasy.newInstance();
- } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {}
- ArrayList<Method> listaMetod = new ArrayList<Method>(Arrays.asList(reflectClass.getMethods()));
- for (Method method : listaMetod) {
- System.out.println("Nazwy metod: " + method.getName() + "\n");
- ArrayList<Class> parametry = new ArrayList<>(Arrays.asList(method.getParameterTypes()));
- MethodToStart methodToStart = method.getAnnotation(MethodToStart.class);
- MethodDisabled methodDisabled = method.getAnnotation(MethodDisabled.class);
- try {
- if (methodToStart != null && methodDisabled == null) {
- int liczbaUruchomien = methodToStart.value();
- if (parametry.size() == 0) {
- for (i = 0; i < liczbaUruchomien; i++) {
- method.invoke(obiektKlasy);
- }
- } else if (parametry.size() == 1) {
- StringParameter parametrString = method.getAnnotation(StringParameter.class);
- String string = parametrString.value();
- for (i = 0; i < liczbaUruchomien; i++) {
- method.invoke(obiektKlasy, string);
- }
- }
- }
- } catch (IllegalAccessException | InvocationTargetException e) {}
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement