Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add record support #17

Closed
wants to merge 20 commits into from
53 changes: 36 additions & 17 deletions javapoet/src/main/java/com/palantir/javapoet/MethodSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

/** A generated constructor or method declaration. */
public final class MethodSpec {
private static final String CONSTRUCTOR = "<init>";
public static final String CONSTRUCTOR = "<init>";
xanderbailey marked this conversation as resolved.
Show resolved Hide resolved

private final String name;
private final CodeBlock javadoc;
Expand Down Expand Up @@ -133,7 +133,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) throws IOException {
void emit(CodeWriter codeWriter, String enclosingName, Set<Modifier> implicitModifiers, boolean compactConstructor)
xanderbailey marked this conversation as resolved.
Show resolved Hide resolved
throws IOException {
codeWriter.emitJavadoc(javadocWithParameters());
codeWriter.emitAnnotations(annotations, false);
codeWriter.emitModifiers(modifiers, implicitModifiers);
Expand All @@ -143,24 +144,17 @@ void emit(CodeWriter codeWriter, String enclosingName, Set<Modifier> implicitMod
codeWriter.emit(" ");
}

if (isConstructor()) {
codeWriter.emit("$L($Z", enclosingName);
if (compactConstructor) {
xanderbailey marked this conversation as resolved.
Show resolved Hide resolved
codeWriter.emit("$L", enclosingName);
} else {
codeWriter.emit("$T $L($Z", returnType, name);
}

boolean firstParameter = true;
for (Iterator<ParameterSpec> i = parameters.iterator(); i.hasNext(); ) {
ParameterSpec parameter = i.next();
if (!firstParameter) {
codeWriter.emit(",").emitWrappingSpace();
if (isConstructor()) {
codeWriter.emit("$L", enclosingName);
} else {
codeWriter.emit("$T $L", returnType, name);
}
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 @@ -196,7 +190,32 @@ void emit(CodeWriter codeWriter, String enclosingName, Set<Modifier> implicitMod
codeWriter.popTypeVariables(typeVariables);
}

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

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

codeWriter.emit(")");
}

private static boolean isVarargs(boolean varargs, Iterator<ParameterSpec> parameterSpec) {
return !parameterSpec.hasNext() && varargs;
}
xanderbailey marked this conversation as resolved.
Show resolved Hide resolved

private CodeBlock javadocWithParameters() {
pkoenig10 marked this conversation as resolved.
Show resolved Hide resolved
return makeJavadocWithParameters(javadoc, parameters);
}

static CodeBlock makeJavadocWithParameters(CodeBlock javadoc, Iterable<ParameterSpec> parameters) {
pkoenig10 marked this conversation as resolved.
Show resolved Hide resolved
CodeBlock.Builder builder = javadoc.toBuilder();
boolean emitTagNewline = true;
for (ParameterSpec parameterSpec : parameters) {
Expand Down Expand Up @@ -236,7 +255,7 @@ public String toString() {
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 UncheckedIOException(e);
Expand Down
Loading