use of spoon.reflect.code.CtInvocation in project spoon by INRIA.
the class TemplateTest method testExtensionBlock.
@Test
public void testExtensionBlock() throws Exception {
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] { "--output-type", "nooutput" });
launcher.addInputResource("./src/test/java/spoon/test/template/testclasses/logger/Logger.java");
launcher.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/logger/LoggerTemplate.java"));
launcher.addProcessor(new LoggerTemplateProcessor());
launcher.getEnvironment().setSourceClasspath(System.getProperty("java.class.path").split(File.pathSeparator));
try {
launcher.run();
} catch (ClassCastException ignored) {
fail();
}
final CtClass<Logger> aLogger = launcher.getFactory().Class().get(Logger.class);
final CtMethod aMethod = aLogger.getMethodsByName("enter").get(0);
assertTrue(aMethod.getBody().getStatement(0) instanceof CtTry);
final CtTry aTry = (CtTry) aMethod.getBody().getStatement(0);
assertTrue(aTry.getFinalizer().getStatement(0) instanceof CtInvocation);
assertEquals("spoon.test.template.testclasses.logger.Logger.exit(\"enter\")", aTry.getFinalizer().getStatement(0).toString());
assertTrue(aTry.getBody().getStatement(0) instanceof CtInvocation);
assertEquals("spoon.test.template.testclasses.logger.Logger.enter(\"Logger\", \"enter\")", aTry.getBody().getStatement(0).toString());
assertTrue(aTry.getBody().getStatements().size() > 1);
}
use of spoon.reflect.code.CtInvocation in project spoon by INRIA.
the class TargetedExpressionTest method testTargetsOfInvInAnonymousClass.
@Test
public void testTargetsOfInvInAnonymousClass() throws Exception {
// contract: Specify declaring type of the executable of an invocation, the target of the invocation, and its result. All this in an anonymous class.
final Factory factory = build(Foo.class, Bar.class, SuperClass.class);
final CtClass<Foo> type = factory.Class().get(Foo.class);
final CtTypeReference<Foo> expectedType = type.getReference();
final CtClass<?> anonymousClass = type.getElements(new TypeFilter<CtClass>(CtClass.class) {
@Override
public boolean matches(CtClass element) {
return element.isAnonymous() && super.matches(element);
}
}).get(0);
final CtTypeReference<?> expectedAnonymousType = anonymousClass.getReference();
final CtThisAccess<Foo> expectedThisAccess = factory.Code().createThisAccess(expectedType);
final CtThisAccess expectedAnonymousThisAccess = factory.Code().createThisAccess(expectedAnonymousType);
final CtMethod<?> method = anonymousClass.getMethodsByName("m").get(0);
final List<CtInvocation> elements = method.getElements(new TypeFilter<>(CtInvocation.class));
assertEquals(2, elements.size());
assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedType).target(expectedThisAccess).result("this.invStatic()"), elements.get(0));
assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedAnonymousType).target(expectedAnonymousThisAccess).result("this.invStatic()"), elements.get(1));
}
use of spoon.reflect.code.CtInvocation in project spoon by INRIA.
the class TargetedExpressionTest method testTargetsOfInvInInnerClass.
@Test
public void testTargetsOfInvInInnerClass() throws Exception {
// contract: Specify declaring type of the executable of an invocation, the target of the invocation and its result. All this in an innerclass.
final Factory factory = build(Foo.class, Bar.class, SuperClass.class);
final CtClass<Foo> type = factory.Class().get(Foo.class);
final CtTypeReference<Foo> expectedType = type.getReference();
final CtTypeReference<SuperClass> expectedSuperClassType = factory.Class().<SuperClass>get(SuperClass.class).getReference();
final CtType<?> innerClass = type.getNestedType("InnerClass");
final CtTypeReference<?> expectedInnerClass = innerClass.getReference();
final CtType<?> nestedTypeScanner = type.getNestedType("1NestedTypeScanner");
final CtTypeReference<?> expectedNested = nestedTypeScanner.getReference();
final CtTypeAccess<Foo> fooTypeAccess = factory.Code().createTypeAccess(expectedType);
final CtThisAccess expectedThisAccess = factory.Code().createThisAccess(expectedType);
final CtThisAccess expectedSuperThisAccess = factory.Code().createThisAccess(expectedSuperClassType);
final CtThisAccess<?> expectedInnerClassAccess = factory.Code().createThisAccess(expectedInnerClass);
final CtThisAccess expectedNestedAccess = factory.Code().createThisAccess(expectedNested);
final CtMethod<?> innerInvMethod = innerClass.getMethodsByName("innerInv").get(0);
final List<CtInvocation<?>> elements = innerInvMethod.getElements(new TypeFilter<CtInvocation<?>>(CtInvocation.class));
assertEquals(8, elements.size());
expectedThisAccess.setType(expectedInnerClass);
assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedType).target(expectedThisAccess).result("inv()"), elements.get(0));
expectedThisAccess.setType(expectedType);
assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedType).target(expectedThisAccess).result("this.inv()"), elements.get(1));
assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedType).target(fooTypeAccess).result("spoon.test.targeted.testclasses.Foo.staticMethod()"), elements.get(2));
assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedType).target(fooTypeAccess).result("spoon.test.targeted.testclasses.Foo.staticMethod()"), elements.get(3));
expectedSuperThisAccess.setType(expectedInnerClass);
assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedSuperClassType).target(expectedSuperThisAccess).result("superMethod()"), elements.get(4));
assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedSuperClassType).target(expectedThisAccess).result("this.superMethod()"), elements.get(5));
assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedInnerClass).target(expectedInnerClassAccess).result("method()"), elements.get(6));
assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedInnerClass).target(expectedInnerClassAccess).result("this.method()"), elements.get(7));
final List<CtInvocation> newElements = nestedTypeScanner.getMethodsByName("checkType").get(0).getElements(new TypeFilter<>(CtInvocation.class));
assertEquals(1, newElements.size());
assertEqualsInvocation(new ExpectedTargetedExpression().declaringType(expectedNested).target(expectedNestedAccess).result("this.checkType(type)"), newElements.get(0));
}
use of spoon.reflect.code.CtInvocation in project spoon by INRIA.
the class VisibilityTest method testInvocationVisibilityInFieldDeclaration.
@Test
public void testInvocationVisibilityInFieldDeclaration() throws Exception {
final Launcher launcher = new Launcher();
launcher.getEnvironment().setNoClasspath(true);
launcher.addInputResource("./src/test/resources/noclasspath/Solver.java");
launcher.setSourceOutputDirectory("./target/spooned");
launcher.buildModel();
final CtType<Object> aSolver = launcher.getFactory().Type().get("org.sat4j.minisat.core.Solver");
final CtField<?> lbdTimerField = aSolver.getField("lbdTimer");
final CtInvocation<?> ctInvocation = lbdTimerField.getElements(new TypeFilter<CtInvocation<?>>(CtInvocation.class) {
@Override
public boolean matches(CtInvocation<?> element) {
return "bound".equals(element.getExecutable().getSimpleName()) && super.matches(element);
}
}).get(0);
assertNotNull(ctInvocation.getTarget());
assertTrue(ctInvocation.getTarget().isImplicit());
assertEquals("bound()", ctInvocation.toString());
}
use of spoon.reflect.code.CtInvocation in project spoon by INRIA.
the class ImportTest method testAnotherMissingImport.
@Test
public void testAnotherMissingImport() throws Exception {
Launcher spoon = new Launcher();
spoon.setArgs(new String[] { "--output-type", "nooutput" });
Factory factory = spoon.createFactory();
factory.getEnvironment().setNoClasspath(true);
factory.getEnvironment().setLevel("OFF");
SpoonModelBuilder compiler = spoon.createCompiler(factory, SpoonResourceHelper.resources("./src/test/resources/import-resources/fr/inria/AnotherMissingImport.java"));
compiler.build();
List<CtMethod> methods = factory.getModel().getElements(new NamedElementFilter<>(CtMethod.class, "doSomething"));
List<CtParameter> parameters = methods.get(0).getParameters();
CtTypeReference<?> type = parameters.get(0).getType();
assertEquals("SomeType", type.getSimpleName());
assertEquals("externallib", type.getPackage().getSimpleName());
CtMethod<?> mainMethod = factory.Class().getAll().get(0).getMethodsByName("main").get(0);
List<CtStatement> statements = mainMethod.getBody().getStatements();
CtStatement invocationStatement = statements.get(1);
assertTrue(invocationStatement instanceof CtInvocation);
CtInvocation invocation = (CtInvocation) invocationStatement;
CtExecutableReference executableReference = invocation.getExecutable();
assertEquals("doSomething(externallib.SomeType)", executableReference.getSignature());
assertSame(methods.get(0), executableReference.getDeclaration());
}
Aggregations