简体   繁体   English

在反射中获取方法参数的类型

[英]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. 而且我需要获取我的set()实体的参数方法以根据类型调用相应的fill方法。

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. 我尝试使用getReturnedType ()getParametrTypes () ,但是反射不输入任何条件。 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.

Method::getParameterTypes returns Class[] . Method::getParameterTypes返回Class[]

So your code method.getParameterTypes().getClass() will always return [Ljava.lang.Class . 因此,您的代码method.getParameterTypes().getClass()将始终返回[Ljava.lang.Class try this code: 试试这个代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM