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

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

Introduction

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

Prototype

public static String createArraySignature(String typeSignature, int arrayCount) 

Source Link

Document

Creates a new type signature with the given amount of array nesting added to the given type signature.

Usage

From source file:at.bestsolution.fxide.jdt.editor.internal.SignatureUtil.java

License:Open Source License

/**
 * Returns the qualified signature corresponding to
 * <code>signature</code>./*from   w  w  w .  j  av a 2 s  .  co  m*/
 *
 * @param signature the signature to qualify
 * @param context the type inside which an unqualified type will be
 *        resolved to find the qualifier, or <code>null</code> if no
 *        context is available
 * @return the qualified signature
 */
public static String qualifySignature(final String signature, final IType context) {
    if (context == null)
        return signature;

    String qualifier = Signature.getSignatureQualifier(signature);
    if (qualifier.length() > 0)
        return signature;

    String elementType = Signature.getElementType(signature);
    String erasure = Signature.getTypeErasure(elementType);
    String simpleName = Signature.getSignatureSimpleName(erasure);
    String genericSimpleName = Signature.getSignatureSimpleName(elementType);

    int dim = Signature.getArrayCount(signature);

    try {
        String[][] strings = context.resolveType(simpleName);
        if (strings != null && strings.length > 0)
            qualifier = strings[0][0];
    } catch (JavaModelException e) {
        // ignore - not found
    }

    if (qualifier.length() == 0)
        return signature;

    String qualifiedType = Signature.toQualifiedName(new String[] { qualifier, genericSimpleName });
    String qualifiedSignature = Signature.createTypeSignature(qualifiedType, true);
    String newSignature = Signature.createArraySignature(qualifiedSignature, dim);

    return newSignature;
}

From source file:org.eclipse.jpt.common.core.internal.resource.java.binary.BinaryMember.java

License:Open Source License

/**
 * no null check/*from  w  ww. ja va  2  s.  c o m*/
 */
static String convertTypeSignatureToTypeName_(String typeSignature, Iterable<ITypeParameter> typeParameters) {
    String erasureSignature = Signature.getTypeErasure(typeSignature);
    if (Signature.getTypeSignatureKind(erasureSignature) == Signature.TYPE_VARIABLE_SIGNATURE) {
        try {
            String typeParameterName = Signature.toString(erasureSignature);
            for (ITypeParameter typeParameter : typeParameters) {
                if (typeParameterName.equals(typeParameter.getElementName())) {
                    String[] bounds = typeParameter.getBoundsSignatures();
                    if (bounds.length > 0) {
                        return convertTypeSignatureToTypeName_(bounds[0], typeParameters);
                    }
                }
            }
        } catch (JavaModelException jme) {
            JptCommonCorePlugin.instance().logError(jme);
        }
    } else if (Signature.getTypeSignatureKind(erasureSignature) == Signature.ARRAY_TYPE_SIGNATURE) {
        int dim = Signature.getArrayCount(erasureSignature);
        String arrayTypeName = convertTypeSignatureToTypeName(Signature.getElementType(erasureSignature),
                typeParameters);
        return Signature.toString(
                Signature.createArraySignature(Signature.createTypeSignature(arrayTypeName, true), dim));
    } else if (Signature.getTypeSignatureKind(erasureSignature) == Signature.WILDCARD_TYPE_SIGNATURE) {
        // if signature is ? (wildcard) or ? super X (bottom bounded), return top bound, which is Object
        if (String.valueOf(Signature.C_STAR).equals(erasureSignature)
                || erasureSignature.startsWith(String.valueOf(Signature.C_SUPER))) {
            return Object.class.getName();
        }
        // return top bound
        return Signature.toString(erasureSignature.substring(1));
    }
    return Signature.toString(erasureSignature);
}

From source file:org.eclipse.jst.jsf.context.symbol.InitializedSymbolFactory.java

License:Open Source License

/**
 * @param type/*ww  w  .jav  a  2s . c om*/
 * @return the signature of the element type of a collection/array,
 *         <code>null</code>, if untyped Collection or no container type
 *         at all.
 */
public final String getElementSignatureFromContainerType(ValueType type) {
    if (type.isArray()) {
        // TODO full signature
        String signature = type.getSignature();
        int arrayCount = Signature.getArrayCount(signature);
        String elementSig = Signature.getElementType(signature);
        return Signature.createArraySignature(elementSig, arrayCount - 1);
    }
    if (type.isInstanceOf(TypeConstants.TYPE_COLLECTION)) {
        final String[] typeArguments = type.getTypeArguments();
        if (typeArguments.length > 0) {
            return typeArguments[0];
        }
    }
    return null;
}

From source file:org.eclipse.jst.jsf.context.symbol.internal.impl.IJavaTypeDescriptor2Impl.java

License:Open Source License

/**
* @generated NOT//from  ww  w .  j  a v a2 s .c  om
*/
public IObjectSymbol getArrayElement() {
    if (isArray()) {
        final String typeSignature = getTypeSignature();
        final int arrayCount_ = Signature.getArrayCount(typeSignature);
        final String baseType = Signature.getElementType(typeSignature);
        final String elementTypeSignature = Signature.createArraySignature(baseType, arrayCount_ - 1);

        final IJavaTypeDescriptor2 elementTypeDesc = SymbolFactory.eINSTANCE.createIJavaTypeDescriptor2();
        final String fullyQualifiedElementType = TypeUtil.getFullyQualifiedName(baseType);

        IType elementType = null;

        try {
            IType myType = getType();
            if (myType != null) {
                elementType = getType().getJavaProject().findType(fullyQualifiedElementType);
            }
        } catch (JavaModelException e) {
            // suppress
        }

        if (elementType != null) {
            elementTypeDesc.setType(elementType);
        } else {
            elementTypeDesc.setTypeSignatureDelegate(elementTypeSignature);
        }

        elementTypeDesc.setArrayCount(Signature.getArrayCount(elementTypeSignature));

        IPropertySymbol newPropertySymbol = SymbolFactory.eINSTANCE.createIPropertySymbol();
        newPropertySymbol.setTypeDescriptor(elementTypeDesc);
        newPropertySymbol.setWritable(true);
        newPropertySymbol.setReadable(true);
        newPropertySymbol.setName(fullyQualifiedElementType);
        return newPropertySymbol;
    }

    return null;
}

From source file:org.eclipse.jst.jsf.context.symbol.internal.impl.IJavaTypeDescriptor2Impl.java

License:Open Source License

public String getTypeSignature() {
    if (getType() == null) {
        if (eIsSet(SymbolPackage.IJAVA_TYPE_DESCRIPTOR2__TYPE_SIGNATURE_DELEGATE)) {
            return getTypeSignatureDelegate();
        }// ww w.  j a  va  2  s.  c o  m

        return null;
    }

    // make sure to array type nesting if using IType
    return Signature.createArraySignature(TypeUtil.getSignature(getType()), getArrayCount());
}

From source file:org.eclipse.jst.jsf.designtime.symbols.JSFSymbolFactory.java

License:Open Source License

/**
 * @param symbolName The name of the symbol to be created. Must not be null
 * @param signature The type signature of the array type. Must not be null
 * @param source the runtime source//from ww w  . ja  v a 2 s  .  com
 * @param javaProject must not be null
 * @return a symbol based approximating an implicit DataModel wrapper for an array
 */
public final ISymbol createArraySymbol(final String symbolName, final String signature,
        final ERuntimeSource source, final IJavaProject javaProject) {
    assert symbolName != null;
    assert signature != null;
    assert javaProject != null;

    final String arrayElementType = Signature.getElementType(signature);
    final int arrayCount = Signature.getArrayCount(signature);
    String adjustedSignature = null;

    // if it is a single array nesting, then it will just be the element type,
    // but if it is a multi-dim array, then the scalar element will be an array
    // with one less nesting level.  It's a strange corner case to have an implicit
    // array of something as a row type, but it is a valid case.  I suppose
    // it may be happen if you want to have tables of tables in which the nested tables
    // in turn use nested variables as their type...
    if (arrayCount > 0) {
        adjustedSignature = Signature.createArraySignature(arrayElementType, arrayCount - 1);
    } else {
        adjustedSignature = arrayElementType;
    }

    return createScalarSymbol(symbolName, adjustedSignature, source, javaProject);
}

From source file:org.eclipse.objectteams.otdt.debug.ui.internal.actions.OTToggleBreakpointAdapter.java

License:Open Source License

/**
 * Returns the resolved type signature for the given signature in the given
 * method, or <code>null</code> if unable to resolve.
 * /*from   www. ja v a2 s.co  m*/
 * @param method method containing the type signature
 * @param typeSignature the type signature to resolve
 * @return the resolved type signature
 * @throws JavaModelException
 */
private static String resolveTypeSignature(IMethod method, String typeSignature) throws JavaModelException {
    int count = Signature.getArrayCount(typeSignature);
    String elementTypeSignature = Signature.getElementType(typeSignature);
    if (elementTypeSignature.length() == 1) {
        // no need to resolve primitive types
        return typeSignature;
    }
    String elementTypeName = Signature.toString(elementTypeSignature);
    IType type = method.getDeclaringType();
    String[][] resolvedElementTypeNames = type.resolveType(elementTypeName);
    if (resolvedElementTypeNames == null || resolvedElementTypeNames.length != 1) {
        // check if type parameter
        ITypeParameter typeParameter = method.getTypeParameter(elementTypeName);
        if (!typeParameter.exists()) {
            typeParameter = type.getTypeParameter(elementTypeName);
        }
        if (typeParameter.exists()) {
            String[] bounds = typeParameter.getBounds();
            if (bounds.length == 0) {
                return "Ljava/lang/Object;"; //$NON-NLS-1$
            }
            String bound = Signature.createTypeSignature(bounds[0], false);
            return Signature.createArraySignature(resolveTypeSignature(method, bound), count);
        }
        // the type name cannot be resolved
        return null;
    }

    String[] types = resolvedElementTypeNames[0];
    types[1] = types[1].replace('.', '$');

    String resolvedElementTypeName = Signature.toQualifiedName(types);
    String resolvedElementTypeSignature = EMPTY_STRING;
    if (types[0].equals(EMPTY_STRING)) {
        resolvedElementTypeName = resolvedElementTypeName.substring(1);
        resolvedElementTypeSignature = Signature.createTypeSignature(resolvedElementTypeName, true);
    } else {
        resolvedElementTypeSignature = Signature.createTypeSignature(resolvedElementTypeName, true).replace('.',
                '/');
    }

    return Signature.createArraySignature(resolvedElementTypeSignature, count);
}

From source file:org.eclipse.pde.api.tools.internal.util.Signatures.java

License:Open Source License

/**
 * Processes the signature for the given {@link Type}
 * /*from  w w w .j  a  va  2s.  c  o  m*/
 * @param type the type to process
 * @return the signature for the type or <code>null</code> if one could not
 *         be derived
 */
public static String getTypeSignature(Type type) {
    switch (type.getNodeType()) {
    case ASTNode.SIMPLE_TYPE: {
        return Signature.createTypeSignature(((SimpleType) type).getName().getFullyQualifiedName(), false);
    }
    case ASTNode.QUALIFIED_TYPE: {
        return Signature.createTypeSignature(((QualifiedType) type).getName().getFullyQualifiedName(), false);
    }
    case ASTNode.ARRAY_TYPE: {
        ArrayType a = (ArrayType) type;
        return Signature.createArraySignature(getTypeSignature(a.getElementType()), a.getDimensions());
    }
    case ASTNode.PARAMETERIZED_TYPE: {
        // we don't need to care about the other scoping types only the
        // base type
        return getTypeSignature(((ParameterizedType) type).getType());
    }
    case ASTNode.PRIMITIVE_TYPE: {
        return Signature.createTypeSignature(((PrimitiveType) type).getPrimitiveTypeCode().toString(), false);
    }
    default:
        break;
    }
    return null;
}