Example usage for org.eclipse.jdt.core.compiler CharOperation concatWith

List of usage examples for org.eclipse.jdt.core.compiler CharOperation concatWith

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.compiler CharOperation concatWith.

Prototype

public static final char[] concatWith(char[][] array, char separator) 

Source Link

Document

Answers the concatenation of the given array parts using the given separator between each part.

Usage

From source file:com.codenvy.ide.ext.java.server.core.search.SearchPattern.java

License:Open Source License

/**
 * Returns a search pattern based on a given Java element.
 * The pattern is used to trigger the appropriate search, and can be parameterized as follows:
 *
 * @param element the Java element the search pattern is based on
 * @param limitTo determines the nature of the expected matches
 *   <ul>//from w  ww .  j  a va2  s.c  o m
 *    <li>{@link IJavaSearchConstants#DECLARATIONS DECLARATIONS}: will search declarations matching
 *          with the corresponding element. In case the element is a method, declarations of matching
 *          methods in sub-types will also be found, allowing to find declarations of abstract methods, etc.
 *            Some additional flags may be specified while searching declaration:
 *            <ul>
 *               <li>{@link IJavaSearchConstants#IGNORE_DECLARING_TYPE IGNORE_DECLARING_TYPE}: declaring type will be ignored
 *                     during the search.<br>
 *                     For example using following test case:
 *               <pre>
 *                  class A { A method() { return null; } }
 *                  class B extends A { B method() { return null; } }
 *                  class C { A method() { return null; } }
 *               </pre>
 *                     search for <code>method</code> declaration with this flag
 *                     will return 2 matches: in A and in C
 *               </li>
 *               <li>{@link IJavaSearchConstants#IGNORE_RETURN_TYPE IGNORE_RETURN_TYPE}: return type will be ignored
 *                     during the search.<br>
 *                     Using same example, search for <code>method</code> declaration with this flag
 *                     will return 2 matches: in A and in B.
 *               </li>
 *            </ul>
 *            Note that these two flags may be combined and both declaring and return types can be ignored
 *            during the search. Then, using same example, search for <code>method</code> declaration
 *            with these 2 flags will return 3 matches: in A, in B  and in C
 *    </li>
 *       <li>{@link IJavaSearchConstants#REFERENCES REFERENCES}: will search references to the given element.</li>
 *       <li>{@link IJavaSearchConstants#ALL_OCCURRENCES ALL_OCCURRENCES}: will search for either declarations or
 *            references as specified above.
 *      </li>
 *       <li>All other fine grain constants defined in the <b>limitTo</b> category
 *            of the {@link IJavaSearchConstants} are also accepted nature:
 *          <table border=0>
 *              <tr>
 *               <th align=left>Fine grain constant
 *               <th align=left>Meaning
 *              <tr>
 *               <td>{@link IJavaSearchConstants#FIELD_DECLARATION_TYPE_REFERENCE FIELD_DECLARATION_TYPE_REFERENCE}
 *               <td>Return only type references used as the type of a field declaration.
 *              <tr>
 *               <td>{@link IJavaSearchConstants#LOCAL_VARIABLE_DECLARATION_TYPE_REFERENCE LOCAL_VARIABLE_DECLARATION_TYPE_REFERENCE}
 *               <td>Return only type references used as the type of a local variable declaration.
 *              <tr>
 *               <td>{@link IJavaSearchConstants#PARAMETER_DECLARATION_TYPE_REFERENCE PARAMETER_DECLARATION_TYPE_REFERENCE}
 *               <td>Return only type references used as the type of a method parameter declaration.
 *              <tr>
 *               <td>{@link IJavaSearchConstants#SUPERTYPE_TYPE_REFERENCE SUPERTYPE_TYPE_REFERENCE}
 *               <td>Return only type references used as a super type or as a super interface.
 *              <tr>
 *               <td>{@link IJavaSearchConstants#THROWS_CLAUSE_TYPE_REFERENCE THROWS_CLAUSE_TYPE_REFERENCE}
 *               <td>Return only type references used in a throws clause.
 *              <tr>
 *               <td>{@link IJavaSearchConstants#CAST_TYPE_REFERENCE CAST_TYPE_REFERENCE}
 *               <td>Return only type references used in a cast expression.
 *              <tr>
 *               <td>{@link IJavaSearchConstants#CATCH_TYPE_REFERENCE CATCH_TYPE_REFERENCE}
 *               <td>Return only type references used in a catch header.
 *              <tr>
 *               <td>{@link IJavaSearchConstants#CLASS_INSTANCE_CREATION_TYPE_REFERENCE CLASS_INSTANCE_CREATION_TYPE_REFERENCE}
 *               <td>Return only type references used in class instance creation.
 *              <tr>
 *               <td>{@link IJavaSearchConstants#RETURN_TYPE_REFERENCE RETURN_TYPE_REFERENCE}
 *               <td>Return only type references used as a method return type.
 *              <tr>
 *               <td>{@link IJavaSearchConstants#IMPORT_DECLARATION_TYPE_REFERENCE IMPORT_DECLARATION_TYPE_REFERENCE}
 *               <td>Return only type references used in an import declaration.
 *              <tr>
 *               <td>{@link IJavaSearchConstants#ANNOTATION_TYPE_REFERENCE ANNOTATION_TYPE_REFERENCE}
 *               <td>Return only type references used as an annotation.
 *              <tr>
 *               <td>{@link IJavaSearchConstants#TYPE_ARGUMENT_TYPE_REFERENCE TYPE_ARGUMENT_TYPE_REFERENCE}
 *               <td>Return only type references used as a type argument in a parameterized type or a parameterized method.
 *              <tr>
 *               <td>{@link IJavaSearchConstants#TYPE_VARIABLE_BOUND_TYPE_REFERENCE TYPE_VARIABLE_BOUND_TYPE_REFERENCE}
 *               <td>Return only type references used as a type variable bound.
 *              <tr>
 *               <td>{@link IJavaSearchConstants#WILDCARD_BOUND_TYPE_REFERENCE WILDCARD_BOUND_TYPE_REFERENCE}
 *               <td>Return only type references used as a wildcard bound.
 *              <tr>
 *               <td>{@link IJavaSearchConstants#INSTANCEOF_TYPE_REFERENCE INSTANCEOF_TYPE_REFERENCE}
 *               <td>Return only type references used as a type of an <code>instanceof</code> expression.
 *              <tr>
 *               <td>{@link IJavaSearchConstants#SUPER_REFERENCE SUPER_REFERENCE}
 *               <td>Return only super field accesses or super method invocations (e.g. using the <code>super</code> qualifier).
 *              <tr>
 *               <td>{@link IJavaSearchConstants#QUALIFIED_REFERENCE QUALIFIED_REFERENCE}
 *               <td>Return only qualified field accesses or qualified method invocations.
 *              <tr>
 *               <td>{@link IJavaSearchConstants#THIS_REFERENCE THIS_REFERENCE}
 *               <td>Return only primary field accesses or primary method invocations (e.g. using the <code>this</code> qualifier).
 *              <tr>
 *               <td>{@link IJavaSearchConstants#IMPLICIT_THIS_REFERENCE IMPLICIT_THIS_REFERENCE}
 *               <td>Return only field accesses or method invocations without any qualification.
 *          </table>
 *    </li>
 *   </ul>
 * @param matchRule one of the following match rules:
 *    <ul>
 *       <li>{@link #R_EXACT_MATCH}</li>
 *       <li>{@link #R_PREFIX_MATCH}</li>
 *       <li>{@link #R_PATTERN_MATCH}</li>
 *       <li>{@link #R_CAMELCASE_MATCH}</li>
 *       <li>{@link #R_CAMELCASE_SAME_PART_COUNT_MATCH}</li>
 *    </ul>
 *    , which may be also combined with one of the following flags:
 *    <ul>
 *       <li>{@link #R_CASE_SENSITIVE}</li>
 *       <li>{@link #R_ERASURE_MATCH}</li>
 *       <li>{@link #R_EQUIVALENT_MATCH}</li>
 *    </ul>
 *      For example,
 *      <ul>
 *         <li>{@link #R_EXACT_MATCH} | {@link #R_CASE_SENSITIVE}: if an exact
 *            and case sensitive match is requested,</li>
 *         <li>{@link #R_PREFIX_MATCH} if a case insensitive prefix match is requested</li>
 *         <li>{@link #R_EXACT_MATCH} | {@link #R_ERASURE_MATCH}: if a case
 *            insensitive and erasure match is requested.</li>
 *      </ul>
 *    Note that {@link #R_ERASURE_MATCH} or {@link #R_EQUIVALENT_MATCH} has no effect
 *    on non-generic types/methods search.
 *    <p>
 *    Note also that default behavior for generic types/methods search is to find exact matches.
 * @return a search pattern for a Java element or <code>null</code> if the given element is ill-formed
 * @since 3.1
 */
public static SearchPattern createPattern(IJavaElement element, int limitTo, int matchRule) {
    SearchPattern searchPattern = null;
    int lastDot;
    boolean ignoreDeclaringType = false;
    boolean ignoreReturnType = false;
    int maskedLimitTo = limitTo
            & ~(IJavaSearchConstants.IGNORE_DECLARING_TYPE + IJavaSearchConstants.IGNORE_RETURN_TYPE);
    if (maskedLimitTo == IJavaSearchConstants.DECLARATIONS
            || maskedLimitTo == IJavaSearchConstants.ALL_OCCURRENCES) {
        ignoreDeclaringType = (limitTo & IJavaSearchConstants.IGNORE_DECLARING_TYPE) != 0;
        ignoreReturnType = (limitTo & IJavaSearchConstants.IGNORE_RETURN_TYPE) != 0;
    }
    if ((matchRule = validateMatchRule(null, matchRule)) == -1) {
        return null;
    }
    char[] declaringSimpleName = null;
    char[] declaringQualification = null;
    switch (element.getElementType()) {
    case IJavaElement.FIELD:
        IField field = (IField) element;
        if (!ignoreDeclaringType) {
            IType declaringClass = field.getDeclaringType();
            declaringSimpleName = declaringClass.getElementName().toCharArray();
            declaringQualification = declaringClass.getPackageFragment().getElementName().toCharArray();
            char[][] enclosingNames = enclosingTypeNames(declaringClass);
            if (enclosingNames.length > 0) {
                declaringQualification = CharOperation.concat(declaringQualification,
                        CharOperation.concatWith(enclosingNames, '.'), '.');
            }
        }
        char[] name = field.getElementName().toCharArray();
        char[] typeSimpleName = null;
        char[] typeQualification = null;
        String typeSignature = null;
        if (!ignoreReturnType) {
            try {
                typeSignature = field.getTypeSignature();
                char[] signature = typeSignature.toCharArray();
                char[] typeErasure = Signature.toCharArray(Signature.getTypeErasure(signature));
                CharOperation.replace(typeErasure, '$', '.');
                if ((lastDot = CharOperation.lastIndexOf('.', typeErasure)) == -1) {
                    typeSimpleName = typeErasure;
                } else {
                    typeSimpleName = CharOperation.subarray(typeErasure, lastDot + 1, typeErasure.length);
                    typeQualification = CharOperation.subarray(typeErasure, 0, lastDot);
                    if (!field.isBinary()) {
                        // prefix with a '*' as the full qualification could be bigger (because of an import)
                        typeQualification = CharOperation.concat(IIndexConstants.ONE_STAR, typeQualification);
                    }
                }
            } catch (JavaModelException e) {
                return null;
            }
        }
        // Create field pattern
        searchPattern = new FieldPattern(name, declaringQualification, declaringSimpleName, typeQualification,
                typeSimpleName, typeSignature, limitTo, matchRule);
        break;
    case IJavaElement.IMPORT_DECLARATION:
        String elementName = element.getElementName();
        lastDot = elementName.lastIndexOf('.');
        if (lastDot == -1)
            return null; // invalid import declaration
        IImportDeclaration importDecl = (IImportDeclaration) element;
        if (importDecl.isOnDemand()) {
            searchPattern = createPackagePattern(elementName.substring(0, lastDot), maskedLimitTo, matchRule);
        } else {
            searchPattern = createTypePattern(elementName.substring(lastDot + 1).toCharArray(),
                    elementName.substring(0, lastDot).toCharArray(), null, null, null, maskedLimitTo,
                    matchRule);
        }
        break;
    case IJavaElement.LOCAL_VARIABLE:
        LocalVariable localVar = (LocalVariable) element;
        searchPattern = new LocalVariablePattern(localVar, limitTo, matchRule);
        break;
    case IJavaElement.TYPE_PARAMETER:
        ITypeParameter typeParam = (ITypeParameter) element;
        boolean findParamDeclarations = true;
        boolean findParamReferences = true;
        switch (maskedLimitTo) {
        case IJavaSearchConstants.DECLARATIONS:
            findParamReferences = false;
            break;
        case IJavaSearchConstants.REFERENCES:
            findParamDeclarations = false;
            break;
        }
        searchPattern = new TypeParameterPattern(findParamDeclarations, findParamReferences, typeParam,
                matchRule);
        break;
    case IJavaElement.METHOD:
        IMethod method = (IMethod) element;
        boolean isConstructor;
        try {
            isConstructor = method.isConstructor();
        } catch (JavaModelException e) {
            return null;
        }
        IType declaringClass = method.getDeclaringType();
        if (ignoreDeclaringType) {
            if (isConstructor)
                declaringSimpleName = declaringClass.getElementName().toCharArray();
        } else {
            declaringSimpleName = declaringClass.getElementName().toCharArray();
            declaringQualification = declaringClass.getPackageFragment().getElementName().toCharArray();
            char[][] enclosingNames = enclosingTypeNames(declaringClass);
            if (enclosingNames.length > 0) {
                declaringQualification = CharOperation.concat(declaringQualification,
                        CharOperation.concatWith(enclosingNames, '.'), '.');
            }
        }
        char[] selector = method.getElementName().toCharArray();
        char[] returnSimpleName = null;
        char[] returnQualification = null;
        String returnSignature = null;
        if (!ignoreReturnType) {
            try {
                returnSignature = method.getReturnType();
                char[] signature = returnSignature.toCharArray();
                char[] returnErasure = Signature.toCharArray(Signature.getTypeErasure(signature));
                CharOperation.replace(returnErasure, '$', '.');
                if ((lastDot = CharOperation.lastIndexOf('.', returnErasure)) == -1) {
                    returnSimpleName = returnErasure;
                } else {
                    returnSimpleName = CharOperation.subarray(returnErasure, lastDot + 1, returnErasure.length);
                    returnQualification = CharOperation.subarray(returnErasure, 0, lastDot);
                    if (!method.isBinary()) {
                        // prefix with a '*' as the full qualification could be bigger (because of an import)
                        CharOperation.concat(IIndexConstants.ONE_STAR, returnQualification);
                    }
                }
            } catch (JavaModelException e) {
                return null;
            }
        }
        String[] parameterTypes = method.getParameterTypes();
        int paramCount = parameterTypes.length;
        char[][] parameterSimpleNames = new char[paramCount][];
        char[][] parameterQualifications = new char[paramCount][];
        String[] parameterSignatures = new String[paramCount];
        for (int i = 0; i < paramCount; i++) {
            parameterSignatures[i] = parameterTypes[i];
            char[] signature = parameterSignatures[i].toCharArray();
            char[] paramErasure = Signature.toCharArray(Signature.getTypeErasure(signature));
            CharOperation.replace(paramErasure, '$', '.');
            if ((lastDot = CharOperation.lastIndexOf('.', paramErasure)) == -1) {
                parameterSimpleNames[i] = paramErasure;
                parameterQualifications[i] = null;
            } else {
                parameterSimpleNames[i] = CharOperation.subarray(paramErasure, lastDot + 1,
                        paramErasure.length);
                parameterQualifications[i] = CharOperation.subarray(paramErasure, 0, lastDot);
                if (!method.isBinary()) {
                    // prefix with a '*' as the full qualification could be bigger (because of an import)
                    CharOperation.concat(IIndexConstants.ONE_STAR, parameterQualifications[i]);
                }
            }
        }

        // Create method/constructor pattern
        if (isConstructor) {
            searchPattern = new ConstructorPattern(declaringSimpleName, declaringQualification,
                    parameterQualifications, parameterSimpleNames, parameterSignatures, method, limitTo,
                    matchRule);
        } else {
            searchPattern = new MethodPattern(selector, declaringQualification, declaringSimpleName,
                    returnQualification, returnSimpleName, returnSignature, parameterQualifications,
                    parameterSimpleNames, parameterSignatures, method, limitTo, matchRule);
        }
        break;
    case IJavaElement.TYPE:
        IType type = (IType) element;
        searchPattern = createTypePattern(type.getElementName().toCharArray(),
                type.getPackageFragment().getElementName().toCharArray(),
                ignoreDeclaringType ? null : enclosingTypeNames(type), null, type, maskedLimitTo, matchRule);
        break;
    case IJavaElement.PACKAGE_DECLARATION:
    case IJavaElement.PACKAGE_FRAGMENT:
        searchPattern = createPackagePattern(element.getElementName(), maskedLimitTo, matchRule);
        break;
    }
    if (searchPattern != null)
        MatchLocator.setFocus(searchPattern, element);
    return searchPattern;
}

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

License:Open Source License

public void initializeImportCaches() {
    if (this.currentPackageName == null) {
        initializePackageCache();/*from  ww w  . j a  va2 s  . c o  m*/
    }

    ImportBinding[] importBindings = this.unitScope.imports;
    int length = importBindings == null ? 0 : importBindings.length;

    for (int i = 0; i < length; i++) {
        ImportBinding importBinding = importBindings[i];
        if (importBinding.onDemand) {
            if (this.onDemandImportsCache == null) {
                this.onDemandImportsCache = new ImportBinding[length - i];
            }
            this.onDemandImportsCache[this.onDemandImportCacheCount++] = importBinding;
        } else {
            if (!(importBinding.resolvedImport instanceof MethodBinding)
                    || importBinding instanceof ImportConflictBinding) {
                if (this.importsCache == null) {
                    this.importsCache = new char[length - i][][];
                }
                this.importsCache[this.importCacheCount++] = new char[][] {
                        importBinding.compoundName[importBinding.compoundName.length - 1],
                        CharOperation.concatWith(importBinding.compoundName, '.') };
            }
        }
    }

    this.importCachesInitialized = true;
}

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

License:Open Source License

public void initializePackageCache() {
    if (this.unitScope.fPackage != null) {
        this.currentPackageName = CharOperation.concatWith(this.unitScope.fPackage.compoundName, '.');
    } else if (this.unitScope.referenceContext != null
            && this.unitScope.referenceContext.currentPackage != null) {
        this.currentPackageName = CharOperation
                .concatWith(this.unitScope.referenceContext.currentPackage.tokens, '.');
    } else {/*w ww. j a  v  a  2s  . c  om*/
        this.currentPackageName = CharOperation.NO_CHAR;
    }
}

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

License:Open Source License

protected boolean mustQualifyType(char[] packageName, char[] typeName, char[] enclosingTypeNames,
        int modifiers) {

    // If there are no types defined into the current CU yet.
    if (this.unitScope == null)
        return true;

    if (!this.importCachesInitialized) {
        initializeImportCaches();/*from   w ww .  j av  a 2s. c om*/
    }

    for (int i = 0; i < this.importCacheCount; i++) {
        char[][] importName = this.importsCache[i];
        if (CharOperation.equals(typeName, importName[0])) {
            char[] fullyQualifiedTypeName = enclosingTypeNames == null || enclosingTypeNames.length == 0
                    ? CharOperation.concat(packageName, typeName, '.')
                    : CharOperation.concat(CharOperation.concat(packageName, enclosingTypeNames, '.'), typeName,
                            '.');
            return !CharOperation.equals(fullyQualifiedTypeName, importName[1]);
        }
    }

    if ((enclosingTypeNames == null || enclosingTypeNames.length == 0)
            && CharOperation.equals(this.currentPackageName, packageName))
        return false;

    char[] fullyQualifiedEnclosingTypeName = null;

    for (int i = 0; i < this.onDemandImportCacheCount; i++) {
        ImportBinding importBinding = this.onDemandImportsCache[i];
        Binding resolvedImport = importBinding.resolvedImport;

        char[][] importName = importBinding.compoundName;
        char[] importFlatName = CharOperation.concatWith(importName, '.');

        boolean isFound = false;
        // resolvedImport is a ReferenceBindng or a PackageBinding
        if (resolvedImport instanceof ReferenceBinding) {
            if (enclosingTypeNames != null && enclosingTypeNames.length != 0) {
                if (fullyQualifiedEnclosingTypeName == null) {
                    fullyQualifiedEnclosingTypeName = CharOperation.concat(packageName, enclosingTypeNames,
                            '.');
                }
                if (CharOperation.equals(fullyQualifiedEnclosingTypeName, importFlatName)) {
                    if (importBinding.isStatic()) {
                        isFound = (modifiers & ClassFileConstants.AccStatic) != 0;
                    } else {
                        isFound = true;
                    }
                }
            }
        } else {
            if (enclosingTypeNames == null || enclosingTypeNames.length == 0) {
                if (CharOperation.equals(packageName, importFlatName)) {
                    if (importBinding.isStatic()) {
                        isFound = (modifiers & ClassFileConstants.AccStatic) != 0;
                    } else {
                        isFound = true;
                    }
                }
            }
        }

        // find potential conflict with another import
        if (isFound) {
            for (int j = 0; j < this.onDemandImportCacheCount; j++) {
                if (i != j) {
                    ImportBinding conflictingImportBinding = this.onDemandImportsCache[j];
                    if (conflictingImportBinding.resolvedImport instanceof ReferenceBinding) {
                        ReferenceBinding refBinding = (ReferenceBinding) conflictingImportBinding.resolvedImport;
                        if (refBinding.getMemberType(typeName) != null) {
                            return true;
                        }
                    } else {
                        char[] conflictingImportName = CharOperation
                                .concatWith(conflictingImportBinding.compoundName, '.');

                        if (this.nameEnvironment.nameLookup.findType(String.valueOf(typeName),
                                String.valueOf(conflictingImportName), false, NameLookup.ACCEPT_ALL,
                                false/*don't check restrictions*/) != null) {
                            return true;
                        }
                    }
                }
            }
            return false;
        }
    }
    return true;
}

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

License:Open Source License

public void acceptType(char[] packageName, char[] simpleTypeName, char[][] enclosingTypeNames, int modifiers,
        AccessRestriction accessRestriction) {
    char[] typeName = enclosingTypeNames == null ? simpleTypeName
            : CharOperation.concat(CharOperation.concatWith(enclosingTypeNames, '.'), simpleTypeName, '.');

    if (CharOperation.equals(simpleTypeName, this.selectedIdentifier)) {
        char[] flatEnclosingTypeNames = enclosingTypeNames == null || enclosingTypeNames.length == 0 ? null
                : CharOperation.concatWith(enclosingTypeNames, '.');
        if (mustQualifyType(packageName, simpleTypeName, flatEnclosingTypeNames, modifiers)) {
            int length = 0;
            int kind = modifiers & (ClassFileConstants.AccInterface | ClassFileConstants.AccEnum
                    | ClassFileConstants.AccAnnotation);
            switch (kind) {
            case ClassFileConstants.AccAnnotation:
            case ClassFileConstants.AccAnnotation | ClassFileConstants.AccInterface:
                char[][] acceptedAnnotation = new char[2][];
                acceptedAnnotation[0] = packageName;
                acceptedAnnotation[1] = typeName;

                if (this.acceptedAnnotations == null) {
                    this.acceptedAnnotations = new char[10][][];
                    this.acceptedAnnotationsModifiers = new int[10];
                    this.acceptedAnnotationsCount = 0;
                }/*from w  ww  .ja va  2s.co m*/
                length = this.acceptedAnnotations.length;
                if (length == this.acceptedAnnotationsCount) {
                    int newLength = (length + 1) * 2;
                    System.arraycopy(this.acceptedAnnotations, 0,
                            this.acceptedAnnotations = new char[newLength][][], 0, length);
                    System.arraycopy(this.acceptedAnnotationsModifiers, 0,
                            this.acceptedAnnotationsModifiers = new int[newLength], 0, length);
                }
                this.acceptedAnnotationsModifiers[this.acceptedAnnotationsCount] = modifiers;
                this.acceptedAnnotations[this.acceptedAnnotationsCount++] = acceptedAnnotation;
                break;
            case ClassFileConstants.AccEnum:
                char[][] acceptedEnum = new char[2][];
                acceptedEnum[0] = packageName;
                acceptedEnum[1] = typeName;

                if (this.acceptedEnums == null) {
                    this.acceptedEnums = new char[10][][];
                    this.acceptedEnumsModifiers = new int[10];
                    this.acceptedEnumsCount = 0;
                }
                length = this.acceptedEnums.length;
                if (length == this.acceptedEnumsCount) {
                    int newLength = (length + 1) * 2;
                    System.arraycopy(this.acceptedEnums, 0, this.acceptedEnums = new char[newLength][][], 0,
                            length);
                    System.arraycopy(this.acceptedEnumsModifiers, 0,
                            this.acceptedEnumsModifiers = new int[newLength], 0, length);
                }
                this.acceptedEnumsModifiers[this.acceptedEnumsCount] = modifiers;
                this.acceptedEnums[this.acceptedEnumsCount++] = acceptedEnum;
                break;
            case ClassFileConstants.AccInterface:
                char[][] acceptedInterface = new char[2][];
                acceptedInterface[0] = packageName;
                acceptedInterface[1] = typeName;

                if (this.acceptedInterfaces == null) {
                    this.acceptedInterfaces = new char[10][][];
                    this.acceptedInterfacesModifiers = new int[10];
                    this.acceptedInterfacesCount = 0;
                }
                length = this.acceptedInterfaces.length;
                if (length == this.acceptedInterfacesCount) {
                    int newLength = (length + 1) * 2;
                    System.arraycopy(this.acceptedInterfaces, 0,
                            this.acceptedInterfaces = new char[newLength][][], 0, length);
                    System.arraycopy(this.acceptedInterfacesModifiers, 0,
                            this.acceptedInterfacesModifiers = new int[newLength], 0, length);
                }
                this.acceptedInterfacesModifiers[this.acceptedInterfacesCount] = modifiers;
                this.acceptedInterfaces[this.acceptedInterfacesCount++] = acceptedInterface;
                break;
            default:
                char[][] acceptedClass = new char[2][];
                acceptedClass[0] = packageName;
                acceptedClass[1] = typeName;

                if (this.acceptedClasses == null) {
                    this.acceptedClasses = new char[10][][];
                    this.acceptedClassesModifiers = new int[10];
                    this.acceptedClassesCount = 0;
                }
                length = this.acceptedClasses.length;
                if (length == this.acceptedClassesCount) {
                    int newLength = (length + 1) * 2;
                    System.arraycopy(this.acceptedClasses, 0, this.acceptedClasses = new char[newLength][][], 0,
                            length);
                    System.arraycopy(this.acceptedClassesModifiers, 0,
                            this.acceptedClassesModifiers = new int[newLength], 0, length);
                }
                this.acceptedClassesModifiers[this.acceptedClassesCount] = modifiers;
                this.acceptedClasses[this.acceptedClassesCount++] = acceptedClass;
                break;
            }
        } else {
            this.noProposal = false;
            this.requestor.acceptType(packageName, typeName, modifiers, false, null, this.actualSelectionStart,
                    this.actualSelectionEnd);
            this.acceptedAnswer = true;
        }
    }
}

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./*ww w.ja va2  s  .  co m*/
        
 *  @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.codeassist.SelectionEngine.java

License:Open Source License

private boolean selectDeclaration(CompilationUnitDeclaration compilationUnit) {

    // the selected identifier is not identical to the parser one (equals but not identical),
    // for traversing the parse tree, the parser assist identifier is necessary for identitiy checks
    char[] assistIdentifier = getParser().assistIdentifier();
    if (assistIdentifier == null)
        return false;

    ImportReference currentPackage = compilationUnit.currentPackage;
    char[] packageName = currentPackage == null ? CharOperation.NO_CHAR
            : CharOperation.concatWith(currentPackage.tokens, '.');
    // iterate over the types
    TypeDeclaration[] types = compilationUnit.types;
    for (int i = 0, length = types == null ? 0 : types.length; i < length; i++) {
        if (selectDeclaration(types[i], assistIdentifier, packageName))
            return true;
    }/*from  ww w . ja  va2  s  .  c  om*/
    return false;
}

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

License:Open Source License

private IAnnotation getAnnotation(char[][] annotationName) {
    return new Annotation(this, manager, new String(CharOperation.concatWith(annotationName, '.')));
}

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

License:Open Source License

private void generateStandardAnnotation(JavaElement javaElement, char[][] typeName, IMemberValuePair[] members,
        HashMap newElements) {/*from  w  w  w. ja va  2 s.c  o  m*/
    IAnnotation annotation = new Annotation(javaElement, javaElement.manager,
            new String(CharOperation.concatWith(typeName, '.')));
    AnnotationInfo annotationInfo = new AnnotationInfo();
    annotationInfo.members = members;
    newElements.put(annotation, annotationInfo);
}

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

License:Open Source License

private IMemberValuePair[] getTargetElementTypes(long tagBits) {
    ArrayList values = new ArrayList();
    String elementType = new String(
            CharOperation.concatWith(TypeConstants.JAVA_LANG_ANNOTATION_ELEMENTTYPE, '.')) + '.';
    if ((tagBits & TagBits.AnnotationForType) != 0) {
        values.add(elementType + new String(TypeConstants.TYPE));
    }//  w w w.j a va 2 s .c om
    if ((tagBits & TagBits.AnnotationForField) != 0) {
        values.add(elementType + new String(TypeConstants.UPPER_FIELD));
    }
    if ((tagBits & TagBits.AnnotationForMethod) != 0) {
        values.add(elementType + new String(TypeConstants.UPPER_METHOD));
    }
    if ((tagBits & TagBits.AnnotationForParameter) != 0) {
        values.add(elementType + new String(TypeConstants.UPPER_PARAMETER));
    }
    if ((tagBits & TagBits.AnnotationForConstructor) != 0) {
        values.add(elementType + new String(TypeConstants.UPPER_CONSTRUCTOR));
    }
    if ((tagBits & TagBits.AnnotationForLocalVariable) != 0) {
        values.add(elementType + new String(TypeConstants.UPPER_LOCAL_VARIABLE));
    }
    if ((tagBits & TagBits.AnnotationForAnnotationType) != 0) {
        values.add(elementType + new String(TypeConstants.UPPER_ANNOTATION_TYPE));
    }
    if ((tagBits & TagBits.AnnotationForPackage) != 0) {
        values.add(elementType + new String(TypeConstants.UPPER_PACKAGE));
    }
    final Object value;
    if (values.size() == 0) {
        if ((tagBits & TagBits.AnnotationTarget) != 0)
            value = CharOperation.NO_STRINGS;
        else
            return Annotation.NO_MEMBER_VALUE_PAIRS;
    } else if (values.size() == 1) {
        value = values.get(0);
    } else {
        value = values.toArray(new String[values.size()]);
    }
    return new IMemberValuePair[] { new IMemberValuePair() {
        public int getValueKind() {
            return IMemberValuePair.K_QUALIFIED_NAME;
        }

        public Object getValue() {
            return value;
        }

        public String getMemberName() {
            return new String(TypeConstants.VALUE);
        }
    } };
}