Skip to content

Commit

Permalink
Always emit method parameters line-by-line
Browse files Browse the repository at this point in the history
  • Loading branch information
Duzhinsky committed Nov 9, 2023
1 parent 1161372 commit 6fe6f1d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public TypeSpec generate(@NotNull Message msgDescriptor) {
addBuilderIfNecessary(msgDescriptor, typeBuilder);

return typeBuilder
.multiLineRecord(true)
.addModifiers(Modifier.PUBLIC)
.addTypes(generateNested(msgDescriptor))
.build();
Expand Down
18 changes: 7 additions & 11 deletions javapoet/src/main/java/com/squareup/javapoet/MethodSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ void emit(CodeWriter codeWriter, String enclosingName, Set<Modifier> implicitMod
codeWriter.emit("$T $L", returnType, name);
}

emitParameters(codeWriter, parameters, varargs, false);
emitParameters(codeWriter, parameters, varargs);
}

if (defaultValue != null && !defaultValue.isEmpty()) {
Expand Down Expand Up @@ -149,25 +149,21 @@ static CodeBlock makeJavadocWithParameters(CodeBlock javadoc,
}

static void emitParameters(CodeWriter codeWriter, Collection<ParameterSpec> parameters,
boolean varargs, boolean separateLines) throws IOException {
boolean varargs) throws IOException {
codeWriter.emit(CodeBlock.of("("));
separateLines = separateLines && parameters.size() > 2;
if (separateLines)
codeWriter.indent();
if (parameters.size() > 1) codeWriter.emit("\n");
codeWriter.indent();
boolean firstParameter = true;
for (Iterator<ParameterSpec> i = parameters.iterator(); i.hasNext(); ) {
ParameterSpec parameter = i.next();
if (!firstParameter) {
codeWriter.emit(",");
if (!separateLines) codeWriter.emitWrappingSpace();
codeWriter.emit(",\n");
}
if (separateLines)
codeWriter.emit("\n");
parameter.emit(codeWriter, !i.hasNext() && varargs);
firstParameter = false;
}
if (separateLines)
codeWriter.emit("\n").unindent();
if (parameters.size() > 1) codeWriter.emit("\n");
codeWriter.unindent();
codeWriter.emit(")");
}

Expand Down
24 changes: 1 addition & 23 deletions javapoet/src/main/java/com/squareup/javapoet/TypeSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public final class TypeSpec {
public final TypeName superclass;
public final List<TypeName> superinterfaces;
public final List<ParameterSpec> recordComponents;
public final boolean multiLineRecord;
public final boolean varargs;
public final Map<String, TypeSpec> enumConstants;
public final boolean shortEnumNotation;
Expand All @@ -67,7 +66,6 @@ private TypeSpec(Builder builder) {
this.superclass = builder.superclass;
this.superinterfaces = Util.immutableList(builder.superinterfaces);
this.recordComponents = Util.immutableList(builder.recordComponents);
this.multiLineRecord = builder.multiLineRecord;
this.varargs = builder.varargs;
this.enumConstants = Util.immutableMap(builder.enumConstants);
this.shortEnumNotation = builder.shortEnumNotation;
Expand Down Expand Up @@ -106,7 +104,6 @@ private TypeSpec(TypeSpec type) {
this.superclass = null;
this.superinterfaces = Collections.emptyList();
this.recordComponents = Collections.emptyList();
this.multiLineRecord = false;
this.varargs = false;
this.enumConstants = Collections.emptyMap();
this.shortEnumNotation = false;
Expand Down Expand Up @@ -157,14 +154,6 @@ public static Builder recordBuilder(ClassName className) {
return recordBuilder(checkNotNull(className, "className == null").simpleName());
}

public static Builder recordBuilder(String name, boolean multiLineRecord) {
return recordBuilder(name).multiLineRecord(multiLineRecord);
}

public static Builder recordBuilder(ClassName className, boolean multiLineRecord) {
return recordBuilder(className).multiLineRecord(multiLineRecord);
}

public static Builder anonymousClassBuilder(String typeArgumentsFormat, Object... args) {
return anonymousClassBuilder(CodeBlock.of(typeArgumentsFormat, args));
}
Expand All @@ -191,7 +180,6 @@ public Builder toBuilder() {
builder.superinterfaces.addAll(superinterfaces);
builder.recordComponents.addAll(recordComponents);
builder.varargs = varargs;
builder.multiLineRecord = multiLineRecord;
builder.enumConstants.putAll(enumConstants);
builder.shortEnumNotation = shortEnumNotation;
builder.compactConstructor = compactConstructor;
Expand Down Expand Up @@ -245,7 +233,7 @@ private boolean emitHeader(CodeWriter codeWriter, String enumName,

// Record components.
if (kind == Kind.RECORD) {
MethodSpec.emitParameters(codeWriter, recordComponents, varargs, multiLineRecord);
MethodSpec.emitParameters(codeWriter, recordComponents, varargs);
}

List<TypeName> extendsTypes;
Expand Down Expand Up @@ -477,7 +465,6 @@ public static final class Builder {
public final Map<String, TypeSpec> enumConstants = new LinkedHashMap<>();
public boolean shortEnumNotation = false;
public final List<ParameterSpec> recordComponents = new ArrayList<>();
public boolean multiLineRecord;
public boolean varargs;
public final List<AnnotationSpec> annotations = new ArrayList<>();
public final List<Modifier> modifiers = new ArrayList<>();
Expand Down Expand Up @@ -666,15 +653,6 @@ public Builder varargs(boolean varargs) {
return this;
}

public Builder multiLineRecord() {
return multiLineRecord(true);
}

public Builder multiLineRecord(boolean multiLineRecord) {
this.multiLineRecord = multiLineRecord;
return this;
}

/**
* Sets the compact constructor for this builder. Its parameters are solely used for javadoc
* generation.
Expand Down
61 changes: 11 additions & 50 deletions javapoet/src/test/java/com/squareup/javapoet/TypeSpecTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,23 @@

import com.google.common.collect.ImmutableMap;
import com.google.testing.compile.CompilationRule;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.EventListener;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.Callable;
import javax.lang.model.element.Element;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.TypeMirror;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.Mockito;

import javax.lang.model.element.Element;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.TypeMirror;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.Callable;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
Expand Down Expand Up @@ -1336,37 +1328,6 @@ public void interfacePrivateMethods() {
+ "}\n");
}

@Test
public void multiLineRecordTest() {
TypeSpec inlineTaco = TypeSpec.recordBuilder("Taco")
.addRecordComponent(ParameterSpec.builder(ClassName.get(String.class), "shell").build())
.addRecordComponent(ParameterSpec.builder(TypeName.INT, "weight").build())
.build();
assertThat(toString(inlineTaco)).isEqualTo("" +
"package com.squareup.tacos;\n" +
"\n" +
"import java.lang.String;\n" +
"\n" +
"record Taco(String shell, int weight) {\n" +
"}\n"
);
TypeSpec multilineTaco = TypeSpec.recordBuilder("Taco", true)
.addRecordComponent(ParameterSpec.builder(ClassName.get(String.class), "shell").build())
.addRecordComponent(ParameterSpec.builder(TypeName.INT, "weight").build())
.build();
assertThat(toString(multilineTaco)).isEqualTo("" +
"package com.squareup.tacos;\n" +
"\n" +
"import java.lang.String;\n" +
"\n" +
"record Taco(\n" +
" String shell,\n" +
" int weight\n" +
") {\n" +
"}\n"
);
}

@Test
public void recordComponentJavadoc() throws Exception {
TypeSpec taco = TypeSpec.recordBuilder("Taco")
Expand Down

0 comments on commit 6fe6f1d

Please sign in to comment.