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

The following examples show how to use org.eclipse.jdt.core.dom.ITypeBinding#equals() . 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: SurroundWithTryCatchRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
public static List<ITypeBinding> filterSubtypeExceptions(ITypeBinding[] exceptions) {
	List<ITypeBinding> filteredExceptions = new ArrayList<>();
	filteredExceptions.addAll(Arrays.asList(exceptions));

	for (Iterator<ITypeBinding> subtypeIterator = filteredExceptions.iterator(); subtypeIterator.hasNext();) {
		ITypeBinding iTypeBinding = subtypeIterator.next();
		for (Iterator<ITypeBinding> supertypeIterator = filteredExceptions.iterator(); supertypeIterator.hasNext();) {
			ITypeBinding superTypeBinding = supertypeIterator.next();
			if (!iTypeBinding.equals(superTypeBinding) && iTypeBinding.isSubTypeCompatible(superTypeBinding)) {
				subtypeIterator.remove();
				break;
			}
		}
	}
	return filteredExceptions;
}
 
Example 2
Source File: SurroundWithTryCatchRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private List<ITypeBinding> filterSubtypeExceptions(ITypeBinding[] exceptions) {
	List<ITypeBinding> filteredExceptions= new ArrayList<ITypeBinding>();
	filteredExceptions.addAll(Arrays.asList(exceptions));

	for (Iterator<ITypeBinding> subtypeIterator= filteredExceptions.iterator(); subtypeIterator.hasNext();) {
		ITypeBinding iTypeBinding= subtypeIterator.next();
		for (Iterator<ITypeBinding> supertypeIterator= filteredExceptions.iterator(); supertypeIterator.hasNext();) {
			ITypeBinding superTypeBinding= supertypeIterator.next();
			if (!iTypeBinding.equals(superTypeBinding) && iTypeBinding.isSubTypeCompatible(superTypeBinding)) {
				subtypeIterator.remove();
				break;
			}
		}
	}
	return filteredExceptions;
}
 
Example 3
Source File: VariableDeclarationFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private boolean isWrittenInTypeConstructors(List<SimpleName> writes, ITypeBinding declaringClass) {

			for (int i= 0; i < writes.size(); i++) {
	            SimpleName name= writes.get(i);

	            MethodDeclaration methodDeclaration= getWritingConstructor(name);
	            if (methodDeclaration == null)
	            	return false;

	            if (!methodDeclaration.isConstructor())
	            	return false;

	            IMethodBinding constructor= methodDeclaration.resolveBinding();
	            if (constructor == null)
	            	return false;

	            ITypeBinding declaringClass2= constructor.getDeclaringClass();
	            if (!declaringClass.equals(declaringClass2))
	            	return false;
            }

	        return true;
        }
 
Example 4
Source File: GenerateHashCodeEqualsAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
RefactoringStatus checkMember(Object memberBinding) {
	RefactoringStatus status= new RefactoringStatus();
	IVariableBinding variableBinding= (IVariableBinding)memberBinding;
	ITypeBinding fieldsType= variableBinding.getType();
	if (fieldsType.isArray())
		fieldsType= fieldsType.getElementType();
	if (!fieldsType.isPrimitive() && !fieldsType.isEnum() && !alreadyCheckedMemberTypes.contains(fieldsType) && !fieldsType.equals(fTypeBinding)) {
		status.merge(checkHashCodeEqualsExists(fieldsType, false));
		alreadyCheckedMemberTypes.add(fieldsType);
	}
	if (Modifier.isTransient(variableBinding.getModifiers()))
		status.addWarning(Messages.format(ActionMessages.GenerateHashCodeEqualsAction_transient_field_included_error, BasicElementLabels.getJavaElementName(variableBinding.getName())),
				createRefactoringStatusContext(variableBinding.getJavaElement()));
	return status;
}
 
Example 5
Source File: LocalCorrectionsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static List<ITypeBinding> filterSubtypeExceptions(ITypeBinding[] exceptions) {
	List<ITypeBinding> filteredExceptions= new ArrayList<ITypeBinding>();
	filteredExceptions.addAll(Arrays.asList(exceptions));

	for (Iterator<ITypeBinding> subtypeIterator= filteredExceptions.iterator(); subtypeIterator.hasNext();) {
		ITypeBinding iTypeBinding= subtypeIterator.next();
		for (Iterator<ITypeBinding> supertypeIterator= filteredExceptions.iterator(); supertypeIterator.hasNext();) {
			ITypeBinding superTypeBinding= supertypeIterator.next();
			if (!iTypeBinding.equals(superTypeBinding) && iTypeBinding.isSubTypeCompatible(superTypeBinding)) {
				subtypeIterator.remove();
				break;
			}
		}
	}
	return filteredExceptions;
}
 
Example 6
Source File: IntroduceIndirectionRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private RefactoringStatus findCommonParent(ITypeBinding typeBinding) {

		RefactoringStatus status= new RefactoringStatus();

		ITypeBinding highest= fIntermediaryFirstParameterType;
		ITypeBinding current= typeBinding;

		if (current.equals(highest) || Bindings.isSuperType(highest, current))
			// current is the same as highest or highest is already a supertype of current in the same hierarchy => no change
			return status;

		// find lowest common supertype with the method
		// search in bottom-up order
		ITypeBinding[] currentAndSupers= getTypeAndAllSuperTypes(current);
		ITypeBinding[] highestAndSupers= getTypeAndAllSuperTypes(highest);

		ITypeBinding foundBinding= null;
		for (int i1= 0; i1 < currentAndSupers.length; i1++) {
			for (int i2= 0; i2 < highestAndSupers.length; i2++) {
				if (highestAndSupers[i2].isEqualTo(currentAndSupers[i1])
						&& (Bindings.findMethodInHierarchy(highestAndSupers[i2], fTargetMethodBinding.getName(), fTargetMethodBinding.getParameterTypes()) != null)) {
					foundBinding= highestAndSupers[i2];
					break;
				}
			}
			if (foundBinding != null)
				break;
		}

		if (foundBinding != null) {
			fIntermediaryFirstParameterType= foundBinding;
		} else {
			String type1= BasicElementLabels.getJavaElementName(fIntermediaryFirstParameterType.getQualifiedName());
			String type2= BasicElementLabels.getJavaElementName(current.getQualifiedName());
			status.addFatalError(Messages.format(RefactoringCoreMessages.IntroduceIndirectionRefactoring_open_hierarchy_error, new String[] { type1, type2 }));
		}

		return status;
	}
 
Example 7
Source File: MethodsSourcePositionComparator.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public int compare(IMethodBinding firstMethodBinding, IMethodBinding secondMethodBinding) {
	if (firstMethodBinding == null || secondMethodBinding == null) {
		return 0;
	}
	ITypeBinding firstMethodType= firstMethodBinding.getDeclaringClass();
	ITypeBinding secondMethodType= secondMethodBinding.getDeclaringClass();

	if (firstMethodType.equals(secondMethodType)) {
		return compareInTheSameType(firstMethodBinding, secondMethodBinding);
	}

	if (firstMethodType.equals(fTypeBinding)) {
		return 1;
	}
	if (secondMethodType.equals(fTypeBinding)) {
		return -1;
	}

	ITypeBinding type= fTypeBinding;
	int count= 0, firstCount= -1, secondCount= -1;
	while ((type= type.getSuperclass()) != null) {
		if (firstMethodType.equals(type)) {
			firstCount= count;
		}
		if (secondMethodType.equals(type)) {
			secondCount= count;
		}
		count++;
	}
	if (firstCount != -1 && secondCount != -1) {
		return (firstCount - secondCount);
	}
	if (firstCount != -1 && secondCount == -1) {
		return 1;
	}
	if (firstCount == -1 && secondCount != -1) {
		return -1;
	}

	ITypeBinding[] interfaces= fTypeBinding.getInterfaces();
	for (int i= 0; i < interfaces.length; i++) {
		if (firstMethodType.equals(interfaces[i])) {
			return 1;
		}
		if (secondMethodType.equals(interfaces[i])) {
			return -1;
		}
	}
	return 0;
}
 
Example 8
Source File: ExtractMethodRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private List<SnippetFinder.Match> findValidDuplicates(ASTNode startNode) {
	List<Match> duplicates = SnippetFinder.perform(startNode, fAnalyzer.getSelectedNodes());
	List<SnippetFinder.Match> validDuplicates = new ArrayList<>();

	for (Match duplicate : duplicates) {
		if (duplicate != null && !duplicate.isInvalidNode()) {
			try {
				ASTNode[] nodes = duplicate.getNodes();
				int duplicateStart = nodes[0].getStartPosition();
				ASTNode lastNode = nodes[nodes.length - 1];
				int duplicateEnd = lastNode.getStartPosition() + lastNode.getLength();
				int duplicateLength = duplicateEnd - duplicateStart;
				ExtractMethodAnalyzer analyzer = new ExtractMethodAnalyzer(fCUnit, Selection.createFromStartLength(duplicateStart, duplicateLength));
				fRoot.accept(analyzer);
				RefactoringStatus result = new RefactoringStatus();
				result.merge(analyzer.checkInitialConditions(fImportRewriter));

				if (!result.hasFatalError()) {
					ITypeBinding originalReturnTypeBinding = fAnalyzer.getReturnTypeBinding();
					ITypeBinding duplicateReturnTypeBinding = analyzer.getReturnTypeBinding();

					if (originalReturnTypeBinding == null && duplicateReturnTypeBinding == null) {
						validDuplicates.add(duplicate);
					} else if (originalReturnTypeBinding != null && duplicateReturnTypeBinding != null) {
						if (!originalReturnTypeBinding.equals(duplicateReturnTypeBinding)) {
							if (duplicateReturnTypeBinding.equals(startNode.getAST().resolveWellKnownType("void"))) { //$NON-NLS-1$
								// extracted snippet returns non-void and duplicate snippet returns void => OK
								validDuplicates.add(duplicate);
							}
						} else {
							IVariableBinding originalReturnValBinding = fAnalyzer.getReturnValue();
							IVariableBinding duplicateReturnValBinding = analyzer.getReturnValue();

							if (originalReturnValBinding == null && duplicateReturnValBinding == null) {
								validDuplicates.add(duplicate);
							} else if (originalReturnValBinding != null && duplicateReturnValBinding != null) {
								BodyDeclaration originalEnclosingBodyDeclaration = fAnalyzer.getEnclosingBodyDeclaration();
								BodyDeclaration duplicateEnclosingBodyDeclaration = analyzer.getEnclosingBodyDeclaration();
								VariableDeclaration originalReturnNode = ASTNodes.findVariableDeclaration(originalReturnValBinding, originalEnclosingBodyDeclaration);
								VariableDeclaration duplicateReturnNode = ASTNodes.findVariableDeclaration(duplicateReturnValBinding, duplicateEnclosingBodyDeclaration);

								if (originalReturnNode != null && duplicateReturnNode != null) {
									boolean matches;
									if (!fAnalyzer.getSelection().covers(originalReturnNode) && !analyzer.getSelection().covers(duplicateReturnNode)) {
										// returned variables are defined outside of the selection => always OK
										matches = true;
									} else {
										matches = matchesLocationInEnclosingBodyDecl(originalEnclosingBodyDeclaration, duplicateEnclosingBodyDeclaration, originalReturnNode, duplicateReturnNode);
									}

									if (matches) {
										validDuplicates.add(duplicate);
									}
								}
							}
						}
					}
				}
			} catch (CoreException e) {
				// consider as invalid duplicate
			}
		}
	}
	return validDuplicates;
}
 
Example 9
Source File: IntroduceIndirectionRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Attempts to qualify a "this" expression for a method invocation with an appropriate qualifier.
 * The invoked method is analyzed according to the following specs:
 *
 * 'this' must be qualified iff method is declared in an enclosing type or a supertype of an enclosing type
 *
 * 1) The method is declared somewhere outside of the cu of the invocation
 *      1a) inside a supertype of the current type
 *      1b) inside a supertype of an enclosing type
 * 2) The method is declared inside of the cu of the invocation
 * 		2a) inside the type of the invocation
 * 		2b) outside the type of the invocation
 *
 * In case of 1a) and 2b), qualify with the enclosing type.
 * @param expr a {@link ThisExpression}
 * @param originalInvocation the original method invocation
 * @param enclosing the enclosing member of the original method invocation
 * @param unitRewriter the rewrite
 * @return resulting status
 *
 */
private RefactoringStatus qualifyThisExpression(ThisExpression expr, MethodInvocation originalInvocation, IMember enclosing, CompilationUnitRewrite unitRewriter) {

	RefactoringStatus status= new RefactoringStatus();

	IMethodBinding methodBinding= originalInvocation.resolveMethodBinding();
	MethodDeclaration methodDeclaration= (MethodDeclaration) ASTNodes.findDeclaration(methodBinding, originalInvocation.getRoot());

	ITypeBinding currentTypeBinding= null;
	if (methodDeclaration != null) {
		// Case 1) : Declaring type is inside this cu => use its name if it's declared in an enclosing type
		if (ASTNodes.isParent(originalInvocation, methodDeclaration.getParent()))
			currentTypeBinding= methodBinding.getDeclaringClass();
		else
			currentTypeBinding= ASTNodes.getEnclosingType(originalInvocation);
	} else {
		// Case 2) : Declaring type is outside of this cu => find subclass in this cu
		ASTNode currentTypeDeclaration= getEnclosingTypeDeclaration(originalInvocation);
		currentTypeBinding= ASTNodes.getEnclosingType(currentTypeDeclaration);
		while (currentTypeDeclaration != null && (Bindings.findMethodInHierarchy(currentTypeBinding, methodBinding.getName(), methodBinding.getParameterTypes()) == null)) {
			currentTypeDeclaration= getEnclosingTypeDeclaration(currentTypeDeclaration.getParent());
			currentTypeBinding= ASTNodes.getEnclosingType(currentTypeDeclaration);
		}
	}

	if (currentTypeBinding == null) {
		status.merge(createWarningAboutCall(enclosing, originalInvocation, RefactoringCoreMessages.IntroduceIndirectionRefactoring_call_warning_declaring_type_not_found));
		return status;
	}

	currentTypeBinding= currentTypeBinding.getTypeDeclaration();

	ITypeBinding typeOfCall= ASTNodes.getEnclosingType(originalInvocation);
	if (!typeOfCall.equals(currentTypeBinding)) {
		if (currentTypeBinding.isAnonymous()) {
			// Cannot qualify, see bug 115277
			status.merge(createWarningAboutCall(enclosing, originalInvocation, RefactoringCoreMessages.IntroduceIndirectionRefactoring_call_warning_anonymous_cannot_qualify));
		} else {
			expr.setQualifier(unitRewriter.getAST().newSimpleName(currentTypeBinding.getName()));
		}
	} else {
		// do not qualify, only use "this.".
	}

	return status;
}
 
Example 10
Source File: MethodsSourcePositionComparator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public int compare(IMethodBinding firstMethodBinding, IMethodBinding secondMethodBinding) {
	if (firstMethodBinding == null || secondMethodBinding == null) {
		return 0;
	}
	ITypeBinding firstMethodType= firstMethodBinding.getDeclaringClass();
	ITypeBinding secondMethodType= secondMethodBinding.getDeclaringClass();

	if (firstMethodType.equals(secondMethodType)) {
		return compareInTheSameType(firstMethodBinding, secondMethodBinding);
	}

	if (firstMethodType.equals(fTypeBinding)) {
		return 1;
	}
	if (secondMethodType.equals(fTypeBinding)) {
		return -1;
	}

	ITypeBinding type= fTypeBinding;
	int count= 0, firstCount= -1, secondCount= -1;
	while ((type= type.getSuperclass()) != null) {
		if (firstMethodType.equals(type)) {
			firstCount= count;
		}
		if (secondMethodType.equals(type)) {
			secondCount= count;
		}
		count++;
	}
	if (firstCount != -1 && secondCount != -1) {
		return (firstCount - secondCount);
	}
	if (firstCount != -1 && secondCount == -1) {
		return 1;
	}
	if (firstCount == -1 && secondCount != -1) {
		return -1;
	}

	ITypeBinding[] interfaces= fTypeBinding.getInterfaces();
	for (int i= 0; i < interfaces.length; i++) {
		if (firstMethodType.equals(interfaces[i])) {
			return 1;
		}
		if (secondMethodType.equals(interfaces[i])) {
			return -1;
		}
	}
	return 0;
}