Example usage for org.eclipse.jdt.internal.compiler.util SimpleSet addIfNotIncluded

List of usage examples for org.eclipse.jdt.internal.compiler.util SimpleSet addIfNotIncluded

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.util SimpleSet addIfNotIncluded.

Prototype

public Object addIfNotIncluded(Object object) 

Source Link

Usage

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

License:Open Source License

/**
 * Calculate and cache the package list available in the zipFile.
 *
 * @param jar/* w w w .j  a va2 s. c om*/
 *         The ClasspathJar to use
 * @return A SimpleSet with the all the package names in the zipFile.
 */
static SimpleSet findPackageSet(ClasspathJar jar) {
    String zipFileName = jar.zipFilename;

    SimpleSet packageSet = new SimpleSet(41);
    packageSet.add(""); //$NON-NLS-1$
    nextEntry: for (Enumeration e = jar.zipFile.entries(); e.hasMoreElements();) {
        String fileName = ((ZipEntry) e.nextElement()).getName();

        // add the package name & all of its parent packages
        int last = fileName.lastIndexOf('/');
        while (last > 0) {
            // extract the package name
            String packageName = fileName.substring(0, last);
            String[] splittedName = Util.splitOn('/', packageName, 0, packageName.length());
            for (String s : splittedName) {
                if (!org.eclipse.jdt.internal.core.util.Util.isValidFolderNameForPackage(s, "1.7", "1.7")) {
                    continue nextEntry;
                }
            }

            if (packageSet.addIfNotIncluded(packageName) == null)
                continue nextEntry; // already existed

            last = packageName.lastIndexOf('/');
        }
    }

    return packageSet;
}

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

License:Open Source License

protected void reportDeclaration(FieldBinding fieldBinding, MatchLocator locator, SimpleSet knownFields)
        throws CoreException {
    // ignore length field
    if (fieldBinding == ArrayBinding.ArrayLength)
        return;/* w ww  . j a  va  2s .  c  o  m*/

    ReferenceBinding declaringClass = fieldBinding.declaringClass;
    IType type = locator.lookupType(declaringClass);
    if (type == null)
        return; // case of a secondary type

    char[] bindingName = fieldBinding.name;
    IField field = type.getField(new String(bindingName));
    if (knownFields.addIfNotIncluded(field) == null)
        return;

    IResource resource = type.getResource();
    boolean isBinary = type.isBinary();
    IBinaryType info = null;
    if (isBinary) {
        if (resource == null)
            resource = type.getJavaProject().getProject();
        info = locator.getBinaryInfo((org.eclipse.jdt.internal.core.ClassFile) type.getClassFile(), resource);
        locator.reportBinaryMemberDeclaration(resource, field, fieldBinding, info, SearchMatch.A_ACCURATE);
    } else {
        if (declaringClass instanceof ParameterizedTypeBinding)
            declaringClass = ((ParameterizedTypeBinding) declaringClass).genericType();
        ClassScope scope = ((SourceTypeBinding) declaringClass).scope;
        if (scope != null) {
            TypeDeclaration typeDecl = scope.referenceContext;
            FieldDeclaration fieldDecl = null;
            FieldDeclaration[] fieldDecls = typeDecl.fields;
            int length = fieldDecls == null ? 0 : fieldDecls.length;
            for (int i = 0; i < length; i++) {
                if (CharOperation.equals(bindingName, fieldDecls[i].name)) {
                    fieldDecl = fieldDecls[i];
                    break;
                }
            }
            if (fieldDecl != null) {
                int offset = fieldDecl.sourceStart;
                this.match = new FieldDeclarationMatch(((JavaElement) field).resolved(fieldBinding),
                        SearchMatch.A_ACCURATE, offset, fieldDecl.sourceEnd - offset + 1,
                        locator.getParticipant(), resource);
                locator.report(this.match);
            }
        }
    }
}

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

License:Open Source License

/**
 * Locates the package declarations corresponding to the search pattern.
 *///w ww.  j a va2  s  .  co  m
protected void locatePackageDeclarations(SearchPattern searchPattern, SearchParticipant participant,
        IJavaProject[] projects) throws CoreException {
    if (this.progressMonitor != null && this.progressMonitor.isCanceled()) {
        throw new OperationCanceledException();
    }
    if (searchPattern instanceof OrPattern) {
        SearchPattern[] patterns = ((OrPattern) searchPattern).patterns;
        for (int i = 0, length = patterns.length; i < length; i++) {
            locatePackageDeclarations(patterns[i], participant, projects);
        }
    } else if (searchPattern instanceof PackageDeclarationPattern) {
        IJavaElement focus = searchPattern.focus;
        if (focus != null) {
            if (encloses(focus)) {
                SearchMatch match = new PackageDeclarationMatch(
                        focus.getAncestor(IJavaElement.PACKAGE_FRAGMENT), SearchMatch.A_ACCURATE, -1, -1,
                        participant, focus.getResource());
                report(match);
            }
            return;
        }
        PackageDeclarationPattern pkgPattern = (PackageDeclarationPattern) searchPattern;
        boolean isWorkspaceScope = this.scope == JavaModelManager.getJavaModelManager().getWorkspaceScope();
        IPath[] scopeProjectsAndJars = isWorkspaceScope ? null : this.scope.enclosingProjectsAndJars();
        int scopeLength = isWorkspaceScope ? 0 : scopeProjectsAndJars.length;
        SimpleSet packages = new SimpleSet();
        for (int i = 0, length = projects.length; i < length; i++) {
            IJavaProject javaProject = projects[i];
            if (this.progressMonitor != null) {
                if (this.progressMonitor.isCanceled())
                    throw new OperationCanceledException();
                this.progressWorked++;
                if ((this.progressWorked % this.progressStep) == 0)
                    this.progressMonitor.worked(this.progressStep);
            }
            // Verify that project belongs to the scope
            if (!isWorkspaceScope) {
                boolean found = false;
                for (int j = 0; j < scopeLength; j++) {
                    if (javaProject.getPath().equals(scopeProjectsAndJars[j])) {
                        found = true;
                        break;
                    }
                }
                if (!found)
                    continue;
            }
            // Get all project package fragment names
            this.nameLookup = ((JavaProject) projects[i]).newNameLookup(this.workingCopies);
            IPackageFragment[] packageFragments = this.nameLookup
                    .findPackageFragments(new String(pkgPattern.pkgName), false, true);
            int pLength = packageFragments == null ? 0 : packageFragments.length;
            // Report matches avoiding duplicate names
            for (int p = 0; p < pLength; p++) {
                IPackageFragment fragment = packageFragments[p];
                if (packages.addIfNotIncluded(fragment) == null)
                    continue;
                if (encloses(fragment)) {
                    IResource resource = fragment.getResource();
                    if (resource == null) // case of a file in an external jar
                        resource = javaProject.getProject();
                    try {
                        if (encloses(fragment)) {
                            SearchMatch match = new PackageDeclarationMatch(fragment, SearchMatch.A_ACCURATE,
                                    -1, -1, participant, resource);
                            report(match);
                        }
                    } catch (JavaModelException e) {
                        throw e;
                    } catch (CoreException e) {
                        throw new JavaModelException(e);
                    }
                }
            }
        }
    }
}

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

License:Open Source License

protected void reportDeclaration(MethodBinding methodBinding, MatchLocator locator, SimpleSet knownMethods)
        throws CoreException {
    ReferenceBinding declaringClass = methodBinding.declaringClass;
    IType type = locator.lookupType(declaringClass);
    if (type == null)
        return; // case of a secondary type

    // Report match for binary
    if (type.isBinary()) {
        IMethod method = null;/*from w  ww.  j  a  v a 2 s. c o  m*/
        TypeBinding[] parameters = methodBinding.original().parameters;
        int parameterLength = parameters.length;
        char[][] parameterTypes = new char[parameterLength][];
        for (int i = 0; i < parameterLength; i++) {
            char[] typeName = parameters[i].qualifiedSourceName();
            for (int j = 0, dim = parameters[i].dimensions(); j < dim; j++) {
                typeName = CharOperation.concat(typeName, new char[] { '[', ']' });
            }
            parameterTypes[i] = typeName;
        }
        method = locator.createBinaryMethodHandle(type, methodBinding.selector, parameterTypes);
        if (method == null || knownMethods.addIfNotIncluded(method) == null)
            return;

        IResource resource = type.getResource();
        if (resource == null)
            resource = type.getJavaProject().getProject();
        IBinaryType info = locator.getBinaryInfo((org.eclipse.jdt.internal.core.ClassFile) type.getClassFile(),
                resource);
        locator.reportBinaryMemberDeclaration(resource, method, methodBinding, info, SearchMatch.A_ACCURATE);
        return;
    }

    // When source is available, report match if method is found in the declaring type
    IResource resource = type.getResource();
    if (declaringClass instanceof ParameterizedTypeBinding)
        declaringClass = ((ParameterizedTypeBinding) declaringClass).genericType();
    ClassScope scope = ((SourceTypeBinding) declaringClass).scope;
    if (scope != null) {
        TypeDeclaration typeDecl = scope.referenceContext;
        AbstractMethodDeclaration methodDecl = typeDecl.declarationOf(methodBinding.original());
        if (methodDecl != null) {
            // Create method handle from method declaration
            String methodName = new String(methodBinding.selector);
            Argument[] arguments = methodDecl.arguments;
            int length = arguments == null ? 0 : arguments.length;
            String[] parameterTypes = new String[length];
            for (int i = 0; i < length; i++) {
                char[][] typeName = arguments[i].type.getParameterizedTypeName();
                parameterTypes[i] = Signature.createTypeSignature(CharOperation.concatWith(typeName, '.'),
                        false);
            }
            IMethod method = type.getMethod(methodName, parameterTypes);
            if (method == null || knownMethods.addIfNotIncluded(method) == null)
                return;

            // Create and report corresponding match
            int offset = methodDecl.sourceStart;
            this.match = new MethodDeclarationMatch(method, SearchMatch.A_ACCURATE, offset,
                    methodDecl.sourceEnd - offset + 1, locator.getParticipant(), resource);
            locator.report(this.match);
        }
    }
}

From source file:net.sf.j2s.core.builder.AbstractImageBuilder.java

License:Open Source License

protected CompilationParticipantResult[] notifyParticipants(SourceFile[] unitsAboutToCompile) {
    CompilationParticipantResult[] results = new CompilationParticipantResult[unitsAboutToCompile.length];
    for (int i = unitsAboutToCompile.length; --i >= 0;)
        results[i] = new CompilationParticipantResult(unitsAboutToCompile[i]);

    // TODO (kent) do we expect to have more than one participant?
    // and if so should we pass the generated files from the each processor to the others to process?
    // and what happens if some participants do not expect to be called with only a few files, after seeing 'all' the files?
    for (int i = 0, l = this.javaBuilder.participants.length; i < l; i++)
        this.javaBuilder.participants[i].buildStarting(results, this instanceof BatchImageBuilder);

    SimpleSet uniqueFiles = null;
    CompilationParticipantResult[] toAdd = null;
    int added = 0;
    for (int i = results.length; --i >= 0;) {
        CompilationParticipantResult result = results[i];
        if (result == null)
            continue;

        IFile[] deletedGeneratedFiles = result.deletedFiles;
        if (deletedGeneratedFiles != null)
            deleteGeneratedFiles(deletedGeneratedFiles);

        IFile[] addedGeneratedFiles = result.addedFiles;
        if (addedGeneratedFiles != null) {
            for (int j = addedGeneratedFiles.length; --j >= 0;) {
                SourceFile sourceFile = findSourceFile(addedGeneratedFiles[j], true);
                if (sourceFile == null)
                    continue;
                if (uniqueFiles == null) {
                    uniqueFiles = new SimpleSet(unitsAboutToCompile.length + 3);
                    for (int f = unitsAboutToCompile.length; --f >= 0;)
                        uniqueFiles.add(unitsAboutToCompile[f]);
                }/* w w w  .  j  a  v  a2  s  .  c om*/
                if (uniqueFiles.addIfNotIncluded(sourceFile) == sourceFile) {
                    CompilationParticipantResult newResult = new CompilationParticipantResult(sourceFile);
                    // is there enough room to add all the addedGeneratedFiles.length ?
                    if (toAdd == null) {
                        toAdd = new CompilationParticipantResult[addedGeneratedFiles.length];
                    } else {
                        int length = toAdd.length;
                        if (added == length)
                            System.arraycopy(toAdd, 0, toAdd = new CompilationParticipantResult[length
                                    + addedGeneratedFiles.length], 0, length);
                    }
                    toAdd[added++] = newResult;
                }
            }
        }
    }

    if (added > 0) {
        int length = results.length;
        System.arraycopy(results, 0, results = new CompilationParticipantResult[length + added], 0, length);
        System.arraycopy(toAdd, 0, results, length, added);
    }
    return results;
}

From source file:net.sf.j2s.core.builder.ClasspathJar.java

License:Open Source License

/**
 * Calculate and cache the package list available in the zipFile.
 * @param jar The ClasspathJar to use/* w  w w  .j ava  2s. co  m*/
 * @return A SimpleSet with the all the package names in the zipFile.
 */
static SimpleSet findPackageSet(ClasspathJar jar) {
    String zipFileName = jar.zipFilename;
    long lastModified = jar.lastModified();
    long fileSize = new File(zipFileName).length();
    PackageCacheEntry cacheEntry = (PackageCacheEntry) PackageCache.get(zipFileName);
    if (cacheEntry != null && cacheEntry.lastModified == lastModified && cacheEntry.fileSize == fileSize)
        return cacheEntry.packageSet;

    SimpleSet packageSet = new SimpleSet(41);
    packageSet.add(""); //$NON-NLS-1$
    nextEntry: for (Enumeration e = jar.zipFile.entries(); e.hasMoreElements();) {
        String fileName = ((ZipEntry) e.nextElement()).getName();

        // add the package name & all of its parent packages
        int last = fileName.lastIndexOf('/');
        while (last > 0) {
            // extract the package name
            String packageName = fileName.substring(0, last);
            if (packageSet.addIfNotIncluded(packageName) == null)
                continue nextEntry; // already existed
            last = packageName.lastIndexOf('/');
        }
    }

    PackageCache.put(zipFileName, new PackageCacheEntry(lastModified, fileSize, packageSet));
    return packageSet;
}