当我编译以下类(可在此处获得,已添加 main 方法以在单个类中重现问题)时,我在 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();
这里怎么了?!?