Search in sources :

Example 41 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 42 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 43 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 44 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)

Example 45 with CtType

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

the class CtRenameLocalVariableRefactoringTest method testRenameLocalVariableToSameName.

@Test
public void testRenameLocalVariableToSameName() throws Exception {
    CtType varRenameClass = ModelUtils.buildClass(CtRenameLocalVariableRefactoringTestSubject.class);
    CtLocalVariable<?> local1Var = varRenameClass.filterChildren((CtLocalVariable<?> var) -> var.getSimpleName().equals("local1")).first();
    CtRenameLocalVariableRefactoring refactor = new CtRenameLocalVariableRefactoring();
    refactor.setTarget(local1Var);
    refactor.setNewName("local1");
    refactor.refactor();
    assertEquals("local1", local1Var.getSimpleName());
}
Also used : CtRenameLocalVariableRefactoring(spoon.refactoring.CtRenameLocalVariableRefactoring) CtType(spoon.reflect.declaration.CtType) 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