Search in sources :

Example 51 with CtType

use of spoon.reflect.declaration.CtType in project spoon by INRIA.

the class AbstractCtPackageAssert method isEqualTo.

/**
 * Verifies that the actual value is equal to the given one.
 *
 * @param expected
 * 		The expected package.
 * @return {@code this} assertion object.
 */
public T isEqualTo(CtPackage expected) {
    assertNotNull(expected);
    if (!actual.getSimpleName().equals(expected.getSimpleName())) {
        throw new AssertionError(String.format("The actual package named %1$s isn't equals to the expected package named %2$s", actual.getSimpleName(), expected.getSimpleName()));
    }
    if (processors != null && !processors.isEmpty()) {
        process(actual.getFactory(), processors);
    }
    class TypeComparator implements Comparator<CtType<?>> {

        @Override
        public int compare(CtType<?> o1, CtType<?> o2) {
            return o1.getSimpleName().compareTo(o2.getSimpleName());
        }
    }
    final List<CtType<?>> actualTypes = new ArrayList<>(actual.getTypes());
    Collections.sort(actualTypes, new TypeComparator());
    final List<CtType<?>> expectedTypes = new ArrayList<>(expected.getTypes());
    Collections.sort(expectedTypes, new TypeComparator());
    for (int i = 0; i < actual.getTypes().size(); i++) {
        final CtType<?> actualType = actualTypes.get(i);
        final CtType<?> expectedType = expectedTypes.get(i);
        if (!actualType.toString().equals(expectedType.toString())) {
            throw new AssertionError(String.format("%1$s and %2$s aren't equals.", actualType.getShortRepresentation(), expectedType.getShortRepresentation()));
        }
    }
    class PackageComparator implements Comparator<CtPackage> {

        @Override
        public int compare(CtPackage o1, CtPackage o2) {
            return o1.getSimpleName().compareTo(o2.getSimpleName());
        }
    }
    final List<CtPackage> actualPackages = new ArrayList<>(actual.getPackages());
    Collections.sort(actualPackages, new PackageComparator());
    final List<CtPackage> expectedPackages = new ArrayList<>(expected.getPackages());
    Collections.sort(expectedPackages, new PackageComparator());
    for (int i = 0; i < actualPackages.size(); i++) {
        final CtPackage actualPackage = actualPackages.get(i);
        final CtPackage expectedPackage = expectedPackages.get(i);
        assertThat(actualPackage).isEqualTo(expectedPackage);
    }
    return this.myself;
}
Also used : CtType(spoon.reflect.declaration.CtType) ArrayList(java.util.ArrayList) CtPackage(spoon.reflect.declaration.CtPackage) Comparator(java.util.Comparator)

Example 52 with CtType

use of spoon.reflect.declaration.CtType in project spoon by INRIA.

the class AbstractFileAssert method isEqualTo.

/**
 * Verifies that the actual value is equal to the given one.
 *
 * @param expected
 * 		The expected location of source code.
 * @return {@code this} assertion object.
 */
public T isEqualTo(File expected) {
    assertNotNull(expected);
    assertExists(expected);
    final Factory actualFactory = build(actual);
    final Factory expectedFactory = build(expected);
    process(actualFactory, processors);
    final List<CtType<?>> allActual = actualFactory.Type().getAll();
    final List<CtType<?>> allExpected = expectedFactory.Type().getAll();
    for (int i = 0; i < allActual.size(); i++) {
        final CtType<?> currentActual = allActual.get(i);
        final CtType<?> currentExpected = allExpected.get(i);
        if (!currentActual.equals(currentExpected)) {
            throw new AssertionError(String.format("%1$s and %2$s aren't equals.", currentActual.getQualifiedName(), currentExpected.getQualifiedName()));
        }
    }
    return this.myself;
}
Also used : CtType(spoon.reflect.declaration.CtType) Factory(spoon.reflect.factory.Factory)

Example 53 with CtType

use of spoon.reflect.declaration.CtType in project spoon by INRIA.

the class IntercessionTest method testSettersAreAllGood.

@Test
public void testSettersAreAllGood() throws Exception {
    ArrayList classpath = new ArrayList();
    for (String classpathEntry : System.getProperty("java.class.path").split(File.pathSeparator)) {
        if (!classpathEntry.contains("test-classes")) {
            classpath.add(classpathEntry);
        }
    }
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/main/java/spoon/reflect/");
    launcher.addInputResource("./src/main/java/spoon/support/");
    launcher.getModelBuilder().setSourceClasspath((String[]) classpath.toArray(new String[] {}));
    launcher.buildModel();
    final Factory factory = launcher.getFactory();
    final List<CtMethod<?>> setters = Query.getElements(factory, new AbstractFilter<CtMethod<?>>(CtMethod.class) {

        @Override
        public boolean matches(CtMethod<?> element) {
            CtType<?> declaringType = element.getDeclaringType();
            if (declaringType.getPackage() != null && (declaringType.getPackage().getQualifiedName().startsWith("spoon.support.visitor") || declaringType.getPackage().getQualifiedName().startsWith("spoon.reflect.visitor"))) {
                return false;
            }
            return declaringType.isInterface() && declaringType.getSimpleName().startsWith("Ct") && (element.getSimpleName().startsWith("set") || element.getSimpleName().startsWith("add"));
        }
    });
    for (CtMethod<?> setter : setters) {
        final String methodLog = setter.getSimpleName() + " in " + setter.getDeclaringType().getSimpleName();
        if (setter.getFormalCtTypeParameters().size() <= 0) {
            fail("Your setter " + methodLog + " don't have a generic type for its return type.");
        }
        boolean isMatch = false;
        // New type parameter declaration.
        for (CtTypeParameter typeParameter : setter.getFormalCtTypeParameters()) {
            if (setter.getType().getSimpleName().equals(typeParameter.getSimpleName())) {
                isMatch = true;
                if (setter.getAnnotation(Override.class) != null) {
                    // interface. So the return type can't be the declaring interface.
                    continue;
                }
                if (!setter.getDeclaringType().getSimpleName().equals(typeParameter.getSuperclass().getSimpleName())) {
                    fail("Your setter " + methodLog + " has a type reference who don't extends " + setter.getDeclaringType().getSimpleName());
                }
            }
        }
        assertTrue("The type of " + methodLog + " don't match with generic types.", isMatch);
    }
}
Also used : CtType(spoon.reflect.declaration.CtType) CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) ArrayList(java.util.ArrayList) Launcher(spoon.Launcher) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 54 with CtType

use of spoon.reflect.declaration.CtType in project spoon by INRIA.

the class SpoonMetaModel method getAllInstantiableMetamodelInterfaces.

public List<CtType<? extends CtElement>> getAllInstantiableMetamodelInterfaces() {
    SpoonAPI interfaces = new Launcher();
    interfaces.addInputResource("src/main/java/spoon/reflect/declaration");
    interfaces.addInputResource("src/main/java/spoon/reflect/code");
    interfaces.addInputResource("src/main/java/spoon/reflect/reference");
    interfaces.buildModel();
    SpoonAPI implementations = new Launcher();
    implementations.addInputResource("src/main/java/spoon/support/reflect/declaration");
    implementations.addInputResource("src/main/java/spoon/support/reflect/code");
    implementations.addInputResource("src/main/java/spoon/support/reflect/reference");
    implementations.buildModel();
    List<CtType<? extends CtElement>> result = new ArrayList<>();
    for (CtType<?> itf : interfaces.getModel().getAllTypes()) {
        String impl = itf.getQualifiedName().replace("spoon.reflect", "spoon.support.reflect") + "Impl";
        CtType implClass = implementations.getFactory().Type().get(impl);
        if (implClass != null && !implClass.hasModifier(ModifierKind.ABSTRACT)) {
            result.add((CtType<? extends CtElement>) itf);
        }
    }
    return result;
}
Also used : CtType(spoon.reflect.declaration.CtType) CtElement(spoon.reflect.declaration.CtElement) ArrayList(java.util.ArrayList) Launcher(spoon.Launcher) SpoonAPI(spoon.SpoonAPI)

Example 55 with CtType

use of spoon.reflect.declaration.CtType in project spoon by INRIA.

the class CtRenameLocalVariableRefactoringTest method testRefactorWrongUsage.

@Test
public void testRefactorWrongUsage() throws Exception {
    CtType varRenameClass = ModelUtils.buildClass(CtRenameLocalVariableRefactoringTestSubject.class);
    CtLocalVariable<?> local1Var = varRenameClass.filterChildren((CtLocalVariable<?> var) -> var.getSimpleName().equals("local1")).first();
    // contract: a target variable is not defined. Throw SpoonException
    CtRenameLocalVariableRefactoring refactor = new CtRenameLocalVariableRefactoring();
    refactor.setNewName("local1");
    try {
        refactor.refactor();
        fail();
    } catch (SpoonException e) {
    // should fail - OK
    }
    // contract: invalid rename request to empty string. Throw SpoonException
    refactor.setTarget(local1Var);
    try {
        refactor.setNewName("");
        fail();
    } catch (SpoonException e) {
    // should fail - OK
    }
    // contract: invalid rename request to variable name which contains space. Throw SpoonException
    try {
        refactor.setNewName("x ");
        fail();
    } catch (SpoonException e) {
    // should fail - OK
    }
    // contract: invalid rename request to variable name which contains space. Throw SpoonException
    try {
        refactor.setNewName("x y");
        fail();
    } catch (SpoonException e) {
    // should fail - OK
    }
    // contract: invalid rename request to variable name which contains character which is not allowed in variable name. Throw SpoonException
    try {
        refactor.setNewName("x(");
        fail();
    } catch (SpoonException e) {
    // should fail - OK
    }
}
Also used : CtRenameLocalVariableRefactoring(spoon.refactoring.CtRenameLocalVariableRefactoring) CtType(spoon.reflect.declaration.CtType) SpoonException(spoon.SpoonException) Test(org.junit.Test)

Aggregations

CtType (spoon.reflect.declaration.CtType)134 Test (org.junit.Test)67 Launcher (spoon.Launcher)60 ArrayList (java.util.ArrayList)42 CtMethod (spoon.reflect.declaration.CtMethod)38 CtTypeReference (spoon.reflect.reference.CtTypeReference)30 DefaultJavaPrettyPrinter (spoon.reflect.visitor.DefaultJavaPrettyPrinter)20 File (java.io.File)19 Factory (spoon.reflect.factory.Factory)19 PrettyPrinter (spoon.reflect.visitor.PrettyPrinter)19 List (java.util.List)18 Collectors (java.util.stream.Collectors)17 CtField (spoon.reflect.declaration.CtField)17 CtElement (spoon.reflect.declaration.CtElement)16 CtPackage (spoon.reflect.declaration.CtPackage)16 InputConfiguration (fr.inria.diversify.utils.sosiefier.InputConfiguration)14 IOException (java.io.IOException)12 SpoonException (spoon.SpoonException)12 DSpotCompiler (fr.inria.diversify.utils.compilation.DSpotCompiler)11 Set (java.util.Set)11