简体   繁体   English

Java IntelliJ IDEA序列化未使用声明检查错误

[英]Java IntelliJ IDEA Serialization UnusedDeclaration inspection error

I am fixing and cleaning up my school project and we are supposed to use the "InspectCode" feature in IntelliJ IDEA. 我正在修复和清理我的学校项目,我们应该使用IntelliJ IDEA中的“ InspectCode”功能。 We are to fix all warnings before returning the project. 我们将在返回项目之前修复所有警告。 I got some warnings under a category "Declaration redundancy/unused declaration. This is the warning category definition: 我在“声明冗余/未使用的声明”类别下得到了一些警告。这是警告类别的定义:

"This inspection reports classes, methods or fields in the specified inspection scope that are not used or not reachable from entry points." “此检查报告了指定检查范围内未从入口点使用或无法到达的类,方法或字段。”

I don't understand why. 我不明白为什么。 I have a total of four warnings that are connected to my serialization code ( see code below). 我总共有四个警告与我的序列化代码相关联(请参见下面的代码)。 The project works fine and I get no errors. 该项目工作正常,我没有任何错误。 I am asking for help to understand and hopefully be able to remove these four warnings. 我正在寻求帮助以了解并希望能够删除这四个警告。

private void readObject(ObjectInputStream is) throws ClassNotFoundException, IOException {
// default de-serialization
is.defaultReadObject();
quests = new ArrayList<>();

QuestManager qm = (QuestManager) is.readObject();

for (String name : qm.questNames) {
    quests.add(QuestBank.getQuest(name));
}

for (Quest q : quests) {
    System.out.println("Quest name: " + q.name);
}
}

This is one of the classes that raises the error: "Method is never used" Both methods raises that warning. 这是引发错误的类之一:“永不使用方法”这两种方法都会引发该警告。

As I said before everything works. 正如我之前所说的,一切正常。 I know these two methods are being called because I have tested to remove them and also att System.out.println(anytexthere) and that is called. 我知道这两个方法正在被调用,因为我已经测试过删除它们,并且也尝试了System.out.println(anytexthere)进行调用。

I wonder if I am supposed to mark them or call them seperately or something else. 我想知道是否应该标记它们或将其单独命名或其他名称。

Thanks in advance. 提前致谢。

This is the way the Java serialization mechanism works. 这就是Java序列化机制的工作方式。

There are some APIs such as readObject that are called reflectively by the serialization mechanism. 有一些API(例如readObject被序列化机制反射地调用。 They are required to be declared private , however. 但是,必须将它们声明为private When looked at from the point of view of just the programming language, it is a private method that is never referenced anywhere else from within this class, therefore, it must be unused. 仅从编程语言的角度来看,它是一个私有方法,永远不会在此类中的任何其他位置引用,因此,必须未使用它。 Perhaps this is what IntelliJ is doing. 也许这就是IntelliJ所做的。 It apparently isn't taking the reflective nature of serialization into account. 显然,它没有考虑序列化的反射性。 This would have to be special-cased in any kind of static analysis. 在任何类型的静态分析中,都必须对此进行特殊处理。

As far as I can see there is nothing wrong with your code. 据我所知,您的代码没有错。 This is just how the serialization mechanism works in Java. 这就是Java中序列化机制的工作方式。

There may be some kind of annotation that can be added (like @SuppressWarnings ?) or option that can be set in IntelliJ to avoid this kind of warning. 可以添加某种注释(例如@SuppressWarnings吗?)或可以在IntelliJ中设置的选项来避免这种警告。

(You mentioned "both methods" but I see only one method here, readObject . I presume the other method is writeObject , which is called the exact same way by the serialization mechanism, and will result in the same kind of warnings.) (您提到了“两个方法”,但是在这里我只看到一个方法readObject 。我猜另一个方法是writeObject ,它被序列化机制称为完全相同的方式,并且会产生相同的警告。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM