Skip to content

Commit

Permalink
Avoid class cast exception when analyzing generic type. (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
cal101 authored and NathanSweet committed May 7, 2017
1 parent c6df1de commit 4a6759c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/com/esotericsoftware/yamlbeans/Beans.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.WildcardType;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -289,7 +290,18 @@ private Class getElementTypeFromGenerics (Type type) {
if (isCollection(rawType) || isMap(rawType)) {
Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
if (actualTypeArguments.length > 0) {
return (Class)actualTypeArguments[actualTypeArguments.length - 1];
final Type cType = actualTypeArguments[actualTypeArguments.length - 1];
if (cType instanceof Class) {
return (Class) cType;
} else if (cType instanceof WildcardType) {
WildcardType t = (WildcardType) cType;
final Type bound = t.getUpperBounds()[0];
return bound instanceof Class ? (Class) bound : null;
} else if (cType instanceof ParameterizedType) {
ParameterizedType t = (ParameterizedType) cType;
final Type rt = t.getRawType();
return rt instanceof Class ? (Class) rt : null;
}
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions test/com/esotericsoftware/yamlbeans/GenericTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ static class Test {
public Map<String, Struct> structMap;
public List<Integer> integerList;
public List<Struct> structList;
public Map<String, List<String>> multiMap;
public Map<String, ?> wildMap;

@Override
public boolean equals(Object o) {
Expand All @@ -151,6 +153,12 @@ public boolean equals(Object o) {
if (structMap != null ? !structMap.equals(test.structMap) : test.structMap != null) {
return false;
}
if (multiMap != null ? !multiMap.equals(test.multiMap) : test.multiMap != null) {
return false;
}
if (wildMap != null ? !wildMap.equals(test.wildMap) : test.wildMap != null) {
return false;
}

return true;
}
Expand All @@ -161,6 +169,8 @@ public int hashCode() {
result = 31 * result + (structMap != null ? structMap.hashCode() : 0);
result = 31 * result + (integerList != null ? integerList.hashCode() : 0);
result = 31 * result + (structList != null ? structList.hashCode() : 0);
result = 31 * result + (multiMap != null ? multiMap.hashCode() : 0);
result = 31 * result + (wildMap != null ? wildMap.hashCode() : 0);
return result;
}
}
Expand Down

0 comments on commit 4a6759c

Please sign in to comment.