use of spoon.SpoonException in project spoon by INRIA.
the class AnnotationTest method testReplaceAnnotationValue.
@Test
public void testReplaceAnnotationValue() throws Exception {
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/annotation/testclasses/Main.java");
launcher.buildModel();
Factory factory = launcher.getFactory();
CtType<?> type = factory.Type().get("spoon.test.annotation.testclasses.Main");
CtMethod<?> m1 = type.getElements(new NamedElementFilter<>(CtMethod.class, "m1")).get(0);
List<CtAnnotation<? extends Annotation>> annotations = m1.getAnnotations();
assertEquals(1, annotations.size());
CtAnnotation<?> a = annotations.get(0);
AnnotParamTypes annot = (AnnotParamTypes) a.getActualAnnotation();
// contract: test replace of single value
CtExpression integerValue = a.getValue("integer");
assertEquals(42, ((CtLiteral<Integer>) integerValue).getValue().intValue());
assertEquals(42, annot.integer());
integerValue.replace(factory.createLiteral(17));
CtExpression newIntegerValue = a.getValue("integer");
assertEquals(17, ((CtLiteral<Integer>) newIntegerValue).getValue().intValue());
assertEquals(17, annot.integer());
// even if second value is null
try {
a.getValue("integer").replace(Arrays.asList(factory.createLiteral(18), null));
fail();
} catch (SpoonException e) {
// OK
}
// contract: replacing of single value by no value
a.getValue("integer").delete();
assertNull(a.getValue("integer"));
try {
annot.integer();
fail();
} catch (NullPointerException e) {
// OK - fails because int cannot be null
}
// contract: replace with null value means remove
a.getValue("string").replace((CtElement) null);
assertNull(a.getValue("string"));
// contract: check that null value can be returned
assertNull(annot.string());
// contract: replace with null value in collection means remove
a.getValue("clazz").replace(Collections.singletonList(null));
assertNull(a.getValue("clazz"));
// contract: check that null value can be returned
assertNull(annot.clazz());
// contract: test replace of item in collection
assertEquals(1, annot.integers().length);
assertEquals(42, annot.integers()[0]);
CtNewArray<?> integersNewArray = (CtNewArray) a.getValue("integers");
integersNewArray.getElements().get(0).replace(Arrays.asList(null, factory.createLiteral(101), null, factory.createLiteral(102)));
assertEquals(2, annot.integers().length);
assertEquals(101, annot.integers()[0]);
assertEquals(102, annot.integers()[1]);
}
use of spoon.SpoonException in project spoon by INRIA.
the class AnnotationTest method testAnnotationNotRepeatableNotArrayAnnotation.
@Test
public void testAnnotationNotRepeatableNotArrayAnnotation() {
// contract: when trying to annotate multiple times with same not repeatable, not array annotation, it should throw an exception
Launcher spoon = new Launcher();
spoon.addInputResource("./src/test/java/spoon/test/annotation/testclasses/notrepeatable/StringAnnot.java");
spoon.buildModel();
CtMethod aMethod = spoon.getFactory().createMethod().setSimpleName(("mamethod"));
spoon.getFactory().Annotation().annotate(aMethod, StringAnnot.class, "value", "foo");
assertEquals(1, aMethod.getAnnotations().size());
String methodContent = aMethod.toString();
assertTrue("Content: " + methodContent, methodContent.contains("@spoon.test.annotation.testclasses.notrepeatable.StringAnnot(\"foo\")"));
try {
spoon.getFactory().Annotation().annotate(aMethod, StringAnnot.class, "value", "bar");
methodContent = aMethod.toString();
fail("You should not be able to add two values to StringAnnot annotation: " + methodContent);
} catch (SpoonException e) {
assertEquals("cannot assign an array to a non-array annotation element", e.getMessage());
}
}
use of spoon.SpoonException in project spoon by INRIA.
the class FileSystemFolderTest method testLauncherWithWrongPathAsInput.
@Test
public void testLauncherWithWrongPathAsInput() {
Launcher spoon = new Launcher();
spoon.addInputResource("./src/wrong/direction/File.java");
try {
spoon.buildModel();
} catch (SpoonException spe) {
Throwable containedException = spe.getCause().getCause();
assertTrue(containedException instanceof FileNotFoundException);
}
}
use of spoon.SpoonException in project spoon by INRIA.
the class ReplaceScanner method getTypeFromTypeParameterReference.
private CtTypeReference getTypeFromTypeParameterReference(CtTypeParameterReference ctTypeParameterRef) {
final CtMethod parentMethod = ctTypeParameterRef.getParent(CtMethod.class);
for (CtTypeParameter formal : parentMethod.getFormalCtTypeParameters()) {
if (formal.getSimpleName().equals(ctTypeParameterRef.getSimpleName())) {
return ((CtTypeParameterReference) formal).getBoundingType();
}
}
final CtInterface parentInterface = ctTypeParameterRef.getParent(CtInterface.class);
for (CtTypeParameter formal : parentInterface.getFormalCtTypeParameters()) {
if (formal.getSimpleName().equals(ctTypeParameterRef.getSimpleName())) {
return formal.getReference().getBoundingType();
}
}
throw new SpoonException("Can't get the type of the CtTypeParameterReference " + ctTypeParameterRef);
}
use of spoon.SpoonException in project spoon by INRIA.
the class StatementTemplate method apply.
public CtStatement apply(CtType<?> targetType) {
CtClass<?> c = Substitution.getTemplateCtClass(targetType, this);
// we substitute the first statement of method statement
CtStatement result = c.getMethod("statement").getBody().getStatements().get(0).clone();
List<CtStatement> statements = new SubstitutionVisitor(c.getFactory(), targetType, this).substitute(result);
if (statements.size() > 1) {
throw new SpoonException("StatementTemplate cannot return more then one statement");
}
return statements.isEmpty() ? null : statements.get(0);
}
Aggregations