Skip to content

Commit

Permalink
fix #141: Avoid attempting to optimize varargs methods
Browse files Browse the repository at this point in the history
  • Loading branch information
carterkozak authored and stevenschlansker committed Jul 7, 2021
1 parent 0b3a723 commit 65e7ea9
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ private MethodHandle directHandle(AnnotatedElement element) {
Stream.of(element)
.filter(Constructor.class::isInstance)
.map(Constructor.class::cast)
.filter(c -> !Modifier.isPrivate(c.getModifiers()))
.filter(c -> !Modifier.isPrivate(c.getModifiers())
// 07-Jul-2021, ckozak: modules-base#141Avoid attempting to optimize varargs
// methods due to class-cast edge cases
&& !c.isVarArgs())
.flatMap(t -> {
try {
return Stream.of(_lookup.unreflectConstructor(t));
Expand All @@ -120,7 +123,10 @@ private MethodHandle directHandle(AnnotatedElement element) {
.map(Method.class::cast)
.filter(m -> {
int mods = m.getModifiers();
return Modifier.isStatic(mods) && !Modifier.isPrivate(mods);
return Modifier.isStatic(mods) && !Modifier.isPrivate(mods)
// 07-Jul-2021, ckozak: modules-base#141Avoid attempting to optimize varargs
// methods due to class-cast edge cases
&& !m.isVarArgs();
})
.flatMap(t -> {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.fasterxml.jackson.module.blackbird.deser;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.blackbird.BlackbirdTestBase;

public class DoubleArrayDeserTest extends BlackbirdTestBase
{
static class Foo141 {
@JsonProperty("bar")
double[] bar;

@JsonCreator
public Foo141(@JsonProperty("bar") double[] bar) {
this.bar = bar;
}
}

private final ObjectMapper MAPPER = newObjectMapper();

public void testDoubleArrayViaCreator()
{
Foo141 foo = new Foo141(new double[] { 2.0, 0.25 });
String serialized = MAPPER.writeValueAsString(foo);
Foo141 foo2 = MAPPER.readValue(serialized, Foo141.class);

assertEquals(2, foo2.bar.length);
assertEquals(0.25, foo2.bar[1]);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.fasterxml.jackson.module.blackbird.failing;
package com.fasterxml.jackson.module.blackbird.deser;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand All @@ -7,7 +7,7 @@
import com.fasterxml.jackson.module.blackbird.BlackbirdTestBase;

// [modules-base#141]
public class DoubleArrayDeser141Test extends BlackbirdTestBase
public class DoubleVarArgsDeser141Test extends BlackbirdTestBase
{
// [modules-base#141]
static class Foo141 {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.fasterxml.jackson.module.blackbird.deser;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.blackbird.BlackbirdTestBase;

import java.util.Arrays;
import java.util.List;

public class ObjectArrayDeserTest extends BlackbirdTestBase
{
static class Bar {
@JsonProperty("baz")
String baz;

@JsonCreator
public Bar(@JsonProperty("baz") String baz) {
this.baz = baz;
}
}

static class Foo141 {
@JsonProperty("bar")
List<Bar> bar;

@JsonCreator
public Foo141(@JsonProperty("bar") Bar[] bar) {
this.bar = Arrays.asList(bar);
}
}

private final ObjectMapper MAPPER = newObjectMapper();

public void testDoubleArrayViaCreator()
{
Foo141 foo = new Foo141(new Bar[] {new Bar("a"), new Bar("b")});
String serialized = MAPPER.writeValueAsString(foo);
Foo141 foo2 = MAPPER.readValue(serialized, Foo141.class);

assertEquals(2, foo2.bar.size());
assertEquals("a", foo2.bar.get(0).baz);
assertEquals("b", foo2.bar.get(1).baz);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.fasterxml.jackson.module.blackbird.deser;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.blackbird.BlackbirdTestBase;

import java.util.Arrays;
import java.util.List;

// [modules-base#141]
public class ObjectVarArgsDeser141Test extends BlackbirdTestBase
{
static class Bar {
@JsonProperty("baz")
String baz;

@JsonCreator
public Bar(@JsonProperty("baz") String baz) {
this.baz = baz;
}
}
// [modules-base#141]
static class Foo141 {
@JsonProperty("bar")
List<Bar> bar;

@JsonCreator
public Foo141(@JsonProperty("bar") Bar... bar) {
this.bar = Arrays.asList(bar);
}
}

private final ObjectMapper MAPPER = newObjectMapper();

// [modules-base#141]
public void testDoubleArrayViaCreator()
{
Foo141 foo = new Foo141(new Bar("a"), new Bar("b"));
String serialized = MAPPER.writeValueAsString(foo);
Foo141 foo2 = MAPPER.readValue(serialized, Foo141.class);

assertEquals(2, foo2.bar.size());
assertEquals("a", foo2.bar.get(0).baz);
assertEquals("b", foo2.bar.get(1).baz);
}
}

0 comments on commit 65e7ea9

Please sign in to comment.