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 @@ -19,7 +19,8 @@ static String createClassStart(
TypeElement type,
String typeParams,
boolean isImported,
String packageName) {
String packageName,
String builderName) {

if (type.getEnclosingElement() instanceof TypeElement) {
isImported = true;
Expand Down Expand Up @@ -54,9 +55,6 @@ static String createClassStart(
final String build =
build(components).transform(s -> numberOfComponents > 6 ? "\n " + s : s);

final var builderName =
ProcessorUtils.shortType(utype.mainType()).replace(".", "$") + "Builder";

final var shortName = type.getSimpleName().toString();
return ClassTemplate.classTemplate(
packageName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,7 @@ private void readElement(TypeElement type, BuilderPrism prism) {
unnamed
? ""
: packageElement.getQualifiedName().toString() + (isImported ? ".builder" : "");
var builderName =
ProcessorUtils.shortType(UType.parse(type.asType()).mainType()).replace(".", "$")
+ "Builder";
var builderName = builderName(type);
try (var writer =
new Append(
createSourceFile((unnamed ? "" : packageName + ".") + builderName).openWriter())) {
Expand All @@ -153,14 +151,28 @@ private void readElement(TypeElement type, BuilderPrism prism) {
.map(Object::toString)
.collect(joining(", "))
.transform(s -> s.isEmpty() ? s : "<" + s + ">");
writer.append(ClassBodyBuilder.createClassStart(type, typeParams, isImported, packageName));
writer.append(ClassBodyBuilder.createClassStart(type, typeParams, isImported, packageName, builderName));

methods(writer, typeParams, builderName, components, prism);
} catch (final IOException e) {
throw new UncheckedIOException(e);
}
}

private static String builderName(TypeElement type) {
var name = ProcessorUtils.shortType(UType.parse(type.asType()).mainType());
int pos = name.lastIndexOf('.');
if (pos > 0 && isNotNestedRecord(type.getEnclosingElement())) {
return name.substring(pos + 1) + "Builder";
}
return name.replace(".", "$") + "Builder";
}

private static boolean isNotNestedRecord(Element enclosingElement) {
ElementKind kind = enclosingElement.getKind();
return kind != ElementKind.RECORD;
}

private void methods(
Append writer,
String typeParams,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.avaje.recordbuilder.nested;

import io.avaje.recordbuilder.RecordBuilder;

public interface SomeInterface {

@RecordBuilder
record MyParams(
String param1,
String param2
) {

public static MyParamsBuilder builder() {
// builder is called MyParamsBuilder rather than SomeInterface$MyParamsBuilder
// ... because the enclosed type is not a record
return MyParamsBuilder.builder();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.avaje.recordbuilder.nested;

import io.avaje.recordbuilder.nested.SomeInterface.MyParams;
import org.junit.jupiter.api.Test;

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

class SomeInterfaceTest {

@Test
void test() {

var params = MyParams.builder()
.param1("p1")
.param2("p2")
.build();

assertThat(params.param1()).isEqualTo("p1");
assertThat(params.param2()).isEqualTo("p2");
}
}