Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
import java.util.List;
import org.javahelpers.codegen.enums.AccessModifier;
import org.javahelpers.codegen.model.annotation.AnnotationDto;
import org.javahelpers.codegen.model.core.ClassFieldDto;
import org.javahelpers.codegen.model.javadoc.JavadocDto;
import org.javahelpers.codegen.model.method.ConstructorDto;
import org.javahelpers.codegen.model.method.MethodDto;

/**
Expand All @@ -55,6 +57,18 @@
/** Methods to be generated in this nested type. */
private final List<MethodDto> methods = new LinkedList<>();

/**
* Fields to be generated in this nested type. Only rendered for nested classes — nested
* interfaces cannot declare instance fields in Java.
*/
private final List<ClassFieldDto> classFields = new LinkedList<>();

/**
* Constructors to be generated in this nested type. Only rendered for nested classes — nested
* interfaces cannot declare constructors in Java.
*/
private final List<ConstructorDto> constructors = new LinkedList<>();

/** Javadoc comment for this nested type. */
private JavadocDto javadoc;

Expand Down Expand Up @@ -112,6 +126,22 @@
this.methods.add(method);
}

public List<ClassFieldDto> getClassFields() {
return classFields;
}

public void addClassField(ClassFieldDto classField) {
this.classFields.add(classField);
}

public List<ConstructorDto> getConstructors() {
return constructors;
}

public void addConstructor(ConstructorDto constructor) {
this.constructors.add(constructor);
}

public JavadocDto getJavadoc() {
return javadoc;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@
import org.javahelpers.codegen.enums.AccessModifier;
import org.javahelpers.codegen.model.annotation.AnnotationDto;
import org.javahelpers.codegen.model.annotation.AnnotationDtoBuilder;
import org.javahelpers.codegen.model.core.ClassFieldDto;
import org.javahelpers.codegen.model.core.ClassFieldDtoBuilder;
import org.javahelpers.codegen.model.javadoc.JavadocDto;
import org.javahelpers.codegen.model.javadoc.JavadocDtoBuilder;
import org.javahelpers.codegen.model.method.ConstructorDto;
import org.javahelpers.codegen.model.method.ConstructorDtoBuilder;
import org.javahelpers.codegen.model.method.MethodDto;
import org.javahelpers.codegen.model.method.MethodDtoBuilder;

Expand Down Expand Up @@ -85,6 +89,12 @@ public class NestedTypeDtoBuilder implements IBuilderBase<NestedTypeDto> {
/** Tracked value for <code>annotations</code>: the replacement collection (not null). */
private TrackedValue<List<AnnotationDto>> annotations = unsetValue();

/** Tracked value for <code>classFields</code>: the replacement collection (not null). */
private TrackedValue<List<ClassFieldDto>> classFields = unsetValue();

/** Tracked value for <code>constructors</code>: the replacement collection (not null). */
private TrackedValue<List<ConstructorDto>> constructors = unsetValue();

/** Tracked value for <code>javadoc</code>: javadoc. */
private TrackedValue<JavadocDto> javadoc = unsetValue();

Expand Down Expand Up @@ -112,6 +122,8 @@ public NestedTypeDtoBuilder() {}
public NestedTypeDtoBuilder(NestedTypeDto instance) {
this.genericParameters = initialValue(instance.getGenericParameters());
this.annotations = initialValue(instance.getAnnotations());
this.classFields = initialValue(instance.getClassFields());
this.constructors = initialValue(instance.getConstructors());
this.javadoc = initialValue(instance.getJavadoc());
this.kind = initialValue(instance.getKind());
this.methods = initialValue(instance.getMethods());
Expand Down Expand Up @@ -683,6 +695,153 @@ public NestedTypeDtoBuilder genericParameterWithUpperBound(String name, Class<?>
name, java.util.Arrays.stream(upperBounds).map(TypeName::of).toArray(TypeName[]::new));
}

/**
* Adds a single element to <code>classFields</code>.
*
* <p>Configures {@link NestedTypeDto#getClassFields()}.
*
* @param element the element to add
* @return current instance of builder
*/
public NestedTypeDtoBuilder classField(ClassFieldDto element) {
List<ClassFieldDto> newCollection;
if (this.classFields.isSet()) {
newCollection = new ArrayList<>(this.classFields.value());
} else {
newCollection = new ArrayList<>();
}
newCollection.add(element);
this.classFields = changedValue(newCollection);
return this;
}

/**
* Appends one element to <code>classFields</code>, configured through its builder.
*
* @param classFieldBuilderConsumer consumer configuring the new element (not null)
* @return current instance of builder
*/
public NestedTypeDtoBuilder classField(Consumer<ClassFieldDtoBuilder> classFieldBuilderConsumer) {
ClassFieldDtoBuilder elementBuilder = ClassFieldDtoBuilder.create();
classFieldBuilderConsumer.accept(elementBuilder);
return classField(elementBuilder.build());
}

/**
* Appends elements to <code>classFields</code>, each configured through its builder.
*
* @param classFieldBuilderConsumers consumers configuring the new elements (not null)
* @return current instance of builder
*/
@SafeVarargs
public final NestedTypeDtoBuilder classFields(
Consumer<ClassFieldDtoBuilder>... classFieldBuilderConsumers) {
for (Consumer<ClassFieldDtoBuilder> consumer : classFieldBuilderConsumers) {
classField(consumer);
}
return this;
}

/**
* Sets the value for <code>classFields</code> by invoking the provided supplier.
*
* <p>Configures {@link NestedTypeDto#getClassFields()}.
*
* @param classFieldsSupplier supplier for the replacement collection (not null)
* @return current instance of builder
*/
public NestedTypeDtoBuilder classFields(Supplier<List<ClassFieldDto>> classFieldsSupplier) {
this.classFields = changedValue(classFieldsSupplier.get());
return this;
}

/**
* Sets the value for <code>classFields</code>.
*
* <p>Configures {@link NestedTypeDto#getClassFields()}.
*
* @param classFields the replacement collection (not null)
* @return current instance of builder
*/
public NestedTypeDtoBuilder classFields(List<ClassFieldDto> classFields) {
this.classFields = changedValue(classFields);
return this;
}

/**
* Adds a single element to <code>constructors</code>.
*
* <p>Configures {@link NestedTypeDto#getConstructors()}.
*
* @param element the element to add
* @return current instance of builder
*/
public NestedTypeDtoBuilder constructor(ConstructorDto element) {
List<ConstructorDto> newCollection;
if (this.constructors.isSet()) {
newCollection = new ArrayList<>(this.constructors.value());
} else {
newCollection = new ArrayList<>();
}
newCollection.add(element);
this.constructors = changedValue(newCollection);
return this;
}

/**
* Appends one element to <code>constructors</code>, configured through its builder.
*
* @param constructorBuilderConsumer consumer configuring the new element (not null)
* @return current instance of builder
*/
public NestedTypeDtoBuilder constructor(
Consumer<ConstructorDtoBuilder> constructorBuilderConsumer) {
ConstructorDtoBuilder elementBuilder = ConstructorDtoBuilder.create();
constructorBuilderConsumer.accept(elementBuilder);
return constructor(elementBuilder.build());
}

/**
* Appends elements to <code>constructors</code>, each configured through its builder.
*
* @param constructorBuilderConsumers consumers configuring the new elements (not null)
* @return current instance of builder
*/
@SafeVarargs
public final NestedTypeDtoBuilder constructors(
Consumer<ConstructorDtoBuilder>... constructorBuilderConsumers) {
for (Consumer<ConstructorDtoBuilder> consumer : constructorBuilderConsumers) {
constructor(consumer);
}
return this;
}

/**
* Sets the value for <code>constructors</code> by invoking the provided supplier.
*
* <p>Configures {@link NestedTypeDto#getConstructors()}.
*
* @param constructorsSupplier supplier for the replacement collection (not null)
* @return current instance of builder
*/
public NestedTypeDtoBuilder constructors(Supplier<List<ConstructorDto>> constructorsSupplier) {
this.constructors = changedValue(constructorsSupplier.get());
return this;
}

/**
* Sets the value for <code>constructors</code>.
*
* <p>Configures {@link NestedTypeDto#getConstructors()}.
*
* @param constructors the replacement collection (not null)
* @return current instance of builder
*/
public NestedTypeDtoBuilder constructors(List<ConstructorDto> constructors) {
this.constructors = changedValue(constructors);
return this;
}

/**
* Builds the configured DTO instance.
*
Expand All @@ -699,6 +858,10 @@ public NestedTypeDto build() {
values -> result.getGenericParameters().addAll(Objects.requireNonNull(values)));
this.annotations.ifSet(
values -> result.getAnnotations().addAll(Objects.requireNonNull(values)));
this.classFields.ifSet(
values -> result.getClassFields().addAll(Objects.requireNonNull(values)));
this.constructors.ifSet(
values -> result.getConstructors().addAll(Objects.requireNonNull(values)));
this.javadoc.ifSet(result::setJavadoc);
this.kind.ifSet(result::setKind);
this.methods.ifSet(values -> result.getMethods().addAll(Objects.requireNonNull(values)));
Expand All @@ -716,6 +879,8 @@ public NestedTypeDto build() {
public String toString() {
return new ToStringBuilder(this)
.append("annotations", this.annotations)
.append("classFields", this.classFields)
.append("constructors", this.constructors)
.append("genericParameters", this.genericParameters)
.append("javadoc", this.javadoc)
.append("kind", this.kind)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ private static NestedTypeDto nested() {
.visibility(AccessModifier.PRIVATE)
.javadoc(javadoc())
.method(method())
.classField(f -> f.fieldName("value").fieldType(String.class))
.constructor(constructor())
.annotation(annotation())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,59 @@ void copyConstructorPreservesGenericParameters() {
NestedTypeDto copy = new NestedTypeDtoBuilder(original).build();
assertEquals(1, copy.getGenericParameters().size());
}

@Test
void classFieldConsumerAddsField() {
NestedTypeDto result =
NestedTypeDtoBuilder.create()
.typeName("Holder")
.classField(f -> f.fieldName("value").fieldType(String.class))
.build();
assertEquals(1, result.getClassFields().size());
assertEquals("value", result.getClassFields().get(0).getFieldName());
}

@Test
void classFieldsVarargsConsumersAddAll() {
NestedTypeDto result =
NestedTypeDtoBuilder.create()
.typeName("Holder")
.classFields(
f -> f.fieldName("first").fieldType(String.class),
f -> f.fieldName("second").fieldType(String.class))
.build();
assertEquals(2, result.getClassFields().size());
}

@Test
void constructorConsumerAddsConstructor() {
NestedTypeDto result =
NestedTypeDtoBuilder.create()
.typeName("Holder")
.constructor(
c -> c.parameter(p -> p.parameterName("value").parameterTypeName(String.class)))
.build();
assertEquals(1, result.getConstructors().size());
assertEquals(1, result.getConstructors().get(0).getParameters().size());
}

@Test
void constructorsVarargsConsumersAddAll() {
NestedTypeDto result =
NestedTypeDtoBuilder.create().typeName("Holder").constructors(c -> {}, c -> {}).build();
assertEquals(2, result.getConstructors().size());
}

@Test
void copyConstructorPreservesFieldsAndConstructors() {
NestedTypeDto original =
NestedTypeDtoBuilder.create()
.typeName("Holder")
.classField(f -> f.fieldName("value").fieldType(String.class))
.constructor(c -> {})
.build();
NestedTypeDto copy = new NestedTypeDtoBuilder(original).build();
assertEquals(1, copy.getClassFields().size());
assertEquals(1, copy.getConstructors().size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,16 @@ private void appendNestedType(
applyAnnotations(nestedSource, nestedType.getAnnotations());
applyJavadoc(nestedSource, nestedType.getJavadoc(), collidingSimpleNames);
boolean isInterface = nestedType.getKind() == NestedTypeDto.NestedTypeKind.INTERFACE;
if (nestedSource instanceof JavaClassSource nestedClass) {
// fields and constructors are only rendered for nested classes: Java interfaces cannot
// declare constructors and their fields are implicitly constants
for (ClassFieldDto fieldDto : nestedType.getClassFields()) {
appendField(nestedClass, fieldDto, collidingSimpleNames);
}
for (ConstructorDto constructor : nestedType.getConstructors()) {
appendConstructor(nestedClass, constructor, collidingSimpleNames);
}
}
for (MethodDto methodDto : nestedType.getMethods()) {
appendNestedMethod(nestedSource, methodDto, isInterface, collidingSimpleNames);
}
Expand Down
Loading