Skip to content

Commit

Permalink
Refactor the jdt tree generator
Browse files Browse the repository at this point in the history
  • Loading branch information
pouryafard75 committed Sep 5, 2024
1 parent d2ee3e2 commit 57d7101
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,13 @@ public TreeContext generate(Reader r) throws IOException {
if ((node.getFlags() & ASTNode.MALFORMED) != 0) // bitwise flag to check if the node has a syntax error
throw new SyntaxException(this, r, null);
node.accept(v);
postProcess(node, scanner, v);
return v.getTreeContext();
}

protected void postProcess(ASTNode node, IScanner scanner, AbstractJdtVisitor v) {
//Left for subclasses to implement
}

protected abstract AbstractJdtVisitor createVisitor(IScanner scanner);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,15 @@
import com.github.gumtreediff.gen.SyntaxException;
import com.github.gumtreediff.tree.TreeContext;
import com.github.gumtreediff.utils.Registry;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.ToolFactory;
import org.eclipse.jdt.core.compiler.IScanner;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.CompilationUnit;

import java.io.IOException;
import java.io.Reader;
import java.util.List;
import java.util.Map;





/* Created by pourya on 2024-09-05*/
Expand All @@ -50,25 +44,7 @@ protected AbstractJdtVisitor createVisitor(IScanner scanner) {
}

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public TreeContext generate(Reader r) throws IOException {
ASTParser parser = ASTParser.newParser(AST.getJLSLatest());
parser.setKind(ASTParser.K_COMPILATION_UNIT);
Map pOptions = JavaCore.getOptions();
pOptions.put(JavaCore.COMPILER_COMPLIANCE, JAVA_VERSION);
pOptions.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JAVA_VERSION);
pOptions.put(JavaCore.COMPILER_SOURCE, JAVA_VERSION);
pOptions.put(JavaCore.COMPILER_DOC_COMMENT_SUPPORT, JavaCore.ENABLED);
parser.setCompilerOptions(pOptions);
char[] source = readerToCharArray(r);
parser.setSource(source);
IScanner scanner = ToolFactory.createScanner(false, false, false, false);
scanner.setSource(source);
AbstractJdtVisitor v = createVisitor(scanner);
ASTNode node = parser.createAST(null);
if ((node.getFlags() & ASTNode.MALFORMED) != 0) // bitwise flag to check if the node has a syntax error
throw new SyntaxException(this, r, null);
node.accept(v);
protected void postProcess(ASTNode node, IScanner scanner, AbstractJdtVisitor v) {
if (node instanceof CompilationUnit)
{
List commentList = ((CompilationUnit) node).getCommentList();
Expand All @@ -77,7 +53,6 @@ public TreeContext generate(Reader r) throws IOException {
comment.accept(new JdtCommentVisitor(scanner, v.getTreeContext()));
}
}
return v.getTreeContext();
}
}

Expand Down

0 comments on commit 57d7101

Please sign in to comment.