11

由于 Eclipse 未报告“未使用”变量,我有“隐藏”错误,因为它们在断言(前提条件)中使用,例如:

public void method(final String text, ...other parameters) {
  assert StringUtils.isNotBlank(text);
  ...
  // The text variable is never used in the method.
  // If Eclipse had reported the variable as 'unused' I would have noticed that something is wrong with the code.
  ...
}

我想告诉 Eclipse 在检查未使用的变量时忽略断言。我怀疑是否有人会传递一个参数来只对其运行一个断言......另外让我知道 FindBugs 或其他工具是否可以做到这一点。

4

1 回答 1

1

这是我自己尝试用“蛮力”解决方案解决这个问题:

  • 编写一个脚本,复制您的源代码并从中删除所有断言
  • 检查断言更少的副本,就像检查原件一样
  • 从原始源代码中手动删除仅在断言中使用的变量

请注意,JavaDoc 链接变量也存在类似的问题......如果在 JavaDoc 中提到了一个变量,它将被视为已使用(由 Eclipse 和可能的其他 IDE)。

于 2014-05-05T07:39:29.490 回答