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 MethodSpec.Builder.clearModifiers() method #114

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-114.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: feature
feature:
description: Add MethodSpec.Builder.clearModifiers() method
links:
- https://github.com/palantir/javapoet/pull/114
5 changes: 5 additions & 0 deletions javapoet/src/main/java/com/palantir/javapoet/MethodSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,11 @@ public Builder addModifiers(Iterable<Modifier> modifiers) {
return this;
}

public Builder clearModifiers() {
this.modifiers.clear();
return this;
}

public Builder addTypeVariables(Iterable<TypeVariableName> typeVariables) {
checkArgument(typeVariables != null, "typeVariables == null");
for (TypeVariableName typeVariable : typeVariables) {
Expand Down
25 changes: 25 additions & 0 deletions javapoet/src/test/java/com/palantir/javapoet/MethodSpecTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,31 @@ public void addModifiersVarargsShouldNotBeNull() {
.hasMessage("modifiers == null");
}

@Test
public void clearModifiers() {
MethodSpec method = MethodSpec.methodBuilder("getRandomNumber")
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.returns(TypeName.INT)
.addCode("return 4; // chosen by fair dice roll. guaranteed to be random.")
.build();
assertThat(method.toString())
.isEqualTo(
"""
public static int getRandomNumber() {
return 4; // chosen by fair dice roll. guaranteed to be random.
}
""");

MethodSpec updatedMethod = method.toBuilder().clearModifiers().build();
assertThat(updatedMethod.toString())
.isEqualTo(
"""
int getRandomNumber() {
return 4; // chosen by fair dice roll. guaranteed to be random.
}
""");
}

@Test
public void modifyMethodName() {
MethodSpec methodSpec = MethodSpec.methodBuilder("initialMethod").build().toBuilder()
Expand Down