Search in sources :

Example 86 with GenericClass

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

the class TestCluster method invalidateGeneratorCache.

public void invalidateGeneratorCache(GenericClass klass) {
    Iterator<Map.Entry<GenericClass, Set<GenericAccessibleObject<?>>>> iter = generatorCache.entrySet().iterator();
    while (iter.hasNext()) {
        Map.Entry entry = iter.next();
        GenericClass gen = (GenericClass) entry.getKey();
        if (gen.isAssignableFrom(klass)) {
            iter.remove();
        }
    }
}
Also used : GenericAccessibleObject(org.evosuite.utils.generic.GenericAccessibleObject) Entry(java.util.Map.Entry) Entry(java.util.Map.Entry) GenericClass(org.evosuite.utils.generic.GenericClass)

Example 87 with GenericClass

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

the class TestCluster method getRandomizedCallsToEnvironment.

public List<GenericAccessibleObject<?>> getRandomizedCallsToEnvironment() {
    if (environmentMethods.isEmpty()) {
        return null;
    }
    List<GenericAccessibleObject<?>> list = new ArrayList<>();
    for (GenericAccessibleObject<?> obj : environmentMethods) {
        try {
            if (obj.getOwnerClass().hasWildcardOrTypeVariables()) {
                GenericClass concreteClass = obj.getOwnerClass().getGenericInstantiation();
                obj = obj.copyWithNewOwner(concreteClass);
            }
            if (obj.hasTypeParameters()) {
                obj = obj.getGenericInstantiation();
            }
        } catch (ConstructionFailedException e) {
            logger.error("Failed generic instantiation in " + obj);
            continue;
        }
        list.add(obj);
    }
    Collections.shuffle(list);
    return list;
}
Also used : GenericAccessibleObject(org.evosuite.utils.generic.GenericAccessibleObject) GenericClass(org.evosuite.utils.generic.GenericClass) ConstructionFailedException(org.evosuite.ga.ConstructionFailedException)

Example 88 with GenericClass

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

the class TestCluster method getRandomObjectGenerator.

/**
 * Randomly select a generator for an Object.class instance
 *
 * @return a generator of type GenericAccessibleObject<?> or <code>null</code>
 * @throws ConstructionFailedException
 */
public GenericAccessibleObject<?> getRandomObjectGenerator() throws ConstructionFailedException {
    logger.debug("Getting random object generator");
    GenericAccessibleObject<?> generator = Randomness.choice(getObjectGenerators());
    if (generator == null) {
        // should NOT occur (from getObjectGenerators())
        logger.warn("Random object generator is null");
        throw new ConstructionFailedException("Random object generator is null");
    }
    if (generator.getOwnerClass().hasWildcardOrTypeVariables()) {
        logger.debug("Generator has wildcard or type: " + generator);
        GenericClass concreteClass = generator.getOwnerClass().getGenericInstantiation();
        generator = generator.copyWithNewOwner(concreteClass);
    }
    if (generator.hasTypeParameters()) {
        logger.debug("Generator has type parameters");
        generator = generator.getGenericInstantiation();
    }
    return generator;
}
Also used : GenericClass(org.evosuite.utils.generic.GenericClass) ConstructionFailedException(org.evosuite.ga.ConstructionFailedException)

Example 89 with GenericClass

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

the class TestCluster method getRandomGenerator.

/**
 * Randomly select one generator
 *
 * @param clazz
 * @return
 * @throws ConstructionFailedException
 */
public GenericAccessibleObject<?> getRandomGenerator(GenericClass clazz) throws ConstructionFailedException {
    if (clazz.hasWildcardOrTypeVariables()) {
        GenericClass concreteClass = clazz.getGenericInstantiation();
        if (concreteClass.hasWildcardOrTypeVariables())
            throw new ConstructionFailedException("Could not found concrete instantiation of generic type");
        return getRandomGenerator(concreteClass);
    }
    GenericAccessibleObject<?> generator = null;
    if (isSpecialCase(clazz)) {
        Collection<GenericAccessibleObject<?>> generators = getGeneratorsForSpecialCase(clazz);
        if (generators.isEmpty()) {
            logger.warn("No generators for class: " + clazz);
        }
        generator = Randomness.choice(generators);
    } else {
        if (!hasGenerator(clazz))
            throw new ConstructionFailedException("No generators of type " + clazz);
        generator = Randomness.choice(generatorCache.get(clazz));
    }
    if (generator == null)
        throw new ConstructionFailedException("No generators of type " + clazz);
    if (generator.hasTypeParameters()) {
        generator = generator.getGenericInstantiation(clazz);
    }
    return generator;
}
Also used : GenericAccessibleObject(org.evosuite.utils.generic.GenericAccessibleObject) GenericClass(org.evosuite.utils.generic.GenericClass) ConstructionFailedException(org.evosuite.ga.ConstructionFailedException)

Example 90 with GenericClass

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

the class TestCluster method getTestCalls.

/**
 * Get a list of all test calls (i.e., constructors and methods)
 *
 * @return
 * @throws ConstructionFailedException
 */
public List<GenericAccessibleObject<?>> getTestCalls() {
    // TODO: Check for generic methods
    List<GenericAccessibleObject<?>> result = new ArrayList<>();
    for (GenericAccessibleObject<?> ao : testMethods) {
        if (ao.getOwnerClass().hasWildcardOrTypeVariables()) {
            try {
                GenericClass concreteClass = ao.getOwnerClass().getGenericInstantiation();
                result.add(ao.copyWithNewOwner(concreteClass));
            } catch (ConstructionFailedException e) {
                logger.debug("Failed to instantiate " + ao);
            }
        } else {
            result.add(ao);
        }
    }
    return result;
}
Also used : GenericAccessibleObject(org.evosuite.utils.generic.GenericAccessibleObject) GenericClass(org.evosuite.utils.generic.GenericClass) ConstructionFailedException(org.evosuite.ga.ConstructionFailedException)

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