我有一个 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/