Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java枚举匿名内部类和反射_Java_Reflection_Enums - Fatal编程技术网

Java枚举匿名内部类和反射

Java枚举匿名内部类和反射,java,reflection,enums,Java,Reflection,Enums,我有一个具有匿名内部类的枚举,如: public enum Status { PRELIMINARY() { @Override boolean process() { return true; } SUBMITTED() { @Override boolean process() { return false; } abstract boolean process(); } 我有一

我有一个具有匿名内部类的枚举,如:

public enum Status {

    PRELIMINARY() {
    @Override
    boolean process() {
        return true;
    }

    SUBMITTED() {
    @Override
    boolean process() {
        return false;
    }

    abstract boolean process();

}
我有一个这样的模特

public class Foo {

    private Status status;

    public void setStatus(Status status) {
        this.status = status;
    }
}
我需要使用反射来设置Foo.status,如:

当Status不包含内部类并且是一个简单的枚举时,这种方法可以工作,但是对于上面的Status类,它将抛出一个NoSuchMethodException。这是因为my value的类将是package.Status$1,而不是package.Status


这有什么好办法吗

您只需更改查找所需方法的方法。类似于以下的方法应该可以工作:

private static @Nullable Method findMethod(Class<?> klass,
                                           final String methodName,
                                           final Object... args) {

    @Nullable Method candidate = null;

    classSearch:
    while (klass != null) {

        // Check all the class' methods for a matching one.
        methodSearch:
        for (final Method method : klass.getDeclaredMethods()) {
            if (!method.getName().equals(methodName)) continue;

            final Class<?>[] parameterTypes = method.getParameterTypes();
            if (parameterTypes.length != args.length) continue;

            // Check all parameters can come from the given args.
            for (int i = 0; i < args.length; i++) {
                if (!parameterTypes[i].isInstance(args[i])) continue methodSearch;
            }

            candidate = method;
            break classSearch;
        }

        // No matching method, check super class.
        klass = klass.getSuperclass();
    }

    // May be 'null' if no match was found.
    // Throw an Exception if this isn't a valid outcome in your case.
    return candidate;
}

将此链接到现有的代码调用。如果返回的方法不为null,则对其进行调用将获得所需的结果。

不要太挑剔,但是示例代码缺少一些括号,并且您的反射示例似乎完全不相关,因为没有与调用相对应的方法。谢谢Dave,我已经更新了代码,以更好地反映我的问题。也许能帮你。然而,我想问你为什么在这里使用反射,因为你似乎对你提前调用的方法非常了解。。。在野外,我生成一个DynamicAsper报告,用户可以在其中指定任意列。
private static @Nullable Method findMethod(Class<?> klass,
                                           final String methodName,
                                           final Object... args) {

    @Nullable Method candidate = null;

    classSearch:
    while (klass != null) {

        // Check all the class' methods for a matching one.
        methodSearch:
        for (final Method method : klass.getDeclaredMethods()) {
            if (!method.getName().equals(methodName)) continue;

            final Class<?>[] parameterTypes = method.getParameterTypes();
            if (parameterTypes.length != args.length) continue;

            // Check all parameters can come from the given args.
            for (int i = 0; i < args.length; i++) {
                if (!parameterTypes[i].isInstance(args[i])) continue methodSearch;
            }

            candidate = method;
            break classSearch;
        }

        // No matching method, check super class.
        klass = klass.getSuperclass();
    }

    // May be 'null' if no match was found.
    // Throw an Exception if this isn't a valid outcome in your case.
    return candidate;
}