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

The following examples show how to use org.eclipse.jdt.core.dom.ITypeBinding#isEqualTo() . 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: OverrideMethodDialog.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public int compare(Viewer viewer, Object first, Object second) {
	if (first instanceof ITypeBinding && second instanceof ITypeBinding) {
		final ITypeBinding left= (ITypeBinding) first;
		final ITypeBinding right= (ITypeBinding) second;
		if (right.getQualifiedName().equals("java.lang.Object")) //$NON-NLS-1$
			return -1;
		if (left.isEqualTo(right))
			return 0;
		if (Bindings.isSuperType(left, right))
			return +1;
		else if (Bindings.isSuperType(right, left))
			return -1;
		return 0;
	} else
		return super.compare(viewer, first, second);
}
 
Example 2
Source File: PreconditionExaminer.java    From JDeodorant with MIT License 6 votes vote down vote up
private static Set<ITypeBinding> getAllSuperTypesUpToCommonSuperclass(ITypeBinding typeBinding, ITypeBinding commonSuperclass) {
	Set<ITypeBinding> superTypes = new LinkedHashSet<ITypeBinding>();
	ITypeBinding superTypeBinding = typeBinding.getSuperclass();
	if(superTypeBinding != null && !superTypeBinding.isEqualTo(commonSuperclass)) {
		superTypes.add(superTypeBinding);
		superTypes.addAll(getAllSuperTypesUpToCommonSuperclass(superTypeBinding, commonSuperclass));
	}
	ITypeBinding[] superInterfaces = typeBinding.getInterfaces();
	for(ITypeBinding superInterface : superInterfaces) {
		if(!superInterface.isEqualTo(commonSuperclass)) {
			superTypes.add(superInterface);
			superTypes.addAll(getAllSuperTypesUpToCommonSuperclass(superInterface, commonSuperclass));
		}
	}
	return superTypes;
}
 
Example 3
Source File: PreconditionExaminer.java    From JDeodorant with MIT License 6 votes vote down vote up
public ITypeBinding getReturnTypeBinding() {
	List<VariableDeclaration> returnedVariables1 = new ArrayList<VariableDeclaration>(getVariablesToBeReturnedG1());
	List<VariableDeclaration> returnedVariables2 = new ArrayList<VariableDeclaration>(getVariablesToBeReturnedG2());
	ITypeBinding returnTypeBinding = null;
	if(returnedVariables1.size() == 1 && returnedVariables2.size() == 1) {
		ITypeBinding returnTypeBinding1 = extractTypeBinding(returnedVariables1.get(0));
		ITypeBinding returnTypeBinding2 = extractTypeBinding(returnedVariables2.get(0));
		if(returnTypeBinding1.isEqualTo(returnTypeBinding2) && returnTypeBinding1.getQualifiedName().equals(returnTypeBinding2.getQualifiedName()))
			returnTypeBinding = returnTypeBinding1;
		else
			returnTypeBinding = ASTNodeMatcher.commonSuperType(returnTypeBinding1, returnTypeBinding2);
	}
	else {
		returnTypeBinding = findReturnTypeBinding();
	}
	return returnTypeBinding;
}
 
Example 4
Source File: PreconditionExaminer.java    From JDeodorant with MIT License 6 votes vote down vote up
private void extractReturnTypeBinding(PDGNode pdgNode, List<ITypeBinding> returnedTypeBindings) {
	if(pdgNode instanceof PDGExitNode) {
		PDGExitNode exitNode = (PDGExitNode)pdgNode;
		ReturnStatement returnStatement = (ReturnStatement)exitNode.getASTStatement();
		Expression returnedExpression = returnStatement.getExpression();
		if(returnedExpression != null && !(returnedExpression instanceof NullLiteral)) {
			ITypeBinding typeBinding = returnedExpression.resolveTypeBinding();
			if(typeBinding != null) {
				boolean alreadyContained = false;
				for(ITypeBinding binding : returnedTypeBindings) {
					if(binding.isEqualTo(typeBinding)) {
						alreadyContained = true;
						break;
					}
				}
				if(!alreadyContained)
					returnedTypeBindings.add(typeBinding);
			}
		}
	}
}
 
Example 5
Source File: GetterSetterUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Checks if the assignment needs a downcast and inserts it if necessary
 *
 * @param expression the right hand-side
 * @param expressionType the type of the right hand-side. Can be null
 * @param ast the AST
 * @param variableType the Type of the variable the expression will be assigned to
 * @param is50OrHigher if <code>true</code> java 5.0 code will be assumed
 * @return the casted expression if necessary
 */
private static Expression createNarrowCastIfNessecary(Expression expression, ITypeBinding expressionType, AST ast, ITypeBinding variableType, boolean is50OrHigher) {
	PrimitiveType castTo= null;
	if (variableType.isEqualTo(expressionType))
		return expression; //no cast for same type
	if (is50OrHigher) {
		if (ast.resolveWellKnownType("java.lang.Character").isEqualTo(variableType)) //$NON-NLS-1$
			castTo= ast.newPrimitiveType(PrimitiveType.CHAR);
		if (ast.resolveWellKnownType("java.lang.Byte").isEqualTo(variableType)) //$NON-NLS-1$
			castTo= ast.newPrimitiveType(PrimitiveType.BYTE);
		if (ast.resolveWellKnownType("java.lang.Short").isEqualTo(variableType)) //$NON-NLS-1$
			castTo= ast.newPrimitiveType(PrimitiveType.SHORT);
	}
	if (ast.resolveWellKnownType("char").isEqualTo(variableType)) //$NON-NLS-1$
		castTo= ast.newPrimitiveType(PrimitiveType.CHAR);
	if (ast.resolveWellKnownType("byte").isEqualTo(variableType)) //$NON-NLS-1$
		castTo= ast.newPrimitiveType(PrimitiveType.BYTE);
	if (ast.resolveWellKnownType("short").isEqualTo(variableType)) //$NON-NLS-1$
		castTo= ast.newPrimitiveType(PrimitiveType.SHORT);
	if (castTo != null) {
		CastExpression cast= ast.newCastExpression();
		if (NecessaryParenthesesChecker.needsParentheses(expression, cast, CastExpression.EXPRESSION_PROPERTY)) {
			ParenthesizedExpression parenthesized= ast.newParenthesizedExpression();
			parenthesized.setExpression(expression);
			cast.setExpression(parenthesized);
		} else
			cast.setExpression(expression);
		cast.setType(castTo);
		return cast;
	}
	return expression;
}
 
Example 6
Source File: FeatureEnvyVisualizationData.java    From JDeodorant with MIT License 5 votes vote down vote up
private void processExternalMethodInvocations(Map<AbstractVariable, ArrayList<MethodInvocationObject>> externalMethodInvocationMap,
		List<FieldInstructionObject> fieldInstructions, List<LocalVariableInstructionObject> localVariableInstructions, ClassObject targetClass) {
	for(AbstractVariable abstractVariable : externalMethodInvocationMap.keySet()) {
		PlainVariable variable = null;
		if(abstractVariable instanceof CompositeVariable) {
			variable = ((CompositeVariable)abstractVariable).getFinalVariable();
		}
		else {
			variable = (PlainVariable)abstractVariable;
		}
		ITypeBinding variableTypeBinding = null;
		if(variable.isField()) {
			FieldInstructionObject fieldInstruction = findFieldInstruction(variable, fieldInstructions);
			if(fieldInstruction != null)
				variableTypeBinding = fieldInstruction.getSimpleName().resolveTypeBinding();
		}
		else if(variable.isParameter()) {
			LocalVariableInstructionObject localVariableInstruction = findLocalVariableInstruction(variable, localVariableInstructions);
			if(localVariableInstruction != null)
				variableTypeBinding = localVariableInstruction.getSimpleName().resolveTypeBinding();
		}
		ITypeBinding targetClassBinding = targetClass.getAbstractTypeDeclaration().resolveBinding();
		if(variable.getVariableType().equals(targetClass.getName()) ||
				(variableTypeBinding != null && targetClassBinding.isEqualTo(variableTypeBinding.getSuperclass()))) {
			List<MethodInvocationObject> externalMethodInvocations = externalMethodInvocationMap.get(abstractVariable);
			for(MethodInvocationObject methodInvocation : externalMethodInvocations) {
				if(targetMethodInvocationMap.containsKey(methodInvocation)) {
					targetMethodInvocationMap.put(methodInvocation, targetMethodInvocationMap.get(methodInvocation) + 1);
				}
				else {
					targetMethodInvocationMap.put(methodInvocation, 1);
				}
			}
		}
	}
}
 
Example 7
Source File: PreconditionExaminer.java    From JDeodorant with MIT License 5 votes vote down vote up
private boolean superclassIsOneOfRefactoredSubclasses(ITypeBinding commonSuperTypeOfSourceTypeDeclarations,
		ITypeBinding typeBinding1, ITypeBinding typeBinding2) {
	if(typeBinding1.isEqualTo(commonSuperTypeOfSourceTypeDeclarations) ||
			typeBinding2.isEqualTo(commonSuperTypeOfSourceTypeDeclarations)) {
		return true;
	}
	return false;
}
 
Example 8
Source File: MethodCallAnalyzer.java    From JDeodorant with MIT License 5 votes vote down vote up
private MethodDeclaration getInvokedMethodDeclaration(IMethodBinding methodBinding) {
	MethodDeclaration invokedMethodDeclaration = null;
	IMethod iMethod2 = (IMethod)methodBinding.getJavaElement();
	if(iMethod2 != null) {
		IClassFile methodClassFile = iMethod2.getClassFile();
		LibraryClassStorage instance = LibraryClassStorage.getInstance();
		CompilationUnit methodCompilationUnit = instance.getCompilationUnit(methodClassFile);
		Set<TypeDeclaration> methodTypeDeclarations = extractTypeDeclarations(methodCompilationUnit);
		for(TypeDeclaration methodTypeDeclaration : methodTypeDeclarations) {
			ITypeBinding methodTypeDeclarationBinding = methodTypeDeclaration.resolveBinding();
			if(methodTypeDeclarationBinding != null && (methodTypeDeclarationBinding.isEqualTo(methodBinding.getDeclaringClass()) ||
					methodTypeDeclarationBinding.getBinaryName().equals(methodBinding.getDeclaringClass().getBinaryName()))) {
				MethodDeclaration[] methodDeclarations2 = methodTypeDeclaration.getMethods();
				for(MethodDeclaration methodDeclaration2 : methodDeclarations2) {
					if(methodDeclaration2.resolveBinding().isEqualTo(methodBinding) ||
							equalSignature(methodDeclaration2.resolveBinding(), methodBinding)) {
						invokedMethodDeclaration = methodDeclaration2;
						break;
					}
				}
				if(invokedMethodDeclaration != null)
					break;
			}
		}
	}
	return invokedMethodDeclaration;
}
 
Example 9
Source File: ASTNodeMatcher.java    From JDeodorant with MIT License 5 votes vote down vote up
private boolean subclassTypeMismatch(ITypeBinding nodeTypeBinding, ITypeBinding otherTypeBinding) {
	if(nodeTypeBinding.isParameterizedType() && otherTypeBinding.isParameterizedType()) {
		ITypeBinding declarationTypeBinding1 = nodeTypeBinding.getTypeDeclaration();
		ITypeBinding declarationTypeBinding2 = otherTypeBinding.getTypeDeclaration();
		return !declarationTypeBinding1.isEqualTo(declarationTypeBinding2) || !typeBindingMatch(nodeTypeBinding, otherTypeBinding);
	}
	return !nodeTypeBinding.isEqualTo(otherTypeBinding) || !nodeTypeBinding.getQualifiedName().equals(otherTypeBinding.getQualifiedName());
}
 
Example 10
Source File: ASTNodeMatcher.java    From JDeodorant with MIT License 5 votes vote down vote up
private static void addTypeBinding(ITypeBinding typeBinding, List<ITypeBinding> typeBindings) {
	boolean found = false;
	for(ITypeBinding typeBinding2 : typeBindings) {
		if(typeBinding.isEqualTo(typeBinding2) && typeBinding.getQualifiedName().equals(typeBinding2.getQualifiedName())) {
			found = true;
			break;
		}
	}
	if(!found) {
		typeBindings.add(typeBinding);
	}
}
 
Example 11
Source File: ExtractMethodFragmentRefactoring.java    From JDeodorant with MIT License 5 votes vote down vote up
private boolean equalOrExtend(ITypeBinding subTypeBinding, ITypeBinding superTypeBinding) {
	if(subTypeBinding.isEqualTo(superTypeBinding))
		return true;
	ITypeBinding superClassTypeBinding = subTypeBinding.getSuperclass();
	if(superClassTypeBinding != null) {
		return equalOrExtend(superClassTypeBinding, superTypeBinding);
	}
	else {
		return false;
	}
}
 
Example 12
Source File: TypeCheckElimination.java    From JDeodorant with MIT License 5 votes vote down vote up
private boolean equalTypeBindings(List<ITypeBinding> typeBindings) {
	ITypeBinding firstTypeBinding = typeBindings.get(0);
	for(int i=1; i<typeBindings.size(); i++) {
		ITypeBinding currentTypeBinding = typeBindings.get(i);
		if(!firstTypeBinding.isEqualTo(currentTypeBinding))
			return false;
	}
	return true;
}
 
Example 13
Source File: ASTNodes.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Returns the type to which an inlined variable initializer should be cast, or
 * <code>null</code> if no cast is necessary.
 * 
 * @param initializer the initializer expression of the variable to inline
 * @param reference the reference to the variable (which is to be inlined)
 * @return a type binding to which the initializer should be cast, or <code>null</code> iff no cast is necessary
 * @since 3.6
 */
public static ITypeBinding getExplicitCast(Expression initializer, Expression reference) {
	ITypeBinding initializerType= initializer.resolveTypeBinding();
	ITypeBinding referenceType= reference.resolveTypeBinding();
	if (initializerType == null || referenceType == null)
		return null;
	
	if (initializerType.isPrimitive() && referenceType.isPrimitive() && ! referenceType.isEqualTo(initializerType)) {
		return referenceType;
		
	} else if (initializerType.isPrimitive() && ! referenceType.isPrimitive()) { // initializer is autoboxed
		ITypeBinding unboxedReferenceType= Bindings.getUnboxedTypeBinding(referenceType, reference.getAST());
		if (!unboxedReferenceType.isEqualTo(initializerType))
			return unboxedReferenceType;
		else if (needsExplicitBoxing(reference))
			return referenceType;
		
	} else if (! initializerType.isPrimitive() && referenceType.isPrimitive()) { // initializer is autounboxed
		ITypeBinding unboxedInitializerType= Bindings.getUnboxedTypeBinding(initializerType, reference.getAST());
		if (!unboxedInitializerType.isEqualTo(referenceType))
			return referenceType;
		
	} else if (initializerType.isRawType() && referenceType.isParameterizedType()) {
		return referenceType; // don't lose the unchecked conversion

	} else if (initializer instanceof LambdaExpression || initializer instanceof MethodReference) {
		if (isTargetAmbiguous(reference, isExplicitlyTypedLambda(initializer))) {
			return referenceType;
		} else {
			ITypeBinding targetType= getTargetType(reference);
			if (targetType == null || targetType != referenceType) {
				return referenceType;
			}
		}

	} else if (! TypeRules.canAssign(initializerType, referenceType)) {
		if (!Bindings.containsTypeVariables(referenceType))
			return referenceType;
	}
	
	return null;
}
 
Example 14
Source File: PreconditionExaminer.java    From JDeodorant with MIT License 4 votes vote down vote up
public boolean isTemplateMethodApplicable() {
	AbstractMethodDeclaration methodObject1 = getPDG1().getMethod();
	AbstractMethodDeclaration methodObject2 = getPDG2().getMethod();
	MethodDeclaration methodDeclaration1 = methodObject1.getMethodDeclaration();
	MethodDeclaration methodDeclaration2 = methodObject2.getMethodDeclaration();
	
	ITypeBinding typeBinding1 = null;
	if(methodDeclaration1.getParent() instanceof AbstractTypeDeclaration) {
		typeBinding1 = ((AbstractTypeDeclaration)methodDeclaration1.getParent()).resolveBinding();
	}
	else if(methodDeclaration1.getParent() instanceof AnonymousClassDeclaration) {
		typeBinding1 = ((AnonymousClassDeclaration)methodDeclaration1.getParent()).resolveBinding();
	}
	ITypeBinding typeBinding2 = null;
	if(methodDeclaration2.getParent() instanceof AbstractTypeDeclaration) {
		typeBinding2 = ((AbstractTypeDeclaration)methodDeclaration2.getParent()).resolveBinding();
	}
	else if(methodDeclaration2.getParent() instanceof AnonymousClassDeclaration) {
		typeBinding2 = ((AnonymousClassDeclaration)methodDeclaration2.getParent()).resolveBinding();
	}
	if(typeBinding1 != null && typeBinding2 != null) {
		//not in the same type
		if(!typeBinding1.isEqualTo(typeBinding2) || !typeBinding1.getQualifiedName().equals(typeBinding2.getQualifiedName())) {
			ITypeBinding commonSuperTypeOfSourceTypeDeclarations = ASTNodeMatcher.commonSuperType(typeBinding1, typeBinding2);
			if(commonSuperTypeOfSourceTypeDeclarations != null) {
				ClassObject classObject = ASTReader.getSystemObject().getClassObject(commonSuperTypeOfSourceTypeDeclarations.getErasure().getQualifiedName());
				//abstract system class
				if(classObject != null && commonSuperTypeOfSourceTypeDeclarations.getErasure().isClass() && classObject.isAbstract()) {
					CompilationUnitCache cache = CompilationUnitCache.getInstance();
					Set<IType> subTypes = cache.getSubTypes((IType)commonSuperTypeOfSourceTypeDeclarations.getJavaElement());
					IType type1 = (IType)typeBinding1.getJavaElement();
					IType type2 = (IType)typeBinding2.getJavaElement();
					//only two subTypes corresponding to the types of the classes containing the clones
					if(subTypes.size() == 2 && subTypes.contains(type1) && subTypes.contains(type2)) {
						return true;
					}
				}
			}
		}
	}
	return false;	
}