简体   繁体   English

禁用IntelliJ IDEA中的“恒定条件和例外”检查字段

[英]Disable “Constant conditions & exceptions” inspection for field in IntelliJ IDEA

I'm using a framework (Minecraft Forge) that injects specific objects into public static final fields with null values (through the @CapabilityInject and @ObjectHolder annotations). 我正在使用一个框架(Minecraft Forge),该框架将特定对象注入具有null值(通过@CapabilityInject@ObjectHolder批注)的public static final字段中。 Unfortunately whenever I reference one of these fields in a context that doesn't allow null values, the "Constant conditions & exceptions" inspection in IntelliJ IDEA warns me that the field may be null (because it doesn't know about the injection). 不幸的是,每当我在不允许null值的上下文中引用这些字段之一时,IntelliJ IDEA中的“恒定条件与异常”检查会警告我该字段可能为null (因为它不知道注入)。

I know that these fields won't be null when they're accessed, so I'd like to disable the warning for them. 我知道这些字段在访问时不会为null ,因此我想为它们禁用警告。

Is there a way to disable the warning for a specific field or all fields in a class? 有没有一种方法可以针对某个特定字段或类中的所有字段禁用警告? Adding @SuppressWarnings("ConstantConditions") or //noinspection ConstantConditions to the field doesn't remove the warning on the field access and adding the annotation to the field's class only removes the warning in that class. @SuppressWarnings("ConstantConditions")//noinspection ConstantConditions到该字段不会删除有关字段访问的警告,并且将注释添加到该字段的类中只会删除该类中的警告。

I'd like to avoid adding //noinspection ConstantConditions or null -checks to every location I access the fields from. 我想避免向我从中访问字段的每个位置添加//noinspection ConstantConditionsnull -checks。

diesieben07 answered this in my thread on the Minecraft Forge Forums . diesieben07在我的世界锻造论坛上的主题中回答了这个问题。

The solution/workaround is to create a method that always returns null , annotate it with @Nonnull and @SuppressWarnings("ConstantConditions") and then use that to initialise the field that will be injected into. 解决方案/解决方案是创建一个始终返回null的方法,并使用@Nonnull@SuppressWarnings("ConstantConditions")对其进行注释,然后使用该方法来初始化将要注入的字段。

For example: 例如:

public class Injection {

    @CapabilityInject
    public static final Capability<IItemHandler> CAPABILITY = getNull();

    @Nonnull
    @SuppressWarnings({"ConstantConditions", "SameReturnValue"})
    private static <T> T getNull() {
        return null;
    }

    public void doStuff() {
        // No warning
        Capability<IItemHandler> capability = CAPABILITY;
    }
}

Salamander offered an alternative solution in my thread on WTDWTF . Salamander在我的WTDWTF线程中提供了另一种解决方案。

They suggested moving the initialisation to a static initialiser block instead of initialising the fields inline. 他们建议将初始化移动到静态初始化程序块,而不是内联初始化字段。 This seems less hackish than a method that always returns null , but it also adds a lot of clutter if there are a lot of fields to initialise (which there are in my case). 与总是返回null的方法相比,这似乎没有太多的技巧,但是如果要初始化的字段很多(在我的例子中),这也会增加很多混乱。

For example: 例如:

public class Injection {

    @CapabilityInject
    public static final Capability<IItemHandler> CAPABILITY;

    static {
        CAPABILITY = null;
    }

    public void doStuff() {
        // No warning
        Capability<IItemHandler> capability = CAPABILITY;
    }
}

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

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