Search in sources :

Example 96 with Factory

use of spoon.reflect.factory.Factory in project spoon by INRIA.

the class TryCatchTest method testFullyQualifiedException.

@Test
public void testFullyQualifiedException() {
    Factory factory = createFactory();
    // test the order of the model
    CtClass<?> clazz = factory.Code().createCodeSnippetStatement("" + "class X {" + "public void foo() {" + " try{}catch(java.lang.RuntimeException e){}" + "}};").compile();
    CtTry tryStmt = (CtTry) clazz.getElements(new TypeFilter<>(CtTry.class)).get(0);
    assertEquals(1, tryStmt.getCatchers().size());
}
Also used : ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) CtTry(spoon.reflect.code.CtTry) Test(org.junit.Test)

Example 97 with Factory

use of spoon.reflect.factory.Factory in project spoon by INRIA.

the class TryCatchTest method testTryCatchVariableGetType.

@Test
public void testTryCatchVariableGetType() throws Exception {
    Factory factory = createFactory();
    CtClass<?> clazz = factory.Code().createCodeSnippetStatement("" + "class X {" + "public void foo() {" + " try{}catch(RuntimeException e){System.exit(0);}" + "}" + "};").compile();
    CtTry tryStmt = (CtTry) clazz.getElements(new TypeFilter<>(CtTry.class)).get(0);
    List<CtCatch> catchers = tryStmt.getCatchers();
    assertEquals(1, catchers.size());
    CtCatchVariable<?> catchVariable = catchers.get(0).getParameter();
    assertEquals(RuntimeException.class, catchVariable.getType().getActualClass());
    assertEquals(1, catchVariable.getMultiTypes().size());
    assertEquals(RuntimeException.class, catchVariable.getMultiTypes().get(0).getActualClass());
    // contract: the manipulation with catch variable type is possible
    catchVariable.setType((CtTypeReference) factory.Type().createReference(IllegalArgumentException.class));
    assertEquals(IllegalArgumentException.class, catchVariable.getType().getActualClass());
    // contract setType influences multitypes
    assertEquals(1, catchVariable.getMultiTypes().size());
    assertEquals(IllegalArgumentException.class, catchVariable.getMultiTypes().get(0).getActualClass());
    catchVariable.setMultiTypes(Collections.singletonList((CtTypeReference) factory.Type().createReference(UnsupportedOperationException.class)));
    assertEquals(UnsupportedOperationException.class, catchVariable.getType().getActualClass());
    // contract setType influences multitypes
    assertEquals(1, catchVariable.getMultiTypes().size());
    assertEquals(UnsupportedOperationException.class, catchVariable.getMultiTypes().get(0).getActualClass());
    catchVariable.setMultiTypes(Arrays.asList(factory.Type().createReference(UnsupportedOperationException.class), factory.Type().createReference(IllegalArgumentException.class)));
    assertEquals(2, catchVariable.getMultiTypes().size());
    assertEquals(UnsupportedOperationException.class, catchVariable.getMultiTypes().get(0).getActualClass());
    assertEquals(IllegalArgumentException.class, catchVariable.getMultiTypes().get(1).getActualClass());
    // contract setMultiTypes influences types, which contains common super class of all multi types
    assertEquals(RuntimeException.class, catchVariable.getType().getActualClass());
}
Also used : CtTypeReference(spoon.reflect.reference.CtTypeReference) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) CtCatch(spoon.reflect.code.CtCatch) CtTry(spoon.reflect.code.CtTry) Test(org.junit.Test)

Example 98 with Factory

use of spoon.reflect.factory.Factory in project spoon by INRIA.

the class AnnotationTest method testModelBuildingAnnotationBoundUsage.

@Test
public void testModelBuildingAnnotationBoundUsage() 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");
    assertEquals("Main", type.getSimpleName());
    CtParameter<?> param = type.getElements(new TypeFilter<CtParameter<?>>(CtParameter.class)).get(0);
    assertEquals("a", param.getSimpleName());
    List<CtAnnotation<? extends Annotation>> annotations = param.getAnnotations();
    assertEquals(1, annotations.size());
    CtAnnotation<?> a = annotations.get(0);
    Bound actualAnnotation = (Bound) a.getActualAnnotation();
    assertEquals(8, actualAnnotation.max());
}
Also used : CtAnnotation(spoon.reflect.declaration.CtAnnotation) Bound(spoon.test.annotation.testclasses.Bound) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) 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) Test(org.junit.Test)

Example 99 with Factory

use of spoon.reflect.factory.Factory 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 100 with Factory

use of spoon.reflect.factory.Factory in project spoon by INRIA.

the class AnnotationTest method testGetAnnotationFromParameter.

@Test
public void testGetAnnotationFromParameter() {
    // contract: Java 8 receiver parameters are handled
    Launcher spoon = new Launcher();
    spoon.addInputResource("src/test/resources/noclasspath/Initializer.java");
    String output = "target/spooned-" + this.getClass().getSimpleName() + "-firstspoon/";
    spoon.setSourceOutputDirectory(output);
    spoon.getEnvironment().setNoClasspath(true);
    Factory factory = spoon.getFactory();
    spoon.buildModel();
    List<CtMethod> methods = factory.getModel().getElements(new NamedElementFilter<>(CtMethod.class, "setField"));
    assertThat(methods.size(), is(1));
    CtMethod methodSet = methods.get(0);
    assertThat(methodSet.getSimpleName(), is("setField"));
    List<CtParameter> parameters = methodSet.getParameters();
    assertThat(parameters.size(), is(1));
    CtParameter thisParameter = parameters.get(0);
    assertThat(thisParameter.getSimpleName(), is("this"));
    CtTypeReference thisParamType = thisParameter.getType();
    assertThat(thisParamType.getSimpleName(), is("Initializer"));
    List<CtAnnotation<?>> annotations = thisParameter.getType().getAnnotations();
    assertThat(annotations.size(), is(2));
    CtAnnotation unknownInit = annotations.get(0);
    CtAnnotation raw = annotations.get(1);
    assertThat(unknownInit.getAnnotationType().getSimpleName(), is("UnknownInitialization"));
    assertThat(raw.getAnnotationType().getSimpleName(), is("Raw"));
}
Also used : CtAnnotation(spoon.reflect.declaration.CtAnnotation) CtTypeReference(spoon.reflect.reference.CtTypeReference) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) CtParameter(spoon.reflect.declaration.CtParameter) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Aggregations

Factory (spoon.reflect.factory.Factory)364 Test (org.junit.Test)322 Launcher (spoon.Launcher)154 CtClass (spoon.reflect.declaration.CtClass)87 CtMethod (spoon.reflect.declaration.CtMethod)73 ModelUtils.createFactory (spoon.testing.utils.ModelUtils.createFactory)65 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)53 File (java.io.File)41 CtStatement (spoon.reflect.code.CtStatement)36 CtTypeReference (spoon.reflect.reference.CtTypeReference)32 CtAnnotation (spoon.reflect.declaration.CtAnnotation)31 CtInvocation (spoon.reflect.code.CtInvocation)30 SpoonModelBuilder (spoon.SpoonModelBuilder)29 DefaultCoreFactory (spoon.support.DefaultCoreFactory)29 List (java.util.List)25 FileSystemFile (spoon.support.compiler.FileSystemFile)25 ArrayList (java.util.ArrayList)24 CtExpression (spoon.reflect.code.CtExpression)20 Annotation (java.lang.annotation.Annotation)19 MainTest (spoon.test.main.MainTest)19