Example usage for org.eclipse.jdt.internal.core.util Util splitOn

List of usage examples for org.eclipse.jdt.internal.core.util Util splitOn

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.core.util Util splitOn.

Prototype

public static final String[] splitOn(char divider, String string, int start, int end) 

Source Link

Document

Return a new array which is the split of the given string using the given divider.

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/*from   w  w  w  . j  a  va 2s.c  o  m*/
 *         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.builder.ClasspathJar.java

License:Open Source License

@Override
public void findPackages(String[] name, ISearchRequestor requestor) {
    SimpleSet knownPackageNames = getKnownPackages();
    for (Object value : knownPackageNames.values) {
        if (value == null) {
            continue;
        }/*w  ww.j  a  v  a2 s  . c  o  m*/
        String pkg = value.toString();
        String[] pkgName = Util.splitOn('/', pkg, 0, pkg.length());

        if (pkgName != null && Util.startsWithIgnoreCase(pkgName, name, true)) {
            requestor.acceptPackage(Util.concatWith(pkgName, '.').toCharArray());
        }

    }
}

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

License:Open Source License

private void initRawPackageInfo(HashtableOfArrayToObject rawPackageInfo, String entryName, boolean isDirectory,
        String compliance) {/*from  www .j  av  a  2 s.  co m*/
    int lastSeparator = isDirectory ? entryName.length() - 1 : entryName.lastIndexOf('/');
    String[] pkgName = Util.splitOn('/', entryName, 0, lastSeparator);
    String[] existing = null;
    int length = pkgName.length;
    int existingLength = length;
    while (existingLength >= 0) {
        existing = (String[]) rawPackageInfo.getKey(pkgName, existingLength);
        if (existing != null)
            break;
        existingLength--;
    }
    //        JavaModelManager manager = JavaModelManager.getJavaModelManager();
    for (int i = existingLength; i < length; i++) {
        // sourceLevel must be null because we know nothing about it based on a jar file
        if (Util.isValidFolderNameForPackage(pkgName[i], null, compliance)) {
            System.arraycopy(existing, 0, existing = new String[i + 1], 0, i);
            existing[i] = manager.intern(pkgName[i]);
            rawPackageInfo.put(existing, new ArrayList[] { EMPTY_LIST, EMPTY_LIST });
        } else {
            // non-Java resource folder
            if (!isDirectory) {
                ArrayList[] children = (ArrayList[]) rawPackageInfo.get(existing);
                if (children[1/*NON_JAVA*/] == EMPTY_LIST)
                    children[1/*NON_JAVA*/] = new ArrayList();
                children[1/*NON_JAVA*/].add(entryName);
            }
            return;
        }
    }
    if (isDirectory)
        return;

    // add classfile info amongst children
    ArrayList[] children = (ArrayList[]) rawPackageInfo.get(pkgName);
    if (org.eclipse.jdt.internal.compiler.util.Util.isClassFileName(entryName)) {
        if (children[0/*JAVA*/] == EMPTY_LIST)
            children[0/*JAVA*/] = new ArrayList();
        String nameWithoutExtension = entryName.substring(lastSeparator + 1, entryName.length() - 6);
        children[0/*JAVA*/].add(nameWithoutExtension);
    } else {
        if (children[1/*NON_JAVA*/] == EMPTY_LIST)
            children[1/*NON_JAVA*/] = new ArrayList();
        children[1/*NON_JAVA*/].add(entryName);
    }

}

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

License:Open Source License

/**
 * Returns the <code>ICompilationUnit</code> which defines the type
 * named <code>qualifiedTypeName</code>, or <code>null</code> if
 * none exists. The domain of the search is bounded by the classpath
 * of the <code>IJavaProject</code> this <code>NameLookup</code> was
 * obtained from./* ww  w. j ava 2s . co  m*/
 * <p/>
 * The name must be fully qualified (eg "java.lang.Object", "java.util.Hashtable$Entry")
 */
public ICompilationUnit findCompilationUnit(String qualifiedTypeName) {
    String[] pkgName = CharOperation.NO_STRINGS;
    String cuName = qualifiedTypeName;

    int index = qualifiedTypeName.lastIndexOf('.');
    if (index != -1) {
        pkgName = Util.splitOn('.', qualifiedTypeName, 0, index);
        cuName = qualifiedTypeName.substring(index + 1);
    }
    index = cuName.indexOf('$');
    if (index != -1) {
        cuName = cuName.substring(0, index);
    }
    int pkgIndex = this.packageFragments.getIndex(pkgName);
    if (pkgIndex != -1) {
        Object value = this.packageFragments.valueTable[pkgIndex];
        // reuse existing String[]
        pkgName = (String[]) this.packageFragments.keyTable[pkgIndex];
        if (value instanceof PackageFragmentRoot) {
            return findCompilationUnit(pkgName, cuName, (PackageFragmentRoot) value);
        } else {
            IPackageFragmentRoot[] roots = (IPackageFragmentRoot[]) value;
            for (int i = 0; i < roots.length; i++) {
                PackageFragmentRoot root = (PackageFragmentRoot) roots[i];
                ICompilationUnit cu = findCompilationUnit(pkgName, cuName, root);
                if (cu != null)
                    return cu;
            }
        }
    }
    return null;
}

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

License:Open Source License

/**
 * Returns the package fragments whose name matches the given
 * (qualified) name or pattern, or <code>null</code> if none exist.
 * <p/>/*  w ww . ja v  a 2s.  co  m*/
 * The name can be:
 * <ul>
 * <li>empty: ""</li>
 * <li>qualified: "pack.pack1.pack2"</li>
 * <li>a pattern: "pack.*.util"</li>
 * </ul>
 *
 * @param partialMatch
 *         partial name matches qualify when <code>true</code>,
 * @param patternMatch
 *         <code>true</code> when the given name might be a pattern,
 *         <code>false</code> otherwise.
 */
public IPackageFragment[] findPackageFragments(String name, boolean partialMatch, boolean patternMatch) {
    boolean isStarPattern = name.equals("*"); //$NON-NLS-1$
    boolean hasPatternChars = isStarPattern
            || (patternMatch && (name.indexOf('*') >= 0 || name.indexOf('?') >= 0));
    if (partialMatch || hasPatternChars) {
        String[] splittedName = Util.splitOn('.', name, 0, name.length());
        IPackageFragment[] oneFragment = null;
        ArrayList pkgs = null;
        char[] lowercaseName = hasPatternChars && !isStarPattern ? name.toLowerCase().toCharArray() : null;
        Object[][] keys = this.packageFragments.keyTable;
        for (int i = 0, length = keys.length; i < length; i++) {
            String[] pkgName = (String[]) keys[i];
            if (pkgName != null) {
                boolean match = isStarPattern || (hasPatternChars
                        ? CharOperation.match(lowercaseName, Util.concatCompoundNameToCharArray(pkgName), false)
                        : Util.startsWithIgnoreCase(pkgName, splittedName, partialMatch));
                if (match) {
                    Object value = this.packageFragments.valueTable[i];
                    if (value instanceof PackageFragmentRoot) {
                        IPackageFragment pkg = ((PackageFragmentRoot) value).getPackageFragment(pkgName);
                        if (oneFragment == null) {
                            oneFragment = new IPackageFragment[] { pkg };
                        } else {
                            if (pkgs == null) {
                                pkgs = new ArrayList();
                                pkgs.add(oneFragment[0]);
                            }
                            pkgs.add(pkg);
                        }
                    } else {
                        IPackageFragmentRoot[] roots = (IPackageFragmentRoot[]) value;
                        for (int j = 0, length2 = roots.length; j < length2; j++) {
                            PackageFragmentRoot root = (PackageFragmentRoot) roots[j];
                            IPackageFragment pkg = root.getPackageFragment(pkgName);
                            if (oneFragment == null) {
                                oneFragment = new IPackageFragment[] { pkg };
                            } else {
                                if (pkgs == null) {
                                    pkgs = new ArrayList();
                                    pkgs.add(oneFragment[0]);
                                }
                                pkgs.add(pkg);
                            }
                        }
                    }
                }
            }
        }
        if (pkgs == null)
            return oneFragment;
        int resultLength = pkgs.size();
        IPackageFragment[] result = new IPackageFragment[resultLength];
        pkgs.toArray(result);
        return result;
    } else {
        String[] splittedName = Util.splitOn('.', name, 0, name.length());
        int pkgIndex = this.packageFragments.getIndex(splittedName);
        if (pkgIndex == -1)
            return null;
        Object value = this.packageFragments.valueTable[pkgIndex];
        // reuse existing String[]
        String[] pkgName = (String[]) this.packageFragments.keyTable[pkgIndex];
        if (value instanceof PackageFragmentRoot) {
            return new IPackageFragment[] { ((PackageFragmentRoot) value).getPackageFragment(pkgName) };
        } else {
            IPackageFragmentRoot[] roots = (IPackageFragmentRoot[]) value;
            IPackageFragment[] result = new IPackageFragment[roots.length];
            for (int i = 0; i < roots.length; i++) {
                result[i] = ((PackageFragmentRoot) roots[i]).getPackageFragment(pkgName);
            }
            return result;
        }
    }
}

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

License:Open Source License

/**
 * Notifies the given requestor of all package fragments with the
 * given name. Checks the requestor at regular intervals to see if the
 * requestor has canceled. The domain of
 * the search is bounded by the <code>IJavaProject</code>
 * this <code>NameLookup</code> was obtained from.
 *
 * @param partialMatch/*from  w w w .  j  av  a 2s .  co  m*/
 *         partial name matches qualify when <code>true</code>;
 *         only exact name matches qualify when <code>false</code>
 */
public void seekPackageFragments(String name, boolean partialMatch, IJavaElementRequestor requestor) {
    /*      if (VERBOSE) {
    Util.verbose(" SEEKING PACKAGE FRAGMENTS");  //$NON-NLS-1$
             Util.verbose(" -> name: " + name);  //$NON-NLS-1$
             Util.verbose(" -> partial match:" + partialMatch);  //$NON-NLS-1$
          }
    */
    if (partialMatch) {
        String[] splittedName = Util.splitOn('.', name, 0, name.length());
        Object[][] keys = this.packageFragments.keyTable;
        for (int i = 0, length = keys.length; i < length; i++) {
            if (requestor.isCanceled())
                return;
            String[] pkgName = (String[]) keys[i];
            if (pkgName != null && Util.startsWithIgnoreCase(pkgName, splittedName, partialMatch)) {
                Object value = this.packageFragments.valueTable[i];
                if (value instanceof PackageFragmentRoot) {
                    PackageFragmentRoot root = (PackageFragmentRoot) value;
                    requestor.acceptPackageFragment(root.getPackageFragment(pkgName));
                } else {
                    IPackageFragmentRoot[] roots = (IPackageFragmentRoot[]) value;
                    for (int j = 0, length2 = roots.length; j < length2; j++) {
                        if (requestor.isCanceled())
                            return;
                        PackageFragmentRoot root = (PackageFragmentRoot) roots[j];
                        requestor.acceptPackageFragment(root.getPackageFragment(pkgName));
                    }
                }
            }
        }
    } else {
        String[] splittedName = Util.splitOn('.', name, 0, name.length());
        int pkgIndex = this.packageFragments.getIndex(splittedName);
        if (pkgIndex != -1) {
            Object value = this.packageFragments.valueTable[pkgIndex];
            // reuse existing String[]
            String[] pkgName = (String[]) this.packageFragments.keyTable[pkgIndex];
            if (value instanceof PackageFragmentRoot) {
                requestor.acceptPackageFragment(((PackageFragmentRoot) value).getPackageFragment(pkgName));
            } else {
                IPackageFragmentRoot[] roots = (IPackageFragmentRoot[]) value;
                if (roots != null) {
                    for (int i = 0, length = roots.length; i < length; i++) {
                        if (requestor.isCanceled())
                            return;
                        PackageFragmentRoot root = (PackageFragmentRoot) roots[i];
                        requestor.acceptPackageFragment(root.getPackageFragment(pkgName));
                    }
                }
            }
        }
    }
}

From source file:org.eclipse.che.jdt.internal.compiler.parser.SourceTypeConverter.java

License:Open Source License

private CompilationUnitDeclaration convert(ISourceType[] sourceTypes, CompilationResult compilationResult)
        throws JavaModelException {
    this.unit = new CompilationUnitDeclaration(this.problemReporter, compilationResult, 0);
    // not filled at this point

    if (sourceTypes.length == 0)
        return this.unit;
    SourceTypeElementInfo topLevelTypeInfo = (SourceTypeElementInfo) sourceTypes[0];
    org.eclipse.jdt.core.ICompilationUnit cuHandle = topLevelTypeInfo.getHandle().getCompilationUnit();
    this.cu = (ICompilationUnit) cuHandle;

    final CompilationUnitElementInfo compilationUnitElementInfo = (CompilationUnitElementInfo) ((JavaElement) this.cu)
            .getElementInfo();/*  w w w  . j  av  a2  s .  c o  m*/
    if (this.has1_5Compliance
            && (compilationUnitElementInfo.annotationNumber >= CompilationUnitElementInfo.ANNOTATION_THRESHOLD_FOR_DIET_PARSE
                    || (compilationUnitElementInfo.hasFunctionalTypes && (this.flags & LOCAL_TYPE) != 0))) {
        // If more than 10 annotations, diet parse as this is faster, but not if
        // the client wants local and anonymous types to be converted (https://bugs.eclipse.org/bugs/show_bug.cgi?id=254738)
        // Also see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=405843
        if ((this.flags & LOCAL_TYPE) == 0) {
            return new Parser(this.problemReporter, true).dietParse(this.cu, compilationResult);
        } else {
            return new Parser(this.problemReporter, true).parse(this.cu, compilationResult);
        }
    }

    /* only positions available */
    int start = topLevelTypeInfo.getNameSourceStart();
    int end = topLevelTypeInfo.getNameSourceEnd();

    /* convert package and imports */
    String[] packageName = ((PackageFragment) cuHandle.getParent()).names;
    if (packageName.length > 0)
        // if its null then it is defined in the default package
        this.unit.currentPackage = createImportReference(packageName, start, end, false,
                ClassFileConstants.AccDefault);
    IImportDeclaration[] importDeclarations = topLevelTypeInfo.getHandle().getCompilationUnit().getImports();
    int importCount = importDeclarations.length;
    this.unit.imports = new ImportReference[importCount];
    for (int i = 0; i < importCount; i++) {
        ImportDeclaration importDeclaration = (ImportDeclaration) importDeclarations[i];
        ISourceImport sourceImport = (ISourceImport) importDeclaration.getElementInfo();
        String nameWithoutStar = importDeclaration.getNameWithoutStar();
        this.unit.imports[i] = createImportReference(
                Util.splitOn('.', nameWithoutStar, 0, nameWithoutStar.length()),
                sourceImport.getDeclarationSourceStart(), sourceImport.getDeclarationSourceEnd(),
                importDeclaration.isOnDemand(), sourceImport.getModifiers());
    }
    /* convert type(s) */
    try {
        int typeCount = sourceTypes.length;
        final TypeDeclaration[] types = new TypeDeclaration[typeCount];
        /*
         * We used a temporary types collection to prevent this.unit.types from being null during a call to
         * convert(...) when the source is syntactically incorrect and the parser is flushing the unit's types.
         * See https://bugs.eclipse.org/bugs/show_bug.cgi?id=97466
         */
        for (int i = 0; i < typeCount; i++) {
            SourceTypeElementInfo typeInfo = (SourceTypeElementInfo) sourceTypes[i];
            types[i] = convert((SourceType) typeInfo.getHandle(), compilationResult);
        }
        this.unit.types = types;
        return this.unit;
    } catch (AnonymousMemberFound e) {
        return new Parser(this.problemReporter, true).parse(this.cu, compilationResult);
    }
}

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

License:Open Source License

/**
 * Find the packages that start with the given prefix.
 * A valid prefix is a qualified name separated by periods
 * (ex. java.util).//from  ww  w. j  ava 2s .c o m
 * The packages found are passed to:
 *    ISearchRequestor.acceptPackage(char[][] packageName)
 */
public void findPackages(char[] prefix, ISearchRequestor requestor) {
    String name = new String(prefix);
    String[] splittedName = Util.splitOn('.', name, 0, name.length());
    for (CodenvyClasspathLocation location : this.locations) {
        location.findPackages(splittedName, requestor);
    }
}

From source file:org.eclipse.jdt.internal.compiler.parser.SourceTypeConverter.java

License:Open Source License

private CompilationUnitDeclaration convert(ISourceType[] sourceTypes, CompilationResult compilationResult)
        throws JavaModelException {
    // GROOVY start
    /* old {/*  w w  w  .  j  a  va 2 s.  c  o m*/
    this.unit = new CompilationUnitDeclaration(this.problemReporter, compilationResult, 0);
    } new */
    this.unit = LanguageSupportFactory.newCompilationUnitDeclaration(
            (ICompilationUnit) ((SourceTypeElementInfo) sourceTypes[0]).getHandle().getCompilationUnit(),
            this.problemReporter, compilationResult, 0);
    // GROOVY end

    // not filled at this point

    if (sourceTypes.length == 0)
        return this.unit;
    SourceTypeElementInfo topLevelTypeInfo = (SourceTypeElementInfo) sourceTypes[0];
    org.eclipse.jdt.core.ICompilationUnit cuHandle = topLevelTypeInfo.getHandle().getCompilationUnit();
    this.cu = (ICompilationUnit) cuHandle;

    // GROOVY start
    // trying to avoid building an incorrect TypeDeclaration below (when it should be a GroovyTypeDeclaration).
    // similar to code below that creates the Parser and calls dietParse
    // FIXASC think about doing the necessary rewrite below rather than this - does it make things too slow?

    //      final boolean isInterestingProject = LanguageSupportFactory.isInterestingProject(compilationResult.getCompilationUnit().getjavaBuilder.getProject());
    // GROOVY should be 'true' here?
    if (LanguageSupportFactory.isInterestingSourceFile(new String(compilationResult.getFileName()))) {
        try {
            return LanguageSupportFactory
                    .getParser(this, this.problemReporter.options, this.problemReporter, true, 3)
                    .dietParse(this.cu, compilationResult);
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }
    // GROOVY end

    if (this.has1_5Compliance
            && ((CompilationUnitElementInfo) ((JavaElement) this.cu).getElementInfo()).annotationNumber > 10) { // experimental value
        // If more than 10 annotations, diet parse as this is faster, but not if
        // the client wants local and anonymous types to be converted (https://bugs.eclipse.org/bugs/show_bug.cgi?id=254738) 
        if ((this.flags & LOCAL_TYPE) == 0) {
            return new Parser(this.problemReporter, true).dietParse(this.cu, compilationResult);
        }
    }

    /* only positions available */
    int start = topLevelTypeInfo.getNameSourceStart();
    int end = topLevelTypeInfo.getNameSourceEnd();

    /* convert package and imports */
    String[] packageName = ((PackageFragment) cuHandle.getParent()).names;
    if (packageName.length > 0)
        // if its null then it is defined in the default package
        this.unit.currentPackage = createImportReference(packageName, start, end, false,
                ClassFileConstants.AccDefault);
    IImportDeclaration[] importDeclarations = topLevelTypeInfo.getHandle().getCompilationUnit().getImports();
    int importCount = importDeclarations.length;
    this.unit.imports = new ImportReference[importCount];
    for (int i = 0; i < importCount; i++) {
        ImportDeclaration importDeclaration = (ImportDeclaration) importDeclarations[i];
        ISourceImport sourceImport = (ISourceImport) importDeclaration.getElementInfo();
        String nameWithoutStar = importDeclaration.getNameWithoutStar();
        this.unit.imports[i] = createImportReference(
                Util.splitOn('.', nameWithoutStar, 0, nameWithoutStar.length()),
                sourceImport.getDeclarationSourceStart(), sourceImport.getDeclarationSourceEnd(),
                importDeclaration.isOnDemand(), sourceImport.getModifiers());
    }
    /* convert type(s) */
    try {
        int typeCount = sourceTypes.length;
        final TypeDeclaration[] types = new TypeDeclaration[typeCount];
        /*
         * We used a temporary types collection to prevent this.unit.types from being null during a call to
         * convert(...) when the source is syntactically incorrect and the parser is flushing the unit's types.
         * See https://bugs.eclipse.org/bugs/show_bug.cgi?id=97466
         */
        for (int i = 0; i < typeCount; i++) {
            SourceTypeElementInfo typeInfo = (SourceTypeElementInfo) sourceTypes[i];
            types[i] = convert((SourceType) typeInfo.getHandle(), compilationResult);
        }
        this.unit.types = types;
        return this.unit;
    } catch (AnonymousMemberFound e) {
        return new Parser(this.problemReporter, true).parse(this.cu, compilationResult);
    }
}