Search in sources :

Example 71 with GenericClass

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

the class EnumPrimitiveStatement method readObject.

@SuppressWarnings("unchecked")
private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
    GenericClass enumGenericClass = (GenericClass) ois.readObject();
    int pos = ois.readInt();
    enumClass = (Class<T>) enumGenericClass.getRawClass();
    try {
        if (enumClass.getEnumConstants().length > 0) {
            this.value = enumClass.getEnumConstants()[0];
            constants = enumClass.getEnumConstants();
            if (constants.length > 0)
                value = constants[pos];
        } else {
            // Coping with empty enums is a bit of a mess
            constants = (T[]) new Enum[0];
        }
    } catch (Throwable t) {
        // Loading the Enum class might fail
        constants = (T[]) new Enum[0];
    }
}
Also used : GenericClass(org.evosuite.utils.generic.GenericClass)

Example 72 with GenericClass

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

the class PrivateMethodStatement method getReflectionParams.

private static List<VariableReference> getReflectionParams(TestCase tc, Class<?> klass, Method method, VariableReference callee, List<VariableReference> inputs) {
    List<VariableReference> list = new ArrayList<>(3 + inputs.size() * 2);
    list.add(new ConstantValue(tc, new GenericClass(Class.class), klass));
    list.add(callee);
    list.add(new ConstantValue(tc, new GenericClass(String.class), method.getName()));
    Class<?>[] parameterTypes = method.getParameterTypes();
    assert (parameterTypes.length == inputs.size());
    for (int parameterNum = 0; parameterNum < parameterTypes.length; parameterNum++) {
        VariableReference vr = inputs.get(parameterNum);
        list.add(vr);
        list.add(new ConstantValue(tc, new GenericClass(Class.class), parameterTypes[parameterNum]));
    }
    return list;
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) GenericClass(org.evosuite.utils.generic.GenericClass) ArrayList(java.util.ArrayList) GenericClass(org.evosuite.utils.generic.GenericClass) ConstantValue(org.evosuite.testcase.variable.ConstantValue)

Example 73 with GenericClass

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

the class TestClusterGenerator method addDependency.

private void addDependency(GenericClass clazz, int recursionLevel) {
    clazz = clazz.getRawGenericClass();
    if (analyzedClasses.contains(clazz.getRawClass()))
        return;
    if (clazz.isPrimitive())
        return;
    if (clazz.isString())
        return;
    if (clazz.getRawClass().equals(Enum.class))
        return;
    if (clazz.isArray()) {
        addDependency(new GenericClass(clazz.getComponentType()), recursionLevel);
        return;
    }
    if (!TestUsageChecker.canUse(clazz.getRawClass()))
        return;
    Class<?> mock = MockList.getMockClass(clazz.getRawClass().getCanonicalName());
    if (mock != null) {
        /*
			 * If we are mocking this class, then such class should not be used
			 * in the generated JUnit test cases, but rather its mock.
			 */
        logger.debug("Adding mock {} instead of {}", mock, clazz);
        clazz = new GenericClass(mock);
    } else {
        if (!TestClusterUtils.checkIfCanUse(clazz.getClassName())) {
            return;
        }
    }
    for (DependencyPair pair : dependencies) {
        if (pair.getDependencyClass().equals(clazz)) {
            return;
        }
    }
    if (analyzedAbstractClasses.contains(clazz)) {
        return;
    }
    logger.debug("Getting concrete classes for " + clazz.getClassName());
    ConstantPoolManager.getInstance().addNonSUTConstant(Type.getType(clazz.getRawClass()));
    List<Class<?>> actualClasses = new ArrayList<>(ConcreteClassAnalyzer.getInstance().getConcreteClasses(clazz.getRawClass(), inheritanceTree));
    // Randomness.shuffle(actualClasses);
    logger.debug("Concrete classes for " + clazz.getClassName() + ": " + actualClasses.size());
    // dependencies.add(new Pair(recursionLevel,
    // Randomness.choice(actualClasses)));
    analyzedAbstractClasses.add(clazz);
    for (Class<?> targetClass : actualClasses) {
        logger.debug("Adding concrete class: " + targetClass);
        dependencies.add(new DependencyPair(recursionLevel, targetClass));
    // if(++num >= Properties.NUM_CONCRETE_SUBTYPES)
    // break;
    }
}
Also used : GenericClass(org.evosuite.utils.generic.GenericClass) ArrayList(java.util.ArrayList) GenericClass(org.evosuite.utils.generic.GenericClass)

Example 74 with GenericClass

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

the class TestClusterGenerator method addDependencyClass.

private boolean addDependencyClass(GenericClass clazz, int recursionLevel) {
    if (recursionLevel > Properties.CLUSTER_RECURSION) {
        logger.debug("Maximum recursion level reached, not adding dependency {}", clazz.getClassName());
        return false;
    }
    clazz = clazz.getRawGenericClass();
    if (analyzedClasses.contains(clazz.getRawClass())) {
        return true;
    }
    analyzedClasses.add(clazz.getRawClass());
    // generic components during runtime
    if (clazz.isAssignableTo(Collection.class) || clazz.isAssignableTo(Map.class)) {
        if (clazz.getNumParameters() > 0) {
            containerClasses.add(clazz.getRawClass());
        }
    }
    if (clazz.isString()) {
        return false;
    }
    try {
        TestCluster cluster = TestCluster.getInstance();
        logger.debug("Adding dependency class " + clazz.getClassName());
        if (!TestUsageChecker.canUse(clazz.getRawClass())) {
            logger.info("*** Cannot use class: " + clazz.getClassName());
            return false;
        }
        // Add all constructors
        for (Constructor<?> constructor : TestClusterUtils.getConstructors(clazz.getRawClass())) {
            String name = "<init>" + org.objectweb.asm.Type.getConstructorDescriptor(constructor);
            if (Properties.TT) {
                String orig = name;
                name = BooleanTestabilityTransformation.getOriginalNameDesc(clazz.getClassName(), "<init>", org.objectweb.asm.Type.getConstructorDescriptor(constructor));
                if (!orig.equals(name))
                    logger.info("TT name: " + orig + " -> " + name);
            }
            if (TestUsageChecker.canUse(constructor)) {
                GenericConstructor genericConstructor = new GenericConstructor(constructor, clazz);
                try {
                    // .getWithWildcardTypes(),
                    cluster.addGenerator(// .getWithWildcardTypes(),
                    clazz, genericConstructor);
                    addDependencies(genericConstructor, recursionLevel + 1);
                    if (logger.isDebugEnabled()) {
                        logger.debug("Keeping track of " + constructor.getDeclaringClass().getName() + "." + constructor.getName() + org.objectweb.asm.Type.getConstructorDescriptor(constructor));
                    }
                } catch (Throwable t) {
                    logger.info("Error adding constructor {}: {}", constructor.getName(), t.getMessage());
                }
            } else {
                logger.debug("Constructor cannot be used: {}", constructor);
            }
        }
        // Add all methods
        for (Method method : TestClusterUtils.getMethods(clazz.getRawClass())) {
            String name = method.getName() + org.objectweb.asm.Type.getMethodDescriptor(method);
            if (Properties.TT) {
                String orig = name;
                name = BooleanTestabilityTransformation.getOriginalNameDesc(clazz.getClassName(), method.getName(), org.objectweb.asm.Type.getMethodDescriptor(method));
                if (!orig.equals(name))
                    logger.info("TT name: " + orig + " -> " + name);
            }
            if (TestUsageChecker.canUse(method, clazz.getRawClass()) && !method.getName().equals("hashCode")) {
                logger.debug("Adding method " + clazz.getClassName() + "." + method.getName() + org.objectweb.asm.Type.getMethodDescriptor(method));
                // TODO: Generic methods cause some troubles, but
                // if (method.getTypeParameters().length > 0) {
                // logger.info("Type parameters in methods are not handled yet, skipping " + method);
                // continue;
                // }
                GenericMethod genericMethod = new GenericMethod(method, clazz);
                try {
                    addDependencies(genericMethod, recursionLevel + 1);
                    if (!Properties.PURE_INSPECTORS) {
                        cluster.addModifier(new GenericClass(clazz), genericMethod);
                    } else {
                        if (!CheapPurityAnalyzer.getInstance().isPure(method)) {
                            cluster.addModifier(new GenericClass(clazz), genericMethod);
                        }
                    }
                    GenericClass retClass = new GenericClass(method.getReturnType());
                    // Only use as generator if its not any of the types with special treatment
                    if (!retClass.isPrimitive() && !retClass.isVoid() && !retClass.isObject() && !retClass.isString()) {
                        // .getWithWildcardTypes(),
                        cluster.addGenerator(// .getWithWildcardTypes(),
                        retClass, genericMethod);
                    }
                } catch (Throwable t) {
                    logger.info("Error adding method " + method.getName() + ": " + t.getMessage());
                }
            } else {
                logger.debug("Method cannot be used: " + method);
            }
        }
        // Add all fields
        for (Field field : TestClusterUtils.getFields(clazz.getRawClass())) {
            logger.debug("Checking field " + field);
            if (TestUsageChecker.canUse(field, clazz.getRawClass())) {
                logger.debug("Adding field " + field + " for class " + clazz);
                try {
                    GenericField genericField = new GenericField(field, clazz);
                    GenericClass retClass = new GenericClass(field.getType());
                    // Only use as generator if its not any of the types with special treatment
                    if (!retClass.isPrimitive() && !retClass.isObject() && !retClass.isString())
                        cluster.addGenerator(new GenericClass(field.getGenericType()), genericField);
                    final boolean isFinalField = isFinalField(field);
                    if (!isFinalField) {
                        // .getWithWildcardTypes(),
                        cluster.addModifier(// .getWithWildcardTypes(),
                        clazz, genericField);
                        addDependencies(genericField, recursionLevel + 1);
                    }
                } catch (Throwable t) {
                    logger.info("Error adding field " + field.getName() + ": " + t.getMessage());
                }
            } else {
                logger.debug("Field cannot be used: " + field);
            }
        }
        logger.info("Finished analyzing " + clazz.getTypeName() + " at recursion level " + recursionLevel);
        cluster.getAnalyzedClasses().add(clazz.getRawClass());
    } catch (Throwable t) {
        /*
			 * NOTE: this is a problem we know it can happen in some cases in
			 * SF110, but don't have a real solution now. As it is bound to
			 * happen, we try to minimize the logging (eg no stack trace),
			 * although we still need to log it
			 */
        logger.error("Problem for " + Properties.TARGET_CLASS + ". Failed to add dependencies for class " + clazz.getClassName() + ": " + t + "\n" + Arrays.asList(t.getStackTrace()));
        return false;
    }
    return true;
}
Also used : GenericConstructor(org.evosuite.utils.generic.GenericConstructor) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) GenericField(org.evosuite.utils.generic.GenericField) Field(java.lang.reflect.Field) GenericClass(org.evosuite.utils.generic.GenericClass) Collection(java.util.Collection) Map(java.util.Map) GenericField(org.evosuite.utils.generic.GenericField)

Example 75 with GenericClass

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

the class TestClusterGenerator method addCastClassForContainer.

/**
 * Update
 *
 * @param clazz
 */
private void addCastClassForContainer(Class<?> clazz) {
    if (concreteCastClasses.contains(clazz))
        return;
    concreteCastClasses.add(clazz);
    // TODO: What if this is generic again?
    genericCastClasses.add(new GenericClass(clazz));
    CastClassManager.getInstance().addCastClass(clazz, 1);
    TestCluster.getInstance().clearGeneratorCache(new GenericClass(clazz));
}
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