Choonster Posted October 1, 2017 Posted October 1, 2017 (edited) This is a cross-post from StackOverflow, I thought I'd ask here as well in case anyone knew the answer. Forge injects specific objects into public static final fields with null values through the @CapabilityInject and @ObjectHolder annotations. 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). I know that these fields won't be null when they're accessed, so I'd like to disable the warning for them. 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. I'd like to avoid adding //noinspection ConstantConditions or null-checks to every location I access the fields from. Edited October 1, 2017 by Choonster Fixed leftover markdown formatting. Quote Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.
Choonster Posted October 1, 2017 Author Posted October 1, 2017 Thanks for the response. Using a method annotated with @SuppressWarnings("ConstantConditions") worked, the @Nonnull wasn't needed on the field access. I have all my packages annotated with @MethodsReturnNonnullByDefault already, so @Nonnull wasn't needed on the method either. I'm not sure what the best alternative to the current injection systems would be, though ideally it would be something that didn't require suppressing IDE warnings. Quote Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.
Choonster Posted October 1, 2017 Author Posted October 1, 2017 Salamander offered an alternative solution in my thread on 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). For example: public class Injection { @CapabilityInject public static final Capability<IItemHandler> CAPABILITY; static { CAPABILITY = null; } public void doStuff() { // No warning Capability<IItemHandler> capability = CAPABILITY; } } Quote Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.