use of java.lang.reflect.WildcardType in project evosuite by EvoSuite.
the class TestCodeVisitor method getParameterString.
private String getParameterString(Type[] parameterTypes, List<VariableReference> parameters, boolean isGenericMethod, boolean isOverloaded, int startPos) {
String parameterString = "";
for (int i = startPos; i < parameters.size(); i++) {
if (i > startPos) {
parameterString += ", ";
}
Type declaredParamType = parameterTypes[i];
Type actualParamType = parameters.get(i).getType();
String name = getVariableName(parameters.get(i));
Class<?> rawParamClass = declaredParamType instanceof WildcardType ? Object.class : GenericTypeReflector.erase(declaredParamType);
if (rawParamClass.isPrimitive() && name.equals("null")) {
parameterString += getPrimitiveNullCast(rawParamClass);
} else if (isGenericMethod && !(declaredParamType instanceof WildcardType)) {
if (!declaredParamType.equals(actualParamType) || name.equals("null")) {
parameterString += "(" + getTypeName(declaredParamType) + ") ";
if (name.contains("(short"))
name = name.replace("(short)", "");
if (name.contains("(byte"))
name = name.replace("(byte)", "");
}
} else if (name.equals("null")) {
parameterString += "(" + getTypeName(declaredParamType) + ") ";
} else if (!GenericClass.isAssignable(declaredParamType, actualParamType)) {
if (TypeUtils.isArrayType(declaredParamType) && TypeUtils.isArrayType(actualParamType)) {
Class<?> componentClass = GenericTypeReflector.erase(declaredParamType).getComponentType();
if (componentClass.equals(Object.class)) {
GenericClass genericComponentClass = new GenericClass(componentClass);
if (genericComponentClass.hasWildcardOrTypeVariables()) {
// If we are assigning a generic array, then we don't need to cast
} else {
// If we are assigning a non-generic array, then we do need to cast
parameterString += "(" + getTypeName(declaredParamType) + ") ";
}
} else {
// if (!GenericClass.isAssignable(GenericTypeReflector.getArrayComponentType(declaredParamType), GenericTypeReflector.getArrayComponentType(actualParamType))) {
parameterString += "(" + getTypeName(declaredParamType) + ") ";
}
} else if (!(actualParamType instanceof ParameterizedType)) {
parameterString += "(" + getTypeName(declaredParamType) + ") ";
}
if (name.contains("(short"))
name = name.replace("(short)", "");
if (name.contains("(byte"))
name = name.replace("(byte)", "");
// }
} else {
// We have to cast between wrappers and primitives in case there
// are overloaded signatures. This could be optimized by checking
// if there actually is a problem of overloaded signatures
GenericClass parameterClass = new GenericClass(declaredParamType);
if (parameterClass.isWrapperType() && parameters.get(i).isPrimitive()) {
parameterString += "(" + getTypeName(declaredParamType) + ") ";
} else if (parameterClass.isPrimitive() && parameters.get(i).isWrapperType()) {
parameterString += "(" + getTypeName(declaredParamType) + ") ";
} else if (isOverloaded) {
// If there is an overloaded method, we need to cast to make sure we use the right version
if (!declaredParamType.equals(actualParamType)) {
parameterString += "(" + getTypeName(declaredParamType) + ") ";
}
}
}
parameterString += name;
}
return parameterString;
}
use of java.lang.reflect.WildcardType in project evosuite by EvoSuite.
the class TestGenericClass method testWildcardIntegerBoundaries.
@Test
public void testWildcardIntegerBoundaries() {
WildcardType objectType = new WildcardTypeImpl(new Type[] { Integer.class }, new Type[] {});
GenericClass integerClass = new GenericClass(Integer.class);
GenericClass comparableClass = new GenericClass(Comparable.class);
GenericClass dateClass = new GenericClass(java.util.Date.class);
GenericClass sqlDateClass = new GenericClass(java.sql.Date.class);
Assert.assertTrue(integerClass.satisfiesBoundaries(objectType));
Assert.assertFalse(comparableClass.satisfiesBoundaries(objectType));
Assert.assertFalse(dateClass.satisfiesBoundaries(objectType));
Assert.assertFalse(sqlDateClass.satisfiesBoundaries(objectType));
}
use of java.lang.reflect.WildcardType in project evosuite by EvoSuite.
the class TestGenericClass method testWildcardNumberBoundaries.
@Test
public void testWildcardNumberBoundaries() {
WildcardType objectType = new WildcardTypeImpl(new Type[] { Number.class }, new Type[] {});
GenericClass integerClass = new GenericClass(Integer.class);
GenericClass comparableClass = new GenericClass(Comparable.class);
GenericClass dateClass = new GenericClass(java.util.Date.class);
GenericClass sqlDateClass = new GenericClass(java.sql.Date.class);
Assert.assertTrue(integerClass.satisfiesBoundaries(objectType));
Assert.assertFalse(comparableClass.satisfiesBoundaries(objectType));
Assert.assertFalse(dateClass.satisfiesBoundaries(objectType));
Assert.assertFalse(sqlDateClass.satisfiesBoundaries(objectType));
}
use of java.lang.reflect.WildcardType in project evosuite by EvoSuite.
the class TestGenericClass method testWildcardInvalidBoundaries.
@Test
public void testWildcardInvalidBoundaries() {
WildcardType objectType = new WildcardTypeImpl(new Type[] { Number.class }, new Type[] { java.util.Date.class });
GenericClass integerClass = new GenericClass(Integer.class);
GenericClass comparableClass = new GenericClass(Comparable.class);
GenericClass dateClass = new GenericClass(java.util.Date.class);
GenericClass sqlDateClass = new GenericClass(java.sql.Date.class);
Assert.assertFalse(integerClass.satisfiesBoundaries(objectType));
Assert.assertFalse(comparableClass.satisfiesBoundaries(objectType));
Assert.assertFalse(dateClass.satisfiesBoundaries(objectType));
Assert.assertFalse(sqlDateClass.satisfiesBoundaries(objectType));
}
use of java.lang.reflect.WildcardType in project evosuite by EvoSuite.
the class TestGenericClass method testWildcardDateSuperBoundaries.
@Test
public void testWildcardDateSuperBoundaries() {
WildcardType objectType = new WildcardTypeImpl(new Type[] { Object.class }, new Type[] { java.util.Date.class });
GenericClass integerClass = new GenericClass(Integer.class);
GenericClass comparableClass = new GenericClass(Comparable.class);
GenericClass dateClass = new GenericClass(java.util.Date.class);
GenericClass sqlDateClass = new GenericClass(java.sql.Date.class);
Assert.assertFalse(integerClass.satisfiesBoundaries(objectType));
Assert.assertFalse(comparableClass.satisfiesBoundaries(objectType));
Assert.assertTrue(dateClass.satisfiesBoundaries(objectType));
Assert.assertFalse(sqlDateClass.satisfiesBoundaries(objectType));
}
Aggregations