From 6fe6f1d146e4f87dbcdd0a35150e851f612ebc88 Mon Sep 17 00:00:00 2001 From: Dmitrii Duzhinskii Date: Thu, 9 Nov 2023 15:13:45 +0300 Subject: [PATCH] Always emit method parameters line-by-line --- .../generator/message/MessageGenerator.java | 1 - .../com/squareup/javapoet/MethodSpec.java | 18 +++--- .../java/com/squareup/javapoet/TypeSpec.java | 24 +------- .../com/squareup/javapoet/TypeSpecTest.java | 61 ++++--------------- 4 files changed, 19 insertions(+), 85 deletions(-) diff --git a/generator/src/main/java/org/sudu/protogen/generator/message/MessageGenerator.java b/generator/src/main/java/org/sudu/protogen/generator/message/MessageGenerator.java index ad8ebb4..ec597b2 100644 --- a/generator/src/main/java/org/sudu/protogen/generator/message/MessageGenerator.java +++ b/generator/src/main/java/org/sudu/protogen/generator/message/MessageGenerator.java @@ -41,7 +41,6 @@ public TypeSpec generate(@NotNull Message msgDescriptor) { addBuilderIfNecessary(msgDescriptor, typeBuilder); return typeBuilder - .multiLineRecord(true) .addModifiers(Modifier.PUBLIC) .addTypes(generateNested(msgDescriptor)) .build(); diff --git a/javapoet/src/main/java/com/squareup/javapoet/MethodSpec.java b/javapoet/src/main/java/com/squareup/javapoet/MethodSpec.java index 2694fff..1406a94 100644 --- a/javapoet/src/main/java/com/squareup/javapoet/MethodSpec.java +++ b/javapoet/src/main/java/com/squareup/javapoet/MethodSpec.java @@ -93,7 +93,7 @@ void emit(CodeWriter codeWriter, String enclosingName, Set implicitMod codeWriter.emit("$T $L", returnType, name); } - emitParameters(codeWriter, parameters, varargs, false); + emitParameters(codeWriter, parameters, varargs); } if (defaultValue != null && !defaultValue.isEmpty()) { @@ -149,25 +149,21 @@ static CodeBlock makeJavadocWithParameters(CodeBlock javadoc, } static void emitParameters(CodeWriter codeWriter, Collection 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 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(")"); } diff --git a/javapoet/src/main/java/com/squareup/javapoet/TypeSpec.java b/javapoet/src/main/java/com/squareup/javapoet/TypeSpec.java index 01d641a..2525a12 100644 --- a/javapoet/src/main/java/com/squareup/javapoet/TypeSpec.java +++ b/javapoet/src/main/java/com/squareup/javapoet/TypeSpec.java @@ -42,7 +42,6 @@ public final class TypeSpec { public final TypeName superclass; public final List superinterfaces; public final List recordComponents; - public final boolean multiLineRecord; public final boolean varargs; public final Map enumConstants; public final boolean shortEnumNotation; @@ -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; @@ -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; @@ -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)); } @@ -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; @@ -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 extendsTypes; @@ -477,7 +465,6 @@ public static final class Builder { public final Map enumConstants = new LinkedHashMap<>(); public boolean shortEnumNotation = false; public final List recordComponents = new ArrayList<>(); - public boolean multiLineRecord; public boolean varargs; public final List annotations = new ArrayList<>(); public final List modifiers = new ArrayList<>(); @@ -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. diff --git a/javapoet/src/test/java/com/squareup/javapoet/TypeSpecTest.java b/javapoet/src/test/java/com/squareup/javapoet/TypeSpecTest.java index c2e0d57..a1d8501 100644 --- a/javapoet/src/test/java/com/squareup/javapoet/TypeSpecTest.java +++ b/javapoet/src/test/java/com/squareup/javapoet/TypeSpecTest.java @@ -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; @@ -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")