diff options
Diffstat (limited to 'src/main/java/com/squareup/javapoet/MethodSpec.java')
-rw-r--r-- | src/main/java/com/squareup/javapoet/MethodSpec.java | 71 |
1 files changed, 63 insertions, 8 deletions
diff --git a/src/main/java/com/squareup/javapoet/MethodSpec.java b/src/main/java/com/squareup/javapoet/MethodSpec.java index a2c7c43..2284ef5 100644 --- a/src/main/java/com/squareup/javapoet/MethodSpec.java +++ b/src/main/java/com/squareup/javapoet/MethodSpec.java @@ -24,6 +24,7 @@ import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; import javax.lang.model.SourceVersion; import javax.lang.model.element.Element; import javax.lang.model.element.ExecutableElement; @@ -82,7 +83,7 @@ public final class MethodSpec { void emit(CodeWriter codeWriter, String enclosingName, Set<Modifier> implicitModifiers) throws IOException { - codeWriter.emitJavadoc(javadoc); + codeWriter.emitJavadoc(javadocWithParameters()); codeWriter.emitAnnotations(annotations, false); codeWriter.emitModifiers(modifiers, implicitModifiers); @@ -132,11 +133,26 @@ public final class MethodSpec { codeWriter.emit(" {\n"); codeWriter.indent(); - codeWriter.emit(code); + codeWriter.emit(code, true); codeWriter.unindent(); codeWriter.emit("}\n"); } + codeWriter.popTypeVariables(typeVariables); + } + + private CodeBlock javadocWithParameters() { + CodeBlock.Builder builder = javadoc.toBuilder(); + boolean emitTagNewline = true; + for (ParameterSpec parameterSpec : parameters) { + if (!parameterSpec.javadoc.isEmpty()) { + // Emit a new line before @param section only if the method javadoc is present. + if (emitTagNewline && !javadoc.isEmpty()) builder.add("\n"); + emitTagNewline = false; + builder.add("@param $L $L", parameterSpec.name, parameterSpec.javadoc); + } + } + return builder.build(); } public boolean hasModifier(Modifier modifier) { @@ -217,7 +233,16 @@ public final class MethodSpec { } methodBuilder.returns(TypeName.get(method.getReturnType())); - methodBuilder.addParameters(ParameterSpec.parametersOf(method)); + // Copying parameter annotations from the overridden method can be incorrect so we're + // deliberately dropping them. See https://github.com/square/javapoet/issues/482. + methodBuilder.addParameters(ParameterSpec.parametersOf(method) + .stream() + .map(parameterSpec -> { + ParameterSpec.Builder builder = parameterSpec.toBuilder(); + builder.annotations.clear(); + return builder.build(); + }) + .collect(Collectors.toList())); methodBuilder.varargs(method.isVarArgs()); for (TypeMirror thrownType : method.getThrownTypes()) { @@ -277,25 +302,31 @@ public final class MethodSpec { } public static final class Builder { - private final String name; + private String name; private final CodeBlock.Builder javadoc = CodeBlock.builder(); - private final List<AnnotationSpec> annotations = new ArrayList<>(); - private final List<Modifier> modifiers = new ArrayList<>(); - private List<TypeVariableName> typeVariables = new ArrayList<>(); private TypeName returnType; - private final List<ParameterSpec> parameters = new ArrayList<>(); private final Set<TypeName> exceptions = new LinkedHashSet<>(); private final CodeBlock.Builder code = CodeBlock.builder(); private boolean varargs; private CodeBlock defaultValue; + public final List<TypeVariableName> typeVariables = new ArrayList<>(); + public final List<AnnotationSpec> annotations = new ArrayList<>(); + public final List<Modifier> modifiers = new ArrayList<>(); + public final List<ParameterSpec> parameters = new ArrayList<>(); + private Builder(String name) { + setName(name); + } + + public Builder setName(String name) { checkNotNull(name, "name == null"); checkArgument(name.equals(CONSTRUCTOR) || SourceVersion.isName(name), "not a valid name: %s", name); this.name = name; this.returnType = name.equals(CONSTRUCTOR) ? null : TypeName.VOID; + return this; } public Builder addJavadoc(String format, Object... args) { @@ -454,6 +485,14 @@ public final class MethodSpec { } /** + * @param codeBlock the control flow construct and its code, such as "if (foo == 5)". + * Shouldn't contain braces or newline characters. + */ + public Builder beginControlFlow(CodeBlock codeBlock) { + return beginControlFlow("$L", codeBlock); + } + + /** * @param controlFlow the control flow construct and its code, such as "else if (foo == 10)". * Shouldn't contain braces or newline characters. */ @@ -462,6 +501,14 @@ public final class MethodSpec { return this; } + /** + * @param codeBlock the control flow construct and its code, such as "else if (foo == 10)". + * Shouldn't contain braces or newline characters. + */ + public Builder nextControlFlow(CodeBlock codeBlock) { + return nextControlFlow("$L", codeBlock); + } + public Builder endControlFlow() { code.endControlFlow(); return this; @@ -476,6 +523,14 @@ public final class MethodSpec { return this; } + /** + * @param codeBlock the optional control flow construct and its code, such as + * "while(foo == 20)". Only used for "do/while" control flows. + */ + public Builder endControlFlow(CodeBlock codeBlock) { + return endControlFlow("$L", codeBlock); + } + public Builder addStatement(String format, Object... args) { code.addStatement(format, args); return this; |