Java Code Examples for org.eclipse.jdt.core.dom.ITypeBinding#getInterfaces()

The following examples show how to use org.eclipse.jdt.core.dom.ITypeBinding#getInterfaces() . 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: AbstractToStringGenerator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Checks whether given type implements given interface
 * 
 * @param memberType binding of the type to check
 * @param interfaceNames fully qualified names of the interfaces to seek for
 * @return array of booleans, every element is set to true if interface at the same position in
 *         <code>interfaceNames</code> is implemented by <code>memberType</code>
 */
protected boolean[] implementsInterfaces(ITypeBinding memberType, String[] interfaceNames) {
	boolean[] result= new boolean[interfaceNames.length];
	for (int i= 0; i < interfaceNames.length; i++) {
		if (memberType.getQualifiedName().equals(interfaceNames[i]))
			result[i]= true;
	}
	ITypeBinding[] interfaces= memberType.getInterfaces();
	for (int i= 0; i < interfaces.length; i++) {
		boolean[] deeper= implementsInterfaces(interfaces[i].getErasure(), interfaceNames);
		for (int j= 0; j < interfaceNames.length; j++) {
			result[j]= result[j] || deeper[j];
		}
	}
	return result;
}
 
Example 2
Source File: ScopeAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static boolean isInSuperTypeHierarchy(ITypeBinding possibleSuperTypeDecl, ITypeBinding type) {
	if (type == possibleSuperTypeDecl) {
		return true;
	}
	ITypeBinding superClass= type.getSuperclass();
	if (superClass != null) {
		if (isInSuperTypeHierarchy(possibleSuperTypeDecl, superClass.getTypeDeclaration())) {
			return true;
		}
	}
	if (possibleSuperTypeDecl.isInterface()) {
		ITypeBinding[] superInterfaces= type.getInterfaces();
		for (int i= 0; i < superInterfaces.length; i++) {
			if (isInSuperTypeHierarchy(possibleSuperTypeDecl, superInterfaces[i].getTypeDeclaration())) {
				return true;
			}
		}
	}
	return false;
}
 
Example 3
Source File: JavaTypeHierarchyExtractor.java    From tassal with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void getTypeBindingParents(ITypeBinding binding) {
	if (binding.isParameterizedType()) {
		binding = binding.getErasure();
	}
	final String bindingName = binding.isRecovered() ? binding
			.getName() : binding.getQualifiedName();
	final ITypeBinding superclassBinding = binding.getSuperclass();
	if (superclassBinding != null) {
		addTypes(
				superclassBinding.isRecovered() ? superclassBinding.getName()
						: superclassBinding.getQualifiedName(),
				bindingName);
		getTypeBindingParents(superclassBinding);
	}

	for (ITypeBinding iface : binding.getInterfaces()) {
		if (iface.isParameterizedType()) {
			iface = iface.getErasure();
		}
		addTypes(
				iface.isRecovered() ? iface.getName()
						: iface.getQualifiedName(), bindingName);
		getTypeBindingParents(iface);
	}
}
 
Example 4
Source File: Bindings.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Normalizes a type binding received from an expression to a type binding that can be used inside a
 * declaration signature, but <em>not</em> as type of a declaration (use {@link #normalizeForDeclarationUse(ITypeBinding, AST)} for that).
 * <p>
 * Anonymous types are normalized to the super class or interface. For
 * null or void bindings, <code>null</code> is returned.
 * </p>
 * 
 * @param binding the binding to normalize
 * @return the normalized binding, can be <code>null</code>
 * 
 * @see #normalizeForDeclarationUse(ITypeBinding, AST)
 */
public static ITypeBinding normalizeTypeBinding(ITypeBinding binding) {
	if (binding != null && !binding.isNullType() && !isVoidType(binding)) {
		if (binding.isAnonymous()) {
			ITypeBinding[] baseBindings= binding.getInterfaces();
			if (baseBindings.length > 0) {
				return baseBindings[0];
			}
			return binding.getSuperclass();
		}
		if (binding.isCapture()) {
			return binding.getWildcard();
		}
		return binding;
	}
	return null;
}
 
Example 5
Source File: JavaTypeHierarchyExtractor.java    From api-mining with GNU General Public License v3.0 6 votes vote down vote up
private void getTypeBindingParents(ITypeBinding binding) {
	if (binding.isParameterizedType()) {
		binding = binding.getErasure();
	}
	final String bindingName = binding.isRecovered() ? binding
			.getName() : binding.getQualifiedName();
	final ITypeBinding superclassBinding = binding.getSuperclass();
	if (superclassBinding != null) {
		addTypes(
				superclassBinding.isRecovered() ? superclassBinding.getName()
						: superclassBinding.getQualifiedName(),
				bindingName);
		getTypeBindingParents(superclassBinding);
	}

	for (ITypeBinding iface : binding.getInterfaces()) {
		if (iface.isParameterizedType()) {
			iface = iface.getErasure();
		}
		addTypes(
				iface.isRecovered() ? iface.getName()
						: iface.getQualifiedName(), bindingName);
		getTypeBindingParents(iface);
	}
}
 
Example 6
Source File: Bindings.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Finds the method specified by <code>methodName</code> and </code>parameters</code> in
 * the type hierarchy denoted by the given type. Returns <code>null</code> if no such method
 * exists. If the method is defined in more than one super type only the first match is
 * returned. First the super class is examined and then the implemented interfaces.
 * 
 * @param type The type to search the method in
 * @param methodName The name of the method to find
 * @param parameters The parameter types of the method to find. If <code>null</code> is passed, only the name is matched and parameters are ignored.
 * @return the method binding representing the method
 */
public static IMethodBinding findMethodInHierarchy(ITypeBinding type, String methodName, ITypeBinding[] parameters) {
	IMethodBinding method= findMethodInType(type, methodName, parameters);
	if (method != null)
		return method;
	ITypeBinding superClass= type.getSuperclass();
	if (superClass != null) {
		method= findMethodInHierarchy(superClass, methodName, parameters);
		if (method != null)
			return method;
	}
	ITypeBinding[] interfaces= type.getInterfaces();
	for (int i= 0; i < interfaces.length; i++) {
		method= findMethodInHierarchy(interfaces[i], methodName, parameters);
		if (method != null)
			return method;
	}
	return null;
}
 
Example 7
Source File: AbstractLoopUtilities.java    From JDeodorant with MIT License 6 votes vote down vote up
public static boolean isSubinterfaceOf(ITypeBinding typeBinding, String qualifiedName)
{
	if (typeBinding.getQualifiedName().startsWith(qualifiedName))
	{
		return true;
	}
	else
	{
		ITypeBinding[] superInterfaces = typeBinding.getInterfaces();
		for (ITypeBinding superInterface : superInterfaces)
		{
			if (isSubinterfaceOf(superInterface, qualifiedName))
			{
				return true;
			}
		}
		return false;
	}
}
 
Example 8
Source File: JavaTypeHierarchyExtractor.java    From codemining-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void getTypeBindingParents(ITypeBinding binding) {
	if (binding.isParameterizedType()) {
		binding = binding.getErasure();
	}
	final String bindingName = binding.isRecovered() ? binding
			.getName() : binding.getQualifiedName();
	final ITypeBinding superclassBinding = binding.getSuperclass();
	if (superclassBinding != null) {
		addTypes(
				superclassBinding.isRecovered() ? superclassBinding.getName()
						: superclassBinding.getQualifiedName(),
				bindingName);
		getTypeBindingParents(superclassBinding);
	}

	for (ITypeBinding iface : binding.getInterfaces()) {
		if (iface.isParameterizedType()) {
			iface = iface.getErasure();
		}
		addTypes(
				iface.isRecovered() ? iface.getName()
						: iface.getQualifiedName(), bindingName);
		getTypeBindingParents(iface);
	}
}
 
Example 9
Source File: IntroduceIndirectionRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private ITypeBinding getExpressionType(final MethodInvocation invocation, ITypeBinding typeBinding) {
	if (typeBinding == null)
		return null;
	for (IMethodBinding iMethodBinding : typeBinding.getDeclaredMethods()) {
		if (invocation.resolveMethodBinding() == iMethodBinding)
			return typeBinding.getTypeDeclaration();
	}
	ITypeBinding expressionType= getExpressionType(invocation, typeBinding.getSuperclass());
	if (expressionType != null) {
		return expressionType;
	}
	for (ITypeBinding interfaceBinding : typeBinding.getInterfaces()) {
		expressionType= getExpressionType(invocation, interfaceBinding);
		if (expressionType != null) {
			return expressionType;
		}
	}
	return null;
}
 
Example 10
Source File: ConvertAnonymousToNestedRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private ITypeBinding getSuperTypeBinding() {
	ITypeBinding types= fAnonymousInnerClassNode.resolveBinding();
	ITypeBinding[] interfaces= types.getInterfaces();
	if (interfaces.length > 0)
		return interfaces[0];
	else
		return types.getSuperclass();
}
 
Example 11
Source File: SuperTypeConstraintsCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the original methods of the method hierarchy of the specified method.
 *
 * @param binding the method binding
 * @param type the current type
 * @param originals the original methods which have already been found (element type: <code>IMethodBinding</code>)
 * @param implementations <code>true</code> to favor implementation methods, <code>false</code> otherwise
 */
private static void getOriginalMethods(final IMethodBinding binding, final ITypeBinding type, final Collection<IMethodBinding> originals, final boolean implementations) {
	final ITypeBinding ancestor= type.getSuperclass();
	if (!implementations) {
		final ITypeBinding[] types= type.getInterfaces();
		for (int index= 0; index < types.length; index++)
			getOriginalMethods(binding, types[index], originals, implementations);
		if (ancestor != null)
			getOriginalMethods(binding, ancestor, originals, implementations);
	}
	if (implementations && ancestor != null)
		getOriginalMethods(binding, ancestor, originals, implementations);
	final IMethodBinding[] methods= type.getDeclaredMethods();
	IMethodBinding method= null;
	for (int index= 0; index < methods.length; index++) {
		method= methods[index];
		if (!binding.getKey().equals(method.getKey())) {
			boolean match= false;
			IMethodBinding current= null;
			for (final Iterator<IMethodBinding> iterator= originals.iterator(); iterator.hasNext();) {
				current= iterator.next();
				if (Bindings.isSubsignature(method, current))
					match= true;
			}
			if (!match && Bindings.isSubsignature(binding, method))
				originals.add(method);
		}
	}
}
 
Example 12
Source File: ASTNodeMatcher.java    From JDeodorant with MIT License 5 votes vote down vote up
public static boolean implementsInterface(ITypeBinding typeBinding, ITypeBinding interfaceType) {
	ITypeBinding[] implementedInterfaces = typeBinding.getInterfaces();
	for(ITypeBinding implementedInterface : implementedInterfaces) {
		if(implementedInterface.getQualifiedName().equals(interfaceType.getQualifiedName()))
			return true;
	}
	return false;
}
 
Example 13
Source File: Utils.java    From txtUML with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean implementsInteraction(ITypeBinding typeBinding) {
	if (typeBinding == null) {
		return false;
	}
	String interactionName = "hu.elte.txtuml.api.model.seqdiag.Interaction";
	if (typeBinding.getQualifiedName().equals(interactionName)) {
		return true;
	}
	for (ITypeBinding interfaceType : typeBinding.getInterfaces()) {
		if (implementsInteraction(interfaceType)) {
			return true;
		}
	}
	return implementsInteraction(typeBinding.getSuperclass());
}
 
Example 14
Source File: Bindings.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns <code>true</code> if the given type is a super type of a candidate.
 * <code>true</code> is returned if the two type bindings are identical (TODO)
 * @param possibleSuperType the type to inspect
 * @param type the type whose super types are looked at
 * @param considerTypeArguments if <code>true</code>, consider type arguments of <code>type</code>
 * @return <code>true</code> iff <code>possibleSuperType</code> is
 * 		a super type of <code>type</code> or is equal to it
 */
public static boolean isSuperType(ITypeBinding possibleSuperType, ITypeBinding type, boolean considerTypeArguments) {
	if (type.isArray() || type.isPrimitive()) {
		return false;
	}
	if (! considerTypeArguments) {
		type= type.getTypeDeclaration();
	}
	if (Bindings.equals(type, possibleSuperType)) {
		return true;
	}
	ITypeBinding superClass= type.getSuperclass();
	if (superClass != null) {
		if (isSuperType(possibleSuperType, superClass, considerTypeArguments)) {
			return true;
		}
	}

	if (possibleSuperType.isInterface()) {
		ITypeBinding[] superInterfaces= type.getInterfaces();
		for (int i= 0; i < superInterfaces.length; i++) {
			if (isSuperType(possibleSuperType, superInterfaces[i], considerTypeArguments)) {
				return true;
			}
		}
	}
	return false;
}
 
Example 15
Source File: ExtractMethodAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public void checkInput(RefactoringStatus status, String methodName, ASTNode destination) {
	ITypeBinding[] arguments= getArgumentTypes();
	ITypeBinding type= ASTNodes.getEnclosingType(destination);
	status.merge(Checks.checkMethodInType(type, methodName, arguments));
	ITypeBinding superClass= type.getSuperclass();
	if (superClass != null) {
		status.merge(Checks.checkMethodInHierarchy(superClass, methodName, null, arguments));
	}
	for (ITypeBinding superInterface : type.getInterfaces()) {
		status.merge(Checks.checkMethodInHierarchy(superInterface, methodName, null, arguments));
	}
}
 
Example 16
Source File: LambdaExpressionsFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
static boolean isFunctionalAnonymous(ClassInstanceCreation node) {
	ITypeBinding typeBinding= node.resolveTypeBinding();
	if (typeBinding == null)
		return false;
	ITypeBinding[] interfaces= typeBinding.getInterfaces();
	if (interfaces.length != 1)
		return false;
	if (interfaces[0].getFunctionalInterfaceMethod() == null)
		return false;

	AnonymousClassDeclaration anonymTypeDecl= node.getAnonymousClassDeclaration();
	if (anonymTypeDecl == null || anonymTypeDecl.resolveBinding() == null)
		return false;
	
	List<BodyDeclaration> bodyDeclarations= anonymTypeDecl.bodyDeclarations();
	// cannot convert if there are fields or additional methods
	if (bodyDeclarations.size() != 1)
		return false;
	BodyDeclaration bodyDeclaration= bodyDeclarations.get(0);
	if (!(bodyDeclaration instanceof MethodDeclaration))
		return false;

	MethodDeclaration methodDecl= (MethodDeclaration) bodyDeclaration;
	IMethodBinding methodBinding= methodDecl.resolveBinding();

	if (methodBinding == null)
		return false;
	// generic lambda expressions are not allowed
	if (methodBinding.isGenericMethod())
		return false;

	// lambda cannot refer to 'this'/'super' literals
	if (SuperThisReferenceFinder.hasReference(methodDecl))
		return false;
	
	if (!isInTargetTypeContext(node))
		return false;
	
	return true;
}
 
Example 17
Source File: IntroduceIndirectionRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void collectSuperTypes(ITypeBinding curr, List<ITypeBinding> list) {
	if (list.add(curr.getTypeDeclaration())) {
		ITypeBinding[] interfaces= curr.getInterfaces();
		for (int i= 0; i < interfaces.length; i++) {
			collectSuperTypes(interfaces[i], list);
		}
		ITypeBinding superClass= curr.getSuperclass();
		if (superClass != null) {
			collectSuperTypes(superClass, list);
		}
	}
}
 
Example 18
Source File: JdtUtils.java    From j2cl with Apache License 2.0 5 votes vote down vote up
public static IMethodBinding getMethodBinding(
    ITypeBinding typeBinding, String methodName, ITypeBinding... parameterTypes) {

  Queue<ITypeBinding> deque = new ArrayDeque<>();
  deque.add(typeBinding);

  while (!deque.isEmpty()) {
    typeBinding = deque.poll();
    for (IMethodBinding methodBinding : typeBinding.getDeclaredMethods()) {
      if (methodBinding.getName().equals(methodName)
          && Arrays.equals(methodBinding.getParameterTypes(), parameterTypes)) {
        return methodBinding;
      }
    }
    ITypeBinding superclass = typeBinding.getSuperclass();
    if (superclass != null) {
      deque.add(superclass);
    }

    ITypeBinding[] superInterfaces = typeBinding.getInterfaces();
    if (superInterfaces != null) {
      for (ITypeBinding superInterface : superInterfaces) {
        deque.add(superInterface);
      }
    }
  }
  return null;
}
 
Example 19
Source File: StubUtility2.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static void findUnimplementedInterfaceMethods(ITypeBinding typeBinding, HashSet<ITypeBinding> visited,
		ArrayList<IMethodBinding> allMethods, IPackageBinding currPack, ArrayList<IMethodBinding> toImplement) {
	
	if (visited.add(typeBinding)) {
		IMethodBinding[] typeMethods= typeBinding.getDeclaredMethods();
		
		nextMethod: for (int i= 0; i < typeMethods.length; i++) {
			IMethodBinding curr= typeMethods[i];
			for (Iterator<IMethodBinding> allIter= allMethods.iterator(); allIter.hasNext();) {
				IMethodBinding oneMethod= allIter.next();
				if (Bindings.isSubsignature(oneMethod, curr)) {
					// We've already seen a method that is a subsignature of curr.
					if (!Bindings.isSubsignature(curr, oneMethod)) {
						// oneMethod is a true subsignature of curr; let's go with oneMethod
						continue nextMethod;
					}
					// Subsignatures are equivalent.
					// Check visibility and return types ('getErasure()' tries to achieve effect of "rename type variables")
					if (Bindings.isVisibleInHierarchy(oneMethod, currPack)
							&& oneMethod.getReturnType().getErasure().isSubTypeCompatible(curr.getReturnType().getErasure())) {
						// oneMethod is visible and curr doesn't have a stricter return type; let's go with oneMethod
						continue nextMethod;
					}
					// curr is stricter than oneMethod, so let's remove oneMethod
					allIter.remove();
					toImplement.remove(oneMethod);
				} else if (Bindings.isSubsignature(curr, oneMethod)) {
					// curr is a true subsignature of oneMethod. Let's remove oneMethod.
					allIter.remove();
					toImplement.remove(oneMethod);
				}
			}
			if (Modifier.isAbstract(curr.getModifiers())) {
				toImplement.add(curr);
				allMethods.add(curr);
			}
		}
		ITypeBinding[] superInterfaces= typeBinding.getInterfaces();
		for (int i= 0; i < superInterfaces.length; i++)
			findUnimplementedInterfaceMethods(superInterfaces[i], visited, allMethods, currPack, toImplement);
	}
}
 
Example 20
Source File: QuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static boolean getConvertAnonymousToNestedProposal(IInvocationContext context, final ASTNode node, Collection<ICommandAccess> proposals) throws CoreException {
	if (!(node instanceof Name))
		return false;

	ASTNode normalized= ASTNodes.getNormalizedNode(node);
	if (normalized.getLocationInParent() != ClassInstanceCreation.TYPE_PROPERTY)
		return false;

	final AnonymousClassDeclaration anonymTypeDecl= ((ClassInstanceCreation) normalized.getParent()).getAnonymousClassDeclaration();
	if (anonymTypeDecl == null || anonymTypeDecl.resolveBinding() == null) {
		return false;
	}

	if (proposals == null) {
		return true;
	}

	final ICompilationUnit cu= context.getCompilationUnit();
	final ConvertAnonymousToNestedRefactoring refactoring= new ConvertAnonymousToNestedRefactoring(anonymTypeDecl);
	
	String extTypeName= ASTNodes.getSimpleNameIdentifier((Name) node);
	ITypeBinding anonymTypeBinding= anonymTypeDecl.resolveBinding();
	String className;
	if (anonymTypeBinding.getInterfaces().length == 0) {
		className= Messages.format(CorrectionMessages.QuickAssistProcessor_name_extension_from_interface, extTypeName);
	} else {
		className= Messages.format(CorrectionMessages.QuickAssistProcessor_name_extension_from_class, extTypeName);
	}
	String[][] existingTypes= ((IType) anonymTypeBinding.getJavaElement()).resolveType(className);
	int i= 1;
	while (existingTypes != null) {
		i++;
		existingTypes= ((IType) anonymTypeBinding.getJavaElement()).resolveType(className + i);
	}
	refactoring.setClassName(i == 1 ? className : className + i);

	if (refactoring.checkInitialConditions(new NullProgressMonitor()).isOK()) {
		LinkedProposalModel linkedProposalModel= new LinkedProposalModel();
		refactoring.setLinkedProposalModel(linkedProposalModel);

		String label= CorrectionMessages.QuickAssistProcessor_convert_anonym_to_nested;
		Image image= JavaPlugin.getImageDescriptorRegistry().get(JavaElementImageProvider.getTypeImageDescriptor(true, false, Flags.AccPrivate, false));
		RefactoringCorrectionProposal proposal= new RefactoringCorrectionProposal(label, cu, refactoring, IProposalRelevance.CONVERT_ANONYMOUS_TO_NESTED, image);
		proposal.setLinkedProposalModel(linkedProposalModel);
		proposal.setCommandId(CONVERT_ANONYMOUS_TO_LOCAL_ID);
		proposals.add(proposal);
	}
	return false;
}