Java Code Examples for org.eclipse.jdt.core.Signature#getArrayCount()

The following examples show how to use org.eclipse.jdt.core.Signature#getArrayCount() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ChangeSignatureProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static List<ParameterInfo> createParameterInfoList(IMethod method) {
	try {
		String[] typeNames= method.getParameterTypes();
		String[] oldNames= method.getParameterNames();
		List<ParameterInfo> result= new ArrayList<ParameterInfo>(typeNames.length);
		for (int i= 0; i < oldNames.length; i++){
			ParameterInfo parameterInfo;
			if (i == oldNames.length - 1 && Flags.isVarargs(method.getFlags())) {
				String varargSignature= typeNames[i];
				int arrayCount= Signature.getArrayCount(varargSignature);
				String baseSignature= Signature.getElementType(varargSignature);
				if (arrayCount > 1)
					baseSignature= Signature.createArraySignature(baseSignature, arrayCount - 1);
				parameterInfo= new ParameterInfo(Signature.toString(baseSignature) + ParameterInfo.ELLIPSIS, oldNames[i], i);
			} else {
				parameterInfo= new ParameterInfo(Signature.toString(typeNames[i]), oldNames[i], i);
			}
			result.add(parameterInfo);
		}
		return result;
	} catch(JavaModelException e) {
		JavaPlugin.log(e);
		return new ArrayList<ParameterInfo>(0);
	}
}
 
Example 2
Source File: CompilationUnitCompletion.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns the type names of all member type bounds, as they would be
 * appear when referenced in the current compilation unit.
 *
 * @return type names of all member type bounds
 */
public String[] getMemberTypeNames() {
	String[] signatures= getMemberTypeSignatures();
	String[] names= new String[signatures.length];

	for (int i= 0; i < signatures.length; i++) {
		String sig= signatures[i];
		String local= fLocalTypes.get(Signature.getElementType(sig));
		int dim= Signature.getArrayCount(sig);
		if (local != null && dim > 0) {
			StringBuffer array= new StringBuffer(local);
			for (int j= 0; j < dim; j++)
				array.append("[]"); //$NON-NLS-1$
			local= array.toString();
		}
		if (local != null)
			names[i]= local;
		else
			names[i]= Signature.getSimpleName(Signature.getSignatureSimpleName(sig));
	}
	return names;
}
 
Example 3
Source File: JavaModelUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Resolves a type name in the context of the declaring type.
 *
 * @param refTypeSig the type name in signature notation (for example 'QVector') this can also be an array type, but dimensions will be ignored.
 * @param declaringType the context for resolving (type where the reference was made in)
 * @return returns the fully qualified type name or build-in-type name. if a unresolved type couldn't be resolved null is returned
 * @throws JavaModelException thrown when the type can not be accessed
 */
public static String getResolvedTypeName(String refTypeSig, IType declaringType) throws JavaModelException {
	int arrayCount= Signature.getArrayCount(refTypeSig);
	char type= refTypeSig.charAt(arrayCount);
	if (type == Signature.C_UNRESOLVED) {
		String name= ""; //$NON-NLS-1$
		int bracket= refTypeSig.indexOf(Signature.C_GENERIC_START, arrayCount + 1);
		if (bracket > 0)
			name= refTypeSig.substring(arrayCount + 1, bracket);
		else {
			int semi= refTypeSig.indexOf(Signature.C_SEMICOLON, arrayCount + 1);
			if (semi == -1) {
				throw new IllegalArgumentException();
			}
			name= refTypeSig.substring(arrayCount + 1, semi);
		}
		String[][] resolvedNames= declaringType.resolveType(name);
		if (resolvedNames != null && resolvedNames.length > 0) {
			return JavaModelUtil.concatenateName(resolvedNames[0][0], resolvedNames[0][1]);
		}
		return null;
	} else {
		return Signature.toString(refTypeSig.substring(arrayCount));
	}
}
 
Example 4
Source File: Binding2JavaModel.java    From lapse-plus with GNU General Public License v3.0 5 votes vote down vote up
private static boolean sameParameter(ITypeBinding type, String candidate, IType scope) throws JavaModelException {
    if (type.getDimensions() != Signature.getArrayCount(candidate))
        return false;
        
    // Normalizes types
    if (type.isArray())
        type= type.getElementType();
    candidate= Signature.getElementType(candidate);
    
    if (isPrimitiveType(candidate) || type.isPrimitive()) {
        return type.getName().equals(Signature.toString(candidate));
    } else {
        if (isResolvedType(candidate)) {
            return Signature.toString(candidate).equals(getFullyQualifiedName(type));
        } else {
            String[][] qualifiedCandidates= scope.resolveType(Signature.toString(candidate));
            if (qualifiedCandidates == null || qualifiedCandidates.length == 0)
                return false;
            String packageName= type.getPackage().isUnnamed() ? "" : type.getPackage().getName(); //$NON-NLS-1$
            String typeName= getTypeQualifiedName(type);
            for (int i= 0; i < qualifiedCandidates.length; i++) {
                String[] qualifiedCandidate= qualifiedCandidates[i];
                if (qualifiedCandidate[0].equals(packageName) &&
                        qualifiedCandidate[1].equals(typeName)) {
                    return true;
                        }
            }
        }
    }
    return false;
}
 
Example 5
Source File: TypeURIHelper.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected void createFragmentForArray(String signature, StringBuilder uriBuilder) {
	String elementType = Signature.getElementType(signature);
	createFragment(elementType, uriBuilder);
	int dim = Signature.getArrayCount(signature);
	for (int i = 0; i < dim; i++) {
		uriBuilder.append("[]");
	}
}
 
Example 6
Source File: JavaElementFinder.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
private void toQualifiedTypeName(String signature, IType context, StringBuilder result) throws JavaModelException {
	switch(Signature.getTypeSignatureKind(signature)) {
		case Signature.ARRAY_TYPE_SIGNATURE:
			toQualifiedTypeName(Signature.getElementType(signature), context, result);
			for(int i = 0; i < Signature.getArrayCount(signature); i++) {
				result.append("[]");
			}
			return;
		case Signature.CLASS_TYPE_SIGNATURE:
			if (signature.charAt(0) == Signature.C_UNRESOLVED) {
				String[][] resolved = context.resolveType(Signature.toString(signature));
				if (resolved != null && resolved.length == 1) {
					if (resolved[0][0] != null)
						result.append(resolved[0][0]);
					if (result.length() > 0)
						result.append('.');
					result.append(resolved[0][1]);
				} else {
					result.append(Signature.toString(signature));
				}
			} else {
				result.append(Signature.toString(signature));
			}
			return;
		default:
			result.append(Signature.toString(signature));
	}
}
 
Example 7
Source File: JavaDocLocations.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static void appendMethodReference(IMethod meth, StringBuffer buf) throws JavaModelException {
	buf.append(meth.getElementName());

	/*
	 * The Javadoc tool for Java SE 8 changed the anchor syntax and now tries to avoid "strange" characters in URLs.
	 * This breaks all clients that directly create such URLs.
	 * We can't know what format is required, so we just guess by the project's compiler compliance.
	 */
	boolean is18OrHigher = JavaModelUtil.is18OrHigher(meth.getJavaProject());
	buf.append(is18OrHigher ? '-' : '(');
	String[] params = meth.getParameterTypes();
	IType declaringType = meth.getDeclaringType();
	boolean isVararg = Flags.isVarargs(meth.getFlags());
	int lastParam = params.length - 1;
	for (int i = 0; i <= lastParam; i++) {
		if (i != 0) {
			buf.append(is18OrHigher ? "-" : ", "); //$NON-NLS-1$ //$NON-NLS-2$
		}
		String curr = Signature.getTypeErasure(params[i]);
		String fullName = JavaModelUtil.getResolvedTypeName(curr, declaringType);
		if (fullName == null) { // e.g. a type parameter "QE;"
			fullName = Signature.toString(Signature.getElementType(curr));
		}
		if (fullName != null) {
			buf.append(fullName);
			int dim = Signature.getArrayCount(curr);
			if (i == lastParam && isVararg) {
				dim--;
			}
			while (dim > 0) {
				buf.append(is18OrHigher ? ":A" : "[]"); //$NON-NLS-1$ //$NON-NLS-2$
				dim--;
			}
			if (i == lastParam && isVararg) {
				buf.append("..."); //$NON-NLS-1$
			}
		}
	}
	buf.append(is18OrHigher ? '-' : ')');
}
 
Example 8
Source File: JavaDocLocations.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static void appendMethodReference(IMethod meth, StringBuffer buf) throws JavaModelException {
	buf.append(meth.getElementName());

	/*
	 * The Javadoc tool for Java SE 8 changed the anchor syntax and now tries to avoid "strange" characters in URLs.
	 * This breaks all clients that directly create such URLs.
	 * We can't know what format is required, so we just guess by the project's compiler compliance.
	 */
	boolean is18OrHigher= JavaModelUtil.is18OrHigher(meth.getJavaProject());
	buf.append(is18OrHigher ? '-' : '(');
	String[] params= meth.getParameterTypes();
	IType declaringType= meth.getDeclaringType();
	boolean isVararg= Flags.isVarargs(meth.getFlags());
	int lastParam= params.length - 1;
	for (int i= 0; i <= lastParam; i++) {
		if (i != 0) {
			buf.append(is18OrHigher ? "-" : ", "); //$NON-NLS-1$ //$NON-NLS-2$
		}
		String curr= Signature.getTypeErasure(params[i]);
		String fullName= JavaModelUtil.getResolvedTypeName(curr, declaringType);
		if (fullName == null) { // e.g. a type parameter "QE;"
			fullName= Signature.toString(Signature.getElementType(curr));
		}
		if (fullName != null) {
			buf.append(fullName);
			int dim= Signature.getArrayCount(curr);
			if (i == lastParam && isVararg) {
				dim--;
			}
			while (dim > 0) {
				buf.append(is18OrHigher ? ":A" : "[]"); //$NON-NLS-1$ //$NON-NLS-2$
				dim--;
			}
			if (i == lastParam && isVararg) {
				buf.append("..."); //$NON-NLS-1$
			}
		}
	}
	buf.append(is18OrHigher ? '-' : ')');
}
 
Example 9
Source File: Bindings.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean sameParameter(ITypeBinding type, String candidate, IType scope) throws JavaModelException {
	if (type.getDimensions() != Signature.getArrayCount(candidate))
		return false;

	// Normalizes types
	if (type.isArray())
		type= type.getElementType();
	candidate= Signature.getElementType(candidate);

	if ((Signature.getTypeSignatureKind(candidate) == Signature.BASE_TYPE_SIGNATURE) != type.isPrimitive()) {
		return false;
	}

	if (type.isPrimitive() || type.isTypeVariable()) {
		return type.getName().equals(Signature.toString(candidate));
	} else {
		// normalize (quick hack until binding.getJavaElement works)
		candidate= Signature.getTypeErasure(candidate);
		type= type.getErasure();

		if (candidate.charAt(Signature.getArrayCount(candidate)) == Signature.C_RESOLVED) {
			return Signature.toString(candidate).equals(Bindings.getFullyQualifiedName(type));
		} else {
			String[][] qualifiedCandidates= scope.resolveType(Signature.toString(candidate));
			if (qualifiedCandidates == null || qualifiedCandidates.length == 0)
				return false;
			String packageName= type.getPackage().isUnnamed() ? "" : type.getPackage().getName(); //$NON-NLS-1$
			String typeName= getTypeQualifiedName(type);
			for (int i= 0; i < qualifiedCandidates.length; i++) {
				String[] qualifiedCandidate= qualifiedCandidates[i];
				if (	qualifiedCandidate[0].equals(packageName) &&
						qualifiedCandidate[1].equals(typeName))
					return true;
			}
		}
	}
	return false;
}
 
Example 10
Source File: SignatureUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the qualified signature corresponding to
 * <code>signature</code>.
 *
 * @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;
}
 
Example 11
Source File: Binding2JavaModel.java    From lapse-plus with GNU General Public License v3.0 4 votes vote down vote up
private static boolean isResolvedType(String s) {
    int arrayCount= Signature.getArrayCount(s);
    return s.charAt(arrayCount) == Signature.C_RESOLVED;
}
 
Example 12
Source File: JavaElementLabelComposer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
protected void appendTypeSignatureLabel(IJavaElement enclosingElement, String typeSig, long flags) {
	int sigKind= Signature.getTypeSignatureKind(typeSig);
	switch (sigKind) {
	case Signature.BASE_TYPE_SIGNATURE:
		fBuilder.append(Signature.toString(typeSig));
		break;
	case Signature.ARRAY_TYPE_SIGNATURE:
		appendTypeSignatureLabel(enclosingElement, Signature.getElementType(typeSig), flags);
		for (int dim= Signature.getArrayCount(typeSig); dim > 0; dim--) {
			fBuilder.append('[').append(']');
		}
		break;
	case Signature.CLASS_TYPE_SIGNATURE:
		String baseType= getSimpleTypeName(enclosingElement, typeSig);
		fBuilder.append(baseType);

		String[] typeArguments= Signature.getTypeArguments(typeSig);
		appendTypeArgumentSignaturesLabel(enclosingElement, typeArguments, flags);
		break;
	case Signature.TYPE_VARIABLE_SIGNATURE:
		fBuilder.append(getSimpleTypeName(enclosingElement, typeSig));
		break;
	case Signature.WILDCARD_TYPE_SIGNATURE:
		char ch= typeSig.charAt(0);
		if (ch == Signature.C_STAR) { //workaround for bug 85713
			fBuilder.append('?');
		} else {
			if (ch == Signature.C_EXTENDS) {
				fBuilder.append("? extends "); //$NON-NLS-1$
				appendTypeSignatureLabel(enclosingElement, typeSig.substring(1), flags);
			} else if (ch == Signature.C_SUPER) {
				fBuilder.append("? super "); //$NON-NLS-1$
				appendTypeSignatureLabel(enclosingElement, typeSig.substring(1), flags);
			}
		}
		break;
	case Signature.CAPTURE_TYPE_SIGNATURE:
		appendTypeSignatureLabel(enclosingElement, typeSig.substring(1), flags);
		break;
	case Signature.INTERSECTION_TYPE_SIGNATURE:
		String[] typeBounds= Signature.getIntersectionTypeBounds(typeSig);
		appendTypeBoundsSignaturesLabel(enclosingElement, typeBounds, flags);
		break;
	default:
		// unknown
	}
}
 
Example 13
Source File: GenerateDelegateMethodsHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private static boolean isArray(IField field) throws JavaModelException {
	return Signature.getArrayCount(field.getTypeSignature()) > 0;
}
 
Example 14
Source File: JavaModelSearch.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 4 votes vote down vote up
private static boolean methodSignaturesEqual(IType type2, String methodName,
    String[] paramTypes, boolean isConstructor, IMethod method2) {
  try {
    // Method names must match, unless we're comparing constructors
    if (!isConstructor && !method2.getElementName().equals(methodName)) {
      return false;
    }

    // Only compare ctors to ctors and methods to methods
    if (isConstructor != method2.isConstructor()) {
      return false;
    }

    // Parameter count must match
    String signature2 = method2.getSignature();
    String[] paramTypes2 = Signature.getParameterTypes(signature2);
    if (paramTypes.length != paramTypes2.length) {
      return false;
    }

    // Compare each parameter type
    for (int i = 0; i < paramTypes.length; i++) {
      String paramType = paramTypes[i];
      String paramType2 = paramTypes2[i];

      // Compare array nesting depth ([] = 1, [][] = 2, etc.)
      if (Signature.getArrayCount(paramType) != Signature.getArrayCount(paramType2)) {
        return false;
      }

      // Remove any array nesting and generic type parameters
      paramType = getBaseType(paramType);
      paramType2 = getBaseType(paramType2);

      // Extract the full type names from the signatures
      String paramTypeName = getQualifiedTypeName(paramType);
      String paramTypeName2 = getQualifiedTypeName(paramType2);

      if (isTypeParameter(method2, paramTypeName2)) {
        // Skip parameters whose type is a generic type parameter of the
        // method we're comparing against, or the method's containing class
        continue;

        /*
         * TODO: we're currently not checking the bounds of generic type
         * parameters, so sometimes we may return true here even when the
         * caller's method signature doesn't match the method we're comparing
         * against. We could try to add that logic here, or better still, we
         * could integrate TypeOracle and take advantage of its type searching
         * capabilities.
         */
      }

      // If we run into an unresolved parameter type in the method we're
      // searching, we'll need to resolve that before doing the comparison
      if (paramType2.charAt(0) == Signature.C_UNRESOLVED) {
        paramTypeName2 = resolveTypeName(type2, paramTypeName2);
      }

      // Finally, compare the type names
      if (!paramTypeName.equals(paramTypeName2)) {
        return false;
      }
    }

    // We passed all the checks, so the signatures are equal
    return true;

  } catch (JavaModelException e) {
    CorePluginLog.logError(e,
        "Error comparing method signatures of {0} and {1}", methodName,
        method2.getElementName());
    return false;
  }
}
 
Example 15
Source File: AddDelegateMethodsAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static boolean isArray(IField field) throws JavaModelException {
	return Signature.getArrayCount(field.getTypeSignature()) > 0;
}
 
Example 16
Source File: JavaElementLabelComposer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
protected void appendTypeSignatureLabel(IJavaElement enclosingElement, String typeSig, long flags) {
	int sigKind= Signature.getTypeSignatureKind(typeSig);
	switch (sigKind) {
		case Signature.BASE_TYPE_SIGNATURE:
			fBuffer.append(Signature.toString(typeSig));
			break;
		case Signature.ARRAY_TYPE_SIGNATURE:
			appendTypeSignatureLabel(enclosingElement, Signature.getElementType(typeSig), flags);
			for (int dim= Signature.getArrayCount(typeSig); dim > 0; dim--) {
				fBuffer.append('[').append(']');
			}
			break;
		case Signature.CLASS_TYPE_SIGNATURE:
			String baseType= getSimpleTypeName(enclosingElement, typeSig);
			fBuffer.append(baseType);

			String[] typeArguments= Signature.getTypeArguments(typeSig);
			appendTypeArgumentSignaturesLabel(enclosingElement, typeArguments, flags);
			break;
		case Signature.TYPE_VARIABLE_SIGNATURE:
			fBuffer.append(getSimpleTypeName(enclosingElement, typeSig));
			break;
		case Signature.WILDCARD_TYPE_SIGNATURE:
			char ch= typeSig.charAt(0);
			if (ch == Signature.C_STAR) { //workaround for bug 85713
				fBuffer.append('?');
			} else {
				if (ch == Signature.C_EXTENDS) {
					fBuffer.append("? extends "); //$NON-NLS-1$
					appendTypeSignatureLabel(enclosingElement, typeSig.substring(1), flags);
				} else if (ch == Signature.C_SUPER) {
					fBuffer.append("? super "); //$NON-NLS-1$
					appendTypeSignatureLabel(enclosingElement, typeSig.substring(1), flags);
				}
			}
			break;
		case Signature.CAPTURE_TYPE_SIGNATURE:
			appendTypeSignatureLabel(enclosingElement, typeSig.substring(1), flags);
			break;
		case Signature.INTERSECTION_TYPE_SIGNATURE:
			String[] typeBounds= Signature.getIntersectionTypeBounds(typeSig);
			appendTypeBoundsSignaturesLabel(enclosingElement, typeBounds, flags);
			break;
		default:
			// unknown
	}
}