Skip to content

Commit

Permalink
Record support
Browse files Browse the repository at this point in the history
Fixes square#829

Signed-off-by: liach <[email protected]>
  • Loading branch information
liach committed May 13, 2021
1 parent 8851788 commit 2d95cc7
Show file tree
Hide file tree
Showing 3 changed files with 377 additions and 84 deletions.
46 changes: 32 additions & 14 deletions src/main/java/com/squareup/javapoet/MethodSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ private boolean lastParameterIsArray(List<ParameterSpec> parameters) {
&& TypeName.asArray((parameters.get(parameters.size() - 1).type)) != null;
}

void emit(CodeWriter codeWriter, String enclosingName, Set<Modifier> implicitModifiers)
void emit(CodeWriter codeWriter, String enclosingName, Set<Modifier> implicitModifiers,
boolean compactConstructor)
throws IOException {
codeWriter.emitJavadoc(javadocWithParameters());
codeWriter.emitAnnotations(annotations, false);
Expand All @@ -91,22 +92,18 @@ void emit(CodeWriter codeWriter, String enclosingName, Set<Modifier> implicitMod
codeWriter.emit(" ");
}

if (isConstructor()) {
codeWriter.emit("$L($Z", enclosingName);
if (compactConstructor) {
codeWriter.emit("$L", enclosingName);
} else {
codeWriter.emit("$T $L($Z", returnType, name);
}
if (isConstructor()) {
codeWriter.emit("$L", enclosingName);
} else {
codeWriter.emit("$T $L", returnType, name);
}

boolean firstParameter = true;
for (Iterator<ParameterSpec> i = parameters.iterator(); i.hasNext(); ) {
ParameterSpec parameter = i.next();
if (!firstParameter) codeWriter.emit(",").emitWrappingSpace();
parameter.emit(codeWriter, !i.hasNext() && varargs);
firstParameter = false;
emitParameters(codeWriter, parameters, varargs);
}

codeWriter.emit(")");

if (defaultValue != null && !defaultValue.isEmpty()) {
codeWriter.emit(" default ");
codeWriter.emit(defaultValue);
Expand Down Expand Up @@ -141,6 +138,11 @@ void emit(CodeWriter codeWriter, String enclosingName, Set<Modifier> implicitMod
}

private CodeBlock javadocWithParameters() {
return makeJavadocWithParameters(javadoc, parameters);
}

static CodeBlock makeJavadocWithParameters(CodeBlock javadoc,
Iterable<ParameterSpec> parameters) {
CodeBlock.Builder builder = javadoc.toBuilder();
boolean emitTagNewline = true;
for (ParameterSpec parameterSpec : parameters) {
Expand All @@ -154,6 +156,22 @@ private CodeBlock javadocWithParameters() {
return builder.build();
}

static void emitParameters(CodeWriter codeWriter, Iterable<ParameterSpec> parameters,
boolean varargs) throws IOException {
codeWriter.emit(CodeBlock.of("($Z"));

boolean firstParameter = true;
for (Iterator<ParameterSpec> i = parameters.iterator(); i.hasNext(); ) {
ParameterSpec parameter = i.next();
if (!firstParameter)
codeWriter.emit(",").emitWrappingSpace();
parameter.emit(codeWriter, !i.hasNext() && varargs);
firstParameter = false;
}

codeWriter.emit(")");
}

public boolean hasModifier(Modifier modifier) {
return modifiers.contains(modifier);
}
Expand All @@ -177,7 +195,7 @@ public boolean isConstructor() {
StringBuilder out = new StringBuilder();
try {
CodeWriter codeWriter = new CodeWriter(out);
emit(codeWriter, "Constructor", Collections.emptySet());
emit(codeWriter, "Constructor", Collections.emptySet(), false);
return out.toString();
} catch (IOException e) {
throw new AssertionError();
Expand Down
Loading

0 comments on commit 2d95cc7

Please sign in to comment.