Example usage for org.eclipse.jdt.core.dom.rewrite ASTRewrite setTargetSourceRangeComputer

List of usage examples for org.eclipse.jdt.core.dom.rewrite ASTRewrite setTargetSourceRangeComputer

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom.rewrite ASTRewrite setTargetSourceRangeComputer.

Prototype

public final void setTargetSourceRangeComputer(TargetSourceRangeComputer computer) 

Source Link

Document

Sets a custom target source range computer for this AST rewriter.

Usage

From source file:org.eclipse.emf.codegen.merge.java.facade.ast.ASTFacadeHelper.java

License:Open Source License

@Override
public ASTJCompilationUnit createCompilationUnit(String name, String contents) {
    // set source
    char[] contentAsCharArray = contents.toCharArray();
    ASTParser astParser = createASTParser();
    astParser.setSource(contentAsCharArray);

    // parse/*from w  w w  .j  a  va2  s.  c  om*/
    CompilationUnit astCompilationUnit = (CompilationUnit) astParser.createAST(null);

    Diagnostic diagnostic = analyzeCompilationUnit(astCompilationUnit, contents);
    if (diagnostic != Diagnostic.OK_INSTANCE) {
        StringBuilder message = new StringBuilder(diagnostic.getMessage());
        for (Diagnostic childDiagnostic : diagnostic.getChildren()) {
            message.append("\n\t").append(childDiagnostic.getMessage());
        }
        message.append(contents);
        CodeGenPlugin.INSTANCE.log(message.toString());

        if (diagnostic.getSeverity() == Diagnostic.ERROR) {
            throw new WrappedException(new DiagnosticException(diagnostic));
        }
    }

    // create rewriter to record changes
    ASTRewrite rewriter = ASTRewrite.create(astCompilationUnit.getAST());

    // keep comments between nodes when removing or moving nodes
    rewriter.setTargetSourceRangeComputer(new CommentAwareSourceRangeComputer(astCompilationUnit, contents));

    // set properties
    astCompilationUnit.setProperty(ASTJCompilationUnit.NAME_PROPERTY, name);

    // create JNode and set properties
    ASTJCompilationUnit compilationUnit = (ASTJCompilationUnit) convertToNode(astCompilationUnit);
    compilationUnit.setOriginalContents(contentAsCharArray);
    compilationUnit.setRewriter(rewriter);

    return compilationUnit;
}