Search in sources :

Example 21 with CtPackage

use of spoon.reflect.declaration.CtPackage 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 22 with CtPackage

use of spoon.reflect.declaration.CtPackage 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 23 with CtPackage

use of spoon.reflect.declaration.CtPackage in project spoon by INRIA.

the class PackageTest method testAddAnnotationToPackage.

@Test
public void testAddAnnotationToPackage() throws Exception {
    // contract: Created package-info should used imports in auto-import
    final Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/java/spoon/test/pkg/testclasses/Foo.java");
    spoon.getEnvironment().setAutoImports(true);
    File outputDir = new File("./target/spoon-packageinfo");
    spoon.getEnvironment().setSourceOutputDirectory(outputDir);
    spoon.buildModel();
    CtAnnotationType annotationType = (CtAnnotationType) spoon.getFactory().Annotation().get(GlobalAnnotation.class);
    CtAnnotation annotation = spoon.getFactory().Core().createAnnotation();
    annotation.setAnnotationType(annotationType.getReference());
    CtPackage ctPackage = spoon.getFactory().Package().get("spoon.test.pkg.testclasses");
    ctPackage.addAnnotation(annotation);
    JavaOutputProcessor outputProcessor = spoon.createOutputWriter();
    outputProcessor.process(ctPackage);
    File packageInfo = new File(outputDir, "spoon/test/pkg/testclasses/package-info.java");
    assertTrue(packageInfo.exists());
    canBeBuilt(packageInfo, 8);
    List<String> lines = Files.readAllLines(packageInfo.toPath());
    assertFalse(lines.isEmpty());
    for (String s : lines) {
        if (s.trim().startsWith("import")) {
            assertEquals("import spoon.test.annotation.testclasses.GlobalAnnotation;", s.trim());
        }
        if (s.trim().startsWith("@")) {
            assertEquals("@GlobalAnnotation", s.trim());
        }
    }
}
Also used : CtAnnotation(spoon.reflect.declaration.CtAnnotation) GlobalAnnotation(spoon.test.annotation.testclasses.GlobalAnnotation) Launcher(spoon.Launcher) CtAnnotationType(spoon.reflect.declaration.CtAnnotationType) CtPackage(spoon.reflect.declaration.CtPackage) File(java.io.File) JavaOutputProcessor(spoon.support.JavaOutputProcessor) Test(org.junit.Test)

Example 24 with CtPackage

use of spoon.reflect.declaration.CtPackage in project spoon by INRIA.

the class TypeFactory method get.

/**
 * Gets a created type from its qualified name.
 *
 * @return a found type or null if does not exist
 */
@SuppressWarnings("unchecked")
public <T> CtType<T> get(final String qualifiedName) {
    int packageIndex = qualifiedName.lastIndexOf(CtPackage.PACKAGE_SEPARATOR);
    CtPackage pack;
    if (packageIndex > 0) {
        pack = factory.Package().get(qualifiedName.substring(0, packageIndex));
    } else {
        pack = factory.Package().getRootPackage();
    }
    if (pack != null) {
        CtType<T> type = pack.getType(qualifiedName.substring(packageIndex + 1));
        if (type != null) {
            return type;
        }
    }
    int inertTypeIndex = qualifiedName.lastIndexOf(CtType.INNERTTYPE_SEPARATOR);
    if (inertTypeIndex > 0) {
        String s = qualifiedName.substring(0, inertTypeIndex);
        CtType<T> t = factory.Type().get(s);
        if (t == null) {
            return null;
        }
        String className = qualifiedName.substring(inertTypeIndex + 1);
        final CtTypeReference<T> reference = t.getReference();
        if (reference.isLocalType()) {
            final List<CtClass<T>> enclosingClasses = t.getElements(new TypeFilter<CtClass<T>>(CtClass.class) {

                @Override
                public boolean matches(CtClass<T> element) {
                    return super.matches(element) && element.getQualifiedName().equals(qualifiedName);
                }
            });
            if (enclosingClasses.size() == 0) {
                return null;
            }
            return enclosingClasses.get(0);
        }
        try {
            // If the class name can't be parsed in integer, the method throws an exception.
            // If the class name is an integer, the class is an anonymous class, otherwise,
            // it is a standard class.
            Integer.parseInt(className);
            final List<CtNewClass> anonymousClasses = t.getElements(new TypeFilter<CtNewClass>(CtNewClass.class) {

                @Override
                public boolean matches(CtNewClass element) {
                    return super.matches(element) && element.getAnonymousClass().getQualifiedName().equals(qualifiedName);
                }
            });
            if (anonymousClasses.size() == 0) {
                return null;
            }
            return anonymousClasses.get(0).getAnonymousClass();
        } catch (NumberFormatException e) {
            return t.getNestedType(className);
        }
    }
    return null;
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtNewClass(spoon.reflect.code.CtNewClass) CtPackage(spoon.reflect.declaration.CtPackage)

Example 25 with CtPackage

use of spoon.reflect.declaration.CtPackage in project spoon by INRIA.

the class PackageFactory method get.

/**
 * Gets a package.
 *
 * @param qualifiedName
 * 		the package to search
 * @return a found package or null
 */
public CtPackage get(String qualifiedName) {
    if (qualifiedName.contains(CtType.INNERTTYPE_SEPARATOR)) {
        throw new RuntimeException("Invalid package name " + qualifiedName);
    }
    StringTokenizer token = new StringTokenizer(qualifiedName, CtPackage.PACKAGE_SEPARATOR);
    CtPackage current = factory.getModel().getRootPackage();
    if (token.hasMoreElements()) {
        current = current.getPackage(token.nextToken());
        while (token.hasMoreElements() && current != null) {
            current = current.getPackage(token.nextToken());
        }
    }
    return current;
}
Also used : StringTokenizer(java.util.StringTokenizer) CtPackage(spoon.reflect.declaration.CtPackage)

Aggregations

CtPackage (spoon.reflect.declaration.CtPackage)53 Test (org.junit.Test)29 Launcher (spoon.Launcher)21 Factory (spoon.reflect.factory.Factory)13 CtType (spoon.reflect.declaration.CtType)12 File (java.io.File)8 CtClass (spoon.reflect.declaration.CtClass)6 CtElement (spoon.reflect.declaration.CtElement)6 CtTypeReference (spoon.reflect.reference.CtTypeReference)6 ArrayList (java.util.ArrayList)4 CtAnnotation (spoon.reflect.declaration.CtAnnotation)4 CtMethod (spoon.reflect.declaration.CtMethod)4 CtModule (spoon.reflect.declaration.CtModule)4 HashSet (java.util.HashSet)3 StringTokenizer (java.util.StringTokenizer)3 SpoonAPI (spoon.SpoonAPI)3 CtStatement (spoon.reflect.code.CtStatement)3 CtField (spoon.reflect.declaration.CtField)3 ModelUtils.createFactory (spoon.testing.utils.ModelUtils.createFactory)3 Annotation (java.lang.annotation.Annotation)2