Java Code Examples for org.eclipse.jdt.core.IType#resolveType()

The following examples show how to use org.eclipse.jdt.core.IType#resolveType() . 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: JavaElementLinks.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private static IType resolveType(IType baseType, String refTypeName) throws JavaModelException {
	if (refTypeName.length() == 0) {
		return baseType;
	}

	String[][] resolvedNames = baseType.resolveType(refTypeName);
	if (resolvedNames != null && resolvedNames.length > 0) {
		return baseType.getJavaProject().findType(resolvedNames[0][0], resolvedNames[0][1].replace('$', '.'), (IProgressMonitor) null);

	} else if (baseType.isBinary()) {
		// workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=206597
		IType type = baseType.getJavaProject().findType(refTypeName, (IProgressMonitor) null);
		if (type == null) {
			// could be unqualified reference:
			type = baseType.getJavaProject().findType(baseType.getPackageFragment().getElementName() + '.' + refTypeName, (IProgressMonitor) null);
		}
		return type;

	} else {
		return null;
	}
}
 
Example 2
Source File: JavaModelSearch.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Resolves a type name to a fully-qualified name.
 * <p>
 * Note: This only returns the first match on ambiguous lookups.
 *
 * @return the fully-qualified name, or null if it could not be resolved
 * @throws JavaModelException if there was an error when resolving
 */
public static String resolveTypeName(IType context, String typeName)
    throws JavaModelException {
  // resolveType may return multiple names if there are ambiguous matches
  String[][] matches = context.resolveType(typeName);
  if (matches == null) {
    return null;
  }

  // If there are multiple matches, we'll take the first one
  String matchPckg = matches[0][0];
  String matchType = matches[0][1];

  if (matchPckg.length() == 0) {
    return matchType;
  }

  return matchPckg + "." + matchType;
}
 
Example 3
Source File: PlatformJavaModelUtilities.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 6 votes vote down vote up
private static String resolveTypeName(IType context, String typeName)
    throws JavaModelException {
  // resolveType may return multiple names if there are ambiguous matches
  String[][] matches = context.resolveType(typeName);
  if (matches == null) {
    return null;
  }

  // If there are multiple matches, we'll take the first one
  String matchPckg = matches[0][0];
  String matchType = matches[0][1];

  if (matchPckg.length() == 0) {
    return matchType;
  }

  return matchPckg + "." + matchType;
}
 
Example 4
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 5
Source File: JavaElementLinks.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static IType resolveType(IType baseType, String refTypeName) throws JavaModelException {
	if (refTypeName.length() == 0)
		return baseType;

	String[][] resolvedNames= baseType.resolveType(refTypeName);
	if (resolvedNames != null && resolvedNames.length > 0) {
		return baseType.getJavaProject().findType(resolvedNames[0][0], resolvedNames[0][1].replace('$', '.'), (IProgressMonitor) null);

	} else if (baseType.isBinary()) {
		// workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=206597
		IType type= baseType.getJavaProject().findType(refTypeName, (IProgressMonitor) null);
		if (type == null) {
			// could be unqualified reference:
			type= baseType.getJavaProject().findType(baseType.getPackageFragment().getElementName() + '.' + refTypeName, (IProgressMonitor) null);
		}
		return type;

	} else {
		return null;
	}
}
 
Example 6
Source File: JavaUtils.java    From developer-studio with Apache License 2.0 6 votes vote down vote up
public static boolean isClassExtendedFrom(IProject project, String fullyQualifiedClassName,String classNameToCheck) throws JavaModelException{
	if (fullyQualifiedClassName.equals(classNameToCheck)){
		return true;
	} else {
		if (fullyQualifiedClassName.equals(TOP_LEVEL_SUPER_CLASS)){
			return false;
		}else{
			IJavaProject jp = JavaCore.create(project);
			IType findType = getJavaITypeForClass(jp, fullyQualifiedClassName);
			if (findType!=null && findType.getSuperclassName()!=null){
				String[][] resolveType = findType.resolveType(findType.getSuperclassName());
				if (resolveType!=null){
					String fullyQualifiedSuperClassName=(resolveType[0][0]).toString()+"."+(resolveType[0][1]).toString();
					boolean result = isClassExtendedFrom(project, fullyQualifiedSuperClassName,classNameToCheck);
					if (result){
						return true;
					}
				}
			}
		}
		return false;
	}
}
 
Example 7
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 8
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 9
Source File: WizardUtils.java    From txtUML with Eclipse Public License 1.0 5 votes vote down vote up
private static String[][] resolveType(IType context, String typeName) {
	try {
		return context.resolveType(typeName);
	} catch (JavaModelException e) {
		return new String[][] {};
	}
}
 
Example 10
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 11
Source File: CompilationUnitCompletion.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns <code>true</code> if <code>signature</code> is a concrete type signature,
 * <code>false</code> if it is a type variable.
 *
 * @param signature the signature to check
 * @param context the context inside which to resolve the type
 * @return <code>true</code> if the given signature is a concrete type signature
 * @throws JavaModelException if finding the type fails
 */
private boolean isConcreteType(String signature, IType context) throws JavaModelException {
	// Inexpensive check for the variable type first
	if (Signature.TYPE_VARIABLE_SIGNATURE == Signature.getTypeSignatureKind(signature))
		return false;

	// try and resolve otherwise
	if (context.isBinary()) {
		return fUnit.getJavaProject().findType(SignatureUtil.stripSignatureToFQN(signature)) != null;
	} else {
		return context.resolveType(SignatureUtil.stripSignatureToFQN(signature)) != null;
	}
}
 
Example 12
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 13
Source File: Jdt2Ecore.java    From sarl with Apache License 2.0 5 votes vote down vote up
private String resolveType(IType type, String signature) throws JavaModelException {
	final String[][] resolved = type.resolveType(Signature.toString(signature));
	if (resolved != null) {
		for (final String[] entry : resolved) {
			if (Strings.isNullOrEmpty(entry[0])) {
				return entry[1];
			}
			return entry[0] + "." + entry[1]; //$NON-NLS-1$
		}
	}
	return null;
}