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

Java 在反射中获取方法参数的类型,java,reflection,Java,Reflection,我用反思来工作。我需要获取集合实体的参数方法,以便根据类型调用相应的fill方法 try{ Class clazz = aClass.getClass(); Object object = clazz.newInstance(); while (clazz != Object.class){ Method[] methods = clazz.getDeclaredMethods();

我用反思来工作。我需要获取集合实体的参数方法,以便根据类型调用相应的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;
    }
我尝试使用getReturnedType和GetParameterTypes,但反射没有进入任何条件。我错在哪里了

我的注解

@Retention(RetentionPolicy.RUNTIME)
@Target(value = ElementType.METHOD)
public @interface ProductAnnotation {
    String value();
}
应该引起反射的方法。根据方法的类型,调用其中一个方法以进一步处理和填充数据

@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.
方法::getParameterTypes返回类[]

因此,您的代码方法.getParameterTypes.getClass将始终返回[Ljava.lang.Class]。请尝试以下代码:

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

ProductAnnotation是否使用@RetentionRetentionPolicy.RUNTIME对自身进行了注释?确切地说:请向我们显示一个,其中包含无法与反射部分一起工作的代码。您在此处的整个代码都检查该注释,您确定它存在于要检查的方法上吗?!代码的所有其他部分工作正常,提供的注释。Th问题是在反射时获取方法的类型参数。再次:向我们展示您希望使用此反射片段的示例代码。@VIRICH您需要展示调用该方法的代码…以及包含上述片段的方法。非常感谢,这对我有帮助