Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

review: feature: ParentFunction #1153

Merged
merged 4 commits into from
Feb 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/main/java/spoon/reflect/CtModelImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ public void accept(CtVisitor visitor) {
public CtElement getParent() throws ParentNotInitializedException {
return null;
}
});

@Override
public Factory getFactory() {
return CtRootPackage.this.getFactory();
}
});
}

@Override
Expand Down
61 changes: 61 additions & 0 deletions src/main/java/spoon/reflect/visitor/filter/ParentFunction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Copyright (C) 2006-2017 INRIA and contributors
* Spoon - http://spoon.gforge.inria.fr/
*
* This software is governed by the CeCILL-C License under French law and
* abiding by the rules of distribution of free software. You can use, modify
* and/or redistribute the software under the terms of the CeCILL-C license as
* circulated by CEA, CNRS and INRIA at http://www.cecill.info.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL-C license and that you accept its terms.
*/
package spoon.reflect.visitor.filter;

import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtPackage;
import spoon.reflect.visitor.chain.CtConsumableFunction;
import spoon.reflect.visitor.chain.CtConsumer;

/**
* This Function expects a {@link CtElement} as input
* and returns all parents of this element.
*
* By default input is not returned,
* but this behavior can be changed by call of {@link #includingSelf(boolean)} with value true
*/
public class ParentFunction implements CtConsumableFunction<CtElement> {

private boolean includingSelf = false;

public ParentFunction() {
}

/**
* @param includingSelf if true then input element is sent to output too. By default it is false.
*/
public ParentFunction includingSelf(boolean includingSelf) {
this.includingSelf = includingSelf;
return this;
}

@Override
public void apply(CtElement input, CtConsumer<Object> outputConsumer) {
if (input == null) {
return;
}
if (includingSelf) {
outputConsumer.accept(input);
}
CtPackage rootPackage = input.getFactory().getModel().getRootPackage();
CtElement parent = input;
while (parent != null && parent != rootPackage) {
parent = parent.getParent();
outputConsumer.accept(parent);
}
}
}
37 changes: 37 additions & 0 deletions src/test/java/spoon/test/filters/FilterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import spoon.reflect.code.CtFieldAccess;
import spoon.reflect.code.CtIf;
import spoon.reflect.code.CtInvocation;
import spoon.reflect.code.CtLocalVariable;
import spoon.reflect.code.CtLoop;
import spoon.reflect.code.CtNewClass;
import spoon.reflect.code.CtStatement;
Expand Down Expand Up @@ -61,6 +62,7 @@
import spoon.reflect.visitor.filter.OverriddenMethodFilter;
import spoon.reflect.visitor.filter.OverriddenMethodQuery;
import spoon.reflect.visitor.filter.OverridingMethodFilter;
import spoon.reflect.visitor.filter.ParentFunction;
import spoon.reflect.visitor.filter.RegexFilter;
import spoon.reflect.visitor.filter.ReturnOrThrowFilter;
import spoon.reflect.visitor.filter.TypeFilter;
Expand Down Expand Up @@ -917,4 +919,39 @@ void failIfTerminated(String place) {
assertTrue(firstMethod!=null);
assertTrue(context.wasTerminated);
}
@Test
public void testParentFunction() throws Exception {
// contract: a mapping function which returns all parents of CtElement

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();

CtClass<?> cls = launcher.getFactory().Class().get(Tacos.class);
CtLocalVariable<?> varStrings = cls.filterChildren(new NameFilter<>("strings")).first();

class Context {
CtElement expectedParent;
}

Context context = new Context();

context.expectedParent = varStrings;

varStrings.map(new ParentFunction()).forEach((parent)->{
context.expectedParent = context.expectedParent.getParent();
assertSame(context.expectedParent, parent);
});

//context.expectedParent is last visited element

//Check that last visited element was root package
assertSame(launcher.getFactory().getModel().getRootPackage(), context.expectedParent);

//contract: if includingSelf(false), then parent of input element is first element
assertSame(varStrings.getParent(), varStrings.map(new ParentFunction().includingSelf(false)).first());
//contract: if includingSelf(true), then input element is first element
assertSame(varStrings, varStrings.map(new ParentFunction().includingSelf(true)).first());
}
}