-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add comments to JdtVisitor (#368)
* 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
1 parent
76e65d3
commit f42e392
Showing
4 changed files
with
202 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
gen.jdt/src/main/java/com/github/gumtreediff/gen/jdt/JdtWithCommentsTreeGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
|
103 changes: 103 additions & 0 deletions
103
gen.jdt/src/main/java/com/github/gumtreediff/gen/jdt/JdtWithCommentsVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters