Skip to content

Commit

Permalink
add default value
Browse files Browse the repository at this point in the history
  • Loading branch information
MehrunesSky authored and edelpierre committed Nov 30, 2024
2 parents 5566380 + 4059177 commit 26f1d2f
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/main/java/org/jilt/Builder.java
Original file line number Diff line number Diff line change
Expand Up @@ -333,4 +333,11 @@
@Retention(RetentionPolicy.SOURCE)
@interface Ignore {
}

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.SOURCE)
@interface Default {

String value();
}
}
2 changes: 2 additions & 0 deletions src/main/java/org/jilt/internal/AbstractBuilderGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,14 @@ public final void generateBuilderClass() throws Exception {
for (VariableElement attribute : attributes) {
String fieldName = attributeSimpleName(attribute);
TypeName fieldType = TypeName.get(attribute.asType());
Builder.Default defaultAnnot = attribute.getAnnotation(Builder.Default.class);

builderClassBuilder.addField(FieldSpec
.builder(fieldType, fieldName,
this.builderClassNeedsToBeAbstract()
? Modifier.PROTECTED
: Modifier.PRIVATE)
.initializer(defaultAnnot != null ? defaultAnnot.value() : "")
.build());

MethodSpec setterMethod = this.generateBuilderSetterMethod(attribute);
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/org/jilt/test/DefaultTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.jilt.test;

import org.jilt.test.data.defaultvalue.DefaultValue;
import org.jilt.test.data.defaultvalue.DefaultValueBuilder;
import org.junit.Test;

import java.util.Collections;

import static org.assertj.core.api.Assertions.assertThat;

public class DefaultTest {

@Test
public void set_attribute_to_default_value_when_is_not_specified_in_builder(){
DefaultValue value = DefaultValueBuilder
.defaultValue()
.attr4(12)
.build();


assertThat(value.attr1).isEqualTo(1);
assertThat(value.attr2).isEqualTo("attr2");
assertThat(value.attr3).isTrue();
assertThat(value.attr4).isEqualTo(12);
assertThat(value.attrs).isEqualTo(Collections.emptySet());
}

}
32 changes: 32 additions & 0 deletions src/test/java/org/jilt/test/data/defaultvalue/DefaultValue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.jilt.test.data.defaultvalue;

import org.jilt.Builder;

import java.util.Set;

@Builder
public class DefaultValue {

@Builder.Default("1")
public int attr1;

@Builder.Default("\"attr2\"")
public String attr2;

@Builder.Default("true")
public boolean attr3;

@Builder.Default("1000")
public int attr4;

@Builder.Default("java.util.Collections.emptySet()")
public Set<String> attrs;

public DefaultValue(int attr1, String attr2, boolean attr3, int attr4, Set<String> attrs) {
this.attr1 = attr1;
this.attr2 = attr2;
this.attr3 = attr3;
this.attr4 = attr4;
this.attrs = attrs;
}
}

0 comments on commit 26f1d2f

Please sign in to comment.