Example usage for org.eclipse.jdt.internal.compiler.ast CompilationUnitDeclaration isPackageInfo

List of usage examples for org.eclipse.jdt.internal.compiler.ast CompilationUnitDeclaration isPackageInfo

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.ast CompilationUnitDeclaration isPackageInfo.

Prototype

public boolean isPackageInfo() 

Source Link

Usage

From source file:com.codenvy.ide.ext.java.server.internal.codeassist.SelectionEngine.java

License:Open Source License

/**
 * Ask the engine to compute the selection at the specified position
 * of the given compilation unit.//from  w w w  . j a  va2  s . com
        
 *  @param sourceUnit org.eclipse.jdt.internal.compiler.env.ICompilationUnit
 *      the source of the current compilation unit.
 *
 *  @param selectionSourceStart int
 *  @param selectionSourceEnd int
 *      a range in the source where the selection is.
 */
public void select(ICompilationUnit sourceUnit, int selectionSourceStart, int selectionSourceEnd) {

    char[] source = sourceUnit.getContents();

    if (DEBUG) {
        System.out.print("SELECTION IN "); //$NON-NLS-1$
        System.out.print(sourceUnit.getFileName());
        System.out.print(" FROM "); //$NON-NLS-1$
        System.out.print(selectionSourceStart);
        System.out.print(" TO "); //$NON-NLS-1$
        System.out.println(selectionSourceEnd);
        System.out.println("SELECTION - Source :"); //$NON-NLS-1$
        System.out.println(source);
    }
    if (!checkSelection(source, selectionSourceStart, selectionSourceEnd)) {
        return;
    }
    if (DEBUG) {
        System.out.print("SELECTION - Checked : \""); //$NON-NLS-1$
        System.out.print(new String(source, this.actualSelectionStart,
                this.actualSelectionEnd - this.actualSelectionStart + 1));
        System.out.println('"');
    }
    try {
        this.acceptedAnswer = false;
        CompilationResult result = new CompilationResult(sourceUnit, 1, 1,
                this.compilerOptions.maxProblemsPerUnit);
        CompilationUnitDeclaration parsedUnit = this.parser.dietParse(sourceUnit, result,
                this.actualSelectionStart, this.actualSelectionEnd);

        if (parsedUnit != null) {
            if (DEBUG) {
                System.out.println("SELECTION - Diet AST :"); //$NON-NLS-1$
                System.out.println(parsedUnit.toString());
            }

            // scan the package & import statements first
            if (parsedUnit.currentPackage instanceof SelectionOnPackageReference) {
                char[][] tokens = ((SelectionOnPackageReference) parsedUnit.currentPackage).tokens;
                this.noProposal = false;
                this.requestor.acceptPackage(CharOperation.concatWith(tokens, '.'));
                return;
            }
            ImportReference[] imports = parsedUnit.imports;
            if (imports != null) {
                for (int i = 0, length = imports.length; i < length; i++) {
                    ImportReference importReference = imports[i];
                    if (importReference instanceof SelectionOnImportReference) {
                        char[][] tokens = ((SelectionOnImportReference) importReference).tokens;
                        this.noProposal = false;
                        this.requestor.acceptPackage(CharOperation.concatWith(tokens, '.'));
                        this.nameEnvironment.findTypes(CharOperation.concatWith(tokens, '.'), false, false,
                                IJavaSearchConstants.TYPE, this);

                        this.lookupEnvironment.buildTypeBindings(parsedUnit, null /*no access restriction*/);
                        if ((this.unitScope = parsedUnit.scope) != null) {
                            int tokenCount = tokens.length;
                            char[] lastToken = tokens[tokenCount - 1];
                            char[][] qualifierTokens = CharOperation.subarray(tokens, 0, tokenCount - 1);

                            if (qualifierTokens != null && qualifierTokens.length > 0) {
                                Binding binding = this.unitScope.getTypeOrPackage(qualifierTokens);
                                if (binding != null && binding instanceof ReferenceBinding) {
                                    ReferenceBinding ref = (ReferenceBinding) binding;
                                    selectMemberTypeFromImport(parsedUnit, lastToken, ref,
                                            importReference.isStatic());
                                    if (importReference.isStatic()) {
                                        selectStaticFieldFromStaticImport(parsedUnit, lastToken, ref);
                                        selectStaticMethodFromStaticImport(parsedUnit, lastToken, ref);
                                    }
                                }
                            }
                        }

                        // accept qualified types only if no unqualified type was accepted
                        if (!this.acceptedAnswer) {
                            acceptQualifiedTypes();
                            if (!this.acceptedAnswer) {
                                this.nameEnvironment.findTypes(this.selectedIdentifier, false, false,
                                        IJavaSearchConstants.TYPE, this);
                                // try with simple type name
                                if (!this.acceptedAnswer) {
                                    acceptQualifiedTypes();
                                }
                            }
                        }
                        if (this.noProposal && this.problem != null) {
                            this.requestor.acceptError(this.problem);
                        }
                        return;
                    }
                }
            }
            if (parsedUnit.types != null || parsedUnit.isPackageInfo()) {
                if (selectDeclaration(parsedUnit))
                    return;
                this.lookupEnvironment.buildTypeBindings(parsedUnit, null /*no access restriction*/);
                if ((this.unitScope = parsedUnit.scope) != null) {
                    try {
                        this.lookupEnvironment.completeTypeBindings(parsedUnit, true);

                        CompilationUnitDeclaration previousUnitBeingCompleted = this.lookupEnvironment.unitBeingCompleted;
                        this.lookupEnvironment.unitBeingCompleted = parsedUnit;
                        parsedUnit.scope.faultInTypes();
                        this.lookupEnvironment.unitBeingCompleted = previousUnitBeingCompleted;
                        ASTNode node = null;
                        if (parsedUnit.types != null)
                            node = parseBlockStatements(parsedUnit, selectionSourceStart);
                        if (DEBUG) {
                            System.out.println("SELECTION - AST :"); //$NON-NLS-1$
                            System.out.println(parsedUnit.toString());
                        }
                        parsedUnit.resolve();
                        if (node != null) {
                            selectLocalDeclaration(node);
                        }
                    } catch (SelectionNodeFound e) {
                        if (e.binding != null) {
                            if (DEBUG) {
                                System.out.println("SELECTION - Selection binding:"); //$NON-NLS-1$
                                System.out.println(e.binding.toString());
                            }
                            // if null then we found a problem in the selection node
                            selectFrom(e.binding, parsedUnit, sourceUnit, e.isDeclaration);
                        }
                    }
                }
            }
        }
        // only reaches here if no selection could be derived from the parsed tree
        // thus use the selected source and perform a textual type search
        if (!this.acceptedAnswer) {
            this.nameEnvironment.findTypes(this.selectedIdentifier, false, false, IJavaSearchConstants.TYPE,
                    this);

            // accept qualified types only if no unqualified type was accepted
            if (!this.acceptedAnswer) {
                acceptQualifiedTypes();

                // accept types from all the workspace only if no type was found in the project scope
                if (this.noProposal) {
                    findAllTypes(this.selectedIdentifier);
                }
            }
        }
        if (this.noProposal && this.problem != null) {
            this.requestor.acceptError(this.problem);
        }
    } catch (IndexOutOfBoundsException e) { // work-around internal failure - 1GEMF6D
        if (DEBUG) {
            System.out.println("Exception caught by SelectionEngine:"); //$NON-NLS-1$
            e.printStackTrace(System.out);
        }
    } catch (AbortCompilation e) { // ignore this exception for now since it typically means we cannot find java.lang.Object
        if (DEBUG) {
            System.out.println("Exception caught by SelectionEngine:"); //$NON-NLS-1$
            e.printStackTrace(System.out);
        }
    } finally {
        reset(true);
    }
}

From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.MatchLocator.java

License:Open Source License

/**
 * Creates an IImportDeclaration from the given import statement
 */// ww  w .  ja v  a2 s .co  m
protected IJavaElement createPackageDeclarationHandle(CompilationUnitDeclaration unit) {
    if (unit.isPackageInfo()) {
        char[] packName = CharOperation.concatWith(unit.currentPackage.getImportName(), '.');
        Openable openable = this.currentPossibleMatch.openable;
        if (openable instanceof CompilationUnit) {
            return ((CompilationUnit) openable).getPackageDeclaration(new String(packName));
        }
    }
    return createTypeHandle(new String(unit.getMainTypeName()));
}

From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.MatchLocator.java

License:Open Source License

protected void process(PossibleMatch possibleMatch, boolean bindingsWereCreated) throws CoreException {
    this.currentPossibleMatch = possibleMatch;
    CompilationUnitDeclaration unit = possibleMatch.parsedUnit;
    try {/*ww  w . j a  v a 2s .  com*/
        if (unit.isEmpty()) {
            if (this.currentPossibleMatch.openable instanceof ClassFile) {
                ClassFile classFile = (ClassFile) this.currentPossibleMatch.openable;
                IBinaryType info = null;
                try {
                    info = getBinaryInfo(classFile, classFile.resource());
                } catch (CoreException ce) {
                    // Do nothing
                }
                if (info != null) {
                    boolean mayBeGeneric = this.patternLocator.mayBeGeneric;
                    this.patternLocator.mayBeGeneric = false; // there's no longer generic in class files
                    try {
                        new ClassFileMatchLocator().locateMatches(this, classFile, info);
                    } finally {
                        this.patternLocator.mayBeGeneric = mayBeGeneric;
                    }
                }
            }
            return;
        }
        if (hasAlreadyDefinedType(unit))
            return; // skip type has it is hidden so not visible

        // Move getMethodBodies to #parseAndBuildings(...) method to allow possible match resolution management
        //getMethodBodies(unit);

        boolean mustResolve = (this.pattern.mustResolve || possibleMatch.nodeSet.mustResolve);
        if (bindingsWereCreated && mustResolve) {
            if (unit.types != null) {
                if (BasicSearchEngine.VERBOSE)
                    System.out
                            .println("Resolving " + this.currentPossibleMatch.openable.toStringWithAncestors()); //$NON-NLS-1$

                this.lookupEnvironment.unitBeingCompleted = unit;
                reduceParseTree(unit);

                if (unit.scope != null) {
                    // fault in fields & methods
                    unit.scope.faultInTypes();
                }
                unit.resolve();
            } else if (unit.isPackageInfo()) {
                if (BasicSearchEngine.VERBOSE)
                    System.out
                            .println("Resolving " + this.currentPossibleMatch.openable.toStringWithAncestors()); //$NON-NLS-1$
                unit.resolve();
            }
        }
        reportMatching(unit, mustResolve);
    } catch (AbortCompilation e) {
        if (BasicSearchEngine.VERBOSE) {
            System.out.println("AbortCompilation while resolving unit " + String.valueOf(unit.getFileName())); //$NON-NLS-1$
            e.printStackTrace();
        }
        // could not resolve: report inaccurate matches
        reportMatching(unit, false); // do not resolve when cu has errors
        if (!(e instanceof AbortCompilationUnit)) {
            // problem with class path
            throw e;
        }
    } finally {
        this.lookupEnvironment.unitBeingCompleted = null;
        this.currentPossibleMatch = null;
    }
}

From source file:org.eclipse.jdt.internal.core.search.matching.MatchLocator.java

License:Open Source License

protected void process(PossibleMatch possibleMatch, boolean bindingsWereCreated) throws CoreException {
    // GROOVY Start
    // Do not process non-Java files.  They use a separate delegated search
    if (LanguageSupportFactory.isInterestingSourceFile(new String(possibleMatch.getFileName()))) {
        try {//from   w ww .j  ava2 s  .c o  m
            this.lookupEnvironment.buildTypeBindings(possibleMatch.parsedUnit, null /*no access restriction*/);
        } catch (Throwable t) {
            t.printStackTrace();
        }
        possibleMatch.parsedUnit.resolve();
        return;
    }
    // GROOVY End

    this.currentPossibleMatch = possibleMatch;
    CompilationUnitDeclaration unit = possibleMatch.parsedUnit;
    try {
        if (unit.isEmpty()) {
            if (this.currentPossibleMatch.openable instanceof ClassFile) {
                ClassFile classFile = (ClassFile) this.currentPossibleMatch.openable;
                IBinaryType info = null;
                try {
                    info = getBinaryInfo(classFile, classFile.resource());
                } catch (CoreException ce) {
                    // Do nothing
                }
                if (info != null) {
                    boolean mayBeGeneric = this.patternLocator.mayBeGeneric;
                    this.patternLocator.mayBeGeneric = false; // there's no longer generic in class files
                    try {
                        new ClassFileMatchLocator().locateMatches(this, classFile, info);
                    } finally {
                        this.patternLocator.mayBeGeneric = mayBeGeneric;
                    }
                }
            }
            return;
        }
        if (hasAlreadyDefinedType(unit))
            return; // skip type has it is hidden so not visible

        // Move getMethodBodies to #parseAndBuildings(...) method to allow possible match resolution management
        //getMethodBodies(unit);

        boolean mustResolve = (this.pattern.mustResolve || possibleMatch.nodeSet.mustResolve);
        if (bindingsWereCreated && mustResolve) {
            if (unit.types != null) {
                if (BasicSearchEngine.VERBOSE)
                    System.out
                            .println("Resolving " + this.currentPossibleMatch.openable.toStringWithAncestors()); //$NON-NLS-1$

                this.lookupEnvironment.unitBeingCompleted = unit;
                reduceParseTree(unit);

                if (unit.scope != null) {
                    // fault in fields & methods
                    unit.scope.faultInTypes();
                }
                unit.resolve();
            } else if (unit.isPackageInfo()) {
                if (BasicSearchEngine.VERBOSE)
                    System.out
                            .println("Resolving " + this.currentPossibleMatch.openable.toStringWithAncestors()); //$NON-NLS-1$
                unit.resolve();
            }
        }
        reportMatching(unit, mustResolve);
    } catch (AbortCompilation e) {
        // could not resolve: report inaccurate matches
        reportMatching(unit, false); // do not resolve when cu has errors
        if (!(e instanceof AbortCompilationUnit)) {
            // problem with class path
            throw e;
        }
    } finally {
        this.lookupEnvironment.unitBeingCompleted = null;
        this.currentPossibleMatch = null;
    }
}