当我编译以下类(可用here时,已添加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:取消选中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
类源代码如下(已添加main以在单个类中重现问题):
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:
List<Throwable> errors = t.getErrors();
这里有什么问题?!?
答案 0 :(得分:5)
您的t
是原始类型的ThreadBase
。
ThreadBase t = new ThreadBase(null, new Object(), 10) {
这意味着删除了所有泛型,甚至是不相关的泛型,例如您的方法getErrors
,这将成为
public List getErrors() {
然后,如果没有此警告,您就无法将List
分配给List<Throwable>
。
如果要消除该警告,则不必须具有ThreadBase
的原始实例。初始化时提供类型参数,例如:
ThreadBase<Config> t = new ThreadBase<Config>(null, new Object(), 10) {
然后,您应该可以使用现有代码成功致电getErrors
:
List<Throwable> errors = t.getErrors();