MinecraftDev icon indicating copy to clipboard operation
MinecraftDev copied to clipboard

Unnecessary warning when modifying @Final fields inside static blocks

Open Syst3ms opened this issue 2 years ago • 0 comments

Minecraft Development for IntelliJ plugin version

1.6.10

IntelliJ version

2023.2

Operating System

Windows

Target platform

Fabric

Description of the bug

The "@Final fields can't be modified" inspection on static final fields triggers even when field writes are done within a static block inside a mixin, which is a context where such writes do actually work.

For a mixin of mine, I need to tweak the value of a static final field while it is being initialized. I considered multiple approaches, and some people suggested I simply use a static block, which would just get merged with the original class's.

My mixin is as follows (I'm sure a non-specific example can be written easily)

@Mixin(RenderLayer.class)
public abstract class RenderLayerMixin {
    @Shadow @Final private static ImmutableList<RenderLayer> BLOCK_LAYERS;

    static {
        BLOCK_LAYERS = ImmutableList.<RenderLayer>builder()
            .addAll(BLOCK_LAYERS)
            .add(EntitionRenderingKt.ESSENCE_RENDER_LAYER)
            .build();
    }
}

This code gives an MCDev error about immutability of @Final fields, but Java allows writing to those inside static blocks, which is precisely the case, so it shouldn't error. Ignoring this, the mixin is successful and has the desired effect (with a superfluous console warning that has more to do with Mixin than MCDev).

Syst3ms avatar Oct 21 '23 15:10 Syst3ms