From 31ea3c859d3388cd58a860a12d6cb0165eec30c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Pedro=20Henrique?= Date: Tue, 12 Mar 2024 22:23:00 -0300 Subject: [PATCH] fix(matching_handlers): Consider spread parameters for score calculation (#48) In the filter condition there was a missing case in which the method contains spread parameters, which is a different token identifier. --- .../base.java | 51 ++++++++++++++++++ .../left.java | 54 +++++++++++++++++++ .../merge.java | 1 + .../right.java | 54 +++++++++++++++++++ .../src/java/method_declaration.rs | 4 +- 5 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 bin/tests/scenarios/overload_methods_with_spread_parameter/base.java create mode 100644 bin/tests/scenarios/overload_methods_with_spread_parameter/left.java create mode 100644 bin/tests/scenarios/overload_methods_with_spread_parameter/merge.java create mode 100644 bin/tests/scenarios/overload_methods_with_spread_parameter/right.java diff --git a/bin/tests/scenarios/overload_methods_with_spread_parameter/base.java b/bin/tests/scenarios/overload_methods_with_spread_parameter/base.java new file mode 100644 index 0000000..cc00551 --- /dev/null +++ b/bin/tests/scenarios/overload_methods_with_spread_parameter/base.java @@ -0,0 +1,51 @@ +package de.fosd.jdime; + +import java.io.BufferedReader; +import java.io.File; +import java.io.IOException; +import java.io.StringReader; +import java.net.URL; +import java.util.Arrays; +import java.util.Iterator; + +import org.junit.BeforeClass; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +public class JDimeTest { + protected static File file(File parent, String child) { + File f = new File(parent, child); + assertTrue(f + " does not exist.", f.exists()); + + return f; + } + + protected static File file(File parent, String name, String... names) { + + if (names != null) { + String path = String.format("%s/%s", name, String.join("/", names)); + return file(parent, path); + } else { + return file(parent, name); + } + } + + protected static File file(String path) throws Exception { + URL res = JDimeTest.class.getResource(path); + + assertNotNull("The file " + path + " was not found.", res); + return new File(res.toURI()); + } + + protected static File file(String name, String... names) throws Exception { + + if (names != null) { + String path = String.format("/%s/%s", name, String.join("/", names)); + return file(path); + } else { + return file("/" + name); + } + } +} diff --git a/bin/tests/scenarios/overload_methods_with_spread_parameter/left.java b/bin/tests/scenarios/overload_methods_with_spread_parameter/left.java new file mode 100644 index 0000000..b61ccb0 --- /dev/null +++ b/bin/tests/scenarios/overload_methods_with_spread_parameter/left.java @@ -0,0 +1,54 @@ +package de.fosd.jdime; + +import java.io.File; +import java.net.URISyntaxException; +import java.net.URL; +import java.util.Arrays; + +import org.junit.BeforeClass; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +public class JDimeTest { + protected static File file(File parent, String child) { + File f = new File(parent, child); + assertTrue(f + " does not exist.", f.exists()); + + return f; + } + + protected static File file(File parent, String name, String... names) { + + if (names != null) { + String path = String.format("%s/%s", name, String.join("/", names)); + return file(parent, path); + } else { + return file(parent, name); + } + } + + protected static File file(String path) { + URL res = JDimeTest.class.getResource(path); + + assertNotNull("The file " + path + " was not found.", res); + + try { + return new File(res.toURI()); + } catch (URISyntaxException e) { + fail(e.getMessage()); + return null; + } + } + + protected static File file(String name, String... names) { + + if (names != null) { + String path = String.format("/%s/%s", name, String.join("/", names)); + return file(path); + } else { + return file("/" + name); + } + } +} diff --git a/bin/tests/scenarios/overload_methods_with_spread_parameter/merge.java b/bin/tests/scenarios/overload_methods_with_spread_parameter/merge.java new file mode 100644 index 0000000..9e34e90 --- /dev/null +++ b/bin/tests/scenarios/overload_methods_with_spread_parameter/merge.java @@ -0,0 +1 @@ + package de . fosd . jdime ; import java . io . File ; import java . net . URISyntaxException ; import java . net . URL ; import java . util . Arrays ; import org . junit . BeforeClass ; import static org . junit . Assert . assertNotNull ; import static org . junit . Assert . assertTrue ; import static org . junit . Assert . fail ; public class JDimeTest { protected static File file ( File parent , String child ) { File f = new File ( parent , child ) ; assertTrue ( f + " does not exist." , f . exists ( ) ) ; return f ; } protected static File file ( File parent , String name , String ... names ) { if ( names != null ) { String path = String . format ( "%s/%s" , name , String . join ( "/" , names ) ) ; return file ( parent , path ) ; } else { return file ( parent , name ) ; } } protected static File file ( String path ) { URL res = JDimeTest . class . getResource ( path ) ; assertNotNull ( "The file " + path + " was not found." , res ) ; try { return new File ( res . toURI ( ) ) ; } catch ( URISyntaxException e ) { fail ( e . getMessage ( ) ) ; return null ; } } protected static File file ( String name , String ... names ) { if ( names != null ) { String path = String . format ( "/%s/%s" , name , String . join ( "/" , names ) ) ; return file ( path ) ; } else { return file ( "/" + name ) ; } } } \ No newline at end of file diff --git a/bin/tests/scenarios/overload_methods_with_spread_parameter/right.java b/bin/tests/scenarios/overload_methods_with_spread_parameter/right.java new file mode 100644 index 0000000..b61ccb0 --- /dev/null +++ b/bin/tests/scenarios/overload_methods_with_spread_parameter/right.java @@ -0,0 +1,54 @@ +package de.fosd.jdime; + +import java.io.File; +import java.net.URISyntaxException; +import java.net.URL; +import java.util.Arrays; + +import org.junit.BeforeClass; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +public class JDimeTest { + protected static File file(File parent, String child) { + File f = new File(parent, child); + assertTrue(f + " does not exist.", f.exists()); + + return f; + } + + protected static File file(File parent, String name, String... names) { + + if (names != null) { + String path = String.format("%s/%s", name, String.join("/", names)); + return file(parent, path); + } else { + return file(parent, name); + } + } + + protected static File file(String path) { + URL res = JDimeTest.class.getResource(path); + + assertNotNull("The file " + path + " was not found.", res); + + try { + return new File(res.toURI()); + } catch (URISyntaxException e) { + fail(e.getMessage()); + return null; + } + } + + protected static File file(String name, String... names) { + + if (names != null) { + String path = String.format("/%s/%s", name, String.join("/", names)); + return file(path); + } else { + return file("/" + name); + } + } +} diff --git a/matching_handlers/src/java/method_declaration.rs b/matching_handlers/src/java/method_declaration.rs index 3f8097d..350804b 100644 --- a/matching_handlers/src/java/method_declaration.rs +++ b/matching_handlers/src/java/method_declaration.rs @@ -45,7 +45,9 @@ fn extract_argument_types_from_formal_parameters(node: &CSTNode) -> Vec CSTNode::NonTerminal(non_terminal) => non_terminal .children .iter() - .filter(|inner_node| inner_node.kind() == "formal_parameter") + .filter(|inner_node| { + inner_node.kind() == "formal_parameter" || inner_node.kind() == "spread_parameter" + }) .filter_map(|inner_node| match inner_node { CSTNode::Terminal(_) => None, CSTNode::NonTerminal(non_terminal) => Some(