use of spoon.reflect.declaration.CtClass in project spoon by INRIA.
the class MetaModelTest method elementAnnotationAdaptedRoleTest.
@Test
public void elementAnnotationAdaptedRoleTest() {
Launcher launcher = new Launcher();
Factory factory = launcher.getFactory();
CtClass<?> type = (CtClass) factory.Core().create(CtClass.class);
CtAnnotation<?> annotation = factory.Annotation().annotate(type, Parameter.class, "value", "abc");
// check adaptation of attribute to modifiable List
List<CtAnnotation<?>> value = RoleHandlerHelper.getRoleHandler(type.getClass(), CtRole.ANNOTATION).asList(type);
assertEquals(1, value.size());
assertSame(annotation, value.get(0));
// check we can remove from this collection
value.remove(annotation);
assertEquals(0, value.size());
assertEquals(0, ((List) type.getValueByRole(CtRole.ANNOTATION)).size());
// check we can add to this collection
value.add(annotation);
assertEquals(1, value.size());
assertSame(annotation, value.get(0));
assertEquals(1, ((List) type.getValueByRole(CtRole.ANNOTATION)).size());
assertEquals(annotation, ((List) type.getValueByRole(CtRole.ANNOTATION)).get(0));
}
use of spoon.reflect.declaration.CtClass in project spoon by INRIA.
the class MetaModelTest method testGetParentRoleHandler.
@Test
public void testGetParentRoleHandler() {
Launcher launcher = new Launcher();
Factory factory = launcher.getFactory();
CtClass<?> type = (CtClass) factory.Core().create(CtClass.class);
CtField<?> field = factory.Field().create(type, Collections.emptySet(), factory.Type().booleanPrimitiveType(), "someField");
assertSame(type, field.getDeclaringType());
// contract: RoleHandlerHelper#getParentRoleHandler returns role handler which handles it's relationship to parent
assertSame(CtRole.TYPE_MEMBER, RoleHandlerHelper.getRoleHandlerWrtParent(field).getRole());
assertSame(CtRole.TYPE_MEMBER, field.getRoleInParent());
// contract: RoleHandlerHelper#getParentRoleHandler returns null if there is no parent
field.setParent(null);
assertNull(RoleHandlerHelper.getRoleHandlerWrtParent(field));
// contract: RoleHandlerHelper#getParentRoleHandler returns null if parent relation cannot be handled in this case
// parent of new CtClass is root package - there is no way how to modify that
assertNull(RoleHandlerHelper.getRoleHandlerWrtParent(type));
}
use of spoon.reflect.declaration.CtClass in project spoon by INRIA.
the class MetaModelTest method elementAnnotationRoleHandlerTest.
@Test
public void elementAnnotationRoleHandlerTest() {
Launcher launcher = new Launcher();
Factory factory = launcher.getFactory();
CtClass<?> type = (CtClass) factory.Core().create(CtClass.class);
CtAnnotation<?> annotation = factory.Annotation().annotate(type, Parameter.class, "value", "abc");
// check contract of low level RoleHandler
RoleHandler roleHandler = RoleHandlerHelper.getRoleHandler(type.getClass(), CtRole.ANNOTATION);
assertNotNull(roleHandler);
assertEquals(CtElement.class, roleHandler.getTargetType());
assertSame(CtRole.ANNOTATION, roleHandler.getRole());
assertSame(ContainerKind.LIST, roleHandler.getContainerKind());
assertEquals(CtAnnotation.class, roleHandler.getValueClass());
// check getting value using role handler
List<CtAnnotation<?>> value = roleHandler.getValue(type);
assertEquals(1, value.size());
assertSame(annotation, value.get(0));
// check we have got direct readonly List
try {
value.remove(annotation);
fail();
} catch (Exception e) {
this.getClass();
}
// check setValueByRole
roleHandler.setValue(type, Collections.emptyList());
value = roleHandler.getValue(type);
assertEquals(0, value.size());
roleHandler.setValue(type, Collections.singletonList(annotation));
value = roleHandler.getValue(type);
assertEquals(1, value.size());
assertSame(annotation, value.get(0));
try {
// contract value must be a list of annotation. One annotation is not actually OK. This contract might be changed in future
roleHandler.setValue(type, annotation);
fail();
} catch (ClassCastException e) {
// OK
}
}
use of spoon.reflect.declaration.CtClass in project spoon by INRIA.
the class ImportScannerImpl method lookForLocalVariables.
protected Set<String> lookForLocalVariables(CtElement parent) {
Set<String> result = new HashSet<>();
// if the first container is the class, then we are not in a block and we can quit now.
while (parent != null && !(parent instanceof CtBlock)) {
if (parent instanceof CtClass) {
return result;
}
parent = parent.getParent();
}
if (parent != null) {
CtBlock block = (CtBlock) parent;
boolean innerClass = false;
// now we have the first container block, we want to check if we're not in an inner class
while (parent != null && !(parent instanceof CtClass)) {
parent = parent.getParent();
}
if (parent != null) {
// let's find the last block BEFORE the class call: some collision could occur because of variables defined in that block
if (!(parent.getParent() instanceof CtPackage)) {
while (parent != null && !(parent instanceof CtBlock)) {
parent = parent.getParent();
}
if (parent != null) {
block = (CtBlock) parent;
}
}
}
AccessibleVariablesFinder avf = new AccessibleVariablesFinder(block);
List<CtVariable> variables = avf.find();
for (CtVariable variable : variables) {
result.add(variable.getSimpleName());
}
}
return result;
}
use of spoon.reflect.declaration.CtClass in project spoon by INRIA.
the class AnnotationTest method testRepeatSameAnnotationOnLocalVariable.
@Test
public void testRepeatSameAnnotationOnLocalVariable() throws Exception {
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/annotation/testclasses/AnnotationsRepeated.java");
launcher.buildModel();
Factory factory = launcher.getFactory();
final CtClass<?> ctClass = (CtClass<?>) factory.Type().get(AnnotationsRepeated.class);
final CtMethod<?> method = ctClass.getMethodsByName("methodWithLocalVariable").get(0);
final CtLocalVariable<?> ctLocalVariable = method.getBody().getElements(new AbstractFilter<CtLocalVariable<?>>(CtLocalVariable.class) {
@Override
public boolean matches(CtLocalVariable<?> element) {
return true;
}
}).get(0);
final List<CtAnnotation<? extends Annotation>> annotations = ctLocalVariable.getAnnotations();
assertEquals("Local variable must to have multi annotation of the same type", 2, annotations.size());
assertEquals("Type of the first annotation is AnnotationRepeated", AnnotationRepeated.class, annotations.get(0).getAnnotationType().getActualClass());
assertEquals("Type of the second annotation is AnnotationRepeated", AnnotationRepeated.class, annotations.get(1).getAnnotationType().getActualClass());
assertEquals("Argument of the first annotation is \"Local 1\"", "Local 1", ((CtLiteral) annotations.get(0).getValue("value")).getValue());
assertEquals("Argument of the second annotation is \"Local 2\"", "Local 2", ((CtLiteral) annotations.get(1).getValue("value")).getValue());
}
Aggregations