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