From 39859a8b9d2f57960b90746e9872d89c8af56cb1 Mon Sep 17 00:00:00 2001 From: Simon Urli Date: Wed, 22 Nov 2017 14:00:45 +0100 Subject: [PATCH 1/3] Add a new way to build a query in factory --- src/main/java/spoon/reflect/factory/Factory.java | 5 +++++ src/main/java/spoon/reflect/factory/FactoryImpl.java | 5 +++++ src/main/java/spoon/reflect/factory/QueryFactory.java | 8 ++++++++ 3 files changed, 18 insertions(+) diff --git a/src/main/java/spoon/reflect/factory/Factory.java b/src/main/java/spoon/reflect/factory/Factory.java index c56d43272e6..1d18298b327 100644 --- a/src/main/java/spoon/reflect/factory/Factory.java +++ b/src/main/java/spoon/reflect/factory/Factory.java @@ -915,4 +915,9 @@ public interface Factory { * @see CoreFactory#create(Class) */ CtElement createElement(Class klass); + + /** + * @see QueryFactory#createQuery(Object...) + */ + CtQuery createQuery(Object... input); } diff --git a/src/main/java/spoon/reflect/factory/FactoryImpl.java b/src/main/java/spoon/reflect/factory/FactoryImpl.java index 4ae4da28124..8c77971d30c 100644 --- a/src/main/java/spoon/reflect/factory/FactoryImpl.java +++ b/src/main/java/spoon/reflect/factory/FactoryImpl.java @@ -1167,4 +1167,9 @@ public CtPackage createPackage(CtPackage parent, String simpleName) { public CtElement createElement(Class klass) { return Core().create(klass); } + + @Override + public CtQuery createQuery(Object... input) { + return Query().createQuery(input); + } } diff --git a/src/main/java/spoon/reflect/factory/QueryFactory.java b/src/main/java/spoon/reflect/factory/QueryFactory.java index 26dcda6eef0..5c4a63d0940 100644 --- a/src/main/java/spoon/reflect/factory/QueryFactory.java +++ b/src/main/java/spoon/reflect/factory/QueryFactory.java @@ -48,4 +48,12 @@ public CtQuery createQuery() { public CtQuery createQuery(Object input) { return new CtQueryImpl(input); } + + /** + * Creates a bound query with an optional number of inputs + * See {@link CtQueryImpl#CtQueryImpl(Object...)} + */ + public CtQuery createQuery(Object... input) { + return new CtQueryImpl(input); + } } From c6392abd1affacb457f014988a2539188f77832d Mon Sep 17 00:00:00 2001 From: Simon Urli Date: Wed, 22 Nov 2017 14:55:20 +0100 Subject: [PATCH 2/3] Change documentation and add test --- .../spoon/reflect/factory/QueryFactory.java | 4 ++-- .../java/spoon/test/filters/FilterTest.java | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/main/java/spoon/reflect/factory/QueryFactory.java b/src/main/java/spoon/reflect/factory/QueryFactory.java index 5c4a63d0940..3773b38bbc1 100644 --- a/src/main/java/spoon/reflect/factory/QueryFactory.java +++ b/src/main/java/spoon/reflect/factory/QueryFactory.java @@ -50,8 +50,8 @@ public CtQuery createQuery(Object input) { } /** - * Creates a bound query with an optional number of inputs - * See {@link CtQueryImpl#CtQueryImpl(Object...)} + * Creates a bound query with an optional number + * of inputs elements to the query (see {@link CtQuery#setInput(Object...)}) */ public CtQuery createQuery(Object... input) { return new CtQueryImpl(input); diff --git a/src/test/java/spoon/test/filters/FilterTest.java b/src/test/java/spoon/test/filters/FilterTest.java index 2dbcfab07a8..637902c03e1 100644 --- a/src/test/java/spoon/test/filters/FilterTest.java +++ b/src/test/java/spoon/test/filters/FilterTest.java @@ -732,6 +732,7 @@ public void testReuseOfQuery() throws Exception { assertSame(cls2, ((CtQueryImpl)q).getInputs().get(0)); } + @Test public void testReuseOfBaseQuery() throws Exception { // contract: an empty query can be used on several inputs @@ -751,7 +752,25 @@ public void testReuseOfBaseQuery() throws Exception { assertEquals("Tostada", q.setInput(cls2).list().get(0)); } + @Test + public void testQueryWithOptionalNumberOfInputs() throws Exception { + // contract: an empty query can be used on several inputs + 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); + CtClass cls2 = launcher.getFactory().Class().get(Tostada.class); + CtClass cls3 = launcher.getFactory().Class().get(Antojito.class); + + // 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 q2 = launcher.getFactory().Query().createQuery(cls, cls3).map((CtClass c) -> c.getSimpleName()); + assertArrayEquals(new String[]{"Tacos", "Antojito"}, q2.list().toArray()); + } // now testing map(CtConsumableFunction) From 8ce8e703d9652e3aeff6178f0e661d8eea23891a Mon Sep 17 00:00:00 2001 From: Simon Urli Date: Wed, 22 Nov 2017 14:58:12 +0100 Subject: [PATCH 3/3] Fix test contract --- src/test/java/spoon/test/filters/FilterTest.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/test/java/spoon/test/filters/FilterTest.java b/src/test/java/spoon/test/filters/FilterTest.java index 637902c03e1..6c1ec7b6366 100644 --- a/src/test/java/spoon/test/filters/FilterTest.java +++ b/src/test/java/spoon/test/filters/FilterTest.java @@ -754,7 +754,7 @@ public void testReuseOfBaseQuery() throws Exception { @Test public void testQueryWithOptionalNumberOfInputs() throws Exception { - // contract: an empty query can be used on several inputs + // contract: QueryFactory allows to create query with an optional number of inputs final Launcher launcher = new Launcher(); launcher.setArgs(new String[] {"--output-type", "nooutput","--level","info" }); launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses"); @@ -770,6 +770,9 @@ public void testQueryWithOptionalNumberOfInputs() throws Exception { CtQuery q2 = launcher.getFactory().Query().createQuery(cls, cls3).map((CtClass c) -> c.getSimpleName()); assertArrayEquals(new String[]{"Tacos", "Antojito"}, q2.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()); } // now testing map(CtConsumableFunction)