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