Skip to content

Commit

Permalink
fix(java): fix wildcard capturer capture NullPointerException (#1637)
Browse files Browse the repository at this point in the history
## What does this PR do?

 fix wildcard capturer capture NullPointerException

## Related issues
Closes #1633 

## Does this PR introduce any user-facing change?

<!--
If any user-facing interface changes, please [open an
issue](https://github.com/apache/incubator-fury/issues/new/choose)
describing the need to do so and update the document if necessary.
-->

- [ ] Does this PR introduce any public API change?
- [ ] Does this PR introduce any binary protocol compatibility change?


## Benchmark

<!--
When the PR has an impact on performance (if you don't know whether the
PR will have an impact on performance, you can submit the PR first, and
if it will have impact on performance, the code reviewer will explain
it), be sure to attach a benchmark data here.
-->
  • Loading branch information
chaokunyang authored May 16, 2024
1 parent 4c19890 commit 262c578
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 9 deletions.
14 changes: 14 additions & 0 deletions java/fury-core/src/main/java/org/apache/fury/reflect/TypeRef.java
Original file line number Diff line number Diff line change
Expand Up @@ -900,16 +900,30 @@ TypeVariable<?> captureAsTypeVariable(Type[] upperBounds) {
for (; i < upperBoundsLength; i++) {
combinedUpperBounds[i] = upperBounds[i];
}
int skipCount = 0;
rootFor:
for (; i < combinedUpperBounds.length; i++) {
Type typeParamBound = typeParamBounds[i - upperBoundsLength];
for (Type upperBound : upperBounds) {
if (upperBound.equals(typeParamBound)) {
skipCount++;
continue rootFor;
}
}
combinedUpperBounds[i] = typeParamBound;
}
if (skipCount > 0) {
i = upperBoundsLength;
while (combinedUpperBounds[i] == null) {
if (i == combinedUpperBounds.length - 1) {
break;
} else {
combinedUpperBounds[i] = combinedUpperBounds[i++];
}
}
combinedUpperBounds =
Arrays.copyOf(combinedUpperBounds, combinedUpperBounds.length - skipCount);
}
}
return super.captureAsTypeVariable(combinedUpperBounds);
}
Expand Down
22 changes: 14 additions & 8 deletions java/fury-core/src/main/java/org/apache/fury/reflect/Types.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,17 +131,23 @@ public int hashCode() {

@Override
public String toString() {
return "ParameterizedTypeImpl{"
+ "actualTypeArguments="
+ Arrays.toString(actualTypeArguments)
+ ", rawType="
+ rawType
+ ", ownerType="
+ ownerType
+ '}';
StringBuilder builder = new StringBuilder();
builder.append(typeName(rawType)).append('<');
int i = 0;
for (Type typeArgument : actualTypeArguments) {
if (i++ != 0) {
builder.append(", ");
}
builder.append(typeName(typeArgument));
}
return builder.append('>').toString();
}
}

static String typeName(Type type) {
return (type instanceof Class) ? ((Class<?>) type).getName() : type.toString();
}

public static class GenericArrayTypeImpl implements GenericArrayType {
private final Type genericComponentType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,17 @@

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.fury.Fury;
import org.apache.fury.FuryTestBase;
import org.apache.fury.collection.Tuple2;
import org.apache.fury.type.TypeUtils;
import org.testng.Assert;
import org.testng.annotations.Test;

public class TypeRefTest {
public class TypeRefTest extends FuryTestBase {
static class MapObject extends LinkedHashMap<String, Object> {}

@Test
Expand All @@ -39,4 +46,42 @@ public void testGetSubtype() {
TypeUtils.mapOf(Map.class, String.class, Object.class),
new TypeRef<Map<String, Object>>() {});
}

@Data
static class MyInternalClass<T> {
public int c = 9;
public T t;
}

@EqualsAndHashCode(callSuper = true)
static class MyInternalBaseClass extends MyInternalClass<String> {
public int d = 19;
}

@Data
static class MyClass {
protected Map<String, MyInternalClass<?>> fields;
private transient int r = 13;

public MyClass() {
fields = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
fields.put("test", new MyInternalBaseClass());
}
}

@Test
public void testWildcardType() {
Tuple2<TypeRef<?>, TypeRef<?>> mapKeyValueType =
TypeUtils.getMapKeyValueType(new TypeRef<Map<String, MyInternalClass<?>>>() {});
Assert.assertEquals(mapKeyValueType.f0.getType(), String.class);
Assert.assertEquals(
mapKeyValueType.f1.getRawType(), new TypeRef<MyInternalClass<?>>() {}.getRawType());
}

@Test(dataProvider = "enableCodegen")
public void testWildcardTypeSerialization(boolean enableCodegen) {
// see issue https://github.com/apache/incubator-fury/issues/1633
Fury fury = builder().withCodegen(enableCodegen).build();
serDeCheck(fury, new MyClass());
}
}

0 comments on commit 262c578

Please sign in to comment.