speedment / jpa-streamer

JPAstreamer is a lightweight library for expressing JPA queries as Java Streams
GNU Lesser General Public License v2.1
345 stars 35 forks source link

Fieldgenerator should not accept private getter #348

Closed julgus closed 1 year ago

julgus commented 1 year ago

Describe the bug Under certain conditions, the fieldgenerator accepts private getters and generates code that won't compile.

Expected behavior If the getter is private the fieldgenerator should issue a warning at runtime and generate a reference getter that throws an exception.

The field films has a private getter:

    @Getter(value = AccessLevel.PRIVATE)
    @ManyToMany(mappedBy = "actors")
    private List<Film> films = new ArrayList<>();

Should generate (whether this is an optimal solution is up for discussion but this is the way it is designed to work at this point) :

    /**
     * This Field corresponds to the {@link Actor} field "films".
     */
    public static final ReferenceField<Actor, List<Film>> films = ReferenceField.create(
        Actor.class,
        "films",
        actor -> {throw new IllegalJavaBeanException(Actor.class, "films");},
        false
    );

Actual behavior The generated ReferenceField references Actor::getFilms which is unavailable to the compiler of that class:

    /**
     * This Field corresponds to the {@link Actor} field "films".
     */
    public static final ReferenceField<Actor, List<Film>> films = ReferenceField.create(
        Actor.class,
        "films",
        Actor::getFilms,
        false
    );

How To Reproduce Build the fieldgenerator-testmodule with IntelliJ (error does not occur when building with Maven).

Build tool IntelliJ

JPAStreamer version 3.0.2

JPA Provider Hibernate 6.0.2.Final

Java Version 11.0.27

julgus commented 1 year ago

Building with IntelliJ:

image

Building with Maven:

image
julgus commented 1 year ago

I think the difference arises because the accessors Lombok generates are available at build time with IntelliJ and are erroneously detected as standard getters.