Java反射与方法声明不一致

发布于 2024-10-08 23:25:15 字数 8282 浏览 1 评论 0原文

抱歉这个问题太长了。我是 Java 新手,我遇到了一些真正难倒我的事情。我对 Java 还很陌生,我什至还不知道所有术语,所以请耐心等待;我有大约 3 年的 PHP 经验(主要是过程性的,而不是 OO),但很少有 Java 经验。我也知道使用 System.out.println 进行调试是错误的方法,但它有效,而且是我所习惯的(如果必须的话,请在此处插入有关 PHP 程序员的笑话)。我仍在尝试弄清楚如何使用 NetBeans 调试器。

我正在努力向使用 Struts (1.x) 的 Web 应用程序添加一项功能。我遇到的问题似乎是一个方法被声明为想要将 String 传递给它,但是对该方法进行反射表明它需要 String[] (字符串数组)。我受到限制,因为我无法真正对应用程序进行重大结构更改,当然我必须确保我不会破坏当前正在运行的应用程序中的任何内容,所以我试图使我的已经存在的环境中的变化。所以,对于问题...

这里是声明该方法的位置(从这些内容中删除了许多行以仅显示我希望的相关位):

AEReportBean.java:

public class AEReportBean {
    private String selectedDownloadFields = null;

    public String getSelectedDownloadFields() {
        return selectedDownloadFields;
    }

    // Note that there is no overloading of this function anywhere, this is the only declaration.
    public void setSelectedDownloadFields(String selectedDownloadFields) {
        this.selectedDownloadFields = selectedDownloadFields;
    }
}

当用户单击表单上的“提交”时,它会被处理作者:AEReportSubmitAction.java:

public class AEReportSubmitAction extends BaseAction {
    public ActionForward doExecute(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response
        ) throws Exception {
        // This works fine, the paramater is getting passed in the request:
        System.out.println("URL parameter: " + request.getParameter("selectedDownloadFields");

        AEReportBean bean = new AEReportBean(request.getLocale(), 0);

        PropertyUtil.setAllFromRequest(request, bean);
        // This prints "Null", meaning the setAllFromRequest line above is failing to set this property.
        System.out.println("AEReportSubmitAction.java - bean.getSelectedDownloadFields() after setAllFromRequest: " + bean.getSelectedDownloadFields());
    }
}

PropertyUtil.setAllFromRequest() 是神奇的地方,也是真正的问题发生的地方:

public class PropertyUtil {
    /**
     * Takes all the parameters from the request object and if there's a matching
     * mutator method in the bean, sets it
     */
    static public void setAllFromRequest(ServletRequest request, Object out) {
        // Iterate through all the request parameter names and try to set each one.
        for (Enumeration parameterNames = request.getParameterNames(); parameterNames.hasMoreElements();) {
            String name = (String) parameterNames.nextElement();
            try {
                PropertyUtil.setSimpleProperty(out, name, request.getParameter(name));
            }
            catch (Exception e) {
                log.info("Exception while setting properties from the Request. parameterName=" + name, e);
            }
        }
    }

    /**
     * Sets the property from an object using the object's mutator method.
     * Assumes naming conventions for accessor methods
     * @param bean the object to get the property from
     * @param property the name of the property to obtain
     * @param newProperty the object to set
     */
    // NOTE: This just seems to be a wrapper for the method below it...
    static public void setSimpleProperty(Object bean, String property, Object newProperty) throws Exception {
        PropertyUtil.setSimpleProperty(bean, property, newProperty, null);
    }

    /**
     * Sets the property from an object using the object's mutator method.
     * Assumes naming conventions for accessor methods
     * @param bean the object to get the property from
     * @param property the name of the property to obtain
     * @param newProperty the object to set
     */
    static public void setSimpleProperty(Object bean, String property, Object newProperty, Class type) throws Exception {
        // Capitalize the first letter in the property and append "set" to the front
        String methodName = "set" + property.substring(0, 1).toUpperCase() + property.substring(1);
        Method method;

        Class[] parameters;

        // If the Type was passed in when this method was called, simply add it to the Class array.
        if (type != null) {
            parameters = new Class[]{type};
        }
        // If the Type was not specified, determine the Type's class by calling getClass() on it; that class will be used below to call the appropriate setter method.
        else {
            parameters = new Class[]{newProperty.getClass()};
        }

        // Here's the reflection problem...
        // Iterate through all the methods in the bean.  If the method is named "setSelectedDownloadFields", print out some info about it.
        for (Method m : bean.getClass().getMethods()) {
            if (m.getName().equals("setSelectedDownloadFields")) {
                // newProperty is the incoming data that ultimately comes from the HTML form field.
                System.out.println("newProperty.getClass(): " + newProperty.getClass()); // Prints "class java.lang.String"

                    // Added for Cameron Skinner in comments.
                    System.out.println("m.toGenericString: " + m.toGenericString()); // Prints "public void com.[company deleted].bean.AEReportBean.setSelectedDownloadFields(java.lang.String[])"

                System.out.println("m.getName(): " + m.getName()); // Prints "setSelectedDownloadFields"
                System.out.println("parameters:");
                for (Class c : m.getParameterTypes()) {
                    System.out.println("--c.getCanonicalName(): " + c.getCanonicalName()); // Prints "java.lang.String[]"
                    System.out.println("--c.getName(): " + c.getName()); // Prints "[Ljava.lang.String;"
                }
            }
        }


        // And here's where it fails...
        try {
            System.out.println("bean.getClass(): " + bean.getClass());  // Prints "class com.[company deleted].bean.AEReportBean"
            System.out.println("methodName: " + methodName); // Prints "setSelectedDownloadFields"
            System.out.println("for (Class p : parameters):");
            for (Class p : parameters) {
                System.out.println("--p.getCanonicalName(): " + p.getCanonicalName()); // Prints "java.lang.String"
            }

            // Here it looks for a method called, effectively, AEReportBean.setSelectedDownloadFields(String s), but above we see that reflection is showing it as AEReportBean.setSelectedDownloadFields(String[] s), so the try block fails.
            method = bean.getClass().getMethod(methodName, parameters);
        }
        catch (NoSuchMethodException e) {
            // All lines below here also fail until it bombs out with the exception at the bottom...

            // If no method can be found, then see if it's a primitive type that
            // has been wrapped
            Class valueClass = newProperty.getClass();
            //System.out.println("valueClass.toString() = " + valueClass.toString());
            try {
                if (valueClass.equals(Integer.class)) {
                    method = bean.getClass().getMethod(methodName, new Class[]{int.class});
                }
                else if (valueClass.equals(Double.class)) {
                    method = bean.getClass().getMethod(methodName, new Class[]{double.class});
                }
                else if (valueClass.equals(Long.class)) {
                    method = bean.getClass().getMethod(methodName, new Class[]{long.class});
                }
                else if (valueClass.equals(Float.class)) {
                    method = bean.getClass().getMethod(methodName, new Class[]{float.class});
                }
                else {
                    throw new Exception(e.getMessage());
                }
            }
            catch (NoSuchMethodException ex) {
                throw new Exception(ex.getMessage());
            }
        }

        // If it had gotten to this point, it would call the method with the appropriate parameters, and the property would be set.
        try {
            // Now execute the method
            method.invoke(bean, new Object[]{newProperty});
        }
        catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
    }
}

我真的不知道我在这里错过了什么,但一定有一些东西。同一页面上的其他 HTML 表单元素可以完美运行。如果需要更多信息,请告诉我。谢谢!

Sorry about the length of this question. I'm new to Java and I've come across something that is really stumping me. I'm so new to Java that I don't even know all the terminology yet, so please bear with me; I have about 3 years of PHP experience (mostly procedural, not OO), but very little Java. I'm also aware that debugging with System.out.println is the Wrong Way to do it, but it works and it's what I'm used to (insert joke about PHP programmers here if you must). I'm still trying to figure out how to use the NetBeans debugger.

I'm working on adding a feature to a web application that uses Struts (1.x). The problem I'm having seems to be that a method is declared to want a String passed to it, but doing Reflection on that method says it wants String[] (a string array). I'm constrained in that I can't really make major structural changes to the app, and of course I have to make sure that I don't break anything in the app that is currently working, so I'm trying to make my changes in the context of what's already there. So, to the problem...

Here is where the method is declared (many many lines cut from these to show only what I hope are the relevant bits):

AEReportBean.java:

public class AEReportBean {
    private String selectedDownloadFields = null;

    public String getSelectedDownloadFields() {
        return selectedDownloadFields;
    }

    // Note that there is no overloading of this function anywhere, this is the only declaration.
    public void setSelectedDownloadFields(String selectedDownloadFields) {
        this.selectedDownloadFields = selectedDownloadFields;
    }
}

When the user clicks Submit on the form, it gets handled by AEReportSubmitAction.java:

public class AEReportSubmitAction extends BaseAction {
    public ActionForward doExecute(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response
        ) throws Exception {
        // This works fine, the paramater is getting passed in the request:
        System.out.println("URL parameter: " + request.getParameter("selectedDownloadFields");

        AEReportBean bean = new AEReportBean(request.getLocale(), 0);

        PropertyUtil.setAllFromRequest(request, bean);
        // This prints "Null", meaning the setAllFromRequest line above is failing to set this property.
        System.out.println("AEReportSubmitAction.java - bean.getSelectedDownloadFields() after setAllFromRequest: " + bean.getSelectedDownloadFields());
    }
}

PropertyUtil.setAllFromRequest() is where the magic, and the real problem, happens:

public class PropertyUtil {
    /**
     * Takes all the parameters from the request object and if there's a matching
     * mutator method in the bean, sets it
     */
    static public void setAllFromRequest(ServletRequest request, Object out) {
        // Iterate through all the request parameter names and try to set each one.
        for (Enumeration parameterNames = request.getParameterNames(); parameterNames.hasMoreElements();) {
            String name = (String) parameterNames.nextElement();
            try {
                PropertyUtil.setSimpleProperty(out, name, request.getParameter(name));
            }
            catch (Exception e) {
                log.info("Exception while setting properties from the Request. parameterName=" + name, e);
            }
        }
    }

    /**
     * Sets the property from an object using the object's mutator method.
     * Assumes naming conventions for accessor methods
     * @param bean the object to get the property from
     * @param property the name of the property to obtain
     * @param newProperty the object to set
     */
    // NOTE: This just seems to be a wrapper for the method below it...
    static public void setSimpleProperty(Object bean, String property, Object newProperty) throws Exception {
        PropertyUtil.setSimpleProperty(bean, property, newProperty, null);
    }

    /**
     * Sets the property from an object using the object's mutator method.
     * Assumes naming conventions for accessor methods
     * @param bean the object to get the property from
     * @param property the name of the property to obtain
     * @param newProperty the object to set
     */
    static public void setSimpleProperty(Object bean, String property, Object newProperty, Class type) throws Exception {
        // Capitalize the first letter in the property and append "set" to the front
        String methodName = "set" + property.substring(0, 1).toUpperCase() + property.substring(1);
        Method method;

        Class[] parameters;

        // If the Type was passed in when this method was called, simply add it to the Class array.
        if (type != null) {
            parameters = new Class[]{type};
        }
        // If the Type was not specified, determine the Type's class by calling getClass() on it; that class will be used below to call the appropriate setter method.
        else {
            parameters = new Class[]{newProperty.getClass()};
        }

        // Here's the reflection problem...
        // Iterate through all the methods in the bean.  If the method is named "setSelectedDownloadFields", print out some info about it.
        for (Method m : bean.getClass().getMethods()) {
            if (m.getName().equals("setSelectedDownloadFields")) {
                // newProperty is the incoming data that ultimately comes from the HTML form field.
                System.out.println("newProperty.getClass(): " + newProperty.getClass()); // Prints "class java.lang.String"

                    // Added for Cameron Skinner in comments.
                    System.out.println("m.toGenericString: " + m.toGenericString()); // Prints "public void com.[company deleted].bean.AEReportBean.setSelectedDownloadFields(java.lang.String[])"

                System.out.println("m.getName(): " + m.getName()); // Prints "setSelectedDownloadFields"
                System.out.println("parameters:");
                for (Class c : m.getParameterTypes()) {
                    System.out.println("--c.getCanonicalName(): " + c.getCanonicalName()); // Prints "java.lang.String[]"
                    System.out.println("--c.getName(): " + c.getName()); // Prints "[Ljava.lang.String;"
                }
            }
        }


        // And here's where it fails...
        try {
            System.out.println("bean.getClass(): " + bean.getClass());  // Prints "class com.[company deleted].bean.AEReportBean"
            System.out.println("methodName: " + methodName); // Prints "setSelectedDownloadFields"
            System.out.println("for (Class p : parameters):");
            for (Class p : parameters) {
                System.out.println("--p.getCanonicalName(): " + p.getCanonicalName()); // Prints "java.lang.String"
            }

            // Here it looks for a method called, effectively, AEReportBean.setSelectedDownloadFields(String s), but above we see that reflection is showing it as AEReportBean.setSelectedDownloadFields(String[] s), so the try block fails.
            method = bean.getClass().getMethod(methodName, parameters);
        }
        catch (NoSuchMethodException e) {
            // All lines below here also fail until it bombs out with the exception at the bottom...

            // If no method can be found, then see if it's a primitive type that
            // has been wrapped
            Class valueClass = newProperty.getClass();
            //System.out.println("valueClass.toString() = " + valueClass.toString());
            try {
                if (valueClass.equals(Integer.class)) {
                    method = bean.getClass().getMethod(methodName, new Class[]{int.class});
                }
                else if (valueClass.equals(Double.class)) {
                    method = bean.getClass().getMethod(methodName, new Class[]{double.class});
                }
                else if (valueClass.equals(Long.class)) {
                    method = bean.getClass().getMethod(methodName, new Class[]{long.class});
                }
                else if (valueClass.equals(Float.class)) {
                    method = bean.getClass().getMethod(methodName, new Class[]{float.class});
                }
                else {
                    throw new Exception(e.getMessage());
                }
            }
            catch (NoSuchMethodException ex) {
                throw new Exception(ex.getMessage());
            }
        }

        // If it had gotten to this point, it would call the method with the appropriate parameters, and the property would be set.
        try {
            // Now execute the method
            method.invoke(bean, new Object[]{newProperty});
        }
        catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
    }
}

I really don't know what I'm missing here, but there must be something. Other HTML form elements on the same page work perfectly. Please let me know if any more information is needed. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呆头 2024-10-15 23:25:16

代码结果不会说谎。这基本上表明这门课不是你所期望的那样。您的项目的类路径中有多个不同版本的 AEReportBean 类(可能位于不同的包中),并且导入了错误的类或在类加载中获得了优先权。在 Netbeans 中进行类型/类搜索,以在类路径中按给定名称查找所有类(我不使用 Netbeans,但在 Eclispe 中它是 Ctrl+Shift+T,Netbeans 等效项可能是 Alt+Shift+O )

更新:另一个可能的原因是 Netbeans 没有在保存源文件时自动构建项目(IDE 应在构建期间创建/刷新 .class 文件)。查看设置中的某处。

The code results doesn't lie. It's basically telling that the class isn't what you expect it to be. You've multiple AEReportBean classes of different versions in the classpath of your project, possibly in different packages, and the wrong one has been imported or got precedence in classloading. Do a Type/Class search in Netbeans to find all classes by the given name in the classpath (I don't do Netbeans, but in Eclispe it's Ctrl+Shift+T, the Netbeans equivalent is probably Alt+Shift+O).

Update: another possible cause is that Netbeans didn't build the project automatically on save of the source file (an IDE should create/refresh .class files during build). Look somewhere in settings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文