Search in sources :

Example 91 with GenericClass

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

the class ObjectPoolManager method initialisePool.

public void initialisePool() {
    if (!Properties.OBJECT_POOLS.isEmpty()) {
        String[] poolFiles = Properties.OBJECT_POOLS.split(File.pathSeparator);
        if (poolFiles.length > 1)
            LoggingUtils.getEvoLogger().info("* Reading object pools:");
        else
            LoggingUtils.getEvoLogger().info("* Reading object pool:");
        for (String fileName : poolFiles) {
            logger.info("Adding object pool from file " + fileName);
            ObjectPool pool = ObjectPool.getPoolFromFile(fileName);
            if (pool == null) {
                logger.error("Failed to load object from " + fileName);
            } else {
                LoggingUtils.getEvoLogger().info(" - Object pool " + fileName + ": " + pool.getNumberOfSequences() + " sequences for " + pool.getNumberOfClasses() + " classes");
                addPool(pool);
            }
        }
        if (logger.isDebugEnabled()) {
            for (GenericClass key : pool.keySet()) {
                logger.debug("Have sequences for " + key + ": " + pool.get(key).size());
            }
        }
    }
    if (Properties.CARVE_OBJECT_POOL) {
        CarvingManager manager = CarvingManager.getInstance();
        for (Class<?> targetClass : manager.getClassesWithTests()) {
            List<TestCase> tests = manager.getTestsForClass(targetClass);
            logger.info("Carved tests for {}: {}", targetClass.getName(), tests.size());
            GenericClass cut = new GenericClass(targetClass);
            for (TestCase test : tests) {
                this.addSequence(cut, test);
            }
        }
        logger.info("Pool after carving: " + this.getNumberOfClasses() + "/" + this.getNumberOfSequences());
    }
}
Also used : CarvingManager(org.evosuite.testcarver.extraction.CarvingManager) GenericClass(org.evosuite.utils.generic.GenericClass) TestCase(org.evosuite.testcase.TestCase)

Example 92 with GenericClass

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

the class CastClassManager method getAssignableClasses.

private List<GenericClass> getAssignableClasses(TypeVariable<?> typeVariable, boolean allowRecursion, Map<TypeVariable<?>, Type> ownerVariableMap) {
    Map<GenericClass, Integer> assignableClasses = new LinkedHashMap<GenericClass, Integer>();
    logger.debug("Getting assignable classes for type variable " + typeVariable);
    for (Entry<GenericClass, Integer> entry : classMap.entrySet()) {
        GenericClass key = entry.getKey();
        logger.debug("Current class for type variable " + typeVariable + ": " + key);
        if (!key.satisfiesBoundaries(typeVariable, ownerVariableMap)) {
            logger.debug("Bounds not satisfied");
            continue;
        }
        if (!allowRecursion && key.hasWildcardOrTypeVariables()) {
            logger.debug("Recursion not allowed but type has wildcard or type variables");
            continue;
        }
        assignableClasses.put(entry.getKey(), entry.getValue());
    }
    logger.debug("Found assignable classes: " + assignableClasses.size());
    return sortByValue(assignableClasses);
}
Also used : GenericClass(org.evosuite.utils.generic.GenericClass) LinkedHashMap(java.util.LinkedHashMap)

Example 93 with GenericClass

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

the class CastClassManager method getAssignableClasses.

private List<GenericClass> getAssignableClasses(WildcardType wildcardType, boolean allowRecursion, Map<TypeVariable<?>, Type> ownerVariableMap) {
    Map<GenericClass, Integer> assignableClasses = new LinkedHashMap<GenericClass, Integer>();
    logger.debug("Getting assignable classes for wildcard type " + wildcardType);
    for (Entry<GenericClass, Integer> entry : classMap.entrySet()) {
        GenericClass key = entry.getKey();
        logger.debug("Current class for wildcard " + wildcardType + ": " + key);
        if (!key.satisfiesBoundaries(wildcardType, ownerVariableMap)) {
            logger.debug("Does not satisfy boundaries");
            continue;
        }
        if (!allowRecursion && key.hasWildcardOrTypeVariables()) {
            logger.debug("Stopping because of type recursion");
            continue;
        }
        logger.debug("Is assignable");
        assignableClasses.put(entry.getKey(), entry.getValue());
    }
    return sortByValue(assignableClasses);
}
Also used : GenericClass(org.evosuite.utils.generic.GenericClass) LinkedHashMap(java.util.LinkedHashMap)

Example 94 with GenericClass

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

the class CastClassManager method addAssignableClass.

private boolean addAssignableClass(TypeVariable<?> typeVariable, Map<TypeVariable<?>, Type> typeMap) {
    Set<Class<?>> classes = TestCluster.getInstance().getAnalyzedClasses();
    Set<Class<?>> assignableClasses = new LinkedHashSet<Class<?>>();
    for (Class<?> clazz : classes) {
        if (!TestUsageChecker.canUse(clazz))
            continue;
        GenericClass genericClass = new GenericClass(clazz).getWithWildcardTypes();
        if (!genericClass.satisfiesBoundaries(typeVariable, typeMap)) {
            logger.debug("Not assignable: " + clazz);
        } else {
            logger.debug("Assignable");
            assignableClasses.add(clazz);
        }
    }
    for (Type t : typeMap.values()) {
        if (t instanceof WildcardType)
            // TODO: For now.
            continue;
        Class<?> clazz = GenericTypeReflector.erase(t);
        if (!TestUsageChecker.canUse(GenericTypeReflector.erase(clazz)))
            continue;
        GenericClass genericClass = new GenericClass(clazz).getWithWildcardTypes();
        if (!genericClass.satisfiesBoundaries(typeVariable, typeMap)) {
            logger.debug("Not assignable: " + clazz);
        } else {
            logger.debug("Assignable");
            assignableClasses.add(clazz);
        }
    }
    /*
		for (Type t : typeVariable.getBounds()) {
			if (typeMap.containsKey(t))
				t = typeMap.get(t);

			Class<?> clazz = GenericTypeReflector.erase(t);
			logger.debug("Checking bound: " + t);

			if (!TestClusterGenerator.canUse(clazz))
				continue;

			GenericClass genericClass = new GenericClass(t);
			//if (genericClass.hasTypeVariables()) {
			logger.debug("Has type variables: " + genericClass
			        + ", checking wildcard version with type map " + typeMap);
			GenericClass wildcardClass = genericClass.getWithWildcardTypes();
			//if (!wildcardClass.satisfiesBoundaries(typeVariable, typeMap)) {
			//	logger.debug("Not assignable: " + clazz);
			//} else {
			//	logger.debug("Assignable");
			assignableClasses.add(clazz);
			//}
			//} else {
			//	logger.debug("Adding directly: " + genericClass);
			//	assignableClasses.add(genericClass.getRawClass());
			//	classMap.put(genericClass, 10);
			//}
		}
		*/
    logger.debug("Found assignable classes for type variable " + typeVariable + ": " + assignableClasses.size());
    if (!assignableClasses.isEmpty()) {
        Class<?> clazz = Randomness.choice(assignableClasses);
        GenericClass castClass = new GenericClass(clazz);
        logger.debug("Adding cast class " + castClass);
        classMap.put(castClass, 10);
        return true;
    } else {
        InheritanceTree inheritanceTree = DependencyAnalysis.getInheritanceTree();
        Set<Class<?>> boundCandidates = new LinkedHashSet<Class<?>>();
        for (Type bound : typeVariable.getBounds()) {
            Class<?> rawBound = GenericTypeReflector.erase(bound);
            boundCandidates.add(rawBound);
            logger.debug("Getting concrete classes for " + rawBound);
            boundCandidates.addAll(ConcreteClassAnalyzer.getInstance().getConcreteClasses(rawBound, inheritanceTree));
        }
        for (Class<?> clazz : boundCandidates) {
            if (!TestUsageChecker.canUse(clazz))
                continue;
            boolean isAssignable = true;
            for (Type bound : typeVariable.getBounds()) {
                if (GenericTypeReflector.erase(bound).equals(Enum.class)) {
                    if (clazz.isEnum())
                        continue;
                }
                if (!GenericClass.isAssignable(bound, clazz)) {
                    isAssignable = false;
                    logger.debug("Not assignable: " + clazz + " to bound " + bound);
                    break;
                }
                if (bound instanceof ParameterizedType) {
                    if (Arrays.asList(((ParameterizedType) bound).getActualTypeArguments()).contains(typeVariable)) {
                        isAssignable = false;
                        break;
                    }
                }
            }
            if (isAssignable) {
                assignableClasses.add(clazz);
            }
        }
        logger.debug("After adding bounds, found " + assignableClasses.size() + " assignable classes for type variable " + typeVariable + ": " + assignableClasses);
        if (!assignableClasses.isEmpty()) {
            // TODO: Add all classes?
            // for(Class<?> clazz : assignableClasses) {
            // GenericClass castClass = new GenericClass(clazz);
            // logger.debug("Adding cast class " + castClass);
            // classMap.put(castClass, 10);
            // }
            Class<?> clazz = Randomness.choice(assignableClasses);
            GenericClass castClass = new GenericClass(clazz);
            logger.debug("Adding cast class " + castClass);
            classMap.put(castClass, 10);
            return true;
        }
    }
    return false;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ParameterizedType(java.lang.reflect.ParameterizedType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) GenericClass(org.evosuite.utils.generic.GenericClass) GenericClass(org.evosuite.utils.generic.GenericClass)

Example 95 with GenericClass

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

the class CastClassManager method initDefaultClasses.

private void initDefaultClasses() {
    classMap.put(new GenericClass(Object.class), 0);
    classMap.put(new GenericClass(String.class), 1);
    classMap.put(new GenericClass(Integer.class), 1);
}
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