use of spoon.reflect.declaration.CtPackage in project spoon by INRIA.
the class APITest method testBasicAPIUsage.
@Test
public void testBasicAPIUsage() throws Exception {
// this test shows a basic usage of the Launcher API without command line
// and asserts there is no exception
Launcher spoon = new Launcher();
spoon.setArgs(new String[] { "--compile", "--output-type", "compilationunits" });
spoon.addInputResource("src/test/resources/spoon/test/api");
spoon.run();
Factory factory = spoon.getFactory();
for (CtPackage p : factory.Package().getAll()) {
spoon.getEnvironment().debugMessage("package: " + p.getQualifiedName());
}
for (CtType<?> s : factory.Class().getAll()) {
spoon.getEnvironment().debugMessage("class: " + s.getQualifiedName());
}
}
use of spoon.reflect.declaration.CtPackage in project spoon by INRIA.
the class SpoonArchitectureEnforcerTest method testFactorySubFactory.
@Test
public void testFactorySubFactory() throws Exception {
// contract:: all subfactory methods must also be in the main factory
// this is very important for usability and discoverability
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/main/java/spoon/reflect/factory");
class SanityCheck {
int val = 0;
}
;
SanityCheck sanityCheck = new SanityCheck();
launcher.addProcessor(new AbstractManualProcessor() {
@Override
public void process() {
CtType factoryImpl = getFactory().Interface().get(Factory.class);
CtPackage factoryPackage = getFactory().Package().getOrCreate("spoon.reflect.factory");
CtInterface itf = getFactory().Interface().create("MegaFactoryItf");
CtClass impl = getFactory().Class().create("MegaFactory");
for (CtType<?> t : factoryPackage.getTypes()) {
//
if (t.getSimpleName().startsWith("Mega"))
continue;
for (CtMethod<?> m : t.getMethods()) {
// we check only public methods
if (m.hasModifier(ModifierKind.PUBLIC) == false)
continue;
// we only consider factory methods
if (!m.getSimpleName().startsWith("create"))
continue;
// too generic, what should we create??
if (m.getSimpleName().equals("create")) {
String simpleNameType = m.getType().getSimpleName().replace("Ct", "");
CtMethod method = m.clone();
method.setSimpleName("create" + simpleNameType);
assertTrue(method.getSignature() + " (from " + t.getQualifiedName() + ") is not present in the main factory", factoryImpl.hasMethod(method));
continue;
}
// too generic, is it a fieldref? an execref? etc
if (m.getSimpleName().equals("createReference"))
continue;
if (m.getModifiers().contains(ModifierKind.ABSTRACT))
continue;
sanityCheck.val++;
// the core assertion
assertTrue(m.getSignature() + " is not present in the main factory", factoryImpl.hasMethod(m));
}
}
}
});
launcher.run();
assertTrue(sanityCheck.val > 100);
}
use of spoon.reflect.declaration.CtPackage in project spoon by INRIA.
the class SpoonArchitectureEnforcerTest method testSpecPackage.
@Test
public void testSpecPackage() throws Exception {
// contract: when a pull-request introduces a new package, it is made explicit during code review
// when a pull-request introduces a new package, this test fails and the author has to explicitly declare the new package here
Set<String> officialPackages = new TreeSet<>();
officialPackages.add("spoon.compiler.builder");
officialPackages.add("spoon.compiler");
officialPackages.add("spoon.experimental.modelobs.action");
officialPackages.add("spoon.experimental.modelobs.context");
officialPackages.add("spoon.experimental.modelobs");
officialPackages.add("spoon.experimental");
officialPackages.add("spoon.legacy");
officialPackages.add("spoon.processing");
officialPackages.add("spoon.refactoring");
officialPackages.add("spoon.reflect.annotations");
officialPackages.add("spoon.reflect.code");
officialPackages.add("spoon.reflect.cu.position");
officialPackages.add("spoon.reflect.cu");
officialPackages.add("spoon.reflect.declaration");
officialPackages.add("spoon.reflect.eval");
officialPackages.add("spoon.reflect.factory");
officialPackages.add("spoon.reflect.path.impl");
officialPackages.add("spoon.reflect.path");
officialPackages.add("spoon.reflect.reference");
officialPackages.add("spoon.reflect.visitor.chain");
officialPackages.add("spoon.reflect.visitor.filter");
officialPackages.add("spoon.reflect.visitor.printer");
officialPackages.add("spoon.reflect.visitor");
officialPackages.add("spoon.reflect");
officialPackages.add("spoon.support.comparator");
officialPackages.add("spoon.support.compiler.jdt");
officialPackages.add("spoon.support.compiler");
officialPackages.add("spoon.support.gui");
officialPackages.add("spoon.support.reflect.code");
officialPackages.add("spoon.support.reflect.cu.position");
officialPackages.add("spoon.support.reflect.cu");
officialPackages.add("spoon.support.reflect.declaration");
officialPackages.add("spoon.support.reflect.eval");
officialPackages.add("spoon.reflect.meta");
officialPackages.add("spoon.reflect.meta.impl");
officialPackages.add("spoon.support.reflect.reference");
officialPackages.add("spoon.support.reflect");
officialPackages.add("spoon.support.template");
officialPackages.add("spoon.support.util");
officialPackages.add("spoon.support.visitor.clone");
officialPackages.add("spoon.support.visitor.equals");
officialPackages.add("spoon.support.visitor.java.internal");
officialPackages.add("spoon.support.visitor.java.reflect");
officialPackages.add("spoon.support.visitor.java");
officialPackages.add("spoon.support.visitor.replace");
officialPackages.add("spoon.support.visitor");
officialPackages.add("spoon.support");
officialPackages.add("spoon.template");
officialPackages.add("spoon.testing.utils");
officialPackages.add("spoon.testing");
officialPackages.add("spoon");
// root package
officialPackages.add("");
SpoonAPI spoon = new Launcher();
spoon.addInputResource("src/main/java/");
spoon.buildModel();
final Set<String> currentPackages = new TreeSet<>();
spoon.getModel().processWith(new AbstractProcessor<CtPackage>() {
@Override
public void process(CtPackage element) {
currentPackages.add(element.getQualifiedName());
}
});
assertSetEquals("you have created a new package or removed an existing one, please declare it explicitly in SpoonArchitectureEnforcerTest#testSpecPackage", officialPackages, currentPackages);
}
use of spoon.reflect.declaration.CtPackage in project spoon by INRIA.
the class AnnotationTest method testRepeatSameAnnotationOnPackage.
@Test
public void testRepeatSameAnnotationOnPackage() throws Exception {
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/annotation/testclasses/AnnotationsRepeated.java");
launcher.addInputResource("./src/test/java/spoon/test/annotation/testclasses/package-info.java");
launcher.buildModel();
Factory factory = launcher.getFactory();
final CtPackage pkg = factory.Package().get("spoon.test.annotation.testclasses");
final List<CtAnnotation<? extends Annotation>> annotations = pkg.getAnnotations();
assertEquals("Local variable must to have multi annotation of the same type", 2, annotations.size());
assertEquals("Type of the first annotation is AnnotationRepeated", AnnotationRepeated.class, annotations.get(0).getAnnotationType().getActualClass());
assertEquals("Type of the second annotation is AnnotationRepeated", AnnotationRepeated.class, annotations.get(1).getAnnotationType().getActualClass());
assertEquals("Argument of the first annotation is \"Package 1\"", "Package 1", ((CtLiteral) annotations.get(0).getValue("value")).getValue());
assertEquals("Argument of the second annotation is \"Package 2\"", "Package 2", ((CtLiteral) annotations.get(1).getValue("value")).getValue());
}
use of spoon.reflect.declaration.CtPackage in project spoon by INRIA.
the class TestCompilationUnit method testGetUnitTypeWorksWithDeclaredPackage.
@Test
public void testGetUnitTypeWorksWithDeclaredPackage() {
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/pkg/package-info.java");
launcher.buildModel();
CtPackage ctPackage = launcher.getFactory().Package().get("spoon.test.pkg");
CompilationUnit compilationUnit = ctPackage.getPosition().getCompilationUnit();
assertEquals(CompilationUnit.UNIT_TYPE.PACKAGE_DECLARATION, compilationUnit.getUnitType());
}
Aggregations