java - How to exclude property from Lombok builder?

Java - How to exclude property from Lombok builder?

In Lombok, you can exclude a property from the generated builder by using the @Builder.Exclude annotation. This annotation instructs Lombok to exclude the annotated field or method from the generated builder.

Here's an example:

import lombok.Builder; import lombok.Builder.Default; import lombok.Getter; import lombok.Setter; @Builder @Getter @Setter public class Example { private String includedField; @Builder.Exclude private String excludedField; // Other fields, getters, setters, etc. public static void main(String[] args) { Example example = Example.builder() .includedField("Included") .excludedField("Excluded") // Set other fields as needed .build(); // Access fields System.out.println("Included Field: " + example.getIncludedField()); System.out.println("Excluded Field: " + example.getExcludedField()); // This will result in a compilation error } } 

In this example:

  • The @Builder annotation generates a builder for the Example class.
  • The includedField is included in the builder.
  • The excludedField is excluded from the builder using @Builder.Exclude.

When you use the builder to create an instance of Example, the excludedField won't be part of the builder methods, and trying to set it will result in a compilation error.

This approach allows you to selectively exclude fields from the generated builder while including others.

Examples

  1. "Lombok exclude specific property from builder"

    • Code:
      import lombok.Builder; import lombok.Builder.Default; import lombok.Getter; @Builder @Getter public class MyObject { private String includedProperty; @Default(exclude = "excludedProperty") private String excludedProperty = "default value"; } 
    • Description: Uses @Builder.Default with exclude to exclude the excludedProperty from the builder.
  2. "Lombok exclude multiple properties from builder"

    • Code:
      import lombok.Builder; import lombok.Builder.Exclude; import lombok.Getter; @Builder @Getter public class MyObject { private String includedProperty; @Exclude private String excludedProperty1; @Exclude private String excludedProperty2; } 
    • Description: Uses multiple @Exclude annotations to exclude multiple properties from the builder.
  3. "Lombok exclude property conditionally from builder"

    • Code:
      import lombok.Builder; import lombok.Getter; import lombok.Setter; import lombok.experimental.Accessors; @Builder @Getter @Setter @Accessors(chain = true) public class MyObject { private String includedProperty; private boolean excludePropertyCondition; @Exclude public String getExcludedProperty() { return excludePropertyCondition ? null : "default value"; } } 
    • Description: Excludes the property conditionally based on excludePropertyCondition.
  4. "Lombok exclude property with custom builder"

    • Code:
      import lombok.Builder; import lombok.Getter; public class MyObject { private String includedProperty; private String excludedProperty; @Getter @Builder(builderMethodName = "hiddenBuilder") public static class MyObjectBuilder { private String includedProperty; public MyObject build() { return new MyObject(includedProperty, "default value"); } } } 
    • Description: Uses a custom builder with a different name (hiddenBuilder) to exclude a specific property.
  5. "Lombok exclude property using NoArgsConstructor"

    • Code:
      import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; @Builder @Getter @NoArgsConstructor(access = AccessLevel.PRIVATE) public class MyObject { private String includedProperty; private String excludedProperty; } 
    • Description: Uses @NoArgsConstructor(access = AccessLevel.PRIVATE) to exclude the property by making it private.
  6. "Lombok exclude property from builder and toString"

    • Code:
      import lombok.Builder; import lombok.Getter; import lombok.ToString; @Builder @Getter @ToString(exclude = "excludedProperty") public class MyObject { private String includedProperty; private String excludedProperty; } 
    • Description: Uses @ToString(exclude = "excludedProperty") to exclude the property from the generated toString method.
  7. "Lombok exclude property with custom method in builder"

    • Code:
      import lombok.Builder; import lombok.Getter; @Builder @Getter public class MyObject { private String includedProperty; private String excludedProperty; public static MyObjectBuilder builder() { return new MyObjectBuilder(); } public static class MyObjectBuilder { private String excludedProperty; public MyObjectBuilder excludedProperty(String excludedProperty) { this.excludedProperty = excludedProperty; return this; } } } 
    • Description: Uses a custom builder with a specific method (excludedProperty) to exclude a property.
  8. "Lombok exclude property from setter in builder"

    • Code:
      import lombok.Builder; import lombok.Getter; @Builder @Getter public class MyObject { private String includedProperty; private String excludedProperty; public static class MyObjectBuilder { public MyObjectBuilder excludedProperty(String excludedProperty) { // Exclude property from setter return this; } } } 
    • Description: Excludes the property from the setter method in the builder.
  9. "Lombok exclude property from equals and hashCode"

    • Code:
      import lombok.Builder; import lombok.EqualsAndHashCode; import lombok.Getter; @Builder @Getter @EqualsAndHashCode(exclude = "excludedProperty") public class MyObject { private String includedProperty; private String excludedProperty; } 
    • Description: Uses @EqualsAndHashCode(exclude = "excludedProperty") to exclude the property from the generated equals and hashCode methods.
  10. "Lombok exclude property with custom constructor in builder"

    • Code:
      import lombok.Builder; import lombok.Getter; @Builder @Getter public class MyObject { private String includedProperty; private String excludedProperty; public static class MyObjectBuilder { private String excludedProperty; public MyObject build() { return new MyObject(includedProperty, "default value"); } } } 
    • Description: Uses a custom builder with a specific constructor to exclude a property.

More Tags

plsql web-services scss-mixins assembly 3des vimeo stress-testing openhardwaremonitor spring-cloud-gateway gettype

More Programming Questions

More Electrochemistry Calculators

More Chemistry Calculators

More Dog Calculators

More Financial Calculators