是什么导致 javac 发出“使用未经检查或不安全的操作”警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/197986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What causes javac to issue the "uses unchecked or unsafe operations" warning
提问by toolbear
For example:
例如:
javac Foo.java
Note: Foo.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
采纳答案by Bill the Lizard
This comes up in Java 5 and later if you're using collections without type specifiers (e.g., Arraylist()
instead of ArrayList<String>()
). It means that the compiler can't check that you're using the collection in a type-safe way, using generics.
如果您使用没有类型说明符的集合(例如,Arraylist()
代替ArrayList<String>()
),这会出现在 Java 5 及更高版本中。这意味着编译器无法使用泛型检查您是否以类型安全的方式使用集合。
To get rid of the warning, just be specific about what type of objects you're storing in the collection. So, instead of
要消除警告,只需具体说明您在集合中存储的对象类型。所以,而不是
List myList = new ArrayList();
use
用
List<String> myList = new ArrayList<String>();
In Java 7 you can shorten generic instantiation by using Type Inference.
在 Java 7 中,您可以使用Type Inference缩短泛型实例化。
List<String> myList = new ArrayList<>();
回答by Matt
for example when you call a function that returns Generic Collections and you don't specify the generic parameters yourself.
例如,当您调用一个返回泛型集合的函数并且您没有自己指定泛型参数时。
for a function
对于一个函数
List<String> getNames()
List names = obj.getNames();
will generate this error.
会产生这个错误。
To solve it you would just add the parameters
要解决它,您只需添加参数
List<String> names = obj.getNames();
回答by Ryan
The "unchecked or unsafe operations" warning was added when java added Generics, if I remember correctly. It's usually asking you to be more explicit about types, in one way or another.
如果我没记错的话,在 java 添加Generics时添加了“未经检查或不安全的操作”警告。它通常要求您以一种或另一种方式更明确地了解类型。
For example. the code ArrayList foo = new ArrayList();
triggers that warning because javac is looking for ArrayList<String> foo = new ArrayList<String>();
例如。代码ArrayList foo = new ArrayList();
触发该警告,因为 javac 正在寻找ArrayList<String> foo = new ArrayList<String>();
回答by Dan Dyer
If you do what it suggests and recompile with the "-Xlint:unchecked" switch, it will give you more detailed information.
如果您按照它的建议进行操作并使用“-Xlint:unchecked”开关重新编译,它将为您提供更详细的信息。
As well as the use of raw types (as described by the other answers), an unchecked cast can also cause the warning.
除了使用原始类型(如其他答案所述)外,未经检查的强制转换也可能导致警告。
Once you've compiled with -Xlint, you should be able to rework your code to avoid the warning. This is not always possible, particularly if you are integrating with legacy code that cannot be changed. In this situation, you may decide to suppress the warning in places where you know that the code is correct:
使用 -Xlint 进行编译后,您应该能够重新编写代码以避免出现警告。这并不总是可行的,特别是如果您正在与无法更改的遗留代码集成。在这种情况下,您可以决定在您知道代码正确的地方取消警告:
@SuppressWarnings("unchecked")
public void myMethod()
{
//...
}
回答by Suganthan Madhavan Pillai
This warning means that your code operates on a raw type, recompile the example with the
此警告意味着您的代码对原始类型进行操作,请使用以下命令重新编译示例
-Xlint:unchecked
to get the details
获取详细信息
like this:
像这样:
javac YourFile.java -Xlint:unchecked
Main.java:7: warning: [unchecked] unchecked cast
clone.mylist = (ArrayList<String>)this.mylist.clone();
^
required: ArrayList<String>
found: Object
1 warning
docs.oracle.com talks about it here: http://docs.oracle.com/javase/tutorial/java/generics/rawTypes.html
docs.oracle.com 在这里讨论它:http://docs.oracle.com/javase/tutorial/java/generics/rawTypes.html
回答by Julius
The solution would be to use specific type in <>
like ArrayList<File>
.
解决方案是在<>
like 中使用特定类型ArrayList<File>
。
example:
例子:
File curfolder = new File( "C:\Users\username\Desktop");
File[] file = curfolder.listFiles();
ArrayList filename = Arrays.asList(file);
above code generate warning because ArrayList
is not of specific type.
上面的代码生成警告,因为ArrayList
不是特定类型。
File curfolder = new File( "C:\Users\username\Desktop");
File[] file = curfolder.listFiles();
ArrayList<File> filename = Arrays.asList(file);
above code will do fine. Only change is in third line after ArrayList
.
上面的代码会很好。唯一的变化是在第三行之后ArrayList
。
回答by Michael Levy
I just want to add one example of the kind of unchecked warning I see quite often. If you use classes that implement an interface like Serializable, often you will call methods that return objects of the interface, and not the actual class. If the class being returned must be cast to a type based on generics, you can get this warning.
我只想添加一个我经常看到的未经检查的警告的例子。如果您使用实现诸如 Serializable 之类的接口的类,通常您将调用返回接口对象的方法,而不是实际的类。如果必须将返回的类强制转换为基于泛型的类型,则会收到此警告。
Here is a brief (and somewhat silly) example to demonstrate:
这是一个简短的(有点愚蠢的)示例来演示:
import java.io.Serializable;
public class SimpleGenericClass<T> implements Serializable {
public Serializable getInstance() {
return this;
}
// @SuppressWarnings("unchecked")
public static void main() {
SimpleGenericClass<String> original = new SimpleGenericClass<String>();
// java: unchecked cast
// required: SimpleGenericClass<java.lang.String>
// found: java.io.Serializable
SimpleGenericClass<String> returned =
(SimpleGenericClass<String>) original.getInstance();
}
}
getInstance() returns an object that implements Serializable. This must be cast to the actual type, but this is an unchecked cast.
getInstance() 返回一个实现 Serializable 的对象。这必须转换为实际类型,但这是一个未经检查的转换。
回答by Borzh
For Android Studio, you need to add:
对于 Android Studio,您需要添加:
allprojects {
gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
options.compilerArgs << "-Xlint:unchecked"
}
}
// ...
}
in your project's build.gradle file to know where this error is produced.
在您项目的 build.gradle 文件中以了解产生此错误的位置。
回答by Mayukh Datta
You can keep it in the generic form and write it as:
您可以将其保留为通用形式并将其写为:
// list 2 is made generic and can store any type of Object
ArrayList<Object> list2 = new ArrayList<Object>();
Setting type of ArrayList as Object gives us the advantage to store any type of data. You don't need to use -Xlint or anything else.
将 ArrayList 的类型设置为 Object 为我们提供了存储任何类型数据的优势。您不需要使用 -Xlint 或其他任何东西。
回答by Oskar
I had 2 years old classes and some new classes. I solved it in Android Studio as follows:
我有 2 岁的课程和一些新课程。我在 Android Studio 中解决了它,如下所示:
allprojects {
gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
options.compilerArgs << "-Xlint:unchecked"
}
}
}
In my project build.gradle file (Borzh solution)
在我的项目 build.gradle 文件中(Borzh 解决方案)
And then if some Metheds is left:
然后如果还剩下一些 Metheds:
@SuppressWarnings("unchecked")
public void myMethod()
{
//...
}