Skip to content

[validation] False positive for [JAVA_PUBLIC_BEAN_METHOD] lint warning #1911

@tommai78101

Description

@tommai78101

Describe the bug
If a @Bean annotation is applied to an @Override method in a class that implements an interface requiring said implemented method to be a public method, the STS4 linter will indicate a false positive.

To Reproduce

  1. Copy the following code a Spring MVC or Spring Boot project of your choice:
package my.pkg.namespace;

import java.nio.charset.StandardCharsets;

import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@Configuration
public class MessageValidatorConfiguration implements WebMvcConfigurer {

	@Bean
	MessageSource messageSource ( ) {
		ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
		messageSource.setBasename("messages");
		messageSource.setDefaultEncoding(StandardCharsets.UTF_8.name());
		return messageSource;
	}

	// WARNING shows here by the "public" accessor keyword.
	@Bean
	@Override
	public LocalValidatorFactoryBean getValidator ( ) {
		LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean();
		bean.setValidationMessageSource(messageSource());
		return bean;
	}
}
  1. As indicated in the comment above, the @Bean can be applied to the getValidator(), which is a method to be implemented for the interface WebMvcConfigurer. However, the side effect is the @Bean would hint that the public access modifier must not need to be present, which conflicts with the @Override tag.
Image

The class WebMvcConfigurer comes from this source code file from the Spring Web MVC project:

https://github.com/spring-projects/spring-framework/blob/main/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurer.java

Is this a false positive? Please let me know. Thank you in advance.

We do use this wacky set up where we can autowire an overridden LocalValidatorFactoryBean class. There is no indication that we're doing this the right or wrong way, but it's being used for a couple of years in production for us.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions