Skip to content
Merged
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 error-prone-contrib/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@
<artifactId>jackson-annotations</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should omit this one from the runtime classpath:

Suggested change
<artifactId>jackson-core</artifactId>
<artifactId>jackson-core</artifactId>
<scope>provided</scope>

<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package tech.picnic.errorprone.refasterrules;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.errorprone.refaster.Refaster;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
Expand Down Expand Up @@ -45,4 +47,20 @@ Optional<JsonNode> after(JsonNode node, String fieldName) {
return node.optional(fieldName);
}
}

/**
* Prefer {@link ObjectMapper#valueToTree(Object)} over more contrived and less efficient
* alternatives.
*/
static final class ObjectMapperValueToTree {
@BeforeTemplate
JsonNode before(ObjectMapper objectMapper, Object object) throws JsonProcessingException {
return objectMapper.readTree(objectMapper.writeValueAsString(object));
}

@AfterTemplate
JsonNode after(ObjectMapper objectMapper, Object object) {
return objectMapper.valueToTree(object);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package tech.picnic.errorprone.refasterrules;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.NullNode;
import com.google.common.collect.ImmutableSet;
import java.util.Optional;
Expand All @@ -22,4 +24,8 @@ ImmutableSet<Optional<JsonNode>> testJsonNodeOptionalString() {
Optional.of(NullNode.getInstance().get("baz")),
Optional.ofNullable(NullNode.getInstance().get("qux")));
}

JsonNode testObjectMapperValueToTree() throws JsonProcessingException {
return new ObjectMapper().readTree(new ObjectMapper().writeValueAsString("foo"));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package tech.picnic.errorprone.refasterrules;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.NullNode;
import com.google.common.collect.ImmutableSet;
import java.util.Optional;
Expand All @@ -22,4 +24,8 @@ ImmutableSet<Optional<JsonNode>> testJsonNodeOptionalString() {
NullNode.getInstance().optional("baz"),
NullNode.getInstance().optional("qux"));
}

JsonNode testObjectMapperValueToTree() throws JsonProcessingException {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how we usually do this, but I am keeping this throws statement for the diff; ObjectMapper#valueToTree does not throw this exception.

return new ObjectMapper().valueToTree("foo");
}
}