From dfd455a0f6cbe62d7f3569ea9c9a89058ba5e87d Mon Sep 17 00:00:00 2001 From: Alexander Milster Date: Tue, 3 Sep 2024 16:55:52 +0200 Subject: [PATCH 1/2] Added java token position tests from J_APPLY to J_ASSIGN --- .../de/jplag/java/tokenPositions/Apply_1.java | 6 ++++++ .../de/jplag/java/tokenPositions/Apply_2.java | 6 ++++++ .../de/jplag/java/tokenPositions/Apply_3.java | 10 ++++++++++ .../de/jplag/java/tokenPositions/ArrayInit_1.java | 7 +++++++ .../de/jplag/java/tokenPositions/ArrayInit_2.java | 7 +++++++ .../de/jplag/java/tokenPositions/Assert_1.java | 6 ++++++ .../de/jplag/java/tokenPositions/Assert_2.java | 6 ++++++ .../de/jplag/java/tokenPositions/Assign_1.java | 6 ++++++ .../de/jplag/java/tokenPositions/Assign_2.java | 7 +++++++ .../de/jplag/java/tokenPositions/Assign_3.java | 7 +++++++ 10 files changed, 68 insertions(+) create mode 100644 languages/java/src/test/resources/de/jplag/java/tokenPositions/Apply_1.java create mode 100644 languages/java/src/test/resources/de/jplag/java/tokenPositions/Apply_2.java create mode 100644 languages/java/src/test/resources/de/jplag/java/tokenPositions/Apply_3.java create mode 100644 languages/java/src/test/resources/de/jplag/java/tokenPositions/ArrayInit_1.java create mode 100644 languages/java/src/test/resources/de/jplag/java/tokenPositions/ArrayInit_2.java create mode 100644 languages/java/src/test/resources/de/jplag/java/tokenPositions/Assert_1.java create mode 100644 languages/java/src/test/resources/de/jplag/java/tokenPositions/Assert_2.java create mode 100644 languages/java/src/test/resources/de/jplag/java/tokenPositions/Assign_1.java create mode 100644 languages/java/src/test/resources/de/jplag/java/tokenPositions/Assign_2.java create mode 100644 languages/java/src/test/resources/de/jplag/java/tokenPositions/Assign_3.java diff --git a/languages/java/src/test/resources/de/jplag/java/tokenPositions/Apply_1.java b/languages/java/src/test/resources/de/jplag/java/tokenPositions/Apply_1.java new file mode 100644 index 000000000..9bf7ee485 --- /dev/null +++ b/languages/java/src/test/resources/de/jplag/java/tokenPositions/Apply_1.java @@ -0,0 +1,6 @@ +>class Apply { +> public static void main(String[] args) { +> System.out.println("Test"); +$ | J_APPLY 18 +> } +>} \ No newline at end of file diff --git a/languages/java/src/test/resources/de/jplag/java/tokenPositions/Apply_2.java b/languages/java/src/test/resources/de/jplag/java/tokenPositions/Apply_2.java new file mode 100644 index 000000000..0cd78b949 --- /dev/null +++ b/languages/java/src/test/resources/de/jplag/java/tokenPositions/Apply_2.java @@ -0,0 +1,6 @@ +>class Apply { +> public static void main(String[] args) { +> new String("test").length(); +$ | J_APPLY 6 +> } +>} \ No newline at end of file diff --git a/languages/java/src/test/resources/de/jplag/java/tokenPositions/Apply_3.java b/languages/java/src/test/resources/de/jplag/java/tokenPositions/Apply_3.java new file mode 100644 index 000000000..ad2e277f9 --- /dev/null +++ b/languages/java/src/test/resources/de/jplag/java/tokenPositions/Apply_3.java @@ -0,0 +1,10 @@ +>import java.io.File; +> +>class Apply { +> public static void main(String[] args) { +> File file = new File("test"); +> file.getName().length(); +$ | J_APPLY 12 +$ | J_APPLY 6 +> } +>} \ No newline at end of file diff --git a/languages/java/src/test/resources/de/jplag/java/tokenPositions/ArrayInit_1.java b/languages/java/src/test/resources/de/jplag/java/tokenPositions/ArrayInit_1.java new file mode 100644 index 000000000..aca7af2e4 --- /dev/null +++ b/languages/java/src/test/resources/de/jplag/java/tokenPositions/ArrayInit_1.java @@ -0,0 +1,7 @@ +>class Apply { +> public static void main(String[] args) { +> int[] data = new int[]{1,2,3,4}; +$ | J_ARRAY_INIT_BEGIN 1 +$ | J_ARRAY_INIT_END 1 +> } +>} diff --git a/languages/java/src/test/resources/de/jplag/java/tokenPositions/ArrayInit_2.java b/languages/java/src/test/resources/de/jplag/java/tokenPositions/ArrayInit_2.java new file mode 100644 index 000000000..276a95a65 --- /dev/null +++ b/languages/java/src/test/resources/de/jplag/java/tokenPositions/ArrayInit_2.java @@ -0,0 +1,7 @@ +>@SuppressWarnings({"Test1", "Test2"}) +$ | J_ARRAY_INIT_BEGIN 1 +$ | J_ARRAY_INIT_END 1 +>class Apply { +> public static void main(String[] args) { +> } +>} diff --git a/languages/java/src/test/resources/de/jplag/java/tokenPositions/Assert_1.java b/languages/java/src/test/resources/de/jplag/java/tokenPositions/Assert_1.java new file mode 100644 index 000000000..da5a93ef4 --- /dev/null +++ b/languages/java/src/test/resources/de/jplag/java/tokenPositions/Assert_1.java @@ -0,0 +1,6 @@ +>class Apply { +> public static void main(String[] args) { +> assert true; +$ | J_ASSERT 6 +> } +>} diff --git a/languages/java/src/test/resources/de/jplag/java/tokenPositions/Assert_2.java b/languages/java/src/test/resources/de/jplag/java/tokenPositions/Assert_2.java new file mode 100644 index 000000000..adff02205 --- /dev/null +++ b/languages/java/src/test/resources/de/jplag/java/tokenPositions/Assert_2.java @@ -0,0 +1,6 @@ +>class Apply { +> public static void main(String[] args) { +> assert args.length == 0; +$ | J_ASSERT 6 +> } +>} diff --git a/languages/java/src/test/resources/de/jplag/java/tokenPositions/Assign_1.java b/languages/java/src/test/resources/de/jplag/java/tokenPositions/Assign_1.java new file mode 100644 index 000000000..11c123737 --- /dev/null +++ b/languages/java/src/test/resources/de/jplag/java/tokenPositions/Assign_1.java @@ -0,0 +1,6 @@ +>class Apply { +> public static void main(String[] args) { +> int i = 1; +$ | J_ASSIGN 3 +> } +>} diff --git a/languages/java/src/test/resources/de/jplag/java/tokenPositions/Assign_2.java b/languages/java/src/test/resources/de/jplag/java/tokenPositions/Assign_2.java new file mode 100644 index 000000000..6f2b25de5 --- /dev/null +++ b/languages/java/src/test/resources/de/jplag/java/tokenPositions/Assign_2.java @@ -0,0 +1,7 @@ +>class Apply { +> public static void main(String[] args) { +> int i = 1; +> i = 2; +$ | J_ASSIGN 3 +> } +>} diff --git a/languages/java/src/test/resources/de/jplag/java/tokenPositions/Assign_3.java b/languages/java/src/test/resources/de/jplag/java/tokenPositions/Assign_3.java new file mode 100644 index 000000000..808aa85ba --- /dev/null +++ b/languages/java/src/test/resources/de/jplag/java/tokenPositions/Assign_3.java @@ -0,0 +1,7 @@ +>class Apply { +> public static void main(String[] args) { +> int i = 1; +> i = (int) Math.random(); +$ | J_ASSIGN 3 +> } +>} From 389cb847642fc0c33ae1d0dbd4a386b27f81339e Mon Sep 17 00:00:00 2001 From: Alexander Milster Date: Tue, 29 Oct 2024 16:28:55 +0100 Subject: [PATCH 2/2] Fixed token lengths --- .../de/jplag/java/TokenGeneratingTreeScanner.java | 11 +++++++++-- .../de/jplag/java/tokenPositions/Apply_1.java | 2 +- .../de/jplag/java/tokenPositions/Apply_2.java | 2 +- .../de/jplag/java/tokenPositions/Apply_3.java | 4 ++-- 4 files changed, 13 insertions(+), 6 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 9589da81b..8b8d9c5e8 100644 --- a/languages/java/src/main/java/de/jplag/java/TokenGeneratingTreeScanner.java +++ b/languages/java/src/main/java/de/jplag/java/TokenGeneratingTreeScanner.java @@ -406,13 +406,13 @@ public Void visitNewArray(NewArrayTree node, Void unused) { scan(node.getDimensions(), null); boolean hasInit = node.getInitializers() != null && !node.getInitializers().isEmpty(); if (hasInit) { - start = positions.getStartPosition(ast, node.getInitializers().get(0)); + start = positions.getStartPosition(ast, node.getInitializers().get(0)) - 1; addToken(JavaTokenType.J_ARRAY_INIT_BEGIN, start, 1, new CodeSemantics()); } scan(node.getInitializers(), null); // super method has annotation processing but we have it disabled anyways if (hasInit) { - end = positions.getEndPosition(ast, node.getInitializers().getLast()) - 1; + end = positions.getEndPosition(ast, node) - 1; addToken(JavaTokenType.J_ARRAY_INIT_END, end, 1, new CodeSemantics()); } return null; @@ -490,6 +490,13 @@ public Void visitConditionalExpression(ConditionalExpressionTree node, Void unus public Void visitMethodInvocation(MethodInvocationTree node, Void unused) { long start = positions.getStartPosition(ast, node); long end = positions.getEndPosition(ast, node.getMethodSelect()); + + String text = node.getMethodSelect().toString(); + if (text.contains(".") && !text.contains("\n")) { + int offset = text.length() - text.lastIndexOf('.'); + start = end - offset; + } + CodeSemantics codeSemantics = CRITICAL_METHODS.contains(node.getMethodSelect().toString()) ? CodeSemantics.createCritical() : CodeSemantics.createControl(); addToken(JavaTokenType.J_APPLY, start, end, codeSemantics); diff --git a/languages/java/src/test/resources/de/jplag/java/tokenPositions/Apply_1.java b/languages/java/src/test/resources/de/jplag/java/tokenPositions/Apply_1.java index 9bf7ee485..4e0f92551 100644 --- a/languages/java/src/test/resources/de/jplag/java/tokenPositions/Apply_1.java +++ b/languages/java/src/test/resources/de/jplag/java/tokenPositions/Apply_1.java @@ -1,6 +1,6 @@ >class Apply { > public static void main(String[] args) { > System.out.println("Test"); -$ | J_APPLY 18 +$ | J_APPLY 8 > } >} \ No newline at end of file diff --git a/languages/java/src/test/resources/de/jplag/java/tokenPositions/Apply_2.java b/languages/java/src/test/resources/de/jplag/java/tokenPositions/Apply_2.java index 0cd78b949..6615b0e5a 100644 --- a/languages/java/src/test/resources/de/jplag/java/tokenPositions/Apply_2.java +++ b/languages/java/src/test/resources/de/jplag/java/tokenPositions/Apply_2.java @@ -1,6 +1,6 @@ >class Apply { > public static void main(String[] args) { > new String("test").length(); -$ | J_APPLY 6 +$ | J_APPLY 7 > } >} \ No newline at end of file diff --git a/languages/java/src/test/resources/de/jplag/java/tokenPositions/Apply_3.java b/languages/java/src/test/resources/de/jplag/java/tokenPositions/Apply_3.java index ad2e277f9..7029c9b34 100644 --- a/languages/java/src/test/resources/de/jplag/java/tokenPositions/Apply_3.java +++ b/languages/java/src/test/resources/de/jplag/java/tokenPositions/Apply_3.java @@ -4,7 +4,7 @@ > public static void main(String[] args) { > File file = new File("test"); > file.getName().length(); -$ | J_APPLY 12 -$ | J_APPLY 6 +$ | J_APPLY 8 +$ | J_APPLY 7 > } >} \ No newline at end of file