From 7c3df6175daa51fd6726db5155d44b18ee0bb0d8 Mon Sep 17 00:00:00 2001 From: Pouria Alikhanifard Date: Fri, 13 Dec 2024 04:46:53 -0500 Subject: [PATCH] =?UTF-8?q?fix:=20visit=20non-attached=20java-docs=20(comm?= =?UTF-8?q?ents)=20same=20as=20the=20rest=20of=20the=20=E2=80=A6=20(#378)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: visit non-attached java-docs (comments) same as the rest of the java-docs fix: correct the non-attached java-doc position in tree fix: bug * fix: update tests accordingly --- .../gen/jdt/JdtWithCommentsVisitor.java | 15 ++++++++++++--- .../gumtreediff/gen/jdt/TestJdtGenerator.java | 6 +++--- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/gen.jdt/src/main/java/com/github/gumtreediff/gen/jdt/JdtWithCommentsVisitor.java b/gen.jdt/src/main/java/com/github/gumtreediff/gen/jdt/JdtWithCommentsVisitor.java index c1b9a6e0..f9a25d7f 100644 --- a/gen.jdt/src/main/java/com/github/gumtreediff/gen/jdt/JdtWithCommentsVisitor.java +++ b/gen.jdt/src/main/java/com/github/gumtreediff/gen/jdt/JdtWithCommentsVisitor.java @@ -52,11 +52,11 @@ public boolean visit(LineComment node) { } public boolean visit(Javadoc node) { - //We have to check if the java doc is attached to any program element or not + //We have to check if the Javadoc is attached to any program element or not //The attached ones, have been already visited, and we do not want to add them twice. if (node.getParent() == null) - //Then it is javadoc which is attached to any program element, - //So it will be visited as same as the other comments but with JavaDoc label + //Then it is Javadoc attached to any program element, + //So we do as the same as the other Javadocs in the original visitors return visitComment(node); return true; } @@ -65,6 +65,15 @@ public boolean visitComment(Comment node) { int start = node.getStartPosition(); int end = start + node.getLength(); Tree parent = findMostInnerEnclosingParent(context.getRoot(), start, end); + if (node instanceof Javadoc) { + trees.push(parent); + node.accept(JdtWithCommentsVisitor.this); + Tree wrongOrderChild = parent.getChild(parent.getChildren().size() - 1); + parent.getChildren().remove(wrongOrderChild); + insertChildProperly(parent, wrongOrderChild); + trees.pop(); + return true; + } Tree t = context.createTree(nodeAsSymbol(node), new String(scanner.getSource(), start, end - start)); t.setPos(start); t.setLength(node.getLength()); diff --git a/gen.jdt/src/test/java/com/github/gumtreediff/gen/jdt/TestJdtGenerator.java b/gen.jdt/src/test/java/com/github/gumtreediff/gen/jdt/TestJdtGenerator.java index d113721c..d5981e10 100644 --- a/gen.jdt/src/test/java/com/github/gumtreediff/gen/jdt/TestJdtGenerator.java +++ b/gen.jdt/src/test/java/com/github/gumtreediff/gen/jdt/TestJdtGenerator.java @@ -254,9 +254,9 @@ public void testComments2() throws IOException { + " PrimitiveType: boolean [60,67]\n" + " SimpleName: b [68,69]\n" + " Block [76,142]\n" - + " Javadoc: /**\n" - + " * test2 \n" - + " */ [86,119]\n" + + " Javadoc [86,119]\n" + + " TagElement [101,107]\n" + + " TextElement: test2 [101,107]\n" + " ExpressionStatement [128,136]\n" + " MethodInvocation [128,135]\n" + " SimpleName: sleep [128,133]";