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;
}
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;
}
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);
}
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));
}
}
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;
}
Aggregations