Search in sources :

Example 16 with GenericClass

use of org.evosuite.utils.generic.GenericClass in project evosuite by EvoSuite.

the class CastClassManager method addCastClass.

public void addCastClass(String className, int depth) {
    try {
        Class<?> clazz = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(className);
        GenericClass castClazz = new GenericClass(clazz);
        addCastClass(castClazz.getWithWildcardTypes(), depth);
    } catch (ClassNotFoundException e) {
        // Ignore
        logger.debug("Error including cast class " + className + " because: " + e);
    }
}
Also used : GenericClass(org.evosuite.utils.generic.GenericClass)

Example 17 with GenericClass

use of org.evosuite.utils.generic.GenericClass in project evosuite by EvoSuite.

the class TestCodeVisitor method getClassName.

/**
 * <p>
 * getClassName
 * </p>
 *
 * @param clazz
 *            a {@link java.lang.Class} object.
 * @return a {@link java.lang.String} object.
 */
public String getClassName(Class<?> clazz) {
    if (classNames.containsKey(clazz))
        return classNames.get(clazz);
    if (clazz.isArray()) {
        return getClassName(clazz.getComponentType()) + "[]";
    }
    GenericClass c = new GenericClass(clazz);
    String name = c.getSimpleName();
    if (classNames.values().contains(name)) {
        name = clazz.getCanonicalName();
    } else {
        /*
			 * If e.g. there is a foo.bar.IllegalStateException with
			 * foo.bar being the SUT package, then we need to use the
			 * full package name for java.lang.IllegalStateException
			 */
        String fullName = Properties.CLASS_PREFIX + "." + name;
        if (!fullName.equals(clazz.getCanonicalName())) {
            try {
                if (ResourceList.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).hasClass(fullName)) {
                    name = clazz.getCanonicalName();
                }
            } catch (IllegalArgumentException e) {
            // If the classpath is not correct, then we just don't check
            // because that cannot happen in regular EvoSuite use, only
            // from test cases
            }
        }
    }
    // Ensure outer classes are imported as well
    Class<?> outerClass = clazz.getEnclosingClass();
    if (outerClass != null) {
        String enclosingName = getClassName(outerClass);
        String simpleOuterName = outerClass.getSimpleName() + ".";
        if (simpleOuterName.equals(enclosingName)) {
            name = enclosingName + name.substring(simpleOuterName.length());
        } else {
            name = enclosingName + name.substring(name.lastIndexOf(simpleOuterName) + simpleOuterName.length() - 1);
        }
    }
    Class<?> declaringClass = clazz.getDeclaringClass();
    if (declaringClass != null) {
        getClassName(declaringClass);
    }
    // We can't use "Test" because of JUnit
    if (name.equals("Test")) {
        name = clazz.getCanonicalName();
    }
    classNames.put(clazz, name);
    return name;
}
Also used : GenericClass(org.evosuite.utils.generic.GenericClass)

Example 18 with GenericClass

use of org.evosuite.utils.generic.GenericClass in project evosuite by EvoSuite.

the class TestCodeVisitor method getParameterStringForFMthatReturnPrimitive.

private String getParameterStringForFMthatReturnPrimitive(Class<?> returnType, List<VariableReference> parameters) {
    assert returnType.isPrimitive();
    String parameterString = "";
    for (int i = 0; i < parameters.size(); i++) {
        if (i > 0) {
            parameterString += ", ";
        }
        String name = getVariableName(parameters.get(i));
        Class<?> parameterType = parameters.get(i).getVariableClass();
        if (returnType.equals(parameterType)) {
            parameterString += name;
            continue;
        }
        GenericClass parameterClass = new GenericClass(parameterType);
        if (parameterClass.isWrapperType()) {
            boolean isRightWrapper = false;
            if (Integer.class.equals(parameterClass.getRawClass())) {
                isRightWrapper = returnType.equals(Integer.TYPE);
            } else if (Character.class.equals(parameterClass.getRawClass())) {
                isRightWrapper = returnType.equals(Character.TYPE);
            } else if (Boolean.class.equals(parameterClass.getRawClass())) {
                isRightWrapper = returnType.equals(Boolean.TYPE);
            } else if (Float.class.equals(parameterClass.getRawClass())) {
                isRightWrapper = returnType.equals(Float.TYPE);
            } else if (Double.class.equals(parameterClass.getRawClass())) {
                isRightWrapper = returnType.equals(Double.TYPE);
            } else if (Long.class.equals(parameterClass.getRawClass())) {
                isRightWrapper = returnType.equals(Long.TYPE);
            } else if (Short.class.equals(parameterClass.getRawClass())) {
                isRightWrapper = returnType.equals(Short.TYPE);
            } else if (Byte.class.equals(parameterClass.getRawClass())) {
                isRightWrapper = returnType.equals(Byte.TYPE);
            }
            if (isRightWrapper) {
                parameterString += name;
                continue;
            }
        }
        // if we arrive here, it means types are different and not a right wrapper (eg Integer for int)
        parameterString += "(" + returnType.getName() + ")" + name;
        if (parameterClass.isWrapperType()) {
            if (Integer.class.equals(parameterClass.getRawClass())) {
                parameterString += ".intValue()";
            } else if (Character.class.equals(parameterClass.getRawClass())) {
                parameterString += ".charValue()";
            } else if (Boolean.class.equals(parameterClass.getRawClass())) {
                parameterString += ".booleanValue()";
            } else if (Float.class.equals(parameterClass.getRawClass())) {
                parameterString += ".floatValue()";
            } else if (Double.class.equals(parameterClass.getRawClass())) {
                parameterString += ".doubleValue()";
            } else if (Long.class.equals(parameterClass.getRawClass())) {
                parameterString += ".longValue()";
            } else if (Short.class.equals(parameterClass.getRawClass())) {
                parameterString += ".shortValue()";
            } else if (Byte.class.equals(parameterClass.getRawClass())) {
                parameterString += ".byteValue()";
            }
        }
    }
    return parameterString;
}
Also used : GenericClass(org.evosuite.utils.generic.GenericClass)

Example 19 with GenericClass

use of org.evosuite.utils.generic.GenericClass 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 20 with GenericClass

use of org.evosuite.utils.generic.GenericClass in project evosuite by EvoSuite.

the class TestFactory method createVariable.

private VariableReference createVariable(TestCase test, Type parameterType, int position, int recursionDepth, VariableReference exclude, boolean allowNull, boolean excludeCalleeGenerators, boolean canUseMocks, boolean canReuseExistingVariables) throws ConstructionFailedException {
    GenericClass clazz = new GenericClass(parameterType);
    if (clazz.hasWildcardOrTypeVariables()) {
        logger.debug("Getting generic instantiation of {}", clazz);
        if (exclude != null)
            clazz = clazz.getGenericInstantiation(exclude.getGenericClass().getTypeVariableMap());
        else
            clazz = clazz.getGenericInstantiation();
        parameterType = clazz.getType();
    }
    if (clazz.isEnum() || clazz.isPrimitive() || clazz.isWrapperType() || clazz.isObject() || clazz.isClass() || EnvironmentStatements.isEnvironmentData(clazz.getRawClass()) || clazz.isString() || clazz.isArray() || TestCluster.getInstance().hasGenerator(parameterType) || Properties.P_FUNCTIONAL_MOCKING > 0 || Properties.MOCK_IF_NO_GENERATOR) {
        logger.debug(" Generating new object of type {}", parameterType);
        // FIXME exclude methods
        VariableReference generatorRefToExclude = null;
        if (excludeCalleeGenerators) {
            generatorRefToExclude = exclude;
        }
        VariableReference reference = attemptGeneration(test, parameterType, position, recursionDepth, allowNull, generatorRefToExclude, canUseMocks, canReuseExistingVariables);
        assert !(!allowNull && ConstraintHelper.isNull(reference, test));
        assert canUseMocks || !(test.getStatement(reference.getStPosition()) instanceof FunctionalMockStatement);
        if (reference.getStPosition() < position && ConstraintHelper.getLastPositionOfBounded(reference, test) >= position) {
            AtMostOnceLogger.warn(logger, "Bounded variable issue when calling createVariable()");
            return null;
        }
        return reference;
    }
    return null;
}
Also used : GenericClass(org.evosuite.utils.generic.GenericClass)

Aggregations

GenericClass (org.evosuite.utils.generic.GenericClass)144 Test (org.junit.Test)64 GenericMethod (org.evosuite.utils.generic.GenericMethod)40 Method (java.lang.reflect.Method)36 VariableReference (org.evosuite.testcase.variable.VariableReference)25 GenericConstructor (org.evosuite.utils.generic.GenericConstructor)24 WildcardType (java.lang.reflect.WildcardType)22 TypeToken (com.googlecode.gentyref.TypeToken)20 ConstructionFailedException (org.evosuite.ga.ConstructionFailedException)17 Type (java.lang.reflect.Type)16 ArrayList (java.util.ArrayList)16 GenericAccessibleObject (org.evosuite.utils.generic.GenericAccessibleObject)16 MethodStatement (org.evosuite.testcase.statements.MethodStatement)15 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)15 ParameterizedType (java.lang.reflect.ParameterizedType)14 WildcardTypeImpl (org.evosuite.utils.generic.WildcardTypeImpl)10 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)9 AnnotatedType (java.lang.reflect.AnnotatedType)8 LinkedList (java.util.LinkedList)8 List (java.util.List)7