插曲一 解決-source 1.4 中不支持註釋和泛型問題

轉載:Eclipse中報錯的解決方案<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Eclipse中報錯要了解詳細信息,請使用-xlint:unchecked重新編譯的解決方案

1、遇到問題

ant執行jasperreportsamples\charts示例的build.xml時,無法編譯,提示錯誤如下:
javac:
[javac]Compiling2sourcefilestoE:\jiangcm\workspace-forict-myeclipse\jasperreports\demo\samples\charts
[javac]
注意:E:\jiangcm\workspace-forict-myeclipse\jasperreports\demo\samples\charts\ChartsApp.java使用了未經檢查或不安全的操作。
[javac]
注意:要了解詳細信息,請使用-Xlint:unchecked重新編譯。

2、查了一下資料,知道是泛型的原因,網上查到的解決方案主要有以下幾種

1.編譯時帶上參數-source1.4
2.
使用@SupressWarnings("unchecked")註釋

3.
更新你的代碼,使用List<Object>.List<Object>的實例能接受任何類型的對象,就像是一個原型List。然而,編譯器不會報錯。
(
以上三種方法來源:http://www.matrix.org.cn/resource/article/43/43634_java_generics.html

4.
如果你用的Ant,使用build.xml編譯的話,可以右擊build.xml文件,
-->
執行-->構成和執行,選擇參數,在《程序參數》裏面輸入-xlint:unchecked即可;

5.
找到build.xml裏面類似的語句,加上一句話:

<!--JavaCompile-->
<targetname="compile"depends="init">
<javacsrcdir="src"destdir="${classdir}"
deprecation="on"encoding="
Windows-31J"debug="on"includes="**/jp/**">
<compilerargvalue="-Xlint:unchecked"/>`<!--
就是這句話!!-->
<classpathrefid="project.class.path"/>
</javac>
</target>
(以上兩種方法來源:http://www.itwenku.com/java/12/47796.htm

3、自己的試驗與結論

第一種:

編譯時帶上參數-source1.4”使用方法:
找到build.xml裏面類似的語句,加上一句話:
<javacsrcdir="${src.dir}"destdir="${classes.dir}">
<classpathrefid="classpath"/>
<compilerargline="-source1.5"/>`<!--
就是這句話!注意與第五種方式不同的是line標籤而不是value-->
</javac>
編譯通過,charts下的報表出現了!


第二種:
使用@SupressWarnings("unchecked")註釋
ChartsApp.javamain函數前加上了這個註釋,但是沒有起作用,提示錯誤依然是“[javac]注意:要了解詳細信息,請使用-Xlint:unchecked重新編譯。,怎麼回事呢?

在一篇文章中找到了答案:
http://www.matrix.org.cn/resource/article/43/43864_Generic_Types.html
在撰寫本文時候,javac並不支持@SuppressWarnings的註解。期望在Java5.1中得到支持。

第三種:

更改代碼爲泛型使用方式:
原來的代碼:
Mapparameters=newHashMap();
parameters.put("MaxOrderID",newInteger(12500));

更改後的代碼:
Map<String,Integer>parameters=newHashMap<String,Integer>();
parameters.put("MaxOrderID",newInteger(12500));
或者:
Map<Object,Object>parameters=newHashMap<Object,Object>();
parameters.put("MaxOrderID",newInteger(12500));

更改後,順利通過編譯,也能看到報表了!!

第四種:
經驗證無效;

第五種:

<javacsrcdir="src"destdir="${classdir}"
deprecation="on"encoding="
Windows-31J"debug="on"includes="**/jp/**">
<compilerargvalue="-Xlint:unchecked"/>`<!--
就是這句話!!-->
<classpathrefid="project.class.path"/>
</javac>

加上"<compilerargvalue="-Xlint:unchecked"/>"後,出現"警告",不過編譯順利通過,也能看到報表了!
編譯提示如下:
[javac]Compiling2sourcefilestoE:\jiangcm\workspace-forict-myeclipse\jasperreports\demo\samples\charts
[javac]E:\jiangcm\workspace-forict-myeclipse\jasperreports\demo\samples\charts\ChartsApp.java:115:
警告:[unchecked]對作爲普通類型java.util.Map的成員的put(K,V)的調用未經檢查
[javac]parameters.put("MaxOrderID",newInteger(12500));
[javac]^
[javac]1
警告

呵呵,答案是豐富多彩的,條條大道通羅馬,這次不僅解決了問題,還找到了四種解決問題的方法。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章