use of spoon.reflect.declaration.CtModule in project spoon by INRIA.
the class TestModule method testGetParentOfRootPackageOfModule.
@Test
public void testGetParentOfRootPackageOfModule() {
// contract: unnamed module root package should have unnamed module as parent
final Launcher launcher = new Launcher();
CtModule unnamedModule = launcher.getFactory().getModel().getUnnamedModule();
assertSame(unnamedModule, unnamedModule.getRootPackage().getParent());
}
use of spoon.reflect.declaration.CtModule in project spoon by INRIA.
the class FilterTest method testCtScannerListener.
@Test
public void testCtScannerListener() throws Exception {
// contract: CtScannerFunction can be subclassed and configured by a CtScannerListener
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] { "--output-type", "nooutput", "--level", "info" });
launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
launcher.run();
class Context {
long nrOfEnter = 0;
long nrOfEnterRetTrue = 0;
long nrOfExit = 0;
long nrOfResults = 0;
}
Context context1 = new Context();
// scan only packages until top level classes. Do not scan class internals
List<CtElement> result1 = launcher.getFactory().getModel().map(new CtScannerFunction().setListener(new CtScannerListener() {
@Override
public ScanningMode enter(CtElement element) {
context1.nrOfEnter++;
if (element instanceof CtType) {
return ScanningMode.SKIP_CHILDREN;
}
return ScanningMode.NORMAL;
}
@Override
public void exit(CtElement element) {
context1.nrOfExit++;
}
})).list();
// check that test is visiting some nodes
assertTrue(context1.nrOfEnter > 0);
assertTrue(result1.size() > 0);
// contract: if enter is called and returns SKIP_CHILDREN or NORMAL, then exit must be called too. Exceptions are ignored for now
assertEquals(context1.nrOfEnter, context1.nrOfExit);
Context context2 = new Context();
Iterator iter = result1.iterator();
// scan only from packages till top level classes. Do not scan class internals
launcher.getFactory().getModel().map(new CtScannerFunction().setListener(new CtScannerListener() {
int inClass = 0;
@Override
public ScanningMode enter(CtElement element) {
context2.nrOfEnter++;
if (inClass > 0) {
// we are in class. skip this node and all children
return ScanningMode.SKIP_ALL;
}
if (element instanceof CtType) {
inClass++;
}
context2.nrOfEnterRetTrue++;
return ScanningMode.NORMAL;
}
@Override
public void exit(CtElement element) {
context2.nrOfExit++;
if (element instanceof CtType) {
inClass--;
}
assertTrue(inClass == 0 || inClass == 1);
}
})).forEach(ele -> {
context2.nrOfResults++;
assertTrue("ele instanceof " + ele.getClass(), ele instanceof CtPackage || ele instanceof CtType || ele instanceof CtModule);
// check that first and second query returned same results
assertSame(ele, iter.next());
});
// check that test is visiting some nodes
assertTrue(context2.nrOfEnter > 0);
assertTrue(context2.nrOfEnter > context2.nrOfEnterRetTrue);
assertEquals(result1.size(), context2.nrOfResults);
// contract: if enter is called and does not returns SKIP_ALL, then exit must be called too. Exceptions are ignored for now
assertEquals(context2.nrOfEnterRetTrue, context2.nrOfExit);
}
use of spoon.reflect.declaration.CtModule in project spoon by INRIA.
the class JDTCommentBuilder method insertCommentInAST.
/**
* Inserts the comment into the AST.
* @param comment the comment to insert
*/
private void insertCommentInAST(final CtComment comment) {
CtElement commentParent = findCommentParent(comment);
if (commentParent == null) {
File file = spoonUnit.getFile();
if (file != null && file.getName().equals(DefaultJavaPrettyPrinter.JAVA_PACKAGE_DECLARATION)) {
spoonUnit.getDeclaredPackage().addComment(comment);
} else if (file != null && file.getName().equals(DefaultJavaPrettyPrinter.JAVA_MODULE_DECLARATION)) {
spoonUnit.getDeclaredModule().addComment(comment);
} else {
comment.setCommentType(CtComment.CommentType.FILE);
addCommentToNear(comment, new ArrayList<CtElement>(spoonUnit.getDeclaredTypes()));
}
return;
}
// visitor that inserts the comment in the element
CtInheritanceScanner insertionVisitor = new CtInheritanceScanner() {
private boolean isScanned = false;
@Override
public void scan(CtElement e) {
if (e == null) {
return;
}
// Do not visit the AST, only the first element
if (!isScanned) {
isScanned = true;
if (e.getPosition().getSourceStart() == comment.getPosition().getSourceStart()) {
e.addComment(comment);
return;
}
super.scan(e);
}
}
@Override
public <R> void visitCtStatementList(CtStatementList e) {
addCommentToNear(comment, new ArrayList<CtElement>(e.getStatements()));
try {
comment.getParent();
} catch (ParentNotInitializedException ex) {
e.addStatement(comment);
}
}
@Override
public <T> void visitCtMethod(CtMethod<T> e) {
e.addComment(comment);
}
@Override
public <T> void visitCtConstructor(CtConstructor<T> e) {
e.addComment(comment);
}
@Override
public <T> void visitCtConditional(CtConditional<T> e) {
List<CtElement> elements = new ArrayList<>();
elements.add(e.getElseExpression());
elements.add(e.getThenExpression());
elements.add(e.getCondition());
addCommentToNear(comment, elements);
}
@Override
public <T> void visitCtBinaryOperator(CtBinaryOperator<T> e) {
List<CtElement> elements = new ArrayList<>();
elements.add(e.getLeftHandOperand());
elements.add(e.getRightHandOperand());
addCommentToNear(comment, elements);
}
@Override
public <T> void visitCtClass(CtClass<T> e) {
if (comment.getPosition().getLine() <= e.getPosition().getLine()) {
e.addComment(comment);
return;
}
final List<CtElement> elements = new ArrayList<>();
for (CtTypeMember typeMember : e.getTypeMembers()) {
if (typeMember instanceof CtField || typeMember instanceof CtMethod || typeMember instanceof CtConstructor) {
elements.add(typeMember);
}
}
addCommentToNear(comment, elements);
try {
comment.getParent();
} catch (ParentNotInitializedException ex) {
e.addComment(comment);
}
}
@Override
public <T> void visitCtInterface(CtInterface<T> e) {
final List<CtElement> elements = new ArrayList<>();
for (CtTypeMember typeMember : e.getTypeMembers()) {
if (typeMember instanceof CtField || typeMember instanceof CtMethod) {
elements.add(typeMember);
}
}
addCommentToNear(comment, elements);
try {
comment.getParent();
} catch (ParentNotInitializedException ex) {
e.addComment(comment);
}
}
@Override
public <T> void visitCtField(CtField<T> e) {
e.addComment(comment);
}
@Override
public <E> void visitCtSwitch(CtSwitch<E> e) {
List<CtCase<? super E>> cases = e.getCases();
CtCase previous = null;
for (int i = 0; i < cases.size(); i++) {
CtCase<? super E> ctCase = cases.get(i);
if (previous == null) {
if (comment.getPosition().getSourceStart() < ctCase.getPosition().getSourceStart() && e.getPosition().getSourceStart() < comment.getPosition().getSourceStart()) {
ctCase.addComment(comment);
return;
}
} else {
if (previous.getPosition().getSourceEnd() < comment.getPosition().getSourceStart() && ctCase.getPosition().getSourceStart() > comment.getPosition().getSourceStart()) {
addCommentToNear(comment, new ArrayList<CtElement>(previous.getStatements()));
try {
comment.getParent();
} catch (ParentNotInitializedException ex) {
previous.addStatement(comment);
}
return;
}
}
previous = ctCase;
}
if (previous.getPosition().getSourceEnd() < comment.getPosition().getSourceStart()) {
addCommentToNear(comment, new ArrayList<CtElement>(previous.getStatements()));
try {
comment.getParent();
} catch (ParentNotInitializedException ex) {
previous.addStatement(comment);
}
return;
}
try {
comment.getParent();
} catch (ParentNotInitializedException ex) {
e.addComment(comment);
}
}
@Override
public void visitCtIf(CtIf e) {
if (!(e.getThenStatement() instanceof CtBlock)) {
if (comment.getPosition().getSourceEnd() <= e.getThenStatement().getPosition().getSourceStart()) {
e.getThenStatement().addComment(comment);
return;
}
}
if (e.getElseStatement() != null) {
SourcePosition thenPosition = e.getThenStatement().getPosition() == null ? ((CtBlock) e.getThenStatement()).getStatement(0).getPosition() : e.getThenStatement().getPosition();
SourcePosition elsePosition = e.getElseStatement().getPosition() == null ? ((CtBlock) e.getElseStatement()).getStatement(0).getPosition() : e.getElseStatement().getPosition();
if (comment.getPosition().getSourceStart() > thenPosition.getSourceEnd() && comment.getPosition().getSourceEnd() < elsePosition.getSourceStart()) {
e.getElseStatement().addComment(comment);
}
}
try {
comment.getParent();
} catch (ParentNotInitializedException ex) {
e.addComment(comment);
}
}
@Override
public void scanCtStatement(CtStatement s) {
if (!(s instanceof CtStatementList || s instanceof CtSwitch)) {
s.addComment(comment);
}
}
@Override
public void visitCtAnonymousExecutable(CtAnonymousExecutable e) {
e.addComment(comment);
}
@Override
public <T> void visitCtNewArray(CtNewArray<T> e) {
addCommentToNear(comment, new ArrayList<CtElement>(e.getElements()));
try {
comment.getParent();
} catch (ParentNotInitializedException ex) {
e.addComment(comment);
}
}
@Override
public <T> void visitCtParameter(CtParameter<T> e) {
e.addComment(comment);
}
@Override
public void visitCtCatch(CtCatch e) {
if (comment.getPosition().getLine() <= e.getPosition().getLine()) {
e.addComment(comment);
return;
}
}
@Override
public void visitCtModule(CtModule module) {
addCommentToNear(comment, new ArrayList<>(module.getModuleDirectives()));
}
};
insertionVisitor.scan(commentParent);
try {
comment.getParent();
} catch (ParentNotInitializedException e) {
LOGGER.error(comment + " is not added into the AST", e);
}
}
use of spoon.reflect.declaration.CtModule in project spoon by INRIA.
the class ReferenceBuilder method getModuleReference.
public CtModuleReference getModuleReference(ModuleReference moduleReference) {
String moduleName = new String(moduleReference.moduleName);
CtModule module = this.jdtTreeBuilder.getFactory().Module().getModule(moduleName);
if (module == null) {
CtModuleReference ctModuleReference = this.jdtTreeBuilder.getFactory().Core().createModuleReference();
ctModuleReference.setSimpleName(moduleName);
return ctModuleReference;
} else {
return module.getReference();
}
}
use of spoon.reflect.declaration.CtModule in project spoon by INRIA.
the class TestModule method testCompleteModuleInfoContentNoClasspath.
@Test
public void testCompleteModuleInfoContentNoClasspath() {
// contract: all information of the module-info should be available through the model
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/resources/spoon/test/module/simple_module/module-info.java");
launcher.getEnvironment().setNoClasspath(true);
launcher.getEnvironment().setComplianceLevel(9);
launcher.buildModel();
assertEquals(2, launcher.getModel().getAllModules().size());
CtModule unnamedModule = launcher.getFactory().Module().getOrCreate(CtModule.TOP_LEVEL_MODULE_NAME);
assertSame(unnamedModule, launcher.getModel().getUnnamedModule());
CtModule moduleGreetings = launcher.getFactory().Module().getOrCreate("simple_module");
assertEquals("simple_module", moduleGreetings.getSimpleName());
assertEquals(7, moduleGreetings.getModuleDirectives().size());
List<CtModuleRequirement> requiredModules = moduleGreetings.getRequiredModules();
assertEquals(1, requiredModules.size());
CtModuleRequirement moduleRequirement = requiredModules.get(0);
assertEquals("java.logging", moduleRequirement.getModuleReference().getSimpleName());
assertTrue(moduleRequirement.getRequiresModifiers().contains(CtModuleRequirement.RequiresModifier.TRANSITIVE));
List<CtPackageExport> moduleExports = moduleGreetings.getExportedPackages();
assertEquals(1, moduleExports.size());
assertEquals("com.greetings.pkg", moduleExports.get(0).getPackageReference().getQualifiedName());
assertEquals(2, moduleExports.get(0).getTargetExport().size());
for (CtModuleReference target : moduleExports.get(0).getTargetExport()) {
if (!target.getSimpleName().equals("com.other.module") && !target.getSimpleName().equals("com.second.module")) {
fail();
}
}
List<CtPackageExport> moduleOpened = moduleGreetings.getOpenedPackages();
assertEquals(2, moduleOpened.size());
CtPackageExport openedFirst = moduleOpened.get(0);
CtPackageExport openedSecond = moduleOpened.get(1);
assertEquals("com.greetings.otherpkg", openedFirst.getPackageReference().getSimpleName());
assertTrue(openedFirst.getTargetExport().isEmpty());
assertEquals("com.greetings.openpkg", openedSecond.getPackageReference().getSimpleName());
assertEquals(1, openedSecond.getTargetExport().size());
assertEquals("com.third.module", openedSecond.getTargetExport().iterator().next().getSimpleName());
List<CtUsedService> consumedService = moduleGreetings.getUsedServices();
assertEquals(1, consumedService.size());
assertEquals("com.greetings.pkg.ConsumedService", consumedService.get(0).getServiceType().getQualifiedName());
List<CtProvidedService> providedServices = moduleGreetings.getProvidedServices();
assertEquals(2, providedServices.size());
CtProvidedService providedService1 = providedServices.get(0);
CtProvidedService providedService2 = providedServices.get(1);
assertEquals("com.greetings.pkg.ConsumedService", providedService1.getServiceType().getQualifiedName());
assertEquals(2, providedService1.getImplementationTypes().size());
assertEquals("com.greetings.pkg.ProvidedClass1", providedService1.getImplementationTypes().get(0).getQualifiedName());
assertEquals("com.greetings.otherpkg.ProvidedClass2", providedService1.getImplementationTypes().get(1).getQualifiedName());
assertEquals("java.logging.Service", providedService2.getServiceType().getQualifiedName());
assertEquals(1, providedService2.getImplementationTypes().size());
assertEquals("com.greetings.logging.Logger", providedService2.getImplementationTypes().get(0).getQualifiedName());
}
Aggregations