2011-12-27 140 views
1

我用一個簡單的嘗試,與資源的語句如下所示:編譯失敗的嘗試,與資源

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"));){ 

回答

1

似乎using optional trailing semicolon to terminate resources list in try-with-resources不允許你的JDK版本,it's illegal under JSR 334

入住此documentation

語法:語法產生的對TryStatement在JLS§14.20現有集與擴充:

TryStatement: 
    try ResourceSpecification Block Catchesopt Finallyopt 

Supporting new grammar productions are added: 

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

[合併語法的一個含義是,一試語句必須至少有一個catch子句,一個finally塊和一個資源規範。此外,允許嘗試聲明具有這三個組件中的一個。 注意,它是非法擁有的資源規範尾隨分號]

嘗試刪除最後一個分號。

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