Search in sources :

Example 41 with WildcardType

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;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) CaptureType(com.googlecode.gentyref.CaptureType) WildcardType(java.lang.reflect.WildcardType) GenericClass(org.evosuite.utils.generic.GenericClass) GenericClass(org.evosuite.utils.generic.GenericClass)

Example 42 with WildcardType

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));
}
Also used : WildcardType(java.lang.reflect.WildcardType) WildcardTypeImpl(org.evosuite.utils.generic.WildcardTypeImpl) GenericClass(org.evosuite.utils.generic.GenericClass) Test(org.junit.Test)

Example 43 with WildcardType

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));
}
Also used : WildcardType(java.lang.reflect.WildcardType) WildcardTypeImpl(org.evosuite.utils.generic.WildcardTypeImpl) GenericClass(org.evosuite.utils.generic.GenericClass) Test(org.junit.Test)

Example 44 with WildcardType

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));
}
Also used : WildcardType(java.lang.reflect.WildcardType) WildcardTypeImpl(org.evosuite.utils.generic.WildcardTypeImpl) GenericClass(org.evosuite.utils.generic.GenericClass) Test(org.junit.Test)

Example 45 with WildcardType

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));
}
Also used : WildcardType(java.lang.reflect.WildcardType) WildcardTypeImpl(org.evosuite.utils.generic.WildcardTypeImpl) GenericClass(org.evosuite.utils.generic.GenericClass) Test(org.junit.Test)

Aggregations

WildcardType (java.lang.reflect.WildcardType)236 ParameterizedType (java.lang.reflect.ParameterizedType)208 Type (java.lang.reflect.Type)180 GenericArrayType (java.lang.reflect.GenericArrayType)160 TypeVariable (java.lang.reflect.TypeVariable)134 Test (org.junit.Test)24 GenericClass (org.evosuite.utils.generic.GenericClass)14 Method (java.lang.reflect.Method)10 WildcardTypeImpl (org.evosuite.utils.generic.WildcardTypeImpl)10 ArrayList (java.util.ArrayList)9 Map (java.util.Map)8 Test (org.junit.jupiter.api.Test)8 CaptureType (com.googlecode.gentyref.CaptureType)7 JSONException (com.alibaba.fastjson.JSONException)5 JSONType (com.alibaba.fastjson.annotation.JSONType)5 CatalogType (org.spongepowered.api.CatalogType)4 GenericDeclaration (java.lang.reflect.GenericDeclaration)3 HashSet (java.util.HashSet)3 List (java.util.List)3 MediaType (javax.ws.rs.core.MediaType)3