j-- java代码dead code_java – try-with-resources中的死代码警告,但不...

下面的代码使用Java 8中引入的try-with-resources结构.偶然的Throw()方法被声明为抛出OccasionalException,Resource的close()方法抛出一个CloseException. Eclipse(版本:Neon Release(4.6.0),Build id:20160613-1800)在标有//死代码的行上添加一个警告,表示该分支是死代码.隐含地,Eclipse确认标有//活动代码的行不是死代码.

Object tryWithResources() throws OccasionalException {

Object value = null;

try (Resource resource = new Resource()) {

occasionallyThrow();

value = new Object();

}

catch (CloseException e) {

if (value == null) {

// alive code

}

else {

// dead code

}

}

return value;

}

我很困惑.如果偶然的Throw()抛出其OccasionalException,那么try-with-resources应该将其作为主要异常捕获,然后尝试关闭资源.如果关闭资源会抛出一个CloseException,那么它将在OccasionalException下被抑制,因此不会捕获CloseException.因此,唯一需要捕获的CloseException是在try中的块成功完成时,这意味着该值为非null.所以看起来“死代码”实际上是活着的,而“活着代码”实际上已经死了.我不确定编译器实际上应该在这里识别出什么,但至少,这里的“死代码”似乎不应该被称为死.

更复杂的是,不使用try-with-resources表单的翻译表单根本没有标记任何死代码警告. (我非常有信心,基于14.20.3.2. Extended try-with-resources,我的翻译是正确的,但如果这里有错误,我不会感到惊讶……)

Object expandedTry() throws OccasionalException {

Object value = null;

try {

Resource resource = new Resource();

Throwable $primary = null;

try {

occasionallyThrow();

value = new Object();

}

catch (Throwable t) {

$primary = t;

throw t;

}

finally {

if (resource != null) {

if ($primary != null) {

try {

resource.close();

}

catch (Throwable $suppressed) {

$primary.addSuppressed($suppressed);

}

}

else {

resource.close();

}

}

}

}

catch (CloseException e) {

if (value == null) {

// alive (not dead!)

}

else {

// alive

}

}

return value;

}

我是否遗漏了一些东西会使if-else中的任何一个分支死于其中一个,而不是另一个?

完整代码

这是包含辅助异常类型,Resource类和顶级类定义的完整代码.

public class TestTryWithResources {

/** Exception thrown by Resource's close() method */

@SuppressWarnings("serial")

static class CloseException extends Exception {}

/** AutoCloseable declared to throw a CloseException */

static class Resource implements AutoCloseable {

@Override

public void close() throws CloseException {}

}

/** An occasionally thrown exception */

@SuppressWarnings("serial")

static class OccasionalException extends Exception {}

/** Method declared to throw an occasional exception */

void occasionallyThrow() throws OccasionalException {}

/*

* Method using try-with-resources. Eclipse warns that the

* portion marked with "// dead code" is Dead code.

*/

Object tryWithResources() throws OccasionalException {

Object value = null;

try (Resource resource = new Resource()) {

occasionallyThrow();

value = new Object();

}

catch (CloseException e) {

if (value == null) {

// alive code

}

else {

// dead code

}

}

return value;

}

/*

* Method not using try-with-resources. This is the translation

* of the try-with-resources in tryWithResources, according to

* [14.20.3 try-with-resources][1]. Eclipse does not warn about

* any of the code being Dead code.

*

* [1]: https://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.20.3

*/

Object expandedTry() throws OccasionalException {

Object value = null;

try {

Resource resource = new Resource();

Throwable $primary = null;

try {

occasionallyThrow();

value = new Object();

}

catch (Throwable t) {

$primary = t;

throw t;

}

finally {

if (resource != null) {

if ($primary != null) {

try {

resource.close();

}

catch (Throwable $suppressed) {

$primary.addSuppressed($suppressed);

}

}

else {

resource.close();

}

}

}

}

catch (CloseException e) {

if (value == null) {

// alive

}

else {

// alive

}

}

return value;

}

}

回应评论

Amin J’s answer建议在设置值之后使用资源来改变Eclipse的代码分析.但这不起作用.在使用资源之后,例如通过打印它,Luna和Neon中仍然存在死码警告:

e3cf9ec856a6016413b016045ee56a75.png

0e2ac1ac0191e5fbfc01832a0343c7bd.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值