use of spoon.reflect.declaration.CtClass in project spoon by INRIA.
the class RefactoringTest method testTransformedInstanceofAfterATransformation.
@Test
public void testTransformedInstanceofAfterATransformation() throws Exception {
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] { "-i", "src/test/java/spoon/test/refactoring/testclasses", "-o", "target/spooned/refactoring", "-p", ThisTransformationProcessor.class.getName() });
launcher.run();
final CtClass<?> aClassX = (CtClass<?>) launcher.getFactory().Type().get("spoon.test.refactoring.testclasses.AClassX");
final CtBinaryOperator<?> instanceofInvocation = aClassX.getElements(new TypeFilter<CtBinaryOperator<?>>(CtBinaryOperator.class)).get(0);
assertEquals(BinaryOperatorKind.INSTANCEOF, instanceofInvocation.getKind());
assertEquals("o", instanceofInvocation.getLeftHandOperand().toString());
assertEquals("spoon.test.refactoring.testclasses.AClassX", instanceofInvocation.getRightHandOperand().toString());
}
use of spoon.reflect.declaration.CtClass in project spoon by INRIA.
the class DefaultPrettyPrinterTest method autoImportUsesFullyQualifiedNameWhenImportedNameAlreadyPresent.
@Test
public void autoImportUsesFullyQualifiedNameWhenImportedNameAlreadyPresent() throws Exception {
final Launcher launcher = new Launcher();
final Factory factory = launcher.getFactory();
factory.getEnvironment().setAutoImports(true);
final SpoonModelBuilder compiler = launcher.createCompiler();
compiler.addInputSource(new File("./src/test/java/spoon/test/prettyprinter/testclasses/sub/TypeIdentifierCollision.java"));
compiler.addInputSource(new File("./src/test/java/spoon/test/prettyprinter/testclasses/TypeIdentifierCollision.java"));
compiler.build();
final CtClass<?> aClass = (CtClass<?>) factory.Type().get(spoon.test.prettyprinter.testclasses.TypeIdentifierCollision.class);
String expected = "public void setFieldUsingExternallyDefinedEnumWithSameNameAsLocal() {" + nl + " localField = spoon.test.prettyprinter.testclasses.sub.TypeIdentifierCollision.ENUM.E1.ordinal();" + nl + "}";
String computed = aClass.getMethodsByName("setFieldUsingExternallyDefinedEnumWithSameNameAsLocal").get(0).toString();
assertEquals("We use FQN for E1", expected, computed);
// This is correct however it could be more concise.
expected = "public void setFieldUsingLocallyDefinedEnum() {" + nl + " localField = TypeIdentifierCollision.ENUM.E1.ordinal();" + nl + "}";
computed = aClass.getMethodsByName("setFieldUsingLocallyDefinedEnum").get(0).toString();
assertEquals(expected, computed);
expected = "public void setFieldOfClassWithSameNameAsTheCompilationUnitClass() {" + nl + " spoon.test.prettyprinter.testclasses.sub.TypeIdentifierCollision.globalField = localField;" + nl + "}";
computed = aClass.getMethodsByName("setFieldOfClassWithSameNameAsTheCompilationUnitClass").get(0).toString();
assertEquals("The static field of an external type with the same identifier as the compilation unit is printed with FQN", expected, computed);
// This is correct however it could be more concise.
expected = "public void referToTwoInnerClassesWithTheSameName() {" + nl + " TypeIdentifierCollision.Class0.ClassA.VAR0 = TypeIdentifierCollision.Class0.ClassA.getNum();" + nl + " TypeIdentifierCollision.Class1.ClassA.VAR1 = TypeIdentifierCollision.Class1.ClassA.getNum();" + nl + "}";
// Ensure the ClassA of Class0 takes precedence over an import statement for ClassA in Class1, and it's identifier can be the short version.
computed = aClass.getMethodsByName("referToTwoInnerClassesWithTheSameName").get(0).toString();
assertEquals("where inner types have the same identifier only one may be shortened and the other should be fully qualified", expected, computed);
expected = "public enum ENUM {" + nl + " E1(spoon.test.prettyprinter.testclasses.sub.TypeIdentifierCollision.globalField,spoon.test.prettyprinter.testclasses.sub.TypeIdentifierCollision.ENUM.E1);" + nl + " final int NUM;" + nl + nl + " final Enum<?> e;" + nl + nl + " private ENUM(int num, Enum<?> e) {" + nl + " NUM = num;" + nl + " this.e = e;" + nl + " }" + nl + "}";
computed = aClass.getNestedType("ENUM").toString();
assertEquals(expected, computed);
}
use of spoon.reflect.declaration.CtClass 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.declaration.CtClass in project spoon by INRIA.
the class PackageTest method testGetFQNInNoClassPath.
@Test
public void testGetFQNInNoClassPath() {
// contract: CtPackageReference simple name is also the fully qualified name of its referenced package, even in noclasspath
final Launcher spoon = new Launcher();
spoon.addInputResource("./src/test/resources/noclasspath/TorIntegration.java");
spoon.getEnvironment().setNoClasspath(true);
spoon.buildModel();
CtClass torClass = spoon.getFactory().Class().get("com.duckduckgo.mobile.android.util.TorIntegration");
CtField field = torClass.getField("orbotHelper");
CtPackageReference fieldPkg = field.getType().getPackage();
assertEquals("info.guardianproject.onionkit.ui", fieldPkg.getSimpleName());
assertEquals("info.guardianproject.onionkit.ui", fieldPkg.getQualifiedName());
}
use of spoon.reflect.declaration.CtClass 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;"));
}
Aggregations