Example usage for org.eclipse.jdt.internal.compiler.env ISourceType getEnclosingType

List of usage examples for org.eclipse.jdt.internal.compiler.env ISourceType getEnclosingType

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.env ISourceType getEnclosingType.

Prototype

ISourceType getEnclosingType();

Source Link

Document

Answer the enclosing type or null if the receiver is a top level type.

Usage

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

License:Open Source License

/**
 * Add additional source types//from  www.  j ava2s . c o  m
 */
public void accept(ISourceType[] sourceTypes, PackageBinding packageBinding,
        AccessRestriction accessRestriction) {
    // case of SearchableEnvironment of an IJavaProject is used
    ISourceType sourceType = sourceTypes[0];
    while (sourceType.getEnclosingType() != null)
        sourceType = sourceType.getEnclosingType();
    if (sourceType instanceof SourceTypeElementInfo) {
        // get source
        SourceTypeElementInfo elementInfo = (SourceTypeElementInfo) sourceType;
        IType type = elementInfo.getHandle();
        ICompilationUnit sourceUnit = (ICompilationUnit) type.getCompilationUnit();
        accept(sourceUnit, accessRestriction);
    } else {
        CompilationResult result = new CompilationResult(sourceType.getFileName(), 1, 1, 0);
        CompilationUnitDeclaration unit = SourceTypeConverter.buildCompilationUnit(sourceTypes,
                SourceTypeConverter.FIELD_AND_METHOD // need field and methods
                        | SourceTypeConverter.MEMBER_TYPE, // need member types
                // no need for field initialization
                this.lookupEnvironment.problemReporter, result);
        this.lookupEnvironment.buildTypeBindings(unit, accessRestriction);
        this.lookupEnvironment.completeTypeBindings(unit, true);
    }
}

From source file:com.codenvy.ide.ext.java.server.internal.core.SearchableEnvironment.java

License:Open Source License

/**
 * Returns the given type in the the given package if it exists,
 * otherwise <code>null</code>.
 *///  w  w w  .  j  av a2 s.  com
protected NameEnvironmentAnswer find(String typeName, String packageName) {
    if (packageName == null)
        packageName = IPackageFragment.DEFAULT_PACKAGE_NAME;
    if (this.owner != null) {
        String source = this.owner.findSource(typeName, packageName);
        if (source != null) {
            ICompilationUnit cu = new BasicCompilationUnit(source.toCharArray(),
                    CharOperation.splitOn('.', packageName.toCharArray()),
                    typeName + Util.defaultJavaExtension());
            return new NameEnvironmentAnswer(cu, null);
        }
    }
    NameLookup.Answer answer = this.nameLookup.findType(typeName, packageName, false/*exact match*/,
            NameLookup.ACCEPT_ALL, this.checkAccessRestrictions);
    if (answer != null) {
        // construct name env answer
        if (answer.type instanceof BinaryType) { // BinaryType
            try {
                return new NameEnvironmentAnswer((IBinaryType) ((BinaryType) answer.type).getElementInfo(),
                        answer.restriction);
            } catch (JavaModelException npe) {
                // fall back to using owner
            }
        } else { //SourceType
            try {
                // retrieve the requested type
                SourceTypeElementInfo sourceType = (SourceTypeElementInfo) ((SourceType) answer.type)
                        .getElementInfo();
                ISourceType topLevelType = sourceType;
                while (topLevelType.getEnclosingType() != null) {
                    topLevelType = topLevelType.getEnclosingType();
                }
                // find all siblings (other types declared in same unit, since may be used for name resolution)
                IType[] types = sourceType.getHandle().getCompilationUnit().getTypes();
                ISourceType[] sourceTypes = new ISourceType[types.length];

                // in the resulting collection, ensure the requested type is the first one
                sourceTypes[0] = sourceType;
                int length = types.length;
                for (int i = 0, index = 1; i < length; i++) {
                    ISourceType otherType = (ISourceType) ((JavaElement) types[i]).getElementInfo();
                    if (!otherType.equals(topLevelType) && index < length) // check that the index is in bounds (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=62861)
                        sourceTypes[index++] = otherType;
                }
                return new NameEnvironmentAnswer(sourceTypes, answer.restriction);
            } catch (JavaModelException jme) {
                if (jme.isDoesNotExist() && String.valueOf(TypeConstants.PACKAGE_INFO_NAME).equals(typeName)) {
                    // in case of package-info.java the type doesn't exist in the model,
                    // but the CU may still help in order to fetch package level annotations.
                    return new NameEnvironmentAnswer((ICompilationUnit) answer.type.getParent(),
                            answer.restriction);
                }
                // no usable answer
            }
        }
    }
    return null;
}

From source file:com.redhat.ceylon.eclipse.core.model.loader.JDTModelLoader.java

License:Open Source License

public void createLookupEnvironment() {
    try {/* w w w  .  j  ava2 s  .c  o m*/
        lookupEnvironment = new LookupEnvironment(new ITypeRequestor() {

            private Parser basicParser;

            @Override
            public void accept(ISourceType[] sourceTypes, PackageBinding packageBinding,
                    AccessRestriction accessRestriction) {
                // case of SearchableEnvironment of an IJavaProject is used
                ISourceType sourceType = sourceTypes[0];
                while (sourceType.getEnclosingType() != null)
                    sourceType = sourceType.getEnclosingType();
                if (sourceType instanceof SourceTypeElementInfo) {
                    // get source
                    SourceTypeElementInfo elementInfo = (SourceTypeElementInfo) sourceType;
                    IType type = elementInfo.getHandle();
                    ICompilationUnit sourceUnit = (ICompilationUnit) type.getCompilationUnit();
                    accept(sourceUnit, accessRestriction);
                } else {
                    CompilationResult result = new CompilationResult(sourceType.getFileName(), 1, 1, 0);
                    CompilationUnitDeclaration unit = SourceTypeConverter.buildCompilationUnit(sourceTypes,
                            SourceTypeConverter.FIELD_AND_METHOD // need field and methods
                                    | SourceTypeConverter.MEMBER_TYPE, // need member types
                            // no need for field initialization
                            lookupEnvironment.problemReporter, result);
                    lookupEnvironment.buildTypeBindings(unit, accessRestriction);
                    lookupEnvironment.completeTypeBindings(unit, true);
                }
            }

            @Override
            public void accept(IBinaryType binaryType, PackageBinding packageBinding,
                    AccessRestriction accessRestriction) {
                lookupEnvironment.createBinaryTypeFrom(binaryType, packageBinding, accessRestriction);
            }

            @Override
            public void accept(ICompilationUnit sourceUnit, AccessRestriction accessRestriction) {
                // Switch the current policy and compilation result for this unit to the requested one.
                CompilationResult unitResult = new CompilationResult(sourceUnit, 1, 1,
                        compilerOptions.maxProblemsPerUnit);
                try {
                    CompilationUnitDeclaration parsedUnit = basicParser().dietParse(sourceUnit, unitResult);
                    lookupEnvironment.buildTypeBindings(parsedUnit, accessRestriction);
                    lookupEnvironment.completeTypeBindings(parsedUnit, true);
                } catch (AbortCompilationUnit e) {
                    // at this point, currentCompilationUnitResult may not be sourceUnit, but some other
                    // one requested further along to resolve sourceUnit.
                    if (unitResult.compilationUnit == sourceUnit) { // only report once
                        //requestor.acceptResult(unitResult.tagAsAccepted());
                    } else {
                        throw e; // want to abort enclosing request to compile
                    }
                }
                // Display unit error in debug mode
                if (BasicSearchEngine.VERBOSE) {
                    if (unitResult.problemCount > 0) {
                        System.out.println(unitResult);
                    }
                }
            }

            private Parser basicParser() {
                if (this.basicParser == null) {
                    ProblemReporter problemReporter = new ProblemReporter(
                            DefaultErrorHandlingPolicies.proceedWithAllProblems(), compilerOptions,
                            new DefaultProblemFactory());
                    this.basicParser = new Parser(problemReporter, false);
                    this.basicParser.reportOnlyOneSyntaxError = true;
                }
                return this.basicParser;
            }
        }, compilerOptions, problemReporter,
                ((JavaProject) javaProject).newSearchableNameEnvironment((WorkingCopyOwner) null));
    } catch (JavaModelException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:org.eclipse.che.jdt.internal.core.search.indexing.SourceIndexer.java

License:Open Source License

public void accept(ISourceType[] sourceTypes, PackageBinding packageBinding,
        AccessRestriction accessRestriction) {
    ISourceType sourceType = sourceTypes[0];
    while (sourceType.getEnclosingType() != null)
        sourceType = sourceType.getEnclosingType();
    SourceTypeElementInfo elementInfo = (SourceTypeElementInfo) sourceType;
    IType type = elementInfo.getHandle();
    ICompilationUnit sourceUnit = (ICompilationUnit) type.getCompilationUnit();
    accept(sourceUnit, accessRestriction);
}

From source file:org.eclipse.jdt.internal.core.hierarchy.HierarchyResolver.java

License:Open Source License

/**
 * Add additional source types/*www . jav a2 s. com*/
 * @param sourceTypes
 * @param packageBinding
 */
public void accept(ISourceType[] sourceTypes, PackageBinding packageBinding,
        AccessRestriction accessRestriction) {
    IProgressMonitor progressMonitor = this.builder.hierarchy.progressMonitor;
    if (progressMonitor != null && progressMonitor.isCanceled())
        throw new OperationCanceledException();

    // find most enclosing type first (needed when explicit askForType(...) is done
    // with a member type (e.g. p.A$B))
    ISourceType sourceType = sourceTypes[0];
    while (sourceType.getEnclosingType() != null)
        sourceType = sourceType.getEnclosingType();

    // build corresponding compilation unit
    CompilationResult result = new CompilationResult(sourceType.getFileName(), 1, 1,
            this.options.maxProblemsPerUnit);
    CompilationUnitDeclaration unit = SourceTypeConverter.buildCompilationUnit(new ISourceType[] { sourceType }, // ignore secondary types, to improve laziness
            SourceTypeConverter.MEMBER_TYPE, // need member types
            // no need for field initialization
            this.lookupEnvironment.problemReporter, result);

    // build bindings
    if (unit != null) {
        try {
            this.lookupEnvironment.buildTypeBindings(unit, accessRestriction);

            org.eclipse.jdt.core.ICompilationUnit cu = ((SourceTypeElementInfo) sourceType).getHandle()
                    .getCompilationUnit();
            rememberAllTypes(unit, cu, false);

            this.lookupEnvironment.completeTypeBindings(unit, true/*build constructor only*/);
        } catch (AbortCompilation e) {
            // missing 'java.lang' package: ignore
        }
    }
}