mardi 4 septembre 2018

Getting the type of method parameters in reflection

I work with reflection. And I need to get the parameter method of my set () entity to call the corresponding fill method in accordance with the type.

try{
            Class clazz = aClass.getClass();
            Object object = clazz.newInstance();
            while (clazz != Object.class){
                Method[] methods = clazz.getDeclaredMethods();
                for (Method method : methods){
                    if (method.isAnnotationPresent(ProductAnnotation.class)) {
                        Object[] strategyObj =  new Object[1];
                        if (method.getReturnType().getName().equals("int")) {              //reflexion never comes in if
                            strategyObj[0] = strategy.setInt(bundle.getString(method.getName().substring(3).toLowerCase()));
                            method.invoke(object, strategyObj);
                        }if (method.getParameterTypes().getClass().getName().equals("String")){   //reflexion never comes in if
                            strategyObj[0] = strategy.setString(bundle.getString(method.getName().substring(3).toLowerCase()));
                            method.invoke(object, strategyObj);
                        }
                    }
                }
                clazz = clazz.getSuperclass();
            }
            return (FlyingMachine) object;
        } catch (IllegalAccessException | IOException | InvocationTargetException | InstantiationException e) {
            e.printStackTrace();
        }
        return null;
    }

I tried to use getReturnedType () and getParametrTypes (), but the reflexion does not enter any condition. What was I wrong about?

My Annotation

@Retention(RetentionPolicy.RUNTIME)
@Target(value = ElementType.METHOD)
public @interface ProductAnnotation {
    String value();
}





Aucun commentaire:

Enregistrer un commentaire