简体   繁体   中英

How can I remove IntelliJ warnings for annotation elements which it thinks are unused?

I have created a custom validator. Intellij is giving "unused" warning for message , groups and payload fields. However, these should be defined and are used by Spring.

@Documented
@Constraint(validatedBy = PasswordConstraintValidator.class)
@Target({FIELD, ANNOTATION_TYPE})
@Retention(RUNTIME)
public @interface ValidPassword {
    String message() default "Invalid Password.";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};
}

Is there a way to get rid of this warning? I am not allowed to suppress warnings.

You could add a unit test which checks those fields. It would not be a very useful test, but it would at least shut IntelliJ up without suppressing the warning.

public class ValidPasswordTest
{
    @ValidPassword
    private final String fakeField = "";

    @Test
    public void checkDefaultValues() throws NoSuchFieldException
    {
        final ValidPassword validPassword = Arrays.stream(ValidPasswordTest.class.getDeclaredField("fakeField").getAnnotations())
            .filter(ValidPassword.class::isInstance)
            .map(ValidPassword.class::cast)
            .findFirst()
            .orElseThrow(RuntimeException::new);

        assertEquals("Invalid Password.", validPassword.message());
        assertArrayEquals(new Class[]{}, validPassword.groups());
        assertArrayEquals(new Class[]{}, validPassword.payload());
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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