Search in sources :

Example 16 with SpoonException

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]);
}
Also used : CtAnnotation(spoon.reflect.declaration.CtAnnotation) AnnotParamTypes(spoon.test.annotation.testclasses.AnnotParamTypes) CtExpression(spoon.reflect.code.CtExpression) SpoonException(spoon.SpoonException) Factory(spoon.reflect.factory.Factory) CtNewArray(spoon.reflect.code.CtNewArray) TypeAnnotation(spoon.test.annotation.testclasses.TypeAnnotation) GlobalAnnotation(spoon.test.annotation.testclasses.GlobalAnnotation) SuperAnnotation(spoon.test.annotation.testclasses.SuperAnnotation) AnnotationDefaultAnnotation(spoon.test.annotation.testclasses.AnnotationDefaultAnnotation) InnerAnnotation(spoon.test.annotation.testclasses.Foo.InnerAnnotation) Annotation(java.lang.annotation.Annotation) MiddleAnnotation(spoon.test.annotation.testclasses.Foo.MiddleAnnotation) OuterAnnotation(spoon.test.annotation.testclasses.Foo.OuterAnnotation) CtAnnotation(spoon.reflect.declaration.CtAnnotation) CtLiteral(spoon.reflect.code.CtLiteral) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 17 with SpoonException

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());
    }
}
Also used : SpoonException(spoon.SpoonException) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 18 with SpoonException

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);
    }
}
Also used : SpoonException(spoon.SpoonException) FileNotFoundException(java.io.FileNotFoundException) Launcher(spoon.Launcher) Test(org.junit.Test) LauncherTest(spoon.LauncherTest)

Example 19 with SpoonException

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);
}
Also used : CtTypeParameterReference(spoon.reflect.reference.CtTypeParameterReference) CtInterface(spoon.reflect.declaration.CtInterface) CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) SpoonException(spoon.SpoonException) CtMethod(spoon.reflect.declaration.CtMethod)

Example 20 with SpoonException

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);
}
Also used : SubstitutionVisitor(spoon.support.template.SubstitutionVisitor) CtStatement(spoon.reflect.code.CtStatement) SpoonException(spoon.SpoonException)

Aggregations

SpoonException (spoon.SpoonException)57 Test (org.junit.Test)15 Launcher (spoon.Launcher)12 CtMethod (spoon.reflect.declaration.CtMethod)9 CtType (spoon.reflect.declaration.CtType)9 CtElement (spoon.reflect.declaration.CtElement)8 Factory (spoon.reflect.factory.Factory)8 File (java.io.File)7 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 CtField (spoon.reflect.declaration.CtField)6 URL (java.net.URL)4 CtTypeReference (spoon.reflect.reference.CtTypeReference)4 Collection (java.util.Collection)3 CompilationUnit (spoon.reflect.cu.CompilationUnit)3 CtExecutable (spoon.reflect.declaration.CtExecutable)3 CtParameter (spoon.reflect.declaration.CtParameter)3 CtScanner (spoon.reflect.visitor.CtScanner)3 Filter (spoon.reflect.visitor.Filter)3 FileNotFoundException (java.io.FileNotFoundException)2