Eclipse IDE -- 类型安全:未经检查的调用

Eclipse IDE -- Type safety: unchecked invocation

我不明白为什么 Eclipse IDE 会给我一个关于此方法的 "Type safety: unchecked invocation" 警告。

    public static Object decode(String _json, Class _class) {
        Gson gson = new Gson();
        return gson.fromJson(_json, _class);
    }

我对代码进行了大量编辑以说明问题。代码工作正常;只是 Eclipse 有奶牛。 "return" 行是带有警告的行。

原样,该方法只是 Gson's object instantiation method 的包装器。 我做错了什么吗?我需要以不同的方式处理某些事情吗?

是因为return是Object类型吗?它必须像那样通用才能 return 所有类型的 类,对吗?

问题出在 Class 参数 _class 上。

Class 需要有一个通用参数:Class<? extends Type> 表示扩展 "Type" 的任何 Class(用您需要的普通超级 class 替换类型)

您可以指定 Class<?>,这与 Class<? extends Object> 相同,表示任何 class - 警告应该消失

Warning: Unchecked invocation execute(new CallableStatementCreator(){}, new CallableStatementCallback(){}) of the generic method execute(CallableStatementCreator, CallableStatementCallback) of type JdbcTemplate.

这是如何抑制警告的答案:

jdbcTemplateObject.execute(new CallableStatementCreator() {                 
                public CallableStatement createCallableStatement(Connection con) throws SQLException {                  
                    cs= con.prepareCall("{?=call Package(?)}");                             
                    cs.registerOutParameter(1, Types.INTEGER); 
                    cs.setString(2,argument);
                    cs.executeUpdate();             
                    int return_Value = cs.getInt(1);                    
                    logger.info("Package Return Value : "+return_Value); 
                    return cs;
                }
            }, new CallableStatementCallback<Object>() {                
                public Object doInCallableStatement(CallableStatement cs) throws SQLException {
                    cs.execute();
                    return null;
                }

            });

CallableStatementCallback 旁边添加 <Object> 消除警告