VIRICH
VIRICH

Reputation: 177

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().getTypeName().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();
}

Methods that should cause reflection.Depending on the type of method, call one of these methods for further processing and filling in the data.

@Override
    public int setInt(String title) throws IOException {
        String line = null;
        checkValue = true;
        while (checkValue) {
            System.out.println(title + "-->");
            line = reader.readLine();
            if (line.matches("\\d*")) {
                System.out.println(title + " = " + Integer.parseInt(line));
                checkValue = false;
            } else {
                System.out.println("Wrong value, try again");
                checkValue = true;
            }
        }
        return Integer.parseInt(line);
    }

setString() works exactly the same scheme.

Upvotes: 0

Views: 104

Answers (1)

zhh
zhh

Reputation: 2406

Method::getParameterTypes returns Class[].

So your code method.getParameterTypes().getClass() will always return [Ljava.lang.Class. try this code:

Class[] types = method.getParameterTypes();
if (types.length == 1 && types[0] == String.class) {
    // your second condition...
}

Upvotes: 1

Related Questions