Java 7 try-with-resources 的 Clover 检测后编译失败

发布于 2024-12-22 20:56:55 字数 1815 浏览 2 评论 0原文

我正在使用一个简单的 try-with-resources 语句,如下所示:

try (final CSVWriter w = new CSVWriter(new OutputStreamWriter(r.getOutputStream(), "UTF-8"));){
    //do stuff......
}

它使用正常的 javac Ant 任务可以正常编译,但是当我首先让 Clover 检测代码时,生成的代码不再编译(请参阅下面包含编译消息)。

根据文档,此版本的 Clover 确实支持 Java 7。其他人遇到过这个问题或者知道问题是什么吗?

Java版本:

java version "1.7.0"
Java(TM) SE Runtime Environment (build pxi3270-20110827_01)
IBM J9 VM (build 2.6, JRE 1.7.0 Linux x86-32 20110810_88604 (JIT enabled, AOT enabled)
J9VM - R26_Java726_GA_20110810_1208_B88592
JIT  - r11_20110810_20466
GC   - R26_Java726_GA_20110810_1208_B88592
J9CL - 20110810_88604)
JCL - 20110809_01 based on Oracle 7b147

Ant任务的输出:

compile:
     [echo] Compiling source code...
    [javac] Compiling 135 source files to /home/*********/WEB-INF/classes
   [clover] Clover Version 3.1.2, built on November 07 2011 (build-842)
   [clover] Loaded from: /home/*******/clover.jar
   [clover] Clover: Commercial License registered to *******.
   [clover] Updating existing database at '/home/********/dist/clover/clover.db'.
   [clover] Processing files at 1.7 source level.
   [clover] Clover all over. Instrumented 135 files (12 packages).
   [clover] Elapsed time = 1.597 secs. (84.534 files/sec, 12,463.369 srclines/sec)
    [javac] /tmp/clover2218935617827048125.tmp/com/****/web/DownloadService.java:232: error: illegal start of type
    [javac]                 __CLR3_1_24ae4aegwpi0zhh.R.inc(5592);try (new java.lang.AutoCloseable() {{__CLR3_1_24ae4aegwpi0zhh.R.inc(5593);}public void close(){}};CSVWriter w = new CSVWriter(new OutputStreamWriter(response.getOutputStream(), "UTF-8"));){

I am using a simple try-with-resources statement like the following:

try (final CSVWriter w = new CSVWriter(new OutputStreamWriter(r.getOutputStream(), "UTF-8"));){
    //do stuff......
}

It compiles fine using a normal javac Ant task, but when I have Clover instrument the code first, the resultant code no longer compiles (see the compilation message included below).

According to the docs, this version of Clover does support Java 7. Has anyone else run across this issue or have any idea what the problem is?

Java version:

java version "1.7.0"
Java(TM) SE Runtime Environment (build pxi3270-20110827_01)
IBM J9 VM (build 2.6, JRE 1.7.0 Linux x86-32 20110810_88604 (JIT enabled, AOT enabled)
J9VM - R26_Java726_GA_20110810_1208_B88592
JIT  - r11_20110810_20466
GC   - R26_Java726_GA_20110810_1208_B88592
J9CL - 20110810_88604)
JCL - 20110809_01 based on Oracle 7b147

Output of Ant task:

compile:
     [echo] Compiling source code...
    [javac] Compiling 135 source files to /home/*********/WEB-INF/classes
   [clover] Clover Version 3.1.2, built on November 07 2011 (build-842)
   [clover] Loaded from: /home/*******/clover.jar
   [clover] Clover: Commercial License registered to *******.
   [clover] Updating existing database at '/home/********/dist/clover/clover.db'.
   [clover] Processing files at 1.7 source level.
   [clover] Clover all over. Instrumented 135 files (12 packages).
   [clover] Elapsed time = 1.597 secs. (84.534 files/sec, 12,463.369 srclines/sec)
    [javac] /tmp/clover2218935617827048125.tmp/com/****/web/DownloadService.java:232: error: illegal start of type
    [javac]                 __CLR3_1_24ae4aegwpi0zhh.R.inc(5592);try (new java.lang.AutoCloseable() {{__CLR3_1_24ae4aegwpi0zhh.R.inc(5593);}public void close(){}};CSVWriter w = new CSVWriter(new OutputStreamWriter(response.getOutputStream(), "UTF-8"));){

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

层林尽染 2024-12-29 20:56:55

似乎使用可选的尾随分号来终止资源您的 JDK 版本不允许 try-with-resources 中的列表,根据 JSR 334,这是非法的。

检查此文档

语法:JLS §14.20 中 TryStatement 的现有语法产生式集是增强:

TryStatement:
    try ResourceSpecification Block Catchesopt Finallyopt 

Supporting new grammar productions are added:

ResourceSpecification:
    ( Resources ) 
Resources:
    Resource 
    Resource ; Resources 
Resource:
    VariableModifiers Type VariableDeclaratorId = Expression 
    Expression 

[组合语法的含义是 try 语句必须至少具有 catch 子句、finally 块和资源规范之一。此外,try 语句允许恰好具有这三个组成部分之一。 请注意,资源规范中尾随分号是非法的。]

尝试删除最后一个分号:

try (final CSVWriter w = new CSVWriter(new OutputStreamWriter(r.getOutputStream(), "UTF-8"))){
//do stuff......
}

It seems that using optional trailing semicolon to terminate resources list in try-with-resources is not allowed with your JDK version, it's illegal under JSR 334.

Check this documentation :

SYNTAX: The existing set of grammar productions for TryStatement in JLS §14.20 is augmented with:

TryStatement:
    try ResourceSpecification Block Catchesopt Finallyopt 

Supporting new grammar productions are added:

ResourceSpecification:
    ( Resources ) 
Resources:
    Resource 
    Resource ; Resources 
Resource:
    VariableModifiers Type VariableDeclaratorId = Expression 
    Expression 

[An implication of the combined grammar is that a try statement must have at least one of a catch clause, a finally block, and a resource specification. Furthermore, it is permissible for a try statement to have exactly one of these three components. Note that it is illegal to have a trailing semi-colon in the resource specification.]

Try to drop the last semicolon :

try (final CSVWriter w = new CSVWriter(new OutputStreamWriter(r.getOutputStream(), "UTF-8"))){
//do stuff......
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文