Search in sources :

Example 21 with NamedElementFilter

use of spoon.reflect.visitor.filter.NamedElementFilter in project spoon by INRIA.

the class InsertMethodsTest method testInsertBeforeSwitchCase.

@Test
public void testInsertBeforeSwitchCase() throws Exception {
    CtMethod<?> sm = insertExampleClass.getElements(new NamedElementFilter<>(CtMethod.class, "switchMethod")).get(0);
    // Adds a new snippet in a case.
    CtSwitch<?> sw = sm.getElements(new TypeFilter<CtSwitch<?>>(CtSwitch.class)).get(0);
    CtCase<?> ctCase1 = sw.getCases().get(2);
    CtCase<?> ctCase2 = sw.getCases().get(3);
    CtCodeSnippetStatement snippet = factory.Code().createCodeSnippetStatement("System.out.println(\"foo\")");
    ctCase1.getStatements().get(0).insertBefore(snippet);
    assertEquals(snippet, ctCase1.getStatements().get(0));
    assertEquals(ctCase1, snippet.getParent());
    CtCodeSnippetStatement snippet2 = snippet.clone();
    ctCase2.getStatements().get(1).insertBefore(snippet2);
    assertEquals(snippet2, ctCase2.getStatements().get(1));
    assertEquals(ctCase2, snippet2.getParent());
    // Creates a new case.
    CtCase<Object> caseElem = factory.Core().createCase();
    CtLiteral<Object> literal = factory.Core().createLiteral();
    literal.setValue(1);
    caseElem.setCaseExpression(literal);
    // here we may call either insertBefore(CtStatement) or insertBefore(CtStatementList)
    // ctCase.insertBefore(caseElem);
    // so force the correct insert
    CtCase<?> ctCase = sw.getCases().get(1);
    ctCase.insertBefore((CtStatement) caseElem);
    assertEquals(5, sw.getCases().size());
    assertEquals(caseElem, sw.getCases().get(1));
    assertEquals(ctCase, sw.getCases().get(2));
    assertEquals(sw, caseElem.getParent());
}
Also used : NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtCodeSnippetStatement(spoon.reflect.code.CtCodeSnippetStatement) Test(org.junit.Test)

Example 22 with NamedElementFilter

use of spoon.reflect.visitor.filter.NamedElementFilter in project spoon by INRIA.

the class ParameterTest method testParameterInNoClasspath.

@Test
public void testParameterInNoClasspath() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/resources/parameter");
    launcher.setSourceOutputDirectory("./target/parameter");
    launcher.getEnvironment().setNoClasspath(true);
    launcher.run();
    final CtClass<Object> aClass = launcher.getFactory().Class().get("org.eclipse.draw2d.text.FlowUtilities");
    final CtParameter<?> parameter = aClass.getElements(new NamedElementFilter<>(CtParameter.class, "font")).get(0);
    assertEquals("font", parameter.getSimpleName());
    assertNotNull(parameter.getType());
    assertEquals("org.eclipse.swt.graphics.Font", parameter.getType().toString());
    assertEquals("org.eclipse.swt.graphics.Font font", parameter.toString());
}
Also used : NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 23 with NamedElementFilter

use of spoon.reflect.visitor.filter.NamedElementFilter in project spoon by INRIA.

the class PackageTest method testRenamePackageAndPrettyPrintNoclasspath.

@Test
public void testRenamePackageAndPrettyPrintNoclasspath() throws Exception {
    final Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/resources/noclasspath/app/Test.java");
    spoon.getEnvironment().setNoClasspath(true);
    spoon.buildModel();
    CtPackage ctPackage = spoon.getModel().getElements(new NamedElementFilter<CtPackage>(CtPackage.class, "app")).get(0);
    ctPackage.setSimpleName("otherName");
    CtClass foo = spoon.getModel().getElements(new NamedElementFilter<CtClass>(CtClass.class, "Test")).get(0);
    assertEquals("otherName.Test", foo.getQualifiedName());
    PrettyPrinter prettyPrinter = new DefaultJavaPrettyPrinter(spoon.getEnvironment());
    prettyPrinter.calculate(spoon.getFactory().CompilationUnit().getOrCreate("./src/test/resources/noclasspath/app/Test.java"), Collections.singletonList(foo));
    String result = prettyPrinter.getResult();
    assertTrue(result.contains("package otherName;"));
}
Also used : CtClass(spoon.reflect.declaration.CtClass) PrettyPrinter(spoon.reflect.visitor.PrettyPrinter) DefaultJavaPrettyPrinter(spoon.reflect.visitor.DefaultJavaPrettyPrinter) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) Launcher(spoon.Launcher) CtPackage(spoon.reflect.declaration.CtPackage) DefaultJavaPrettyPrinter(spoon.reflect.visitor.DefaultJavaPrettyPrinter) Test(org.junit.Test)

Example 24 with NamedElementFilter

use of spoon.reflect.visitor.filter.NamedElementFilter in project spoon by INRIA.

the class PackageTest method testRenamePackageAndPrettyPrint.

@Test
public void testRenamePackageAndPrettyPrint() throws Exception {
    final Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/java/spoon/test/pkg/testclasses/Foo.java");
    spoon.buildModel();
    CtPackage ctPackage = spoon.getModel().getElements(new NamedElementFilter<CtPackage>(CtPackage.class, "spoon")).get(0);
    ctPackage.setSimpleName("otherName");
    CtClass foo = spoon.getModel().getElements(new NamedElementFilter<CtClass>(CtClass.class, "Foo")).get(0);
    assertEquals("otherName.test.pkg.testclasses.Foo", foo.getQualifiedName());
    PrettyPrinter prettyPrinter = new DefaultJavaPrettyPrinter(spoon.getEnvironment());
    prettyPrinter.calculate(spoon.getFactory().CompilationUnit().getOrCreate("./src/test/java/spoon/test/pkg/testclasses/Foo.java"), Collections.singletonList(foo));
    String result = prettyPrinter.getResult();
    assertTrue(result.contains("package otherName.test.pkg.testclasses;"));
}
Also used : CtClass(spoon.reflect.declaration.CtClass) PrettyPrinter(spoon.reflect.visitor.PrettyPrinter) DefaultJavaPrettyPrinter(spoon.reflect.visitor.DefaultJavaPrettyPrinter) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) Launcher(spoon.Launcher) CtPackage(spoon.reflect.declaration.CtPackage) DefaultJavaPrettyPrinter(spoon.reflect.visitor.DefaultJavaPrettyPrinter) Test(org.junit.Test)

Example 25 with NamedElementFilter

use of spoon.reflect.visitor.filter.NamedElementFilter in project spoon by INRIA.

the class GenericsTest method testGetDeclarationOfTypeParameterReference.

@Test
public void testGetDeclarationOfTypeParameterReference() {
    Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/java/spoon/test/generics/testclasses/ExtendedPaella.java");
    launcher.addInputResource("./src/test/java/spoon/test/generics/testclasses/Paella.java");
    launcher.buildModel();
    Factory factory = launcher.getFactory();
    CtClass extendedPaella = factory.getModel().getElements(new NamedElementFilter<>(CtClass.class, "ExtendedPaella")).get(0);
    List<CtTypeParameter> typeParameterList = extendedPaella.getFormalCtTypeParameters();
    assertEquals(1, typeParameterList.size());
    CtMethod totoMethod = factory.getModel().getElements(new NamedElementFilter<>(CtMethod.class, "toto")).get(0);
    CtTypeReference returnTypeToto = totoMethod.getType();
    CtTypeReference paramToto = ((CtParameter) totoMethod.getParameters().get(0)).getType();
    CtType declaration = returnTypeToto.getDeclaration();
    assertSame(typeParameterList.get(0), declaration);
    assertSame(typeParameterList.get(0), paramToto.getDeclaration());
    CtMethod machinMethod = factory.getModel().getElements(new NamedElementFilter<>(CtMethod.class, "machin")).get(0);
    CtTypeReference returnTypeMachin = machinMethod.getType();
    List<CtTypeParameter> formalCtTypeParameters = machinMethod.getFormalCtTypeParameters();
    assertEquals(1, formalCtTypeParameters.size());
    CtType declarationMachin = returnTypeMachin.getDeclaration();
    assertNotSame(typeParameterList.get(0), declarationMachin);
    assertSame(formalCtTypeParameters.get(0), declarationMachin);
    CtClass innerPaella = factory.getModel().getElements(new NamedElementFilter<>(CtClass.class, "InnerPaella")).get(0);
    List<CtTypeParameter> innerTypeParametersList = innerPaella.getFormalCtTypeParameters();
    assertEquals(typeParameterList.get(0), innerTypeParametersList.get(0).getSuperclass().getDeclaration());
    CtMethod innerMachinMethod = factory.getModel().getElements(new NamedElementFilter<>(CtMethod.class, "innerMachin")).get(0);
    CtTypeReference returnTypeInnerMachin = innerMachinMethod.getType();
    CtTypeReference paramInnerMachinType = ((CtParameter) innerMachinMethod.getParameters().get(0)).getType();
    List<CtTypeParameter> innerMachinFormalCtType = innerMachinMethod.getFormalCtTypeParameters();
    assertSame(typeParameterList.get(0), returnTypeInnerMachin.getDeclaration());
    assertSame(innerMachinFormalCtType.get(0), paramInnerMachinType.getDeclaration());
    CtMethod innerTotoMethod = factory.getModel().getElements(new NamedElementFilter<>(CtMethod.class, "innerToto")).get(0);
    CtTypeReference returnInnerToto = innerTotoMethod.getType();
    CtTypeReference paramInnerToto = ((CtParameter) innerTotoMethod.getParameters().get(0)).getType();
    List<CtTypeParameter> innerTotoFormatCtType = innerTotoMethod.getFormalCtTypeParameters();
    assertSame(innerTotoFormatCtType.get(0), paramInnerToto.getDeclaration());
    assertSame(innerTypeParametersList.get(0), returnInnerToto.getDeclaration());
}
Also used : LikeCtClass(spoon.test.generics.testclasses2.LikeCtClass) CtClass(spoon.reflect.declaration.CtClass) CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) CtType(spoon.reflect.declaration.CtType) CtTypeReference(spoon.reflect.reference.CtTypeReference) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) Launcher(spoon.Launcher) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) CtParameter(spoon.reflect.declaration.CtParameter) CtMethod(spoon.reflect.declaration.CtMethod) MainTest(spoon.test.main.MainTest) Test(org.junit.Test)

Aggregations

NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)43 Test (org.junit.Test)42 Launcher (spoon.Launcher)19 Factory (spoon.reflect.factory.Factory)17 CtClass (spoon.reflect.declaration.CtClass)10 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)10 CtMethod (spoon.reflect.declaration.CtMethod)8 MainTest (spoon.test.main.MainTest)8 CtIf (spoon.reflect.code.CtIf)7 CtTypeParameter (spoon.reflect.declaration.CtTypeParameter)7 CtTypeReference (spoon.reflect.reference.CtTypeReference)7 ModelUtils.createFactory (spoon.testing.utils.ModelUtils.createFactory)7 CtBlock (spoon.reflect.code.CtBlock)6 CtCodeSnippetStatement (spoon.reflect.code.CtCodeSnippetStatement)6 File (java.io.File)4 CtAnnotation (spoon.reflect.declaration.CtAnnotation)4 CtElement (spoon.reflect.declaration.CtElement)4 Annotation (java.lang.annotation.Annotation)3 CtLiteral (spoon.reflect.code.CtLiteral)3 CtParameter (spoon.reflect.declaration.CtParameter)3