当我编译以下类时(可在here获取,已添加主要方法以在单个类中重现问题),我在 IntelliJ 或 maven 命令行中收到以下编译器警告:
java: /Users/luigi/var/src/owner/src/test/java/org/aeonbits/owner/multithread/ThreadBase.java uses unchecked or unsafe operations.
java: Recompile with -Xlint:unchecked for details.
然后我将 -Xlint:unchecked 添加到 Maven 以查看详细信息,我得到了这个:
[WARNING] /Users/luigi/var/src/owner/src/test/java/org/aeonbits/owner/multithread/ThreadBase.java:[74,45] unchecked conversion
required: java.util.List<java.lang.Throwable>
found: java.util.List
类源如下(已添加主要内容以在单个类中重现问题):
package org.aeonbits.owner.multithread;
import org.aeonbits.owner.Config;
import org.aeonbits.owner.UtilTest.MyCloneable;
import java.util.ArrayList;
import java.util.List;
import static org.aeonbits.owner.UtilTest.debug;
abstract class ThreadBase<T extends Config> extends Thread implements MyCloneable {
private static long counter = 0;
final long uniqueThreadId = ++counter;
final T cfg;
final Object lock;
final int loops;
final List<Throwable> errors = new ArrayList<Throwable>();
ThreadBase(T cfg, Object lock, int loops) {
this.cfg = cfg;
this.lock = lock;
this.loops = loops;
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
@Override
public void run() {
synchronized (lock) {
try {
lock.wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
}
for (int i = 0; i < loops; i++) {
debug("%s[%d] started loop #%d.\n", getClass().getName(), uniqueThreadId, i);
try {
execute();
} catch (Throwable throwable) {
debug("%s[%d] thrown an error in loop #%d.\n", getClass().getName(), uniqueThreadId, i);
errors.add(throwable);
}
yield();
debug("%s[%d] completed loop #%d.\n", getClass().getName(), uniqueThreadId, i);
}
}
abstract void execute() throws Throwable;
public List<Throwable> getErrors() {
return errors;
}
public static void main(String[] args) {
ThreadBase t = new ThreadBase(null, new Object(), 10) {
@Override
void execute() throws Throwable {
}
};
List<Throwable> errors = t.getErrors();
// ^ compiler reports the warning here!
}
}
其他详细信息:
$ mvn --version
Apache Maven 3.0.4 (r1232337; 2012-01-17 09:44:56+0100)
Maven home: /Users/luigi/opt/apache-maven-3.0.4
Java version: 1.7.0_25, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.7.0_25.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.8.4", arch: "x86_64", family: "mac"
在pom.xml中,我添加了compiler.source和compiler.target 1.5,因为我的库面向jdk 1.5或更高版本:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
<compilerArgument>-Xlint:unchecked</compilerArgument>
</configuration>
</plugin>
</plugins>
</pluginManagement>
有人可以解释一下这里出了什么问题吗?
对我来说,它没有任何意义,因为 t.getErrors() 返回一个列表:
List<Throwable> errors = t.getErrors();
这里出了什么问题?!?
您的
t
是 ThreadBase
的 raw类型。
ThreadBase t = new ThreadBase(null, new Object(), 10) {
这意味着所有泛型都被删除,甚至是不相关的泛型,例如您的方法
getErrors
,它变成了
public List getErrors() {
那么如果没有该警告,您就无法将
List
分配给 List<Throwable>
。
如果您想消除该警告,则必须not拥有
ThreadBase
的原始实例。 初始化时提供类型参数,例如:
ThreadBase<Config> t = new ThreadBase<Config>(null, new Object(), 10) {
然后您应该能够使用现有代码成功调用
getErrors
:
List<Throwable> errors = t.getErrors();