use of spoon.reflect.declaration.CtAnnotation in project dspot by STAMP-project.
the class AmplificationHelper method cloneTestMethod.
/**
* Clones a test method.
*
* Performs necessary integration with JUnit and adds timeout.
*
* @param method Method to be cloned
* @param suffix Suffix for the cloned method's name
* @return The cloned method
*/
private static CtMethod cloneTestMethod(CtMethod method, String suffix) {
CtMethod cloned_method = cloneMethod(method, suffix);
CtAnnotation testAnnotation = cloned_method.getAnnotations().stream().filter(annotation -> annotation.toString().contains("Test")).findFirst().orElse(null);
if (testAnnotation != null) {
cloned_method.removeAnnotation(testAnnotation);
}
testAnnotation = method.getFactory().Core().createAnnotation();
CtTypeReference<Object> ref = method.getFactory().Core().createTypeReference();
ref.setSimpleName("Test");
CtPackageReference refPackage = method.getFactory().Core().createPackageReference();
refPackage.setSimpleName("org.junit");
ref.setPackage(refPackage);
testAnnotation.setAnnotationType(ref);
Map<String, Object> elementValue = new HashMap<>();
elementValue.put("timeout", timeOutInMs);
testAnnotation.setElementValues(elementValue);
cloned_method.addAnnotation(testAnnotation);
cloned_method.addThrownType(method.getFactory().Type().createReference(Exception.class));
return cloned_method;
}
use of spoon.reflect.declaration.CtAnnotation in project dspot by STAMP-project.
the class AmplificationHelper method cloneMethod.
/**
* Clones a method.
*
* @param method Method to be cloned
* @param suffix Suffix for the cloned method's name
* @return The cloned method
*/
private static CtMethod cloneMethod(CtMethod method, String suffix) {
CtMethod cloned_method = method.clone();
// rename the clone
cloned_method.setSimpleName(method.getSimpleName() + (suffix.isEmpty() ? "" : suffix + cloneNumber));
cloneNumber++;
CtAnnotation toRemove = cloned_method.getAnnotations().stream().filter(annotation -> annotation.toString().contains("Override")).findFirst().orElse(null);
if (toRemove != null) {
cloned_method.removeAnnotation(toRemove);
}
return cloned_method;
}
use of spoon.reflect.declaration.CtAnnotation in project spoon by INRIA.
the class ParentExiter method scanCtElement.
@Override
public void scanCtElement(CtElement e) {
if (child instanceof CtAnnotation && this.jdtTreeBuilder.getContextBuilder().annotationValueName.isEmpty()) {
// we check if the current element can have the annotation attached
CtAnnotatedElementType annotatedElementType = CtAnnotation.getAnnotatedElementTypeForCtElement(e);
annotatedElementType = (e instanceof CtTypeParameter || e instanceof CtTypeParameterReference) ? CtAnnotatedElementType.TYPE_USE : annotatedElementType;
// in case of noclasspath, we cannot be 100% sure, so we guess it must be attached...
if (this.jdtTreeBuilder.getFactory().getEnvironment().getNoClasspath() || (annotatedElementType != null && JDTTreeBuilderQuery.hasAnnotationWithType((Annotation) childJDT, annotatedElementType))) {
e.addAnnotation((CtAnnotation<?>) child);
}
// in this case the annotation should be (also) attached to the type
if (e instanceof CtTypedElement && JDTTreeBuilderQuery.hasAnnotationWithType((Annotation) childJDT, CtAnnotatedElementType.TYPE_USE)) {
List<CtAnnotation> annotations = new ArrayList<>();
if (!annotationsMap.containsKey(e)) {
annotationsMap.put((CtTypedElement<?>) e, annotations);
} else {
annotations = annotationsMap.get(e);
}
annotations.add((CtAnnotation) child.clone());
annotationsMap.put((CtTypedElement<?>) e, annotations);
}
}
}
use of spoon.reflect.declaration.CtAnnotation 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.CtAnnotation 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
}
}
Aggregations