java - 为什么 ant 中的 javac 使用不兼容的类型构建脚本错误

标签 java spring ant

我有一个 ant 构建脚本,该脚本出错并显示以下消息。

javac incompatible types by ant build

ProductDao.java:41: incompatible types
found   : java.lang.Object
required: java.util.List<com.sample.dto.Product>
            listProductsIds = jdbc.execute("{ call find_Product_id(?,?,?,?,?) }",
                                        ^
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error

但是如果我在我的 eclipse IDE 中测试它一切正常。

我正在使用 Spring JdbcTemplate,这是我定义调用的方式。

    @SuppressWarnings("unchecked")
    public List<Product> getProducts(final String parent, final String tagDesc,
            final int pageSize, final int pageNo, 
            final String userId, final int maxrowcount) {

        List<Product> listProductIds = new ArrayList<Product>();

        listProductIds = jdbc.execute("{ call find_Product_id(?,?,?,?,?) }",
                new CallableStatementCallback() {

                    public Object doInCallableStatement(
                            CallableStatement callableStatement)
                            throws SQLException, DataAccessException {
                        callableStatement.setString(1, parent);
                        callableStatement.setString(2, tagDesc);
                        callableStatement.setInt(3, pageSize);
                        callableStatement.setInt(4, pageNo);
                        callableStatement.setString(5, userId);
                        callableStatement.execute();

最佳答案

看起来 Eclipse 的编译器在这里过于宽容,并且编译了它不应该做的事情,而 javac(由 Ant 使用)则更加严格。 jdbc.execute 方法,正如您在此处使用的那样,返回 Object,您不能将其分配给 List

您已经抑制了代码中的警告,这也无济于事,因此您实际上忽略了告诉您出了什么问题的消息。

您需要使用泛型来帮助您,以便使用正确的返回类型:

List<Product> listProductIds = jdbc.execute(
    "{ call find_Product_id(?,?,?,?,?) }",
    new CallableStatementCallback<List<Product>>() {
        public List<Product> doInCallableStatement(CallableStatement callableStatement) throws SQLException, DataAccessException {
            ....
        }
    }
);

请注意,CallableStatementCallback 的通用类型签名与 doInCallableStatement 的返回类型以及 listProductIds 变量的类型匹配。您不需要也不应该在此类代码中禁止显示警告。

关于java - 为什么 ant 中的 javac 使用不兼容的类型构建脚本错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5519296/

相关文章:

java - 如何使用 Spring Mobile (spring-mobile-device) Junit 一个 RestController

c++ - ant cpptask 与 ivy

java - 如何构建具有相同源代码但不同资源的不同 APK

Spring Boot EnvironmentPostProcessor 重写命令行

ant - Gradle的AntBuilder文档在哪里?

Java 8 : CMS does not kick on for old generation even though CMSInitiatingOccupancyFraction specified

java - 响应消息 : javax. mail.MessageRemovedException : can't retrieve message #1 in POP3Message. getContentStream

java - 为什么 tomcat-maven-plugin 试图部署到错误的 URL?

java - 在评估 spring 集成表达式时获取类转换表达式

java - Spring Integration - 外部化 JDBC 查询