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

feature: Factory#createQuery(Iterable) #1777

Merged
merged 2 commits into from
Dec 7, 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
5 changes: 5 additions & 0 deletions src/main/java/spoon/reflect/factory/Factory.java
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,11 @@ public interface Factory {
*/
CtQuery createQuery(Object... input);

/**
* @see QueryFactory#createQuery(Iterable)
*/
CtQuery createQuery(Iterable<?> input);

/**
*
* @see AnnotationFactory#create(String)
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/spoon/reflect/factory/FactoryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,11 @@ public CtQuery createQuery(Object[] input) {
return Query().createQuery(input);
}

@Override
public CtQuery createQuery(Iterable<?> input) {
return Query().createQuery(input);
}

@Override
public CtAnnotationType createAnnotationType(String qualifiedName) {
return Annotation().create(qualifiedName);
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/spoon/reflect/factory/QueryFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ public CtQuery createQuery(Object input) {
return new CtQueryImpl(input);
}

/**
* Creates a bound query. Use directly
* {@link CtQuery#forEach(spoon.reflect.visitor.chain.CtConsumer)}
* or {@link CtQuery#list()} to evaluate the query
*/
public CtQuery createQuery(Iterable<?> inputs) {
return new CtQueryImpl().addInput(inputs);
}

/**
* Creates a bound query with an optional number
* of inputs elements to the query (see {@link CtQuery#setInput(Object...)})
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/spoon/reflect/visitor/chain/CtQueryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@ public CtQueryImpl addInput(Object... input) {
}
return this;
}
public CtQueryImpl addInput(Iterable<?> input) {
if (this.inputs == null) {
this.inputs = new ArrayList<>();
}
if (input != null) {
for (Object in : input) {
this.inputs.add(in);
}
}
return this;
}

@Override
public <R> void forEach(CtConsumer<R> consumer) {
Expand Down
17 changes: 12 additions & 5 deletions src/test/java/spoon/test/filters/FilterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,7 @@ public void testReuseOfBaseQuery() throws Exception {
@Test
public void testQueryWithOptionalNumberOfInputs() throws Exception {
// contract: QueryFactory allows to create query with an optional number of inputs
// the input can be provided as Array or Iterable
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] {"--output-type", "nooutput","--level","info" });
launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
Expand All @@ -768,13 +769,19 @@ public void testQueryWithOptionalNumberOfInputs() throws Exception {
// here is the query
CtQuery q1 = launcher.getFactory().Query().createQuery(cls, cls2).map((CtClass c) -> c.getSimpleName());
assertArrayEquals(new String[]{"Tacos", "Tostada"}, q1.list().toArray());
CtQuery q1b = launcher.getFactory().Query().createQuery(Arrays.asList(cls, cls2)).map((CtClass c) -> c.getSimpleName());
assertArrayEquals(new String[]{"Tacos", "Tostada"}, q1b.list().toArray());

CtQuery q2 = launcher.getFactory().Query().createQuery(cls, cls3).map((CtClass c) -> c.getSimpleName());
assertArrayEquals(new String[]{"Tacos", "Antojito"}, q2.list().toArray());
CtQuery q2b = launcher.getFactory().Query().createQuery(Arrays.asList(cls, cls3)).map((CtClass c) -> c.getSimpleName());
assertArrayEquals(new String[]{"Tacos", "Antojito"}, q2b.list().toArray());

CtQuery q3 = launcher.getFactory().Query().createQuery(cls, cls2, cls3).map((CtClass c) -> c.getSimpleName());
assertArrayEquals(new String[]{"Tacos", "Tostada", "Antojito"}, q3.list().toArray());
}
CtQuery q3b = launcher.getFactory().Query().createQuery(Arrays.asList(cls, cls2, cls3)).map((CtClass c) -> c.getSimpleName());
assertArrayEquals(new String[]{"Tacos", "Tostada", "Antojito"}, q3b.list().toArray());
}

// now testing map(CtConsumableFunction)

Expand Down Expand Up @@ -865,16 +872,16 @@ public void testEmptyQuery() throws Exception {

//contract: empty query returns no element
assertEquals(0, launcher.getFactory().createQuery().list().size());
assertEquals(0, launcher.getFactory().createQuery(null).list().size());
assertEquals(0, launcher.getFactory().createQuery((Object) null).list().size());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a cast to null is not elegant IMHO. One alternative solution would be to replace createQuery(null) by createQuery()

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

createQuery() is already used on the line 867, the purpose of this test is to check handling of null values. I guess that cast is OK for the test. Or optionally I can use new API and call createQuery(Arrays.asList(null)). WDYT?

//contract: empty query returns no element
launcher.getFactory().createQuery().forEach(x->fail());
launcher.getFactory().createQuery(null).forEach(x->fail());
launcher.getFactory().createQuery((Object) null).forEach(x->fail());
//contract: empty query calls no mapping
assertEquals(0, launcher.getFactory().createQuery().map(x->{fail();return true;}).list().size());
assertEquals(0, launcher.getFactory().createQuery(null).map(x->{fail();return true;}).list().size());
assertEquals(0, launcher.getFactory().createQuery((Object) null).map(x->{fail();return true;}).list().size());
//contract: empty query calls no filterChildren
assertEquals(0, launcher.getFactory().createQuery().filterChildren(x->{fail();return true;}).list().size());
assertEquals(0, launcher.getFactory().createQuery(null).filterChildren(x->{fail();return true;}).list().size());
assertEquals(0, launcher.getFactory().createQuery((Object) null).filterChildren(x->{fail();return true;}).list().size());
}

@Test
Expand Down