Java Code Examples for org.eclipse.jdt.core.dom.IBinding#getKind()

The following examples show how to use org.eclipse.jdt.core.dom.IBinding#getKind() . 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: RemoveDeclarationCorrectionProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public String getName() {
	IBinding binding= fName.resolveBinding();
	String name= BasicElementLabels.getJavaElementName(fName.getIdentifier());
	switch (binding.getKind()) {
		case IBinding.TYPE:
			return Messages.format(CorrectionMessages.RemoveDeclarationCorrectionProposal_removeunusedtype_description, name);
		case IBinding.METHOD:
			if (((IMethodBinding) binding).isConstructor()) {
				return Messages.format(CorrectionMessages.RemoveDeclarationCorrectionProposal_removeunusedconstructor_description, name);
			} else {
				return Messages.format(CorrectionMessages.RemoveDeclarationCorrectionProposal_removeunusedmethod_description, name);
			}
		case IBinding.VARIABLE:
			if (((IVariableBinding) binding).isField()) {
				return Messages.format(CorrectionMessages.RemoveDeclarationCorrectionProposal_removeunusedfield_description, name);
			} else {
				return Messages.format(CorrectionMessages.RemoveDeclarationCorrectionProposal_removeunusedvar_description, name);
			}
		default:
			return super.getDisplayString();
	}
}
 
Example 2
Source File: ASTResolving.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static boolean isVariableDefinedInContext(IBinding binding, ITypeBinding typeVariable) {
	if (binding.getKind() == IBinding.VARIABLE) {
		IVariableBinding var= (IVariableBinding) binding;
		binding= var.getDeclaringMethod();
		if (binding == null) {
			binding= var.getDeclaringClass();
		}
	}
	if (binding instanceof IMethodBinding) {
		if (binding == typeVariable.getDeclaringMethod()) {
			return true;
		}
		binding= ((IMethodBinding) binding).getDeclaringClass();
	}

	while (binding instanceof ITypeBinding) {
		if (binding == typeVariable.getDeclaringClass()) {
			return true;
		}
		if (Modifier.isStatic(binding.getModifiers())) {
			break;
		}
		binding= ((ITypeBinding) binding).getDeclaringClass();
	}
	return false;
}
 
Example 3
Source File: SemanticHighlightings.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public boolean consumes(final SemanticToken token) {
	final SimpleName node= token.getNode();
	if (node.isDeclaration()) {
		return false;
	}

	final IBinding binding= token.getBinding();
	if (binding == null || binding.getKind() != IBinding.VARIABLE) {
		return false;
	}

	ITypeBinding currentType= Bindings.getBindingOfParentType(node);
	ITypeBinding declaringType= ((IVariableBinding) binding).getDeclaringClass();
	if (declaringType == null || currentType == declaringType)
		return false;

	return Bindings.isSuperType(declaringType, currentType);
}
 
Example 4
Source File: PreconditionExaminer.java    From JDeodorant with MIT License 6 votes vote down vote up
private boolean isVariableWithTypeMismatchDifference(Expression expression1, Expression expression2, ASTNodeDifference difference) {
	if(expression1 instanceof SimpleName && expression2 instanceof SimpleName) {
		SimpleName simpleName1 = (SimpleName)expression1;
		SimpleName simpleName2 = (SimpleName)expression2;
		IBinding binding1 = simpleName1.resolveBinding();
		IBinding binding2 = simpleName2.resolveBinding();
		//check if both simpleNames refer to variables
		if(binding1 != null && binding1.getKind() == IBinding.VARIABLE && binding2 != null && binding2.getKind() == IBinding.VARIABLE) {
			List<Difference> differences = difference.getDifferences();
			if(differences.size() == 1) {
				Difference diff = differences.get(0);
				if(diff.getType().equals(DifferenceType.SUBCLASS_TYPE_MISMATCH) || diff.getType().equals(DifferenceType.VARIABLE_TYPE_MISMATCH)) {
					return true;
				}
			}
		}
	}
	return false;
}
 
Example 5
Source File: QuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static boolean getRenameLocalProposals(IInvocationContext context, ASTNode node, IProblemLocation[] locations, Collection<ICommandAccess> resultingCollections) {
	if (!(node instanceof SimpleName)) {
		return false;
	}
	SimpleName name= (SimpleName) node;
	IBinding binding= name.resolveBinding();
	if (binding != null && binding.getKind() == IBinding.PACKAGE) {
		return false;
	}

	if (containsQuickFixableRenameLocal(locations)) {
		return false;
	}

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

	LinkedNamesAssistProposal proposal= new LinkedNamesAssistProposal(context, name);
	if (locations.length != 0) {
		proposal.setRelevance(IProposalRelevance.LINKED_NAMES_ASSIST_ERROR);
	}

	resultingCollections.add(proposal);
	return true;
}
 
Example 6
Source File: AbstractLoopUtilities.java    From JDeodorant with MIT License 6 votes vote down vote up
public static VariableDeclaration getVariableDeclaration(SimpleName variable)
{
	VariableDeclaration variableDeclaration        = null;
	MethodDeclaration method                       = findParentMethodDeclaration(variable);
	List<VariableDeclaration> variableDeclarations = getAllVariableDeclarations(method);
	// find the variable's initializer
	IBinding binding = variable.resolveBinding();
	if (binding.getKind() == IBinding.VARIABLE)
	{
		IVariableBinding variableBinding = (IVariableBinding) binding;
		for (VariableDeclaration currentVariableDeclaration : variableDeclarations)
		{
			IVariableBinding currentVariableDeclarationBinding = currentVariableDeclaration.resolveBinding();
			if (currentVariableDeclarationBinding.isEqualTo(variableBinding))
			{
				variableDeclaration = currentVariableDeclaration;
				break;
			}
		}
	}
	return variableDeclaration;
}
 
Example 7
Source File: Bindings.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static IBinding getDeclaration(IBinding binding) {
	switch (binding.getKind()) {
		case IBinding.TYPE:
			return ((ITypeBinding) binding).getTypeDeclaration();
		case IBinding.VARIABLE:
			return ((IVariableBinding) binding).getVariableDeclaration();
		case IBinding.METHOD:
			return ((IMethodBinding) binding).getMethodDeclaration();
	}
	return binding;
}
 
Example 8
Source File: OccurrencesFinder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private IBinding getBindingDeclaration(IBinding binding) {
	switch (binding.getKind()) {
		case IBinding.TYPE :
			return ((ITypeBinding)binding).getTypeDeclaration();
		case IBinding.METHOD :
			return ((IMethodBinding)binding).getMethodDeclaration();
		case IBinding.VARIABLE :
			return ((IVariableBinding)binding).getVariableDeclaration();
		default:
			return binding;
	}
}
 
Example 9
Source File: ExtractFieldRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean visit(SimpleName node) {
	ITypeBinding typeBinding = node.resolveTypeBinding();
	if (typeBinding != null && typeBinding.isLocal()) {
		if (node.isDeclaration()) {
			fLocalDefinitions.add(typeBinding);
		} else if (!fLocalDefinitions.contains(typeBinding)) {
			fLocalReferencesToEnclosing.add(node);
		}
	}
	if (typeBinding != null && typeBinding.isTypeVariable()) {
		if (node.isDeclaration()) {
			fLocalDefinitions.add(typeBinding);
		} else if (!fLocalDefinitions.contains(typeBinding)) {
			if (fMethodTypeVariables.contains(typeBinding)) {
				fLocalReferencesToEnclosing.add(node);
			} else {
				fClassTypeVariablesUsed = true;
			}
		}
	}
	IBinding binding = node.resolveBinding();
	if (binding != null && binding.getKind() == IBinding.VARIABLE && !((IVariableBinding) binding).isField()) {
		if (node.isDeclaration()) {
			fLocalDefinitions.add(binding);
		} else if (!fLocalDefinitions.contains(binding)) {
			fLocalReferencesToEnclosing.add(node);
		}
	}
	return super.visit(node);
}
 
Example 10
Source File: RenameAnalyzeUtil.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static VariableDeclaration getVariableDeclaration(Name node) {
	IBinding binding= node.resolveBinding();
	if (binding == null && node.getParent() instanceof VariableDeclaration) {
		return (VariableDeclaration) node.getParent();
	}

	if (binding != null && binding.getKind() == IBinding.VARIABLE) {
		CompilationUnit cu= ASTNodes.getParent(node, CompilationUnit.class);
		return ASTNodes.findVariableDeclaration( ((IVariableBinding) binding), cu);
	}
	return null;
}
 
Example 11
Source File: StyledStringVisitor.java    From JDeodorant with MIT License 5 votes vote down vote up
private boolean isNamedConstant(ASTNode node){
	if (node instanceof SimpleName){
		SimpleName simpleName = (SimpleName) node;
		IBinding binding = simpleName.resolveBinding();
		if(binding != null && binding.getKind() == IBinding.VARIABLE) {
			IVariableBinding variableBinding = (IVariableBinding)binding;
			if(variableBinding.isField() && (variableBinding.getModifiers() & Modifier.STATIC) != 0) 
				return true;
		}
	}
	return false;
}
 
Example 12
Source File: SystemObject.java    From JDeodorant with MIT License 5 votes vote down vote up
private boolean allStaticFieldsWithinSystemBoundary(List<SimpleName> staticFields) {
	for(SimpleName staticField : staticFields) {
		IBinding binding = staticField.resolveBinding();
		if(binding != null && binding.getKind() == IBinding.VARIABLE) {
			IVariableBinding variableBinding = (IVariableBinding)binding;
			ITypeBinding declaringClassTypeBinding = variableBinding.getDeclaringClass();
			if(declaringClassTypeBinding != null) {
				if(getPositionInClassList(declaringClassTypeBinding.getQualifiedName()) == -1 && !declaringClassTypeBinding.isEnum())
					return false;
			}
		}
	}
	return true;
}
 
Example 13
Source File: SemanticHighlightings.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean consumes(SemanticToken token) {
	IBinding binding = token.getBinding();
	if (binding != null && binding.getKind() == IBinding.VARIABLE && !((IVariableBinding) binding).isField()) {
		ASTNode decl = token.getRoot().findDeclaringNode(binding);
		return decl instanceof VariableDeclaration;
	}
	return false;
}
 
Example 14
Source File: NullAnnotationsFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean isComplainingAboutArgument(ASTNode selectedNode) {
	if (!(selectedNode instanceof SimpleName))
		return false;
	SimpleName nameNode= (SimpleName) selectedNode;
	IBinding binding= nameNode.resolveBinding();
	if (binding.getKind() == IBinding.VARIABLE && ((IVariableBinding) binding).isParameter())
		return true;
	VariableDeclaration argDecl= (VariableDeclaration) ASTNodes.getParent(selectedNode, VariableDeclaration.class);
	if (argDecl != null)
		binding= argDecl.resolveBinding();
	if (binding.getKind() == IBinding.VARIABLE && ((IVariableBinding) binding).isParameter())
		return true;
	return false;
}
 
Example 15
Source File: SemanticHighlightings.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean consumes(SemanticToken token) {
	SimpleName node = token.getNode();
	if (node.isDeclaration()) {
		return false;
	}

	IBinding binding = token.getBinding();
	return binding != null && binding.getKind() == IBinding.METHOD && (binding.getModifiers() & Modifier.STATIC) == Modifier.STATIC;
}
 
Example 16
Source File: SwitchControlCase.java    From JDeodorant with MIT License 5 votes vote down vote up
private String getConstantValue(Name name)
{
	IBinding nameBinding = name.resolveBinding();
	if (nameBinding.getKind() == IBinding.VARIABLE)
	{
		IVariableBinding nameVariableBinding = (IVariableBinding)nameBinding;
		Object valueObject = nameVariableBinding.getConstantValue();
		if (valueObject instanceof Integer || valueObject instanceof Byte || valueObject instanceof Character || valueObject instanceof String)
		{
			return valueObject.toString();
		}
	}
	return null;
}
 
Example 17
Source File: SemanticHighlightings.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public boolean consumes(SemanticToken token) {
	IBinding binding = getBinding(token);
	return binding != null && binding.getKind() == IBinding.METHOD;
}
 
Example 18
Source File: SemanticHighlightings.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public boolean consumes(SemanticToken token) {
	IBinding binding = token.getBinding();
	return binding != null && binding.getKind() == IBinding.VARIABLE && ((IVariableBinding) binding).isField();
}
 
Example 19
Source File: SemanticHighlightings.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public boolean consumes(SemanticToken token) {
	IBinding binding= token.getBinding();
	return binding != null && binding.getKind() == IBinding.VARIABLE && ((IVariableBinding)binding).isField();
}
 
Example 20
Source File: SemanticHighlightings.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public boolean consumes(SemanticToken token) {
	IBinding binding= token.getBinding();
	return binding != null && binding.getKind() == IBinding.VARIABLE && ((IVariableBinding)binding).isField() && (binding.getModifiers() & Modifier.STATIC) == Modifier.STATIC;
}