Example usage for org.eclipse.jdt.internal.core IJavaElementRequestor acceptType

List of usage examples for org.eclipse.jdt.internal.core IJavaElementRequestor acceptType

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.core IJavaElementRequestor acceptType.

Prototype

public void acceptType(IType type);

Source Link

Usage

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

License:Open Source License

/**
 * Performs type search in a binary package.
 *///from w  ww.  ja v a2s .com
protected void seekTypesInBinaryPackage(String name, IPackageFragment pkg, boolean partialMatch,
        int acceptFlags, IJavaElementRequestor requestor) {
    long start = -1;
    if (VERBOSE)
        start = System.currentTimeMillis();
    try {
        if (!partialMatch) {
            // exact match
            if (requestor.isCanceled())
                return;
            ClassFile classFile = new ClassFile((PackageFragment) pkg, manager, name);
            if (classFile.existsUsingJarTypeCache()) {
                IType type = classFile.getType();
                if (acceptType(type, acceptFlags, false/*not a source type*/)) {
                    requestor.acceptType(type);
                }
            }
        } else {
            IJavaElement[] classFiles = null;
            try {
                classFiles = pkg.getChildren();
            } catch (JavaModelException npe) {
                return; // the package is not present
            }
            int length = classFiles.length;
            String unqualifiedName = name;
            int index = name.lastIndexOf('$');
            if (index != -1) {
                //the type name of the inner type
                unqualifiedName = Util.localTypeName(name, index, name.length());
                // unqualifiedName is empty if the name ends with a '$' sign.
                // See http://dev.eclipse.org/bugs/show_bug.cgi?id=14642
            }
            int matchLength = name.length();
            for (int i = 0; i < length; i++) {
                if (requestor.isCanceled())
                    return;
                IJavaElement classFile = classFiles[i];
                // MatchName will never have the extension ".class" and the elementName always will.
                String elementName = classFile.getElementName();
                if (elementName.regionMatches(true /*ignore case*/, 0, name, 0, matchLength)) {
                    IType type = ((ClassFile) classFile).getType();
                    String typeName = type.getElementName();
                    if (typeName.length() > 0 && !Character.isDigit(typeName.charAt(0))) { //not an anonymous type
                        if (nameMatches(unqualifiedName, type, true/*partial match*/)
                                && acceptType(type, acceptFlags, false/*not a source type*/))
                            requestor.acceptType(type);
                    }
                }
            }
        }
    } finally {
        if (VERBOSE)
            this.timeSpentInSeekTypesInBinaryPackage += System.currentTimeMillis() - start;
    }
}

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

License:Open Source License

/**
 * Performs type search in a source package.
 *//*from w  w  w  .  j  a  va  2  s .c o  m*/
protected void seekTypesInSourcePackage(String name, IPackageFragment pkg, int firstDot, boolean partialMatch,
        String topLevelTypeName, int acceptFlags, IJavaElementRequestor requestor) {

    long start = -1;
    if (VERBOSE)
        start = System.currentTimeMillis();
    try {
        if (!partialMatch) {
            try {
                IJavaElement[] compilationUnits = pkg.getChildren();
                for (int i = 0, length = compilationUnits.length; i < length; i++) {
                    if (requestor.isCanceled())
                        return;
                    IJavaElement cu = compilationUnits[i];
                    String cuName = cu.getElementName();
                    int lastDot = cuName.lastIndexOf('.');
                    if (lastDot != topLevelTypeName.length()
                            || !topLevelTypeName.regionMatches(0, cuName, 0, lastDot))
                        continue;

                    // https://bugs.eclipse.org/bugs/show_bug.cgi?id=351697
                    // If we are looking at source location, just ignore binary types
                    if (!(cu instanceof ICompilationUnit))
                        continue;
                    IType type = ((ICompilationUnit) cu).getType(topLevelTypeName);
                    type = getMemberType(type, name, firstDot);
                    if (acceptType(type, acceptFlags, true/*a source type*/)) { // accept type checks for existence
                        requestor.acceptType(type);
                        break; // since an exact match was requested, no other matching type can exist
                    }
                }
            } catch (JavaModelException e) {
                // package doesn't exist -> ignore
            }
        } else {
            try {
                String cuPrefix = firstDot == -1 ? name : name.substring(0, firstDot);
                IJavaElement[] compilationUnits = pkg.getChildren();
                for (int i = 0, length = compilationUnits.length; i < length; i++) {
                    if (requestor.isCanceled())
                        return;
                    IJavaElement cu = compilationUnits[i];
                    if (!cu.getElementName().toLowerCase().startsWith(cuPrefix))
                        continue;
                    try {
                        IType[] types = ((ICompilationUnit) cu).getTypes();
                        for (int j = 0, typeLength = types.length; j < typeLength; j++)
                            seekTypesInTopLevelType(name, firstDot, types[j], requestor, acceptFlags);
                    } catch (JavaModelException e) {
                        // cu doesn't exist -> ignore
                    }
                }
            } catch (JavaModelException e) {
                // package doesn't exist -> ignore
            }
        }
    } finally {
        if (VERBOSE)
            this.timeSpentInSeekTypesInSourcePackage += System.currentTimeMillis() - start;
    }
}

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

License:Open Source License

protected boolean seekTypesInTopLevelType(String prefix, int firstDot, IType topLevelType,
        IJavaElementRequestor requestor, int acceptFlags) {
    if (!topLevelType.getElementName().toLowerCase().startsWith(prefix))
        return false;
    if (firstDot == -1) {
        if (acceptType(topLevelType, acceptFlags, true/*a source type*/)) {
            requestor.acceptType(topLevelType);
            return true;
        }//from w w  w  . j ava2 s . co m
    } else {
        return seekTypesInType(prefix, firstDot, topLevelType, requestor, acceptFlags);
    }
    return false;
}

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

License:Open Source License

protected boolean seekTypesInWorkingCopies(String name, IPackageFragment pkg, int firstDot,
        boolean partialMatch, String topLevelTypeName, int acceptFlags, IJavaElementRequestor requestor,
        boolean considerSecondaryTypes) {

    if (!partialMatch) {
        HashMap typeMap = (HashMap) (this.typesInWorkingCopies == null ? null
                : this.typesInWorkingCopies.get(pkg));
        if (typeMap != null) {
            Object object = typeMap.get(topLevelTypeName);
            if (object instanceof IType) {
                IType type = getMemberType((IType) object, name, firstDot);
                if (!considerSecondaryTypes && !isPrimaryType(name, (IType) object, false))
                    return false;
                if (acceptType(type, acceptFlags, true/*a source type*/)) {
                    requestor.acceptType(type);
                    return true; // don't continue with compilation unit
                }//  w  ww. j a v  a2s .c  o  m
            } else if (object instanceof IType[]) {
                if (object == NO_TYPES) {
                    // all types where deleted -> type is hidden, OR it is the fake type package-info
                    String packageInfoName = String.valueOf(TypeConstants.PACKAGE_INFO_NAME);
                    if (packageInfoName.equals(name))
                        requestor.acceptType(pkg.getCompilationUnit(packageInfoName.concat(SUFFIX_STRING_java))
                                .getType(name));
                    return true;
                }
                IType[] topLevelTypes = (IType[]) object;
                for (int i = 0, length = topLevelTypes.length; i < length; i++) {
                    if (requestor.isCanceled())
                        return false;
                    IType type = getMemberType(topLevelTypes[i], name, firstDot);
                    if (acceptType(type, acceptFlags, true/*a source type*/)) {
                        requestor.acceptType(type);
                        return true; // return the first one
                    }
                }
            }
        }
    } else {
        HashMap typeMap = (HashMap) (this.typesInWorkingCopies == null ? null
                : this.typesInWorkingCopies.get(pkg));
        if (typeMap != null) {
            Iterator iterator = typeMap.values().iterator();
            while (iterator.hasNext()) {
                if (requestor.isCanceled())
                    return false;
                Object object = iterator.next();
                if (object instanceof IType) {
                    if (!considerSecondaryTypes && !isPrimaryType(name, (IType) object, true))
                        continue;
                    seekTypesInTopLevelType(name, firstDot, (IType) object, requestor, acceptFlags);
                } else if (object instanceof IType[]) {
                    IType[] topLevelTypes = (IType[]) object;
                    for (int i = 0, length = topLevelTypes.length; i < length; i++)
                        seekTypesInTopLevelType(name, firstDot, topLevelTypes[i], requestor, acceptFlags);
                }
            }
        }
    }
    return false;
}

From source file:org.codehaus.groovy.eclipse.core.builder.GroovyNameLookup.java

License:Apache License

/**
 * Copied from parent class//from  ww w. ja  v a2  s  .  c  o  m
 * Changes marked with // GROOVY begin and // GROOVY end
 */
@Override
protected void seekTypesInSourcePackage(String name, IPackageFragment pkg, int firstDot, boolean partialMatch,
        String topLevelTypeName, int acceptFlags, IJavaElementRequestor requestor) {

    long start = -1;
    if (VERBOSE)
        start = System.currentTimeMillis();
    try {
        if (!partialMatch) {
            try {
                IJavaElement[] compilationUnits = pkg.getChildren();
                for (int i = 0, length = compilationUnits.length; i < length; i++) {
                    if (requestor.isCanceled())
                        return;
                    // GROOVY begin
                    // removed statements that continue if type is not the same name as the compilation unit
                    ICompilationUnit cu = (ICompilationUnit) compilationUnits[i];
                    IType[] allTypes = cu.getAllTypes();
                    IType type = cu.getType(name);
                    if (
                    // GROOVY begin
                    type.exists() &&
                    // GROOVY end
                            acceptType(type, acceptFlags, true/*a source type*/)) { // accept type checks for existence
                        requestor.acceptType(type);
                        break; // since an exact match was requested, no other matching type can exist
                    }

                    // now look for member types

                    String mainType = cu.getElementName();
                    int dotIndex = mainType.indexOf('.');
                    mainType = mainType.substring(0, dotIndex);
                    type = cu.getType(mainType);
                    if (type.exists()) {
                        type = getMemberType(type, name, firstDot);
                        if (
                        // GROOVY begin
                        type.exists() &&
                        // GROOVY end
                                acceptType(type, acceptFlags, true/*a source type*/)) { // accept type checks for existence
                            requestor.acceptType(type);
                            break; // since an exact match was requested, no other matching type can exist
                        }
                    }
                    // GROOVY end
                }
            } catch (JavaModelException e) {
                // package doesn't exist -> ignore
            }
        } else {
            try {
                String cuPrefix = firstDot == -1 ? name : name.substring(0, firstDot);
                IJavaElement[] compilationUnits = pkg.getChildren();
                for (int i = 0, length = compilationUnits.length; i < length; i++) {
                    if (requestor.isCanceled())
                        return;
                    IJavaElement cu = compilationUnits[i];
                    if (!cu.getElementName().toLowerCase().startsWith(cuPrefix))
                        continue;
                    try {
                        IType[] types = ((ICompilationUnit) cu).getTypes();
                        for (int j = 0, typeLength = types.length; j < typeLength; j++)
                            seekTypesInTopLevelType(name, firstDot, types[j], requestor, acceptFlags);
                    } catch (JavaModelException e) {
                        // cu doesn't exist -> ignore
                    }
                }
            } catch (JavaModelException e) {
                // package doesn't exist -> ignore
            }
        }
    } finally {
        if (VERBOSE)
            this.timeSpentInSeekTypesInSourcePackage += System.currentTimeMillis() - start;
    }
}

From source file:org.codehaus.groovy.eclipse.core.builder.GroovyNameLookup.java

License:Apache License

/**
 * Copied from parent class/*from  ww  w.j  ava 2  s .c  om*/
 * Changes marked with // GROOVY begin and // GROOVY end
 */
@Override
protected void seekTypesInBinaryPackage(String name, IPackageFragment pkg, boolean partialMatch,
        int acceptFlags, IJavaElementRequestor requestor) {
    long start = -1;
    if (VERBOSE)
        start = System.currentTimeMillis();
    try {
        // GROOVY begin
        // ensure ends with .class
        if (!name.endsWith(".class")) {
            name += ".class";
        }
        // GROOVY end
        if (!partialMatch) {
            // exact match
            if (requestor.isCanceled())
                return;
            ClassFile classFile = (ClassFile) pkg.getClassFile(name);
            if (classFile.existsUsingJarTypeCache()) {
                IType type = classFile.getType();
                if (acceptType(type, acceptFlags, false/*not a source type*/)) {
                    requestor.acceptType(type);
                }
            }

            // GROOVY begin
            // class file may still exist as an inner type
            IJavaElement[] classFiles = null;
            try {
                classFiles = pkg.getChildren();
            } catch (JavaModelException npe) {
                return; // the package is not present
            }
            for (IJavaElement elt : classFiles) {
                classFile = (ClassFile) elt;
                if (classFile.getElementName().endsWith("$" + name)) {
                    IType type = classFile.getType();
                    if (acceptType(type, acceptFlags, false/*not a source type*/)) {
                        requestor.acceptType(type);
                    }
                }
            }
            // GROOVY end

        } else {
            IJavaElement[] classFiles = null;
            try {
                classFiles = pkg.getChildren();
            } catch (JavaModelException npe) {
                return; // the package is not present
            }
            int length = classFiles.length;
            String unqualifiedName = name;
            int index = name.lastIndexOf('$');
            if (index != -1) {
                //the type name of the inner type
                unqualifiedName = Util.localTypeName(name, index, name.length());
                // unqualifiedName is empty if the name ends with a '$' sign.
                // See http://dev.eclipse.org/bugs/show_bug.cgi?id=14642
            }
            int matchLength = name.length();
            for (int i = 0; i < length; i++) {
                if (requestor.isCanceled())
                    return;
                IJavaElement classFile = classFiles[i];
                // MatchName will never have the extension ".class" and the elementName always will.
                String elementName = classFile.getElementName();
                if (elementName.regionMatches(true /*ignore case*/, 0, name, 0, matchLength)) {
                    IType type = ((ClassFile) classFile).getType();
                    String typeName = type.getElementName();
                    if (typeName.length() > 0 && !Character.isDigit(typeName.charAt(0))) { //not an anonymous type
                        if (nameMatches(unqualifiedName, type, true/*partial match*/)
                                && acceptType(type, acceptFlags, false/*not a source type*/))
                            requestor.acceptType(type);
                    }
                }
            }
        }
    } finally {
        if (VERBOSE)
            this.timeSpentInSeekTypesInBinaryPackage += System.currentTimeMillis() - start;
    }
}