Example usage for org.eclipse.jdt.core.dom ASTParser setWorkingCopyOwner

List of usage examples for org.eclipse.jdt.core.dom ASTParser setWorkingCopyOwner

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom ASTParser setWorkingCopyOwner.

Prototype

public void setWorkingCopyOwner(WorkingCopyOwner owner) 

Source Link

Document

Sets the working copy owner used when resolving bindings, where null means the primary owner.

Usage

From source file:org.eclipse.ajdt.internal.ui.refactoring.ITDRenameRefactoringProvider.java

License:Open Source License

/**
 * Creates an AST for the given compilation unit with translated code.
 * The source locations of the created AST have been converted back so that they reflect the source locations
 * of the original, non-translated source.
 *//*from  ww w  . jav  a 2 s .  co m*/
public CompilationUnit createSourceConvertedAST(String contents, ICompilationUnit unit, boolean resolveBindings,
        boolean statementsRecovery, boolean bindingsRecovery, IProgressMonitor monitor) {
    AspectsConvertingParser converter = new AspectsConvertingParser(contents.toCharArray());
    converter.setUnit(unit);
    final ArrayList<Replacement> replacements = converter.convert(ConversionOptions.CODE_COMPLETION);

    ASTParser fParser = ASTParser.newParser(AST.JLS4);
    fParser.setResolveBindings(resolveBindings);
    fParser.setStatementsRecovery(statementsRecovery);
    fParser.setBindingsRecovery(bindingsRecovery);
    fParser.setProject(unit.getJavaProject());
    fParser.setSource(converter.content);
    fParser.setUnitName(unit.getElementName());
    if (unit.getOwner() != null) {
        fParser.setWorkingCopyOwner(unit.getOwner());
    }
    CompilationUnit result = (CompilationUnit) fParser.createAST(monitor);
    result.accept(new ASTVisitor() {
        @Override
        public void preVisit(ASTNode node) {
            int newStart = AspectsConvertingParser.translatePositionToBeforeChanges(node.getStartPosition(),
                    replacements);
            int newEnd = AspectsConvertingParser
                    .translatePositionToBeforeChanges(node.getStartPosition() + node.getLength(), replacements);
            node.setSourceRange(newStart, newEnd - newStart);
        }
    });
    return result;
}