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