Example usage for org.eclipse.jdt.core.dom DefaultBindingResolver DefaultBindingResolver

List of usage examples for org.eclipse.jdt.core.dom DefaultBindingResolver DefaultBindingResolver

Introduction

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

Prototype

DefaultBindingResolver(LookupEnvironment lookupEnvironment, WorkingCopyOwner workingCopyOwner,
            BindingTables bindingTables, boolean isRecoveringBindings, boolean fromJavaProject) 

Source Link

Usage

From source file:org.eclipse.jdt.core.dom.CompilationUnitResolver.java

License:Open Source License

IBinding createBinding(String key) {
    if (this.bindingTables == null)
        throw new RuntimeException("Cannot be called outside ASTParser#createASTs(...)"); //$NON-NLS-1$
    BindingKeyResolver keyResolver = new BindingKeyResolver(key, this, this.lookupEnvironment);
    Binding compilerBinding = keyResolver.getCompilerBinding();
    if (compilerBinding == null)
        return null;
    DefaultBindingResolver resolver = new DefaultBindingResolver(this.lookupEnvironment, null/*no owner*/,
            this.bindingTables, false, this.fromJavaProject);
    return resolver.getBinding(compilerBinding);
}

From source file:org.eclipse.jdt.core.dom.CompilationUnitResolver.java

License:Open Source License

public static CompilationUnit convert(CompilationUnitDeclaration compilationUnitDeclaration, char[] source,
        int apiLevel, Map options, boolean needToResolveBindings, WorkingCopyOwner owner,
        DefaultBindingResolver.BindingTables bindingTables, int flags, IProgressMonitor monitor,
        boolean fromJavaProject) {
    BindingResolver resolver = null;//from   ww  w .j  a v  a  2 s  . c om
    AST ast = AST.newAST(apiLevel);
    ast.setDefaultNodeFlag(ASTNode.ORIGINAL);
    CompilationUnit compilationUnit = null;
    ASTConverter converter = new ASTConverter(options, needToResolveBindings, monitor);
    if (needToResolveBindings) {
        resolver = new DefaultBindingResolver(compilationUnitDeclaration.scope, owner, bindingTables,
                (flags & ICompilationUnit.ENABLE_BINDINGS_RECOVERY) != 0, fromJavaProject);
        ast.setFlag(flags | AST.RESOLVED_BINDINGS);
    } else {
        resolver = new BindingResolver();
        ast.setFlag(flags);
    }
    ast.setBindingResolver(resolver);
    converter.setAST(ast);
    compilationUnit = converter.convert(compilationUnitDeclaration, source);
    compilationUnit.setLineEndTable(compilationUnitDeclaration.compilationResult.getLineSeparatorPositions());
    ast.setDefaultNodeFlag(0);
    ast.setOriginalModificationCount(ast.modificationCount());
    return compilationUnit;
}

From source file:org.eclipse.jdt.core.dom.CompilationUnitResolver.java

License:Open Source License

private void resolve(ICompilationUnit[] compilationUnits, String[] bindingKeys, ASTRequestor astRequestor,
        int apiLevel, Map compilerOptions, WorkingCopyOwner owner, int flags) {

    // temporarily connect ourselves to the ASTResolver - must disconnect when done
    astRequestor.compilationUnitResolver = this;
    this.bindingTables = new DefaultBindingResolver.BindingTables();
    CompilationUnitDeclaration unit = null;
    try {/*from w  ww .ja  va  2s.c  o  m*/
        int length = compilationUnits.length;
        org.eclipse.jdt.internal.compiler.env.ICompilationUnit[] sourceUnits = new org.eclipse.jdt.internal.compiler.env.ICompilationUnit[length];
        System.arraycopy(compilationUnits, 0, sourceUnits, 0, length);
        beginToCompile(sourceUnits, bindingKeys);
        // process all units (some more could be injected in the loop by the lookup environment)
        for (int i = 0; i < this.totalUnits; i++) {
            if (resolvedRequestedSourcesAndKeys(i)) {
                // no need to keep resolving if no more ASTs and no more binding keys are needed
                // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=114935
                // cleanup remaining units
                for (; i < this.totalUnits; i++) {
                    this.unitsToProcess[i].cleanUp();
                    this.unitsToProcess[i] = null;
                }
                break;
            }
            unit = this.unitsToProcess[i];
            try {
                super.process(unit, i); // this.process(...) is optimized to not process already known units

                // requested AST
                char[] fileName = unit.compilationResult.getFileName();
                ICompilationUnit source = (ICompilationUnit) this.requestedSources.get(fileName);
                if (source != null) {
                    // convert AST
                    CompilationResult compilationResult = unit.compilationResult;
                    org.eclipse.jdt.internal.compiler.env.ICompilationUnit sourceUnit = compilationResult.compilationUnit;
                    char[] contents = sourceUnit.getContents();
                    AST ast = AST.newAST(apiLevel);
                    ast.setFlag(flags | AST.RESOLVED_BINDINGS);
                    ast.setDefaultNodeFlag(ASTNode.ORIGINAL);
                    ASTConverter converter = new ASTConverter(compilerOptions, true/*need to resolve bindings*/,
                            this.monitor);
                    BindingResolver resolver = new DefaultBindingResolver(unit.scope, owner, this.bindingTables,
                            (flags & ICompilationUnit.ENABLE_BINDINGS_RECOVERY) != 0, this.fromJavaProject);
                    ast.setBindingResolver(resolver);
                    converter.setAST(ast);
                    CompilationUnit compilationUnit = converter.convert(unit, contents);
                    compilationUnit.setTypeRoot(source);
                    compilationUnit.setLineEndTable(compilationResult.getLineSeparatorPositions());
                    ast.setDefaultNodeFlag(0);
                    ast.setOriginalModificationCount(ast.modificationCount());

                    // pass it to requestor
                    astRequestor.acceptAST(source, compilationUnit);

                    worked(1);

                    // remove at the end so that we don't resolve twice if a source and a key for the same file name have been requested
                    this.requestedSources.put(fileName, null); // mark it as removed
                }

                // requested binding
                Object key = this.requestedKeys.get(fileName);
                if (key != null) {
                    if (key instanceof BindingKeyResolver) {
                        reportBinding(key, astRequestor, owner, unit);
                        worked(1);
                    } else if (key instanceof ArrayList) {
                        Iterator iterator = ((ArrayList) key).iterator();
                        while (iterator.hasNext()) {
                            reportBinding(iterator.next(), astRequestor, owner, unit);
                            worked(1);
                        }
                    }

                    // remove at the end so that we don't resolve twice if a source and a key for the same file name have been requested
                    this.requestedKeys.put(fileName, null); // mark it as removed
                }
            } finally {
                // cleanup compilation unit result
                unit.cleanUp();
            }
            this.unitsToProcess[i] = null; // release reference to processed unit declaration
            this.requestor.acceptResult(unit.compilationResult.tagAsAccepted());
        }

        // remaining binding keys
        DefaultBindingResolver resolver = new DefaultBindingResolver(this.lookupEnvironment, owner,
                this.bindingTables, (flags & ICompilationUnit.ENABLE_BINDINGS_RECOVERY) != 0, true);
        Object[] keys = this.requestedKeys.valueTable;
        for (int j = 0, keysLength = keys.length; j < keysLength; j++) {
            BindingKeyResolver keyResolver = (BindingKeyResolver) keys[j];
            if (keyResolver == null)
                continue;
            Binding compilerBinding = keyResolver.getCompilerBinding();
            IBinding binding = compilerBinding == null ? null : resolver.getBinding(compilerBinding);
            // pass it to requestor
            astRequestor.acceptBinding(((BindingKeyResolver) this.requestedKeys.valueTable[j]).getKey(),
                    binding);
            worked(1);
        }
    } catch (OperationCanceledException e) {
        throw e;
    } catch (AbortCompilation e) {
        this.handleInternalException(e, unit);
    } catch (Error e) {
        this.handleInternalException(e, unit, null);
        throw e; // rethrow
    } catch (RuntimeException e) {
        this.handleInternalException(e, unit, null);
        throw e; // rethrow
    } finally {
        // disconnect ourselves from ast requestor
        astRequestor.compilationUnitResolver = null;
    }
}

From source file:org.eclipse.jdt.core.dom.CompilationUnitResolver.java

License:Open Source License

private void resolve(String[] sourceCompilationUnits, String[] encodings, String[] bindingKeys,
        FileASTRequestor astRequestor, int apiLevel, Map compilerOptions, int flags) {

    // temporarily connect ourselves to the ASTResolver - must disconnect when done
    astRequestor.compilationUnitResolver = this;
    this.bindingTables = new DefaultBindingResolver.BindingTables();
    CompilationUnitDeclaration unit = null;
    try {/*from w  ww .  j ava2  s. c o m*/
        int length = sourceCompilationUnits.length;
        org.eclipse.jdt.internal.compiler.env.ICompilationUnit[] sourceUnits = new org.eclipse.jdt.internal.compiler.env.ICompilationUnit[length];
        int count = 0;
        for (int i = 0; i < length; i++) {
            char[] contents = null;
            String encoding = encodings != null ? encodings[i] : null;
            String sourceUnitPath = sourceCompilationUnits[i];
            try {
                contents = Util.getFileCharContent(new File(sourceUnitPath), encoding);
            } catch (IOException e) {
                // go to the next unit
                continue;
            }
            if (contents == null) {
                // go to the next unit
                continue;
            }
            sourceUnits[count++] = new org.eclipse.jdt.internal.compiler.batch.CompilationUnit(contents,
                    sourceUnitPath, encoding);
        }
        beginToCompile(sourceUnits, bindingKeys);
        // process all units (some more could be injected in the loop by the lookup environment)
        for (int i = 0; i < this.totalUnits; i++) {
            if (resolvedRequestedSourcesAndKeys(i)) {
                // no need to keep resolving if no more ASTs and no more binding keys are needed
                // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=114935
                // cleanup remaining units
                for (; i < this.totalUnits; i++) {
                    this.unitsToProcess[i].cleanUp();
                    this.unitsToProcess[i] = null;
                }
                break;
            }
            unit = this.unitsToProcess[i];
            try {
                super.process(unit, i); // this.process(...) is optimized to not process already known units

                // requested AST
                char[] fileName = unit.compilationResult.getFileName();
                org.eclipse.jdt.internal.compiler.env.ICompilationUnit source = (org.eclipse.jdt.internal.compiler.env.ICompilationUnit) this.requestedSources
                        .get(fileName);
                if (source != null) {
                    // convert AST
                    CompilationResult compilationResult = unit.compilationResult;
                    org.eclipse.jdt.internal.compiler.env.ICompilationUnit sourceUnit = compilationResult.compilationUnit;
                    char[] contents = sourceUnit.getContents();
                    AST ast = AST.newAST(apiLevel);
                    ast.setFlag(flags | AST.RESOLVED_BINDINGS);
                    ast.setDefaultNodeFlag(ASTNode.ORIGINAL);
                    ASTConverter converter = new ASTConverter(compilerOptions, true/*need to resolve bindings*/,
                            this.monitor);
                    BindingResolver resolver = new DefaultBindingResolver(unit.scope, null, this.bindingTables,
                            (flags & ICompilationUnit.ENABLE_BINDINGS_RECOVERY) != 0, this.fromJavaProject);
                    ast.setBindingResolver(resolver);
                    converter.setAST(ast);
                    CompilationUnit compilationUnit = converter.convert(unit, contents);
                    compilationUnit.setTypeRoot(null);
                    compilationUnit.setLineEndTable(compilationResult.getLineSeparatorPositions());
                    ast.setDefaultNodeFlag(0);
                    ast.setOriginalModificationCount(ast.modificationCount());

                    // pass it to requestor
                    astRequestor.acceptAST(new String(source.getFileName()), compilationUnit);

                    worked(1);

                    // remove at the end so that we don't resolve twice if a source and a key for the same file name have been requested
                    this.requestedSources.put(fileName, null); // mark it as removed
                }

                // requested binding
                Object key = this.requestedKeys.get(fileName);
                if (key != null) {
                    if (key instanceof BindingKeyResolver) {
                        reportBinding(key, astRequestor, unit);
                        worked(1);
                    } else if (key instanceof ArrayList) {
                        Iterator iterator = ((ArrayList) key).iterator();
                        while (iterator.hasNext()) {
                            reportBinding(iterator.next(), astRequestor, unit);
                            worked(1);
                        }
                    }

                    // remove at the end so that we don't resolve twice if a source and a key for the same file name have been requested
                    this.requestedKeys.put(fileName, null); // mark it as removed
                }
            } finally {
                // cleanup compilation unit result
                unit.cleanUp();
            }
            this.unitsToProcess[i] = null; // release reference to processed unit declaration
            this.requestor.acceptResult(unit.compilationResult.tagAsAccepted());
        }

        // remaining binding keys
        DefaultBindingResolver resolver = new DefaultBindingResolver(this.lookupEnvironment, null,
                this.bindingTables, (flags & ICompilationUnit.ENABLE_BINDINGS_RECOVERY) != 0, true);
        Object[] keys = this.requestedKeys.valueTable;
        for (int j = 0, keysLength = keys.length; j < keysLength; j++) {
            BindingKeyResolver keyResolver = (BindingKeyResolver) keys[j];
            if (keyResolver == null)
                continue;
            Binding compilerBinding = keyResolver.getCompilerBinding();
            IBinding binding = compilerBinding == null ? null : resolver.getBinding(compilerBinding);
            // pass it to requestor
            astRequestor.acceptBinding(((BindingKeyResolver) this.requestedKeys.valueTable[j]).getKey(),
                    binding);
            worked(1);
        }
    } catch (OperationCanceledException e) {
        throw e;
    } catch (AbortCompilation e) {
        this.handleInternalException(e, unit);
    } catch (Error e) {
        this.handleInternalException(e, unit, null);
        throw e; // rethrow
    } catch (RuntimeException e) {
        this.handleInternalException(e, unit, null);
        throw e; // rethrow
    } finally {
        // disconnect ourselves from ast requestor
        astRequestor.compilationUnitResolver = null;
    }
}

From source file:org.eclipse.jdt.core.dom.CompilationUnitResolver.java

License:Open Source License

private void reportBinding(Object key, ASTRequestor astRequestor, WorkingCopyOwner owner,
        CompilationUnitDeclaration unit) {
    BindingKeyResolver keyResolver = (BindingKeyResolver) key;
    Binding compilerBinding = keyResolver.getCompilerBinding();
    if (compilerBinding != null) {
        DefaultBindingResolver resolver = new DefaultBindingResolver(unit.scope, owner, this.bindingTables,
                false, this.fromJavaProject);
        AnnotationBinding annotationBinding = keyResolver.getAnnotationBinding();
        IBinding binding;/*w ww  .  j av  a  2 s.  c  om*/
        if (annotationBinding != null) {
            binding = resolver.getAnnotationInstance(annotationBinding);
        } else {
            binding = resolver.getBinding(compilerBinding);
        }
        if (binding != null)
            astRequestor.acceptBinding(keyResolver.getKey(), binding);
    }
}

From source file:org.eclipse.jdt.core.dom.CompilationUnitResolver.java

License:Open Source License

private void reportBinding(Object key, FileASTRequestor astRequestor, CompilationUnitDeclaration unit) {
    BindingKeyResolver keyResolver = (BindingKeyResolver) key;
    Binding compilerBinding = keyResolver.getCompilerBinding();
    if (compilerBinding != null) {
        DefaultBindingResolver resolver = new DefaultBindingResolver(unit.scope, null, this.bindingTables,
                false, this.fromJavaProject);
        AnnotationBinding annotationBinding = keyResolver.getAnnotationBinding();
        IBinding binding;/* w w w. j  ava 2  s  .c  om*/
        if (annotationBinding != null) {
            binding = resolver.getAnnotationInstance(annotationBinding);
        } else {
            binding = resolver.getBinding(compilerBinding);
        }
        if (binding != null)
            astRequestor.acceptBinding(keyResolver.getKey(), binding);
    }
}