spring-io / spring-javaformat

Apache License 2.0
797 stars 110 forks source link

Package visibility does not check inner classes #158

Open philwebb opened 4 years ago

philwebb commented 4 years ago

See https://github.com/spring-projects/spring-boot/issues/19395

dreis2211 commented 4 years ago

I have a first version for this, but that creates follow-up problems/questions with the method visibility checks.

E.g. take the following: https://github.com/spring-projects/spring-boot/blob/a66c4d3096877b33119366ea9ea165c892b462c8/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomObjectMapperProviderTests.java#L71-L86

    @MinimalWebConfiguration
    @ApplicationPath("/rest")
    @Path("/message")
    public static class Application extends ResourceConfig {

        Application() {
            register(Application.class);
        }

        @GET
        public Message message() {
            return new Message("Jersey", null);
        }

        static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }

    }

Making Application package private will cause the method visibility checks to kick in on message(). Yet, this method cannot be made package private as it is required to be public.

A similar issue appears in https://github.com/spring-projects/spring-boot/blob/a66c4d3096877b33119366ea9ea165c892b462c8/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/mock/mockito/SpyBeanWithAopProxyTests.java#L87

    @Service
    public static class DateService {

        @Cacheable(cacheNames = "test")
        public Long getDate(boolean arg) {
            return System.nanoTime();
        }

    }

@Cacheable only works on public methods, so we can't change this here either.

I wonder if we should extend the MethodVisibilityChecks to check for some more annotations, but that might be cumbersome.

I've only checked a couple of modules so far, but I suspect more problems with some of the tests that do serialization and make use of inner classes and don't have any annotation, so the only option there would be to suppress the checks.

What do you think?

Cheers, Christoph

philwebb commented 3 years ago

I seem to remember we had a few of those in the past and the only option was to exclude them from checkstyle. Perhaps it's best if we leave things alone for now.