简体   繁体   English

为什么 IntelliJ 有时会抛出未使用的警告,但有时不会?

[英]Why does IntelliJ sometimes throw an unused warning but sometimes not?

I have many Java classes with two constructors:我有许多带有两个构造函数的 Java 类:

  1. a private constructor with no arguments and with unused warning suppression called by gson;一个没有 arguments 且具有由 gson 调用的未使用警告抑制的私有构造函数;
  2. a public constructor with arguments.带有 arguments 的公共构造函数。

In one class IntelliJ informs me with a warning that the public constructor is never used.在一个 class IntelliJ 通知我警告说公共构造函数从未使用过。 In all other classes there isn't the warning, but if I click Find Usage for the method it displays "Nothing found in project files".在所有其他类中都没有警告,但是如果我单击 Find Usage 的方法,它会显示“在项目文件中找不到任何内容”。

Why is there a warning in some cases and not in others?为什么在某些情况下有警告而在其他情况下没有? How can I make IntelliJ always behave in the same way?如何使 IntelliJ 始终以相同的方式运行?

This is the class where the public constructor throws the warning:这是公共构造函数抛出警告的 class :

public class ShopMarketAction extends Action {

    private Boolean inRow = null;
    private Integer index = null;

    @SuppressWarnings("unused") // Called by gson
    private ShopMarketAction() {
        super(ActionType.SHOP_MARKET);
    }

    // THIS METHOD THROWS AN UNUSED WARNING
    public ShopMarketAction(boolean inRow, int index) {
        super(ActionType.SHOP_MARKET);
        this.inRow = inRow;
        this.index = index;
    }

}

This is an example class where the public constructor doesn't throw the warning:这是一个示例 class ,其中公共构造函数不会引发警告:

public class ProductionAction extends Action {

    private Integer cardIndex = null;

    @SuppressWarnings("unused") // Called by gson
    private ProductionAction() {
        super(ActionType.PRODUCE);
    }

    // THIS METHOD DOESN'T THROW THE WARNING BUT IS NEVER USED
    public ProductionAction(int cardIndex) {
        super(ActionType.PRODUCE);
        this.cardIndex = cardIndex;
    }

}

I specify that in both classes the method is not used yet.我指定在这两个类中该方法尚未使用。

The Action class that both classes extends:两个类都扩展的 Action class:

public abstract class Action {

    @SuppressWarnings({"unused", "FieldCanBeLocal"})
    private final ActionType type; // Used by gson

    protected Action(ActionType type) {
        this.type = type;
    }

}

The ActionType enum: ActionType 枚举:

public enum ActionType {
    SHOP_MARKET, PRODUCE;
}

警告

无警告

没有使用

I found what causes the problem: I have a non-Java file (an UXF file created by another application) which contains the name of all these methods and for some reason IntelliJ consider them as used because of this.我找到了导致问题的原因:我有一个非 Java 文件(由另一个应用程序创建的 UXF 文件),其中包含所有这些方法的名称,并且出于某种原因 IntelliJ 认为它们因此而被使用。

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

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