Example usage for org.eclipse.jdt.core Signature createCharArrayTypeSignature

List of usage examples for org.eclipse.jdt.core Signature createCharArrayTypeSignature

Introduction

In this page you can find the example usage for org.eclipse.jdt.core Signature createCharArrayTypeSignature.

Prototype

public static char[] createCharArrayTypeSignature(char[] typeName, boolean isResolved) 

Source Link

Document

Creates a new type signature from the given type name encoded as a character array.

Usage

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

License:Open Source License

/**
 * Asks the engine to compute the selection of the given type
 * from the given context/*from  w w  w . java  2  s  .  co  m*/
 *
 *  @param typeName char[]
 *      a type name which is to be resolved in the context of a compilation unit.
 *      NOTE: the type name is supposed to be correctly reduced (no whitespaces, no unicodes left)
 *
 *  @param context org.eclipse.jdt.core.IType
 *      the context in which code assist is invoked.
 */
public void selectType(char[] typeName, IType context) throws JavaModelException {
    try {
        this.acceptedAnswer = false;

        // only the type erasure are returned by IType.resolvedType(...)
        if (CharOperation.indexOf('<', typeName) != -1) {
            char[] typeSig = Signature.createCharArrayTypeSignature(typeName, false/*not resolved*/);
            typeSig = Signature.getTypeErasure(typeSig);
            typeName = Signature.toCharArray(typeSig);
        }

        CompilationUnitDeclaration parsedUnit = null;
        TypeDeclaration typeDeclaration = null;
        org.eclipse.jdt.core.ICompilationUnit cu = context.getCompilationUnit();
        if (cu != null) {
            IType[] topLevelTypes = cu.getTypes();
            int length = topLevelTypes.length;
            SourceTypeElementInfo[] topLevelInfos = new SourceTypeElementInfo[length];
            for (int i = 0; i < length; i++) {
                topLevelInfos[i] = (SourceTypeElementInfo) ((SourceType) topLevelTypes[i]).getElementInfo();
            }
            CompilationResult result = new CompilationResult(
                    (org.eclipse.jdt.internal.compiler.env.ICompilationUnit) cu, 1, 1,
                    this.compilerOptions.maxProblemsPerUnit);
            int flags = SourceTypeConverter.FIELD_AND_METHOD | SourceTypeConverter.MEMBER_TYPE;
            if (context.isAnonymous() || context.isLocal())
                flags |= SourceTypeConverter.LOCAL_TYPE;
            parsedUnit = SourceTypeConverter.buildCompilationUnit(topLevelInfos, flags,
                    this.parser.problemReporter(), result);
            if (parsedUnit != null && parsedUnit.types != null) {
                if (DEBUG) {
                    System.out.println("SELECTION - Diet AST :"); //$NON-NLS-1$
                    System.out.println(parsedUnit.toString());
                }
                // find the type declaration that corresponds to the original source type
                typeDeclaration = new ASTNodeFinder(parsedUnit).findType(context);
            }
        } else { // binary type
            ClassFile classFile = (ClassFile) context.getClassFile();
            ClassFileReader reader = (ClassFileReader) classFile.getBinaryTypeInfo(
                    (IFile) null/*classFile.resource()*/,
                    false/*don't fully initialize so as to keep constant pool (used below)*/);
            CompilationResult result = new CompilationResult(reader.getFileName(), 1, 1,
                    this.compilerOptions.maxProblemsPerUnit);
            parsedUnit = new CompilationUnitDeclaration(this.parser.problemReporter(), result, 0);
            HashSetOfCharArrayArray typeNames = new HashSetOfCharArrayArray();

            BinaryTypeConverter converter = new BinaryTypeConverter(this.parser.problemReporter(), result,
                    typeNames);
            typeDeclaration = converter.buildTypeDeclaration(context, parsedUnit);
            parsedUnit.imports = converter.buildImports(reader);
        }

        if (typeDeclaration != null) {

            // add fake field with the type we're looking for
            // note: since we didn't ask for fields above, there is no field defined yet
            FieldDeclaration field = new FieldDeclaration();
            int dot;
            if ((dot = CharOperation.lastIndexOf('.', typeName)) == -1) {
                this.selectedIdentifier = typeName;
                field.type = new SelectionOnSingleTypeReference(typeName, -1);
                // position not used
            } else {
                char[][] previousIdentifiers = CharOperation.splitOn('.', typeName, 0, dot);
                char[] selectionIdentifier = CharOperation.subarray(typeName, dot + 1, typeName.length);
                this.selectedIdentifier = selectionIdentifier;
                field.type = new SelectionOnQualifiedTypeReference(previousIdentifiers, selectionIdentifier,
                        new long[previousIdentifiers.length + 1]);
            }
            field.name = "<fakeField>".toCharArray(); //$NON-NLS-1$
            typeDeclaration.fields = new FieldDeclaration[] { field };

            // build bindings
            this.lookupEnvironment.buildTypeBindings(parsedUnit, null /*no access restriction*/);
            if ((this.unitScope = parsedUnit.scope) != null) {
                try {
                    // build fields
                    // note: this builds fields only in the parsed unit (the buildFieldsAndMethods flag is not passed along)
                    this.lookupEnvironment.completeTypeBindings(parsedUnit, true);

                    // resolve
                    parsedUnit.scope.faultInTypes();
                    parsedUnit.resolve();
                } 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, e.isDeclaration);
                    }
                }
            }
        }
        if (this.noProposal && this.problem != null) {
            this.requestor.acceptError(this.problem);
        }
    } catch (AbortCompilation e) { // ignore this exception for now since it typically means we cannot find java.lang.Object
    } finally {
        reset(true);
    }
}

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

License:Open Source License

protected String getKey(IMethod method, boolean forceOpen) throws JavaModelException {
    StringBuffer key = new StringBuffer();

    // declaring class
    String declaringKey = getKey((IType) method.getParent(), forceOpen);
    key.append(declaringKey);/* www.j a v  a2 s  .c  o  m*/

    // selector
    key.append('.');
    String selector = method.getElementName();
    key.append(selector);

    // type parameters
    if (forceOpen) {
        ITypeParameter[] typeParameters = method.getTypeParameters();
        int length = typeParameters.length;
        if (length > 0) {
            key.append('<');
            for (int i = 0; i < length; i++) {
                ITypeParameter typeParameter = typeParameters[i];
                String[] bounds = typeParameter.getBounds();
                int boundsLength = bounds.length;
                char[][] boundSignatures = new char[boundsLength][];
                for (int j = 0; j < boundsLength; j++) {
                    boundSignatures[j] = Signature.createCharArrayTypeSignature(bounds[j].toCharArray(),
                            method.isBinary());
                    CharOperation.replace(boundSignatures[j], '.', '/');
                }
                char[] sig = Signature.createTypeParameterSignature(
                        typeParameter.getElementName().toCharArray(), boundSignatures);
                key.append(sig);
            }
            key.append('>');
        }
    }

    // parameters
    key.append('(');
    String[] parameters = method.getParameterTypes();
    for (int i = 0, length = parameters.length; i < length; i++)
        key.append(parameters[i].replace('.', '/'));
    key.append(')');

    // return type
    if (forceOpen)
        key.append(method.getReturnType().replace('.', '/'));
    else
        key.append('V');

    return key.toString();
}

From source file:com.codenvy.ide.ext.java.server.internal.core.search.indexing.BinaryIndexer.java

License:Open Source License

private int extractArgCount(char[] signature, char[] className) throws ClassFormatException {
    int indexOfClosingParen = CharOperation.lastIndexOf(')', signature);
    if (indexOfClosingParen == 1) {
        // there is no parameter
        return 0;
    }/*ww  w .ja v  a  2  s . c o  m*/
    if (indexOfClosingParen == -1) {
        throw new ClassFormatException(ClassFormatException.ErrInvalidMethodSignature);
    }
    int parameterTypesCounter = 0;
    for (int i = 1; i < indexOfClosingParen; i++) {
        switch (signature[i]) {
        case 'B':
        case 'C':
        case 'D':
        case 'F':
        case 'I':
        case 'J':
        case 'S':
        case 'Z':
            parameterTypesCounter++;
            break;
        case 'L':
            int indexOfSemiColon = CharOperation.indexOf(';', signature, i + 1);
            if (indexOfSemiColon == -1)
                throw new ClassFormatException(ClassFormatException.ErrInvalidMethodSignature);
            // verify if first parameter is synthetic
            if (className != null && parameterTypesCounter == 0) {
                char[] classSignature = Signature.createCharArrayTypeSignature(className, true);
                int length = indexOfSemiColon - i + 1;
                if (classSignature.length > (length + 1)) {
                    // synthetic means that parameter type has same signature than given class
                    for (int j = i, k = 0; j < indexOfSemiColon; j++, k++) {
                        if (!(signature[j] == classSignature[k]
                                || (signature[j] == '/' && classSignature[k] == '.'))) {
                            parameterTypesCounter++;
                            break;
                        }
                    }
                } else {
                    parameterTypesCounter++;
                }
                className = null; // do not verify following parameters
            } else {
                parameterTypesCounter++;
            }
            i = indexOfSemiColon;
            break;
        case '[':
            break;
        default:
            throw new ClassFormatException(ClassFormatException.ErrInvalidMethodSignature);
        }
    }
    return parameterTypesCounter;
}

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

License:Open Source License

/**
 * NOT API, public only for access by Unit tests.
 * Converts these type names to unqualified signatures. This needs to be done in order to be consistent
 * with the way the source range is retrieved.
 *
 * @see org.eclipse.jdt.internal.core.SourceMapper#getUnqualifiedMethodHandle
 * @see org.eclipse.jdt.core.Signature//from   ww  w. ja v a2s . c  o m
 */
public String[] convertTypeNamesToSigs(char[][] typeNames) {
    if (typeNames == null)
        return CharOperation.NO_STRINGS;
    int n = typeNames.length;
    if (n == 0)
        return CharOperation.NO_STRINGS;
    String[] typeSigs = new String[n];
    for (int i = 0; i < n; ++i) {
        char[] typeSig = Signature.createCharArrayTypeSignature(typeNames[i], false);

        // transforms signatures that contains a qualification into unqualified signatures
        // e.g. "QX<+QMap.Entry;>;" becomes "QX<+QEntry;>;"
        StringBuffer simpleTypeSig = null;
        int start = 0;
        int dot = -1;
        int length = typeSig.length;
        for (int j = 0; j < length; j++) {
            switch (typeSig[j]) {
            case Signature.C_UNRESOLVED:
                if (simpleTypeSig != null)
                    simpleTypeSig.append(typeSig, start, j - start);
                start = j;
                break;
            case Signature.C_DOT:
                dot = j;
                break;
            case Signature.C_GENERIC_START:
                int matchingEnd = findMatchingGenericEnd(typeSig, j + 1);
                if (matchingEnd > 0 && matchingEnd + 1 < length
                        && typeSig[matchingEnd + 1] == Signature.C_DOT) {
                    // found Head<Param>.Tail -> discard everything except Tail
                    if (simpleTypeSig == null)
                        simpleTypeSig = new StringBuffer().append(typeSig, 0, start);
                    simpleTypeSig.append(Signature.C_UNRESOLVED);
                    start = j = matchingEnd + 2;
                    break;
                }
                //$FALL-THROUGH$
            case Signature.C_NAME_END:
                if (dot > start) {
                    if (simpleTypeSig == null)
                        simpleTypeSig = new StringBuffer().append(typeSig, 0, start);
                    simpleTypeSig.append(Signature.C_UNRESOLVED);
                    simpleTypeSig.append(typeSig, dot + 1, j - dot - 1);
                    start = j;
                }
                break;
            }
        }
        if (simpleTypeSig == null) {
            typeSigs[i] = new String(typeSig);
        } else {
            simpleTypeSig.append(typeSig, start, length - start);
            typeSigs[i] = simpleTypeSig.toString();
        }
    }
    return typeSigs;
}

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

License:Open Source License

/**
 * @see org.eclipse.jdt.core.IMethod#getTypeParameterSignatures()
 * @since 3.0/*from  ww w . jav  a2  s .  com*/
 * @deprecated
 */
public String[] getTypeParameterSignatures() throws JavaModelException {
    ITypeParameter[] typeParameters = getTypeParameters();
    int length = typeParameters.length;
    String[] typeParameterSignatures = new String[length];
    for (int i = 0; i < length; i++) {
        TypeParameter typeParameter = (TypeParameter) typeParameters[i];
        TypeParameterElementInfo info = (TypeParameterElementInfo) typeParameter.getElementInfo();
        char[][] bounds = info.bounds;
        if (bounds == null) {
            typeParameterSignatures[i] = Signature.createTypeParameterSignature(typeParameter.getElementName(),
                    CharOperation.NO_STRINGS);
        } else {
            int boundsLength = bounds.length;
            char[][] boundSignatures = new char[boundsLength][];
            for (int j = 0; j < boundsLength; j++) {
                boundSignatures[j] = Signature.createCharArrayTypeSignature(bounds[j], false);
            }
            typeParameterSignatures[i] = new String(Signature.createTypeParameterSignature(
                    typeParameter.getElementName().toCharArray(), boundSignatures));
        }
    }
    return typeParameterSignatures;
}

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

License:Open Source License

public String[] getBoundsSignatures() throws JavaModelException {

    String[] boundSignatures = null;
    TypeParameterElementInfo info = (TypeParameterElementInfo) this.getElementInfo();

    // For a binary type or method, the signature is already available from the .class file.
    // No need to construct again
    if (this.parent instanceof BinaryMember) {
        char[][] boundsSignatures = info.boundsSignatures;
        if (boundsSignatures == null || boundsSignatures.length == 0) {
            return CharOperation.NO_STRINGS;
        }//ww w  . ja v  a 2  s  .co  m
        return CharOperation.toStrings(info.boundsSignatures);
    }

    char[][] bounds = info.bounds;
    if (bounds == null || bounds.length == 0) {
        return CharOperation.NO_STRINGS;
    }

    int boundsLength = bounds.length;
    boundSignatures = new String[boundsLength];
    for (int i = 0; i < boundsLength; i++) {
        boundSignatures[i] = new String(Signature.createCharArrayTypeSignature(bounds[i], false));
    }
    return boundSignatures;
}

From source file:org.codehaus.groovy.eclipse.codeassist.processors.GroovyProposalTypeSearchRequestor.java

License:Apache License

/**
 * @param parameterTypes/*w w w . j av a2 s  .  c o m*/
 * @return
 */
private char[] createConstructorSignature(char[][] parameterTypes, boolean isQualified) {
    char[][] parameterTypeSigs;
    if (parameterTypes == null) {
        parameterTypeSigs = CharOperation.NO_CHAR_CHAR;
    } else {
        parameterTypeSigs = new char[parameterTypes.length][];
        for (int i = 0; i < parameterTypes.length; i++) {
            char[] copy = new char[parameterTypes[i].length];
            System.arraycopy(parameterTypes[i], 0, copy, 0, copy.length);
            CharOperation.replace(copy, '/', '.');
            parameterTypeSigs[i] = Signature.createCharArrayTypeSignature(copy, isQualified);
        }
    }
    return Signature.createMethodSignature(parameterTypeSigs, new char[] { 'V' });
}

From source file:org.eclipse.ajdt.core.javaelements.IntertypeElement.java

License:Open Source License

public char[] getQualifiedReturnType() throws JavaModelException {
    IntertypeElementInfo info = (IntertypeElementInfo) getElementInfo();
    char[] returnTypeName = info.getQualifiedReturnType();
    if (returnTypeName == null) {
        returnTypeName = getQualifiedReturnTypeName(info);
        info.setQualifiedReturnType(returnTypeName);
    }/*from  w  w w . j  a  v a 2s  .  com*/
    return Signature.createCharArrayTypeSignature(returnTypeName, false);
}

From source file:org.eclipse.jdt.internal.core.SourceMapper.java

License:Open Source License

/**
 * Converts these type names to unqualified signatures. This needs to be done in order to be consistent
 * with the way the source range is retrieved.
 * @see SourceMapper#getUnqualifiedMethodHandle
 * @see Signature/*  w ww . j av  a2s  .  c  o m*/
 */
private String[] convertTypeNamesToSigs(char[][] typeNames) {
    if (typeNames == null)
        return CharOperation.NO_STRINGS;
    int n = typeNames.length;
    if (n == 0)
        return CharOperation.NO_STRINGS;
    String[] typeSigs = new String[n];
    for (int i = 0; i < n; ++i) {
        char[] typeSig = Signature.createCharArrayTypeSignature(typeNames[i], false);

        // transforms signatures that contains a qualification into unqualified signatures
        // e.g. "QX<+QMap.Entry;>;" becomes "QX<+QEntry;>;"
        StringBuffer simpleTypeSig = null;
        int start = 0;
        int dot = -1;
        int length = typeSig.length;
        for (int j = 0; j < length; j++) {
            switch (typeSig[j]) {
            case Signature.C_UNRESOLVED:
                if (simpleTypeSig != null)
                    simpleTypeSig.append(typeSig, start, j - start);
                start = j;
                break;
            case Signature.C_DOT:
                dot = j;
                break;
            case Signature.C_GENERIC_START:
            case Signature.C_NAME_END:
                if (dot > start) {
                    if (simpleTypeSig == null)
                        simpleTypeSig = new StringBuffer().append(typeSig, 0, start);
                    simpleTypeSig.append(Signature.C_UNRESOLVED);
                    simpleTypeSig.append(typeSig, dot + 1, j - dot - 1);
                    start = j;
                }
                break;
            }
        }
        if (simpleTypeSig == null) {
            typeSigs[i] = new String(typeSig);
        } else {
            simpleTypeSig.append(typeSig, start, length - start);
            typeSigs[i] = simpleTypeSig.toString();
        }
    }
    return typeSigs;
}