From df41c12bea0220a95a3e3fceae13c34320c38acf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timur=20Sa=C4=9Flam?= Date: Fri, 17 Feb 2023 15:56:32 +0100 Subject: [PATCH 01/10] Add two more test cases for the java language regarding try blocks and variable initialization. --- .../jplag/java/AbstractJavaLanguageTest.java | 45 +++++++++++++++++++ .../java/de/jplag/java/JavaBlockTest.java | 34 +------------- .../java/de/jplag/java/JavaNewClassTest.java | 19 ++++++++ .../test/java/de/jplag/java/JavaTryTest.java | 20 +++++++++ .../test/resources/java/AssignAndCreate.java | 9 ++++ .../test/resources/java/AssignAndCreate2.java | 10 +++++ .../java/src/test/resources/java/Try.java | 28 ++++++++++++ .../test/resources/java/TryWithResource.java | 26 +++++++++++ 8 files changed, 158 insertions(+), 33 deletions(-) create mode 100644 languages/java/src/test/java/de/jplag/java/AbstractJavaLanguageTest.java create mode 100644 languages/java/src/test/java/de/jplag/java/JavaNewClassTest.java create mode 100644 languages/java/src/test/java/de/jplag/java/JavaTryTest.java create mode 100644 languages/java/src/test/resources/java/AssignAndCreate.java create mode 100644 languages/java/src/test/resources/java/AssignAndCreate2.java create mode 100644 languages/java/src/test/resources/java/Try.java create mode 100644 languages/java/src/test/resources/java/TryWithResource.java diff --git a/languages/java/src/test/java/de/jplag/java/AbstractJavaLanguageTest.java b/languages/java/src/test/java/de/jplag/java/AbstractJavaLanguageTest.java new file mode 100644 index 000000000..2fe1369c0 --- /dev/null +++ b/languages/java/src/test/java/de/jplag/java/AbstractJavaLanguageTest.java @@ -0,0 +1,45 @@ +package de.jplag.java; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.File; +import java.nio.file.Path; +import java.util.List; +import java.util.Set; + +import org.junit.jupiter.api.BeforeEach; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import de.jplag.ParsingException; +import de.jplag.Token; +import de.jplag.TokenPrinter; +import de.jplag.TokenType; + +/** + * Basic test class for testing the Java language module. + */ +public class AbstractJavaLanguageTest { + + private static final Path BASE_PATH = Path.of("src", "test", "resources", "java"); + private static final String LOG_MESSAGE = "Tokens of {}: {}"; + private final Logger logger = LoggerFactory.getLogger(JavaBlockTest.class); + private de.jplag.Language language; + protected File baseDirectory; + + @BeforeEach + void setUp() { + language = new Language(); + baseDirectory = BASE_PATH.toFile(); + assertTrue(baseDirectory.exists(), "Could not find base directory!"); + } + + protected List parseJavaFile(String fileName) throws ParsingException { + List parsedTokens = language.parse(Set.of(new File(baseDirectory, fileName))); + List tokenTypes = parsedTokens.stream().map(Token::getType).toList(); + logger.info(LOG_MESSAGE, fileName, tokenTypes); + logger.debug(TokenPrinter.printTokens(parsedTokens, BASE_PATH.toAbsolutePath().toFile())); + return tokenTypes; + } + +} \ No newline at end of file diff --git a/languages/java/src/test/java/de/jplag/java/JavaBlockTest.java b/languages/java/src/test/java/de/jplag/java/JavaBlockTest.java index 78e39842d..57f52c704 100644 --- a/languages/java/src/test/java/de/jplag/java/JavaBlockTest.java +++ b/languages/java/src/test/java/de/jplag/java/JavaBlockTest.java @@ -1,45 +1,20 @@ package de.jplag.java; import static org.junit.jupiter.api.Assertions.assertIterableEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; -import java.io.File; -import java.nio.file.Path; -import java.util.List; -import java.util.Set; import java.util.stream.Stream; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import de.jplag.ParsingException; -import de.jplag.Token; -import de.jplag.TokenType; /** * Test cases regarding the extraction from implicit vs. explicit blocks in Java code. */ -class JavaBlockTest { - private static final Path BASE_PATH = Path.of("src", "test", "resources", "java"); - private static final String LOG_MESSAGE = "Tokens of {}: {}"; - - private final Logger logger = LoggerFactory.getLogger(JavaBlockTest.class); - - private de.jplag.Language language; - private File baseDirectory; - - @BeforeEach - public void setUp() { - language = new Language(); - baseDirectory = BASE_PATH.toFile(); - assertTrue(baseDirectory.exists(), "Could not find base directory!"); - } - +class JavaBlockTest extends AbstractJavaLanguageTest { @ParameterizedTest @MethodSource("provideClassPairs") @DisplayName("Test pairs of classes with explicit vs. implicit blocks.") @@ -47,13 +22,6 @@ void testJavaClassPair(String fileName1, String fileName2) throws ParsingExcepti assertIterableEquals(parseJavaFile(fileName1), parseJavaFile(fileName2)); } - private List parseJavaFile(String fileName) throws ParsingException { - List parsedTokens = language.parse(Set.of(new File(baseDirectory, fileName))); - List tokenTypes = parsedTokens.stream().map(Token::getType).toList(); - logger.info(LOG_MESSAGE, fileName, tokenTypes); - return tokenTypes; - } - /** * Argument source for the test case {@link testJavaClassPair(String, String)). */ diff --git a/languages/java/src/test/java/de/jplag/java/JavaNewClassTest.java b/languages/java/src/test/java/de/jplag/java/JavaNewClassTest.java new file mode 100644 index 000000000..4824461ca --- /dev/null +++ b/languages/java/src/test/java/de/jplag/java/JavaNewClassTest.java @@ -0,0 +1,19 @@ +package de.jplag.java; + +import static org.junit.jupiter.api.Assertions.assertIterableEquals; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import de.jplag.ParsingException; + +/** + * Test cases regarding the generation of the assign token when initializing directly. + */ +class JavaNewClassTest extends AbstractJavaLanguageTest { + @Test + @DisplayName("Test assign token generation when declaring and initializing a variable") + void testJavaClassPair() throws ParsingException { + assertIterableEquals(parseJavaFile("AssignAndCreate.java"), parseJavaFile("AssignAndCreate2.java")); + } +} \ No newline at end of file diff --git a/languages/java/src/test/java/de/jplag/java/JavaTryTest.java b/languages/java/src/test/java/de/jplag/java/JavaTryTest.java new file mode 100644 index 000000000..c5ee683d9 --- /dev/null +++ b/languages/java/src/test/java/de/jplag/java/JavaTryTest.java @@ -0,0 +1,20 @@ + +package de.jplag.java; + +import static org.junit.jupiter.api.Assertions.assertIterableEquals; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import de.jplag.ParsingException; + +/** + * Test cases regarding the extraction from try vs. try with resource. + */ +class JavaTryTest extends AbstractJavaLanguageTest { + @Test + @DisplayName("Test difference between try block and try-with-resource block.") + void testJavaClassPair() throws ParsingException { + assertIterableEquals(parseJavaFile("Try.java"), parseJavaFile("TryWithResource.java")); + } +} diff --git a/languages/java/src/test/resources/java/AssignAndCreate.java b/languages/java/src/test/resources/java/AssignAndCreate.java new file mode 100644 index 000000000..8fb3d856f --- /dev/null +++ b/languages/java/src/test/resources/java/AssignAndCreate.java @@ -0,0 +1,9 @@ +package de.jplag.java; + +import java.util.Scanner; + +public class AssignAndCreate { + public static void main(String[] args) { + Scanner scanner = new Scanner(""); + } +} diff --git a/languages/java/src/test/resources/java/AssignAndCreate2.java b/languages/java/src/test/resources/java/AssignAndCreate2.java new file mode 100644 index 000000000..10f458847 --- /dev/null +++ b/languages/java/src/test/resources/java/AssignAndCreate2.java @@ -0,0 +1,10 @@ +package de.jplag.java; + +import java.util.Scanner; + +public class AssignAndCreate2 { + public static void main(String[] args) { + Scanner scanner; + scanner = new Scanner(""); + } +} diff --git a/languages/java/src/test/resources/java/Try.java b/languages/java/src/test/resources/java/Try.java new file mode 100644 index 000000000..5dc460465 --- /dev/null +++ b/languages/java/src/test/resources/java/Try.java @@ -0,0 +1,28 @@ +package de.jplag.java; + +import java.io.File; +import java.io.FileNotFoundException; +import java.util.Scanner; + +public class Try { + public static void main(String[] args) { + new Try().load("DoesNotExist.txt"); + } + + public void load(String path) { + Scanner scanner = null; + try { + Scanner other = null; // This is just here to keep the tokens similar. + scanner = new Scanner(new File(path)); + while (scanner.hasNext()) { + System.out.println(scanner.nextLine()); + } + } catch (FileNotFoundException exception) { + exception.printStackTrace(); + } finally { + if (scanner != null) { + scanner.close(); + } + } + } +} diff --git a/languages/java/src/test/resources/java/TryWithResource.java b/languages/java/src/test/resources/java/TryWithResource.java new file mode 100644 index 000000000..01c1de320 --- /dev/null +++ b/languages/java/src/test/resources/java/TryWithResource.java @@ -0,0 +1,26 @@ +package de.jplag.java; + +import java.io.File; +import java.io.FileNotFoundException; +import java.util.Scanner; + +public class TryWithResource { + public static void main(String[] args) { + new TryWithResource().load("DoesNotExist.txt"); + } + + public void load(String path) { + Scanner other = null; // This is just here to keep the tokens similar. + try (Scanner scanner = new Scanner(new File(path))) { + while (scanner.hasNext()) { + System.out.println(scanner.nextLine()); + } + } catch (FileNotFoundException exception) { + exception.printStackTrace(); + } finally { + if (other != null) { // This as well... + other.close(); // This as well... + } + } + } +} From 032194e7ca80d3e2341482c0c07dfabc409adc51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timur=20Sa=C4=9Flam?= Date: Fri, 17 Feb 2023 16:21:25 +0100 Subject: [PATCH 02/10] Further generalize the declaration and initialization test case. --- .../java/src/test/java/de/jplag/java/JavaNewClassTest.java | 7 ++++--- .../{AssignAndCreate2.java => DeclareAndInitialize1.java} | 4 +++- .../{AssignAndCreate.java => DeclareAndInitialize2.java} | 3 ++- 3 files changed, 9 insertions(+), 5 deletions(-) rename languages/java/src/test/resources/java/{AssignAndCreate2.java => DeclareAndInitialize1.java} (66%) rename languages/java/src/test/resources/java/{AssignAndCreate.java => DeclareAndInitialize2.java} (68%) diff --git a/languages/java/src/test/java/de/jplag/java/JavaNewClassTest.java b/languages/java/src/test/java/de/jplag/java/JavaNewClassTest.java index 4824461ca..524ed6e27 100644 --- a/languages/java/src/test/java/de/jplag/java/JavaNewClassTest.java +++ b/languages/java/src/test/java/de/jplag/java/JavaNewClassTest.java @@ -8,12 +8,13 @@ import de.jplag.ParsingException; /** - * Test cases regarding the generation of the assign token when initializing directly. + * Test cases regarding the generation of the assign token when initializing a variable directly. Whether the + * initialization is done on the same line as the declaration should not affect the extracted tokens. */ class JavaNewClassTest extends AbstractJavaLanguageTest { @Test - @DisplayName("Test assign token generation when declaring and initializing a variable") + @DisplayName("Test init and declare on same line vs. different lines") void testJavaClassPair() throws ParsingException { - assertIterableEquals(parseJavaFile("AssignAndCreate.java"), parseJavaFile("AssignAndCreate2.java")); + assertIterableEquals(parseJavaFile("DeclareAndInitialize1.java"), parseJavaFile("DeclareAndInitialize2.java")); } } \ No newline at end of file diff --git a/languages/java/src/test/resources/java/AssignAndCreate2.java b/languages/java/src/test/resources/java/DeclareAndInitialize1.java similarity index 66% rename from languages/java/src/test/resources/java/AssignAndCreate2.java rename to languages/java/src/test/resources/java/DeclareAndInitialize1.java index 10f458847..4a91a6cdd 100644 --- a/languages/java/src/test/resources/java/AssignAndCreate2.java +++ b/languages/java/src/test/resources/java/DeclareAndInitialize1.java @@ -2,9 +2,11 @@ import java.util.Scanner; -public class AssignAndCreate2 { +public class DeclareAndInitialize1 { public static void main(String[] args) { Scanner scanner; scanner = new Scanner(""); + Scanner same; + same = scanner; } } diff --git a/languages/java/src/test/resources/java/AssignAndCreate.java b/languages/java/src/test/resources/java/DeclareAndInitialize2.java similarity index 68% rename from languages/java/src/test/resources/java/AssignAndCreate.java rename to languages/java/src/test/resources/java/DeclareAndInitialize2.java index 8fb3d856f..5fed9c517 100644 --- a/languages/java/src/test/resources/java/AssignAndCreate.java +++ b/languages/java/src/test/resources/java/DeclareAndInitialize2.java @@ -2,8 +2,9 @@ import java.util.Scanner; -public class AssignAndCreate { +public class DeclareAndInitialize2 { public static void main(String[] args) { Scanner scanner = new Scanner(""); + Scanner same = scanner; } } From 4363e21b394db13d45afe04aa81b037b10f05cb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timur=20Sa=C4=9Flam?= Date: Fri, 17 Feb 2023 16:26:41 +0100 Subject: [PATCH 03/10] Extract try and try-with-resource as single token type, extract end tokens for try and finally. --- .../main/java/de/jplag/java/JavaTokenType.java | 7 +++---- .../de/jplag/java/TokenGeneratingTreeScanner.java | 15 ++++++++------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/languages/java/src/main/java/de/jplag/java/JavaTokenType.java b/languages/java/src/main/java/de/jplag/java/JavaTokenType.java index a58143210..84e4b5046 100644 --- a/languages/java/src/main/java/de/jplag/java/JavaTokenType.java +++ b/languages/java/src/main/java/de/jplag/java/JavaTokenType.java @@ -22,9 +22,11 @@ public enum JavaTokenType implements TokenType { J_SWITCH_END("}SWITCH"), // check J_CASE("CASE"), // check J_TRY_BEGIN("TRY{"), // check + J_TRY_END("}TRY"), // check J_CATCH_BEGIN("CATCH{"), // check J_CATCH_END("}CATCH"), // check - J_FINALLY("FINALLY"), // check + J_FINALLY_BEGIN("FINALLY{"), // check + J_FINALLY_END("}FINALLY"), // check J_IF_BEGIN("IF{"), // check J_ELSE("ELSE"), // check J_IF_END("}IF"), // check @@ -62,9 +64,6 @@ public enum JavaTokenType implements TokenType { J_ANNO_C_BEGIN("ANNO_C{"), // ?? J_ANNO_C_END("}ANNO_C"), // ?? - // new in 1.7 - J_TRY_WITH_RESOURCE("TRY_RES"), // check - // new in 1.9 J_REQUIRES("REQUIRES"), // check J_PROVIDES("PROVIDES"), // check diff --git a/languages/java/src/main/java/de/jplag/java/TokenGeneratingTreeScanner.java b/languages/java/src/main/java/de/jplag/java/TokenGeneratingTreeScanner.java index e78502c63..aa42bc387 100644 --- a/languages/java/src/main/java/de/jplag/java/TokenGeneratingTreeScanner.java +++ b/languages/java/src/main/java/de/jplag/java/TokenGeneratingTreeScanner.java @@ -226,18 +226,19 @@ public Object visitCase(CaseTree node, Object p) { @Override public Object visitTry(TryTree node, Object p) { long start = positions.getStartPosition(ast, node); - if (node.getResources().isEmpty()) - addToken(JavaTokenType.J_TRY_BEGIN, start, 3); - else - addToken(JavaTokenType.J_TRY_WITH_RESOURCE, start, 3); + addToken(JavaTokenType.J_TRY_BEGIN, start, 3); scan(node.getResources(), p); scan(node.getBlock(), p); + long end = positions.getEndPosition(ast, node); + addToken(JavaTokenType.J_TRY_END, end, 1); scan(node.getCatches(), p); if (node.getFinallyBlock() != null) { start = positions.getStartPosition(ast, node.getFinallyBlock()); - addToken(JavaTokenType.J_FINALLY, start, 3); + addToken(JavaTokenType.J_FINALLY_BEGIN, start, 3); + scan(node.getFinallyBlock(), p); + end = positions.getEndPosition(ast, node.getFinallyBlock()); + addToken(JavaTokenType.J_FINALLY_END, end, 1); } - scan(node.getFinallyBlock(), p); return null; // return value isn't used } @@ -308,7 +309,7 @@ public Object visitNewClass(NewClassTree node, Object p) { @Override public Object visitTypeParameter(TypeParameterTree node, Object p) { long start = positions.getStartPosition(ast, node); - // This is odd, but also done like this in Java17 + // This is odd, but also done like this in Java 1.7 addToken(JavaTokenType.J_GENERIC, start, 1); return super.visitTypeParameter(node, p); } From bbda5abc8c3a8b86c9695c9b1672e0cca1706473 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timur=20Sa=C4=9Flam?= Date: Fri, 17 Feb 2023 16:31:08 +0100 Subject: [PATCH 04/10] Always extract assign token when initializing and declaring a variable. --- .../java/de/jplag/java/TokenGeneratingTreeScanner.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/languages/java/src/main/java/de/jplag/java/TokenGeneratingTreeScanner.java b/languages/java/src/main/java/de/jplag/java/TokenGeneratingTreeScanner.java index aa42bc387..42b7866b5 100644 --- a/languages/java/src/main/java/de/jplag/java/TokenGeneratingTreeScanner.java +++ b/languages/java/src/main/java/de/jplag/java/TokenGeneratingTreeScanner.java @@ -23,6 +23,7 @@ import com.sun.source.tree.EnhancedForLoopTree; import com.sun.source.tree.ErroneousTree; import com.sun.source.tree.ExportsTree; +import com.sun.source.tree.ExpressionTree; import com.sun.source.tree.ForLoopTree; import com.sun.source.tree.IfTree; import com.sun.source.tree.ImportTree; @@ -369,6 +370,13 @@ public Object visitAssert(AssertTree node, Object p) { public Object visitVariable(VariableTree node, Object p) { long start = positions.getStartPosition(ast, node); addToken(JavaTokenType.J_VARDEF, start, node.toString().length()); + + ExpressionTree initializer = node.getInitializer(); + // extract assign token if the variable was just declared and directly initialized: + if (initializer != null) { + start = positions.getEndPosition(ast, node.getType()) + 1; // after type + addToken(JavaTokenType.J_ASSIGN, start, node.getName().length()); + } return super.visitVariable(node, p); } From 1b9a2d618f216f9d766c3cab89cdf9e2d99e084c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timur=20Sa=C4=9Flam?= Date: Fri, 17 Feb 2023 16:31:37 +0100 Subject: [PATCH 05/10] Add Javadoc to the Java test base. --- .../java/de/jplag/java/AbstractJavaLanguageTest.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/languages/java/src/test/java/de/jplag/java/AbstractJavaLanguageTest.java b/languages/java/src/test/java/de/jplag/java/AbstractJavaLanguageTest.java index 2fe1369c0..3c3dfd1bb 100644 --- a/languages/java/src/test/java/de/jplag/java/AbstractJavaLanguageTest.java +++ b/languages/java/src/test/java/de/jplag/java/AbstractJavaLanguageTest.java @@ -27,6 +27,9 @@ public class AbstractJavaLanguageTest { private de.jplag.Language language; protected File baseDirectory; + /** + * Sets up the base directory and the language module. + */ @BeforeEach void setUp() { language = new Language(); @@ -34,11 +37,17 @@ void setUp() { assertTrue(baseDirectory.exists(), "Could not find base directory!"); } + /** + * Parses a java file in the {@link AbstractJavaLanguageTest.baseDirectory} and returns the list of token types. + * @param fileName is the name of the file to parse. + * @return the token types. + * @throws ParsingException if parsing fails. + */ protected List parseJavaFile(String fileName) throws ParsingException { List parsedTokens = language.parse(Set.of(new File(baseDirectory, fileName))); List tokenTypes = parsedTokens.stream().map(Token::getType).toList(); logger.info(LOG_MESSAGE, fileName, tokenTypes); - logger.debug(TokenPrinter.printTokens(parsedTokens, BASE_PATH.toAbsolutePath().toFile())); + logger.info(TokenPrinter.printTokens(parsedTokens, BASE_PATH.toAbsolutePath().toFile())); return tokenTypes; } From 0fb1521f15387e43e9b6dd4c8770c4efbd00d609 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timur=20Sa=C4=9Flam?= Date: Fri, 17 Feb 2023 16:42:37 +0100 Subject: [PATCH 06/10] Fix try test case. --- languages/java/src/test/resources/java/Try.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/languages/java/src/test/resources/java/Try.java b/languages/java/src/test/resources/java/Try.java index 5dc460465..3aad216cb 100644 --- a/languages/java/src/test/resources/java/Try.java +++ b/languages/java/src/test/resources/java/Try.java @@ -12,7 +12,7 @@ public static void main(String[] args) { public void load(String path) { Scanner scanner = null; try { - Scanner other = null; // This is just here to keep the tokens similar. + Scanner other; // This is just here to keep the tokens similar. scanner = new Scanner(new File(path)); while (scanner.hasNext()) { System.out.println(scanner.nextLine()); From dc75e0553580583f38290347ef1c7c3cf4f597ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timur=20Sa=C4=9Flam?= Date: Mon, 20 Feb 2023 09:21:53 +0100 Subject: [PATCH 07/10] Make abstract class actually abstract. --- .../src/test/java/de/jplag/java/AbstractJavaLanguageTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/languages/java/src/test/java/de/jplag/java/AbstractJavaLanguageTest.java b/languages/java/src/test/java/de/jplag/java/AbstractJavaLanguageTest.java index 3c3dfd1bb..a5b02418f 100644 --- a/languages/java/src/test/java/de/jplag/java/AbstractJavaLanguageTest.java +++ b/languages/java/src/test/java/de/jplag/java/AbstractJavaLanguageTest.java @@ -19,7 +19,7 @@ /** * Basic test class for testing the Java language module. */ -public class AbstractJavaLanguageTest { +public abstract class AbstractJavaLanguageTest { private static final Path BASE_PATH = Path.of("src", "test", "resources", "java"); private static final String LOG_MESSAGE = "Tokens of {}: {}"; From 3aee3a6965cd6e9d99466720758bd8aa741ff2ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timur=20Sa=C4=9Flam?= Date: Mon, 20 Feb 2023 10:46:14 +0100 Subject: [PATCH 08/10] Remove initialization and declaration fix, as it increases the rate of false positives. --- .../java/de/jplag/java/TokenGeneratingTreeScanner.java | 7 ------- 1 file changed, 7 deletions(-) diff --git a/languages/java/src/main/java/de/jplag/java/TokenGeneratingTreeScanner.java b/languages/java/src/main/java/de/jplag/java/TokenGeneratingTreeScanner.java index 42b7866b5..f3d40334d 100644 --- a/languages/java/src/main/java/de/jplag/java/TokenGeneratingTreeScanner.java +++ b/languages/java/src/main/java/de/jplag/java/TokenGeneratingTreeScanner.java @@ -23,7 +23,6 @@ import com.sun.source.tree.EnhancedForLoopTree; import com.sun.source.tree.ErroneousTree; import com.sun.source.tree.ExportsTree; -import com.sun.source.tree.ExpressionTree; import com.sun.source.tree.ForLoopTree; import com.sun.source.tree.IfTree; import com.sun.source.tree.ImportTree; @@ -371,12 +370,6 @@ public Object visitVariable(VariableTree node, Object p) { long start = positions.getStartPosition(ast, node); addToken(JavaTokenType.J_VARDEF, start, node.toString().length()); - ExpressionTree initializer = node.getInitializer(); - // extract assign token if the variable was just declared and directly initialized: - if (initializer != null) { - start = positions.getEndPosition(ast, node.getType()) + 1; // after type - addToken(JavaTokenType.J_ASSIGN, start, node.getName().length()); - } return super.visitVariable(node, p); } From 4e004fbdd88233d1a380af0f9990a59c69d47c7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timur=20Sa=C4=9Flam?= Date: Mon, 20 Feb 2023 10:55:47 +0100 Subject: [PATCH 09/10] Adapt expected values of the core test cases. --- .../test/java/de/jplag/BasicFunctionalityTest.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/core/src/test/java/de/jplag/BasicFunctionalityTest.java b/core/src/test/java/de/jplag/BasicFunctionalityTest.java index b0c80d635..3af11f6f8 100644 --- a/core/src/test/java/de/jplag/BasicFunctionalityTest.java +++ b/core/src/test/java/de/jplag/BasicFunctionalityTest.java @@ -69,17 +69,17 @@ void testPartialPlagiarism() throws ExitException { .forEach(comparison -> assertEquals(0, comparison.similarity(), DELTA)); // Hard coded assertions on selected comparisons - assertEquals(0.238, getSelectedPercent(result, "A", "B"), DELTA); + assertEquals(0.237, getSelectedPercent(result, "A", "B"), DELTA); assertEquals(0.996, getSelectedPercent(result, "A", "C"), DELTA); - assertEquals(0.748, getSelectedPercent(result, "A", "D"), DELTA); - assertEquals(0.238, getSelectedPercent(result, "B", "C"), DELTA); - assertEquals(0.283, getSelectedPercent(result, "B", "D"), DELTA); - assertEquals(0.748, getSelectedPercent(result, "C", "D"), DELTA); + assertEquals(0.751, getSelectedPercent(result, "A", "D"), DELTA); + assertEquals(0.237, getSelectedPercent(result, "B", "C"), DELTA); + assertEquals(0.281, getSelectedPercent(result, "B", "D"), DELTA); + assertEquals(0.751, getSelectedPercent(result, "C", "D"), DELTA); // More detailed assertions for the plagiarism in A-D var biggestMatch = getSelectedComparison(result, "A", "D"); assertEquals(0.946, biggestMatch.get().maximalSimilarity(), DELTA); - assertEquals(0.619, biggestMatch.get().minimalSimilarity(), DELTA); + assertEquals(0.622, biggestMatch.get().minimalSimilarity(), DELTA); assertEquals(11, biggestMatch.get().matches().size()); } From 40e3e773dce2e33a67b3c43df4e193d035ef28da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timur=20Sa=C4=9Flam?= Date: Mon, 20 Feb 2023 11:13:27 +0100 Subject: [PATCH 10/10] Adapt Java language tests to the removal of the third fix. --- .../java/de/jplag/java/JavaNewClassTest.java | 20 ------------------- .../resources/java/DeclareAndInitialize1.java | 12 ----------- .../resources/java/DeclareAndInitialize2.java | 10 ---------- .../test/resources/java/TryWithResource.java | 2 +- 4 files changed, 1 insertion(+), 43 deletions(-) delete mode 100644 languages/java/src/test/java/de/jplag/java/JavaNewClassTest.java delete mode 100644 languages/java/src/test/resources/java/DeclareAndInitialize1.java delete mode 100644 languages/java/src/test/resources/java/DeclareAndInitialize2.java diff --git a/languages/java/src/test/java/de/jplag/java/JavaNewClassTest.java b/languages/java/src/test/java/de/jplag/java/JavaNewClassTest.java deleted file mode 100644 index 524ed6e27..000000000 --- a/languages/java/src/test/java/de/jplag/java/JavaNewClassTest.java +++ /dev/null @@ -1,20 +0,0 @@ -package de.jplag.java; - -import static org.junit.jupiter.api.Assertions.assertIterableEquals; - -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - -import de.jplag.ParsingException; - -/** - * Test cases regarding the generation of the assign token when initializing a variable directly. Whether the - * initialization is done on the same line as the declaration should not affect the extracted tokens. - */ -class JavaNewClassTest extends AbstractJavaLanguageTest { - @Test - @DisplayName("Test init and declare on same line vs. different lines") - void testJavaClassPair() throws ParsingException { - assertIterableEquals(parseJavaFile("DeclareAndInitialize1.java"), parseJavaFile("DeclareAndInitialize2.java")); - } -} \ No newline at end of file diff --git a/languages/java/src/test/resources/java/DeclareAndInitialize1.java b/languages/java/src/test/resources/java/DeclareAndInitialize1.java deleted file mode 100644 index 4a91a6cdd..000000000 --- a/languages/java/src/test/resources/java/DeclareAndInitialize1.java +++ /dev/null @@ -1,12 +0,0 @@ -package de.jplag.java; - -import java.util.Scanner; - -public class DeclareAndInitialize1 { - public static void main(String[] args) { - Scanner scanner; - scanner = new Scanner(""); - Scanner same; - same = scanner; - } -} diff --git a/languages/java/src/test/resources/java/DeclareAndInitialize2.java b/languages/java/src/test/resources/java/DeclareAndInitialize2.java deleted file mode 100644 index 5fed9c517..000000000 --- a/languages/java/src/test/resources/java/DeclareAndInitialize2.java +++ /dev/null @@ -1,10 +0,0 @@ -package de.jplag.java; - -import java.util.Scanner; - -public class DeclareAndInitialize2 { - public static void main(String[] args) { - Scanner scanner = new Scanner(""); - Scanner same = scanner; - } -} diff --git a/languages/java/src/test/resources/java/TryWithResource.java b/languages/java/src/test/resources/java/TryWithResource.java index 01c1de320..1bcde6c2b 100644 --- a/languages/java/src/test/resources/java/TryWithResource.java +++ b/languages/java/src/test/resources/java/TryWithResource.java @@ -11,7 +11,7 @@ public static void main(String[] args) { public void load(String path) { Scanner other = null; // This is just here to keep the tokens similar. - try (Scanner scanner = new Scanner(new File(path))) { + try (Scanner scanner = other = new Scanner(new File(path))) { // same for = other = while (scanner.hasNext()) { System.out.println(scanner.nextLine()); }