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