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());
}
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());
}
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());
}
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]);
}
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"));
}
Aggregations