dimanche 10 octobre 2021

How to get return type of Methods in the order Reflection API

When I get values from the array Field[] everything is OK

 List<String> valuesToInsert = new ArrayList<>();
    Field[] field = entity.getClass().getDeclaredFields();
    for (Field field1 : field) {
        field1.setAccessible(true);
        try {
            Object value = field1.get(entity);
            valuesToInsert.add(value.toString());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
    valuesToInsert.remove(0);
    System.out.println("Values to insert : " + valuesToInsert);

and I always get values in the order I pass.

Result is constant : Values to insert : [1, Ivan, Petrov]

But when I do the same with Methods[] I get data in the different orders

List<String> returnTypes = new ArrayList<>();
    for (Method method : methods) {

        if (method.getName().startsWith(GET_PREFIX) || method.getName().startsWith(IS_PREFIX)) {
            returnTypes.add(String.valueOf(method.getReturnType()));
        }
    }
    returnTypes.remove(0);
    System.out.println("RETURN TYPES  : " + returnTypes);

And I also need to get RETURN TYPES : [int, class java.lang.String, class java.lang.String] always.

But I get it always in the different orders, how can I do it in the order I want?





Aucun commentaire:

Enregistrer un commentaire