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

标签 java instrumentation java-7 clover try-with-resources

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

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

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

According to the docs ,这个版本的 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"));){

最佳答案

看来using optional trailing semicolon to terminate resources list in try-with-resources您的 JDK 版本不允许, it's illegal under JSR 334

检查这个documentation :

语法: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 

[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 (final CSVWriter w = new CSVWriter(new OutputStreamWriter(r.getOutputStream(), "UTF-8"))){
//do stuff......
}

关于java - Java 7 try-with-resources 的 Clover 检测后编译失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8650213/

相关文章:

java - 检查值只能是 0 或 1,仅使用 if block

Java 将文本文件读取到流中并将其拆分

java - 如何使用没有进一步调用的 JVMTI 代理重新转换执行方法?

java - 获取给定文件夹下文件的所有绝对路径

java - 如何从 HashMap 中删除元素而不出现 ConcurrentModificationException

java - 手动打开 Java 7 自动管理的资源

java - Java中的字符串二分查找

java - 如何用Java打印报告?

android - 单元/仪器测试 Android Gradle。仪器测试不会运行

c# - winform 的 AOP