gpt4 book ai didi

java - 不安全操作、未经检查的转换

转载 作者:行者123 更新时间:2023-12-01 17:22:51 27 4
gpt4 key购买 nike

当我编译以下类(可用 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: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();

这里出了什么问题?!?

最佳答案

您的tThreadBase原始类型.

ThreadBase t = new ThreadBase(null, new Object(), 10) {

这意味着所有泛型都被删除,甚至是不相关的泛型,例如您的方法 getErrors ,变成

public List getErrors() {

那么你就不能分配 ListList<Throwable>没有这个警告。

您不得没有 ThreadBase 的原始实例如果你想消除该警告。初始化时提供类型参数,例如:

ThreadBase<Config> t = new ThreadBase<Config>(null, new Object(), 10) {

那么您应该可以调用 getErrors使用您现有的代码成功:

List<Throwable> errors = t.getErrors();

关于java - 不安全操作、未经检查的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17308003/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com