Search in sources :

Example 16 with GenericAccessibleObject

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

the class TestCluster method addNumericConstructor.

/**
 * FIXME: This is a workaround for a bug where Integer is not contained in
 * the generatorCache, but there is a key. No idea how it comes to place
 *
 * @param clazz
 */
private void addNumericConstructor(GenericClass clazz) {
    if (!generatorCache.containsKey(clazz)) {
        generatorCache.put(clazz, new LinkedHashSet<GenericAccessibleObject<?>>());
    }
    if (!generators.containsKey(clazz)) {
        generators.put(clazz, new LinkedHashSet<GenericAccessibleObject<?>>());
    }
    logger.info("addNumericConstructor for class " + clazz);
    for (Constructor<?> constructor : clazz.getRawClass().getConstructors()) {
        if (constructor.getParameterTypes().length == 1) {
            Class<?> parameterClass = constructor.getParameterTypes()[0];
            if (!parameterClass.equals(String.class)) {
                GenericConstructor genericConstructor = new GenericConstructor(constructor, clazz);
                generatorCache.get(clazz).add(genericConstructor);
                generators.get(clazz).add(genericConstructor);
            }
        }
    }
    logger.info("Constructors for class " + clazz + ": " + generators.get(clazz).size());
}
Also used : GenericAccessibleObject(org.evosuite.utils.generic.GenericAccessibleObject) GenericConstructor(org.evosuite.utils.generic.GenericConstructor)

Example 17 with GenericAccessibleObject

use of org.evosuite.utils.generic.GenericAccessibleObject 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)

Example 18 with GenericAccessibleObject

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

the class TestFactory method attemptObjectGeneration.

/**
 * Try to generate an object suitable for Object.class
 *
 * @param test
 * @param position
 * @param recursionDepth
 * @param allowNull
 * @return
 * @throws ConstructionFailedException
 */
protected VariableReference attemptObjectGeneration(TestCase test, int position, int recursionDepth, boolean allowNull) throws ConstructionFailedException {
    if (allowNull && Randomness.nextDouble() <= Properties.NULL_PROBABILITY) {
        logger.debug("Using a null reference to satisfy the type: {}", Object.class);
        return createNull(test, Object.class, position, recursionDepth);
    }
    Set<GenericClass> castClasses = new LinkedHashSet<>(CastClassManager.getInstance().getCastClasses());
    // needed a copy because hasGenerator(c) does modify that set...
    List<GenericClass> classes = castClasses.stream().filter(c -> TestCluster.getInstance().hasGenerator(c) || c.isString()).collect(Collectors.toList());
    classes.add(new GenericClass(Object.class));
    // TODO if classes is empty, should we use FM here?
    GenericClass choice = Randomness.choice(classes);
    logger.debug("Chosen class for Object: {}", choice);
    if (choice.isString()) {
        return createOrReuseVariable(test, String.class, position, recursionDepth, null, allowNull, false, false);
    }
    GenericAccessibleObject<?> o = TestCluster.getInstance().getRandomGenerator(choice);
    currentRecursion.add(o);
    if (o == null) {
        if (!TestCluster.getInstance().hasGenerator(Object.class)) {
            logger.debug("We have no generator for Object.class ");
        }
        throw new ConstructionFailedException("Generator is null");
    } else if (o.isField()) {
        logger.debug("Attempting generating of Object.class via field of type Object.class");
        VariableReference ret = addField(test, (GenericField) o, position, recursionDepth + 1);
        ret.setDistance(recursionDepth + 1);
        logger.debug("Success in generating type Object.class");
        return ret;
    } else if (o.isMethod()) {
        logger.debug("Attempting generating of Object.class via method {} of type Object.class", o);
        VariableReference ret = addMethod(test, (GenericMethod) o, position, recursionDepth + 1);
        logger.debug("Success in generating type Object.class");
        ret.setDistance(recursionDepth + 1);
        return ret;
    } else if (o.isConstructor()) {
        logger.debug("Attempting generating of Object.class via constructor {} of type Object.class", o);
        VariableReference ret = addConstructor(test, (GenericConstructor) o, position, recursionDepth + 1);
        logger.debug("Success in generating Object.class");
        ret.setDistance(recursionDepth + 1);
        return ret;
    } else {
        logger.debug("No generators found for Object.class");
        throw new ConstructionFailedException("No generator found for Object.class");
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) GenericClass(org.evosuite.utils.generic.GenericClass) Arrays(java.util.Arrays) ConstructionFailedException(org.evosuite.ga.ConstructionFailedException) org.evosuite.testcase.statements(org.evosuite.testcase.statements) TimeController(org.evosuite.TimeController) EnvironmentStatements(org.evosuite.testcase.statements.environment.EnvironmentStatements) PrivateMethodStatement(org.evosuite.testcase.statements.reflection.PrivateMethodStatement) InstanceOnlyOnce(org.evosuite.testcase.jee.InstanceOnlyOnce) LoggerFactory(org.slf4j.LoggerFactory) MockList(org.evosuite.runtime.mock.MockList) ArrayList(java.util.ArrayList) EvoServletState(org.evosuite.runtime.javaee.javax.servlet.EvoServletState) ClassUtils(org.apache.commons.lang3.ClassUtils) GenericMethod(org.evosuite.utils.generic.GenericMethod) org.evosuite.testcase.variable(org.evosuite.testcase.variable) Constraints(org.evosuite.runtime.annotation.Constraints) org.evosuite.setup(org.evosuite.setup) LinkedHashSet(java.util.LinkedHashSet) InjectionSupport(org.evosuite.testcase.jee.InjectionSupport) java.lang.reflect(java.lang.reflect) AtMostOnceLogger(org.evosuite.runtime.util.AtMostOnceLogger) Properties(org.evosuite.Properties) Logger(org.slf4j.Logger) GenericTypeReflector(com.googlecode.gentyref.GenericTypeReflector) Iterator(java.util.Iterator) HttpServlet(javax.servlet.http.HttpServlet) Collection(java.util.Collection) Inputs(org.evosuite.runtime.util.Inputs) ObjectPoolManager(org.evosuite.seeding.ObjectPoolManager) GenericAccessibleObject(org.evosuite.utils.generic.GenericAccessibleObject) Set(java.util.Set) CastClassManager(org.evosuite.seeding.CastClassManager) RandomInsertion(org.evosuite.testcase.mutation.RandomInsertion) GenericField(org.evosuite.utils.generic.GenericField) ReflectionFactory(org.evosuite.testcase.statements.reflection.ReflectionFactory) Collectors(java.util.stream.Collectors) Randomness(org.evosuite.utils.Randomness) TestGenerationContext(org.evosuite.TestGenerationContext) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) List(java.util.List) PrivateFieldStatement(org.evosuite.testcase.statements.reflection.PrivateFieldStatement) Injector(org.evosuite.runtime.javaee.injection.Injector) GenericUtils(org.evosuite.utils.generic.GenericUtils) CaptureType(com.googlecode.gentyref.CaptureType) Collections(java.util.Collections) ServletSupport(org.evosuite.testcase.jee.ServletSupport) GenericClass(org.evosuite.utils.generic.GenericClass) GenericAccessibleObject(org.evosuite.utils.generic.GenericAccessibleObject) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) GenericField(org.evosuite.utils.generic.GenericField) ConstructionFailedException(org.evosuite.ga.ConstructionFailedException)

Example 19 with GenericAccessibleObject

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

the class TestFactory method getPossibleCalls.

/**
 * Retrieve all the replacement calls that can be inserted at this position
 * without changing the length
 *
 * @param returnType
 * @param objects
 * @return
 */
private List<GenericAccessibleObject<?>> getPossibleCalls(Type returnType, List<VariableReference> objects) {
    List<GenericAccessibleObject<?>> calls = new ArrayList<GenericAccessibleObject<?>>();
    Set<GenericAccessibleObject<?>> allCalls;
    try {
        allCalls = TestCluster.getInstance().getGenerators(new GenericClass(returnType), true);
    } catch (ConstructionFailedException e) {
        return calls;
    }
    for (GenericAccessibleObject<?> call : allCalls) {
        Set<Type> dependencies = null;
        if (call.isMethod()) {
            GenericMethod method = (GenericMethod) call;
            if (method.hasTypeParameters()) {
                try {
                    call = method.getGenericInstantiation(new GenericClass(returnType));
                } catch (ConstructionFailedException e) {
                    continue;
                }
            }
            if (!((GenericMethod) call).getReturnType().equals(returnType))
                continue;
            dependencies = getDependencies((GenericMethod) call);
        } else if (call.isConstructor()) {
            dependencies = getDependencies((GenericConstructor) call);
        } else if (call.isField()) {
            if (!((GenericField) call).getFieldType().equals(returnType))
                continue;
            dependencies = getDependencies((GenericField) call);
        } else {
            assert (false);
        }
        if (dependenciesSatisfied(dependencies, objects)) {
            calls.add(call);
        }
    }
    return calls;
}
Also used : GenericAccessibleObject(org.evosuite.utils.generic.GenericAccessibleObject) CaptureType(com.googlecode.gentyref.CaptureType) GenericClass(org.evosuite.utils.generic.GenericClass) ArrayList(java.util.ArrayList) GenericMethod(org.evosuite.utils.generic.GenericMethod) GenericField(org.evosuite.utils.generic.GenericField) ConstructionFailedException(org.evosuite.ga.ConstructionFailedException)

Example 20 with GenericAccessibleObject

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

the class EnvironmentTestClusterAugmenter method addEnvironmentDependency.

private void addEnvironmentDependency(Class<?> klass) {
    if (handledClasses.contains(klass.getCanonicalName()) || !TestClusterUtils.isEvoSuiteClass(klass)) {
        // already handled, or not valid
        return;
    }
    handledClasses.add(klass.getCanonicalName());
    boolean excludeClass = klass.getAnnotation(EvoSuiteClassExclude.class) != null;
    for (Method m : klass.getMethods()) {
        if (shouldSkip(excludeClass, m)) {
            continue;
        }
        GenericAccessibleObject gm = new GenericMethod(m, klass);
        GenericClass gc = new GenericClass(klass);
        TestCluster.getInstance().addModifier(gc, gm);
        testClusterGenerator.addNewDependencies(Arrays.asList(m.getParameterTypes()));
        Class<?> returnType = m.getReturnType();
        if (!returnType.equals(Void.TYPE)) {
            GenericClass genclass = new GenericClass(returnType);
            TestCluster.getInstance().invalidateGeneratorCache(genclass);
            TestCluster.getInstance().addGenerator(genclass, gm);
            addEnvironmentDependency(returnType);
        }
    }
}
Also used : GenericAccessibleObject(org.evosuite.utils.generic.GenericAccessibleObject) GenericClass(org.evosuite.utils.generic.GenericClass) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod)

Aggregations

GenericAccessibleObject (org.evosuite.utils.generic.GenericAccessibleObject)20 GenericClass (org.evosuite.utils.generic.GenericClass)14 ConstructionFailedException (org.evosuite.ga.ConstructionFailedException)11 GenericMethod (org.evosuite.utils.generic.GenericMethod)8 GenericConstructor (org.evosuite.utils.generic.GenericConstructor)7 CaptureType (com.googlecode.gentyref.CaptureType)3 Constructor (java.lang.reflect.Constructor)3 ArrayList (java.util.ArrayList)3 Entry (java.util.Map.Entry)3 Collectors (java.util.stream.Collectors)3 Properties (org.evosuite.Properties)3 TestGenerationContext (org.evosuite.TestGenerationContext)3 AtMostOnceLogger (org.evosuite.runtime.util.AtMostOnceLogger)3 Inputs (org.evosuite.runtime.util.Inputs)3 CastClassManager (org.evosuite.seeding.CastClassManager)3 TestCluster (org.evosuite.setup.TestCluster)3 InstanceOnlyOnce (org.evosuite.testcase.jee.InstanceOnlyOnce)3 Method (java.lang.reflect.Method)2 Type (java.lang.reflect.Type)2 java.util (java.util)2