projectlombok / lombok

Very spicy additions to the Java programming language.
https://projectlombok.org/
Other
12.92k stars 2.39k forks source link

multiple @Builder annotation on static methods without builderClassName produce incorrect results #912

Closed ukeller closed 9 years ago

ukeller commented 9 years ago

When annotating at least 2 static methods with @Builder and renaming them to have 2 different builder methods they seem to overwrite each others builder class when not specifying the builderClassName explicitly leading to unexepected behavior. This behaviour doesn't seem to be documented if it is wanted. Example:

import lombok.Builder;

public class Main {

    public static class A {
        private final int n;

        private A(int n) {
            this.n = n;
        }

        @Builder(builderMethodName = "with1", builderClassName = "Other")
        private static A create1() {
            return new A(1);
        }

        @Builder(builderMethodName = "with2")
        private static A create2() {
            return new A(2);
        }

        @Builder(builderMethodName = "with3")
        private static A create3() {
            return new A(3);
        }

        @Override
        public String toString() {
            return String.format("A(%d)", n);
        }

    }

    public static void main(String[] args) {
        System.out.println(A.with1().build());
        System.out.println(A.with2().build());
        System.out.println(A.with3().build());

        // output is:
        // A(1)
        // A(2)
        // A(2)

        // output should:
        // A(1)
        // A(2)
        // A(3)

    }
}
askoning commented 9 years ago

See #730