Search in sources :

Example 71 with CtClass

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());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) Launcher(spoon.Launcher) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Test(org.junit.Test)

Example 72 with CtClass

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);
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) CtClass(spoon.reflect.declaration.CtClass) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) File(java.io.File) Test(org.junit.Test)

Example 73 with CtClass

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;"));
}
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 74 with CtClass

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());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtPackageReference(spoon.reflect.reference.CtPackageReference) CtField(spoon.reflect.declaration.CtField) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 75 with CtClass

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

Aggregations

CtClass (spoon.reflect.declaration.CtClass)168 Test (org.junit.Test)151 Launcher (spoon.Launcher)102 Factory (spoon.reflect.factory.Factory)84 CtMethod (spoon.reflect.declaration.CtMethod)42 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)22 CtAnnotation (spoon.reflect.declaration.CtAnnotation)19 SpoonModelBuilder (spoon.SpoonModelBuilder)17 CtInvocation (spoon.reflect.code.CtInvocation)16 File (java.io.File)15 CtTypeReference (spoon.reflect.reference.CtTypeReference)15 OuterAnnotation (spoon.test.annotation.testclasses.Foo.OuterAnnotation)15 Annotation (java.lang.annotation.Annotation)14 AbstractFilter (spoon.reflect.visitor.filter.AbstractFilter)14 AnnotationDefaultAnnotation (spoon.test.annotation.testclasses.AnnotationDefaultAnnotation)14 InnerAnnotation (spoon.test.annotation.testclasses.Foo.InnerAnnotation)14 MiddleAnnotation (spoon.test.annotation.testclasses.Foo.MiddleAnnotation)14 GlobalAnnotation (spoon.test.annotation.testclasses.GlobalAnnotation)14 SuperAnnotation (spoon.test.annotation.testclasses.SuperAnnotation)14 TypeAnnotation (spoon.test.annotation.testclasses.TypeAnnotation)14