Search in sources :

Example 81 with GenericClass

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

the class InsertionMutationSystemTest method getIntTest.

private TestCase getIntTest(int x) throws NoSuchMethodException, SecurityException, ConstructionFailedException, ClassNotFoundException {
    Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS);
    GenericClass clazz = new GenericClass(sut);
    DefaultTestCase test = new DefaultTestCase();
    GenericConstructor gc = new GenericConstructor(clazz.getRawClass().getConstructors()[0], clazz);
    TestFactory testFactory = TestFactory.getInstance();
    VariableReference callee = testFactory.addConstructor(test, gc, 0, 0);
    VariableReference intVar = test.addStatement(new IntPrimitiveStatement(test, x));
    Method m = clazz.getRawClass().getMethod("testMe", new Class<?>[] { int.class });
    GenericMethod method = new GenericMethod(m, sut);
    MethodStatement ms = new MethodStatement(test, method, callee, Arrays.asList(new VariableReference[] { intVar }));
    test.addStatement(ms);
    return test;
}
Also used : MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) GenericClass(org.evosuite.utils.generic.GenericClass) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)

Example 82 with GenericClass

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

the class InsertionMutationSystemTest method getTwoIntTest.

private TestCase getTwoIntTest(int x, int y) throws NoSuchMethodException, SecurityException, ConstructionFailedException, ClassNotFoundException {
    Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS);
    GenericClass clazz = new GenericClass(sut);
    DefaultTestCase test = new DefaultTestCase();
    GenericConstructor gc = new GenericConstructor(clazz.getRawClass().getConstructors()[0], clazz);
    TestFactory testFactory = TestFactory.getInstance();
    VariableReference callee = testFactory.addConstructor(test, gc, 0, 0);
    VariableReference intVar1 = test.addStatement(new IntPrimitiveStatement(test, x));
    VariableReference intVar2 = test.addStatement(new IntPrimitiveStatement(test, y));
    Method m = clazz.getRawClass().getMethod("testMe", new Class<?>[] { int.class, int.class });
    GenericMethod method = new GenericMethod(m, sut);
    MethodStatement ms = new MethodStatement(test, method, callee, Arrays.asList(new VariableReference[] { intVar1, intVar2 }));
    test.addStatement(ms);
    return test;
}
Also used : MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) GenericClass(org.evosuite.utils.generic.GenericClass) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)

Example 83 with GenericClass

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

the class TestCluster method cacheGenerators.

/**
 * Calculate and cache all generators for a particular type. All generic
 * types on the generator are instantiated according to the produced type
 *
 * @param clazz
 * @throws ConstructionFailedException
 */
private void cacheGenerators(GenericClass clazz) throws ConstructionFailedException {
    if (generatorCache.containsKey(clazz)) {
        return;
    }
    logger.debug("1. Caching generators for {}", clazz);
    Set<GenericAccessibleObject<?>> targetGenerators = new LinkedHashSet<>();
    if (clazz.isObject()) {
        logger.debug("2. Target class is object: {}", clazz);
        for (GenericClass generatorClazz : generators.keySet()) {
            if (generatorClazz.isObject()) {
                targetGenerators.addAll(generators.get(generatorClazz));
            }
        }
    } else {
        logger.debug("2. Target class is not object: {}", clazz);
        for (GenericClass generatorClazz : generators.keySet()) {
            if (generatorClazz.canBeInstantiatedTo(clazz)) {
                // logger.debug("4. generator " + generatorClazz + " can be instantiated to " + clazz);
                GenericClass instantiatedGeneratorClazz = generatorClazz.getWithParametersFromSuperclass(clazz);
                logger.debug("Instantiated type: {} for {} and superclass {}", instantiatedGeneratorClazz, generatorClazz, clazz);
                for (GenericAccessibleObject<?> generator : generators.get(generatorClazz)) {
                    logger.debug("5. current instantiated generator: {}", generator);
                    try {
                        if ((generator.isMethod() || generator.isField()) && clazz.isParameterizedType() && GenericClass.isMissingTypeParameters(generator.getGenericGeneratedType())) {
                            logger.debug("No type parameters present in generator for {}: {}", clazz, generator);
                            continue;
                        }
                        // Set owner type parameters from new return type
                        GenericAccessibleObject<?> newGenerator = generator.copyWithOwnerFromReturnType(instantiatedGeneratorClazz);
                        boolean hadTypeParameters = false;
                        // Instantiate potential further type variables based on type variables of return type
                        if (newGenerator.getOwnerClass().hasWildcardOrTypeVariables()) {
                            logger.debug("Instantiating type parameters of owner type: {}", newGenerator.getOwnerClass());
                            GenericClass concreteClass = newGenerator.getOwnerClass().getGenericInstantiation(clazz.getTypeVariableMap());
                            newGenerator = newGenerator.copyWithNewOwner(concreteClass);
                            hadTypeParameters = true;
                        }
                        // If it is a generic method, instantiate generic type variables for the produced class
                        if (newGenerator.hasTypeParameters()) {
                            logger.debug("Instantiating type parameters");
                            /*
								 * TODO:
								 * public class Foo<X> {
								 *   public <X> Foo<X> getFoo() {
								 *     // ...
								 *   }
								 * }
								 *
								 * Here X and X are two different type variables, and these need to be matched here!
								 *
								 */
                            newGenerator = newGenerator.getGenericInstantiationFromReturnValue(clazz);
                            hadTypeParameters = true;
                        // newGenerator = newGenerator.getGenericInstantiation(clazz);
                        }
                        logger.debug("Current generator: {}", newGenerator);
                        if ((!hadTypeParameters && generatorClazz.equals(clazz)) || clazz.isAssignableFrom(newGenerator.getGeneratedType())) {
                            logger.debug("Got new generator: {} which generated: {}", newGenerator, newGenerator.getGeneratedClass());
                            logger.debug("{} vs {}", (!hadTypeParameters && generatorClazz.equals(clazz)), clazz.isAssignableFrom(newGenerator.getGeneratedType()));
                            targetGenerators.add(newGenerator);
                        } else if (logger.isDebugEnabled()) {
                            logger.debug("New generator not assignable: {}", newGenerator);
                            logger.debug("Had type parameters: {}", hadTypeParameters);
                            logger.debug("generatorClazz.equals(clazz): {}", generatorClazz.equals(clazz));
                            try {
                                logger.debug("clazz.isAssignableFrom({}): ", newGenerator.getGeneratedType());
                                logger.debug("                        {}", clazz.isAssignableFrom(newGenerator.getGeneratedType()));
                            } catch (Throwable t) {
                                logger.debug("Error", t);
                            }
                        }
                    } catch (ConstructionFailedException e) {
                        logger.debug("5. ERROR", e);
                    }
                }
            // FIXME:
            // There are cases where this might lead to relevant cast classes not being included
            // but in manycases it will pull in large numbers of useless dependencies.
            // Commented out for now, until we find a case where the problem can be properly studied.
            // } else {
            // logger.debug("4. generator {} CANNOT be instantiated to {}", generatorClazz, clazz);
            // for(GenericClass boundClass : generatorClazz.getGenericBounds()) {
            // CastClassManager.getInstance().addCastClass(boundClass, 0);
            // }
            }
        }
        logger.debug("Found generators for {}: {}", clazz, targetGenerators.size());
    }
    logger.debug("]");
    generatorCache.put(clazz, targetGenerators);
}
Also used : GenericAccessibleObject(org.evosuite.utils.generic.GenericAccessibleObject) GenericClass(org.evosuite.utils.generic.GenericClass) ConstructionFailedException(org.evosuite.ga.ConstructionFailedException)

Example 84 with GenericClass

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

the class TestCluster method addCastClassForContainer.

/**
 * Add a new class observed at runtime for container methods
 *
 * @param clazz
 */
public void addCastClassForContainer(Class<?> clazz) {
    if (TestUsageChecker.canUse(clazz)) {
        CastClassManager.getInstance().addCastClass(clazz, 1);
        clearGeneratorCache(new GenericClass(clazz));
    }
}
Also used : GenericClass(org.evosuite.utils.generic.GenericClass)

Example 85 with GenericClass

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

the class TestCluster method getRandomTestCall.

/**
 * Get random method or constructor of unit under test
 *
 * @return
 * @throws ConstructionFailedException
 */
public GenericAccessibleObject<?> getRandomTestCall(TestCase test) throws ConstructionFailedException {
    List<GenericAccessibleObject<?>> candidateTestMethods = new ArrayList<>(testMethods);
    if (candidateTestMethods.isEmpty()) {
        logger.debug("No more calls");
        // TODO: return null, or throw ConstructionFailedException?
        return null;
    }
    // If test already has a SUT call, remove all constructors
    if (doesTestHaveSUTInstance(test)) {
        candidateTestMethods = filterConstructors(candidateTestMethods);
        // It may happen that all remaining test calls are constructors. In this case it's ok.
        if (candidateTestMethods.isEmpty())
            candidateTestMethods = new ArrayList<>(testMethods);
    }
    if (Properties.SORT_CALLS) {
        candidateTestMethods = sortCalls(candidateTestMethods);
    }
    GenericAccessibleObject<?> choice = Properties.SORT_CALLS ? ListUtil.selectRankBiased(candidateTestMethods) : Randomness.choice(candidateTestMethods);
    logger.debug("Chosen call: " + choice);
    if (choice.getOwnerClass().hasWildcardOrTypeVariables()) {
        GenericClass concreteClass = choice.getOwnerClass().getGenericInstantiation();
        logger.debug("Concrete class is: " + concreteClass.getTypeName());
        choice = choice.copyWithNewOwner(concreteClass);
        logger.debug("Concrete class is: " + choice.getOwnerClass().getTypeName());
        logger.debug("Type variables: " + choice.getOwnerClass().getTypeVariableMap());
        logger.debug(Arrays.asList(choice.getTypeParameters()).toString());
        logger.debug("Chosen call with generic parameter set: " + choice);
        logger.debug("Call owner type: " + choice.getOwnerClass().getTypeName());
    }
    if (choice.hasTypeParameters()) {
        logger.debug("Instantiating chosen call: " + choice);
        choice = choice.getGenericInstantiation();
        logger.debug("Chosen instantiation: " + choice);
    }
    return choice;
}
Also used : GenericAccessibleObject(org.evosuite.utils.generic.GenericAccessibleObject) 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