Skip to content

Commit

Permalink
feat: Add comments to JdtVisitor (#368)
Browse files Browse the repository at this point in the history
* feat: Add comments to JdtVisitor

* feat: Add JavaDoc comments JdtVisitor

* Further improvements for jdt with comments
(addressing #368 (comment))

* Change the priority of JdtWithComments to high from maximum

* Refactor the jdt tree generator

* Write separate Visitor and Generator for JdtWithComments

* Remove unused imports and add some documentations
  • Loading branch information
pouryafard75 authored Sep 9, 2024
1 parent 76e65d3 commit f42e392
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class JdtVisitor extends AbstractJdtVisitor {
private static final Type ARRAY_INITIALIZER = nodeAsSymbol(ASTNode.ARRAY_INITIALIZER);
private static final Type SIMPLE_NAME = nodeAsSymbol(ASTNode.SIMPLE_NAME);

private IScanner scanner;
protected IScanner scanner;

public JdtVisitor(IScanner scanner) {
super();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* This file is part of GumTree.
*
* GumTree is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GumTree is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with GumTree. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2011-2015 Jean-Rémy Falleri <[email protected]>
* Copyright 2011-2015 Floréal Morandat <[email protected]>
*/

package com.github.gumtreediff.gen.jdt;

import com.github.gumtreediff.gen.Register;
import com.github.gumtreediff.utils.Registry;
import org.eclipse.jdt.core.compiler.IScanner;


/* Created by pourya on 2024-09-05*/
@Register(id = "java-jdtc", accept = "\\.java$", priority = Registry.Priority.HIGH)
public class JdtWithCommentsTreeGenerator extends AbstractJdtTreeGenerator {
@Override
protected AbstractJdtVisitor createVisitor(IScanner scanner) {
return new JdtWithCommentsVisitor(scanner);
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* This file is part of GumTree.
*
* GumTree is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GumTree is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with GumTree. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2011-2015 Jean-Rémy Falleri <[email protected]>
* Copyright 2011-2015 Floréal Morandat <[email protected]>
*/

package com.github.gumtreediff.gen.jdt;

import com.github.gumtreediff.tree.*;
import org.eclipse.jdt.core.compiler.IScanner;
import org.eclipse.jdt.core.dom.*;

import java.util.List;

/* Created by pourya on 2024-08-28*/
public class JdtWithCommentsVisitor extends JdtVisitor {

public JdtWithCommentsVisitor(IScanner scanner) {
super(scanner);
}

@Override
public void endVisit(CompilationUnit node) {
super.endVisit(node);
for (Object o : node.getCommentList()) {
ASTNode comment = (ASTNode) o;
comment.accept(new CommentsVisitor());
}
}

class CommentsVisitor extends ASTVisitor {
public boolean visit(BlockComment node) {
return visitComment(node);
}

public boolean visit(LineComment node) {
return visitComment(node);
}

public boolean visit(Javadoc node) {
//We have to check if the java doc 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
return visitComment(node);
return true;
}

public boolean visitComment(Comment node) {
int start = node.getStartPosition();
int end = start + node.getLength();
Tree parent = findMostInnerEnclosingParent(context.getRoot(), start, end);
Tree t = context.createTree(nodeAsSymbol(node), new String(scanner.getSource(), start, end - start));
t.setPos(start);
t.setLength(node.getLength());
insertChildProperly(parent, t);
return true;
}

public void insertChildProperly(Tree parent, Tree newChild) {
int position = 0;
for (Tree child : parent.getChildren()) {
if (child.getPos() < newChild.getPos()) {
position += 1;
} else
break;
}
parent.insertChild(newChild, position);
}

private Tree findMostInnerEnclosingParent(Tree root, int start, int end) {
Tree mostInnerParent = root;
List<Tree> children = root.getChildren();

for (Tree child : children) {
if (child.getPos() <= start && child.getEndPos() >= end) {
Tree candidate = findMostInnerEnclosingParent(child, start, end);
if (candidate.getPos() >= mostInnerParent.getPos()
&& candidate.getEndPos() <= mostInnerParent.getEndPos()) {
mostInnerParent = candidate;
}
}
}

return mostInnerParent;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,65 @@ public void testTagElement() throws IOException {
+ " SimpleName: C [26,27]";
assertEquals(expected, ct.getRoot().toTreeString());
}

@Test
public void testComments() throws IOException {
String input = "class bar {\n"
+ " void foo(/*int a*/)\n"
+ " {\n"
+ " //run();\n"
+ " }\n"
+ "}\n";
TreeContext ct = new JdtWithCommentsTreeGenerator().generateFrom().string(input);
String expected = "CompilationUnit [0,87]\n"
+ " TypeDeclaration [0,86]\n"
+ " TYPE_DECLARATION_KIND: class [0,5]\n"
+ " SimpleName: bar [6,9]\n"
+ " MethodDeclaration [20,84]\n"
+ " PrimitiveType: void [20,24]\n"
+ " SimpleName: foo [25,28]\n"
+ " BlockComment: /*int a*/ [29,38]\n"
+ " Block [48,84]\n"
+ " LineComment: //run(); [66,74]";
assertEquals(expected, ct.getRoot().toTreeString());
}

@Test
public void testComments2() throws IOException {
String input = "/**\n"
+ " * test\n"
+ " */\n"
+ "public class X {\n"
+ " void A(boolean b\n"
+ " ) {\n"
+ " /**\n"
+ " * test2 \n"
+ " */\n"
+ " sleep();\n"
+ " }\n"
+ "}\n";
String expected = "CompilationUnit [0,145]\n"
+ " TypeDeclaration [0,144]\n"
+ " Javadoc [0,31]\n"
+ " TagElement [15,19]\n"
+ " TextElement: test [15,19]\n"
+ " Modifier: public [32,38]\n"
+ " TYPE_DECLARATION_KIND: class [39,44]\n"
+ " SimpleName: X [45,46]\n"
+ " MethodDeclaration [53,142]\n"
+ " PrimitiveType: void [53,57]\n"
+ " SimpleName: A [58,59]\n"
+ " SingleVariableDeclaration [60,69]\n"
+ " PrimitiveType: boolean [60,67]\n"
+ " SimpleName: b [68,69]\n"
+ " Block [76,142]\n"
+ " Javadoc: /**\n"
+ " * test2 \n"
+ " */ [86,119]\n"
+ " ExpressionStatement [128,136]\n"
+ " MethodInvocation [128,135]\n"
+ " SimpleName: sleep [128,133]";
TreeContext ct = new JdtWithCommentsTreeGenerator().generateFrom().string(input);
assertEquals(expected, ct.getRoot().toTreeString());
}
}

0 comments on commit f42e392

Please sign in to comment.