Skip to content

Commit b856923

Browse files
committed
fix factory formatting
1 parent a566be3 commit b856923

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

inject-generator/src/main/java/io/avaje/inject/generator/SimpleAssistWriter.java

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ final class SimpleAssistWriter {
2525
private final String suffix;
2626
private Append writer;
2727
private final List<Element> assistedElements;
28+
private final boolean hasNoContructorParams;
2829

2930
SimpleAssistWriter(AssistBeanReader beanReader) {
3031
this.beanReader = beanReader;
@@ -33,6 +34,11 @@ final class SimpleAssistWriter {
3334
this.suffix = "$AssistFactory";
3435
this.assistedElements = beanReader.assistElements();
3536
this.originName = packageName + "." + shortName;
37+
this.hasNoContructorParams =
38+
beanReader.constructor().params().stream()
39+
.filter(not(MethodParam::assisted))
40+
.findAny()
41+
.isEmpty();
3642
}
3743

3844
private Writer createFileWriter() throws IOException {
@@ -110,25 +116,30 @@ private void writeInjectFields() {
110116
var type = UType.parse(element.asType());
111117
writer.append(" %s %s$field;", type.shortType(), field.fieldName()).eol().eol();
112118
}
113-
writer.eol();
119+
if (beanReader.injectMethods().isEmpty() && hasNoContructorParams) {
120+
writer.eol();
121+
}
114122
}
115123

116124
private void writeMethodFields() {
117125
if (beanReader.injectMethods().isEmpty()) {
118126
return;
119127
}
120128
beanReader.injectMethods().stream()
121-
.flatMap(m -> m.params().stream())
122-
.filter(not(MethodParam::assisted))
123-
.forEach(
124-
p -> {
125-
var element = p.element();
126-
writer
127-
.append(" private %s %s$method;", UType.parse(element.asType()).shortType(), p.simpleName())
128-
.eol()
129-
.eol();
130-
});
131-
writer.eol();
129+
.flatMap(m -> m.params().stream())
130+
.filter(not(MethodParam::assisted))
131+
.forEach(
132+
p -> {
133+
var element = p.element();
134+
writer
135+
.append(
136+
" private %s %s$method;",
137+
UType.parse(element.asType()).shortType(), p.simpleName())
138+
.eol();
139+
});
140+
if (hasNoContructorParams) {
141+
writer.eol();
142+
}
132143
}
133144

134145
private void writeConstructor() {
@@ -146,7 +157,7 @@ private void writeConstructor() {
146157
if (beanReader.beanType().getNestingKind().isNested()) {
147158
shortName = shortName.replace(".", "$");
148159
}
149-
writer.append(" ").append(shortName).append(suffix).append("(");
160+
writer.eol().append(" ").append(shortName).append(suffix).append("(");
150161

151162
for (var iterator = injectParams.iterator(); iterator.hasNext(); ) {
152163
var p = iterator.next();
@@ -170,7 +181,7 @@ private void writeFieldsForInjected(List<MethodParam> injectParams) {
170181
for (MethodParam p : injectParams) {
171182
var element = p.element();
172183
var type = UType.parse(element.asType()).shortType();
173-
writer.append(" private final %s %s;", type, p.simpleName()).eol().eol();
184+
writer.append(" private final %s %s;", type, p.simpleName()).eol();
174185
}
175186
}
176187

inject/src/main/java/io/avaje/inject/AssistFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* because the factory has some managed dependencies that should be injected for us and some
1919
* dependencies that are provided by the application (and annotated with {@code Assisted}).
2020
*
21-
* <p>The factory class must be a functional interface, or an abstract class with only one
21+
* <p>The factory supertype must be a functional interface, or an abstract class with only one
2222
* abstract method defined.
2323
*
2424
* <pre>{@code

0 commit comments

Comments
 (0)