Java Code Examples for org.eclipse.jdt.core.dom.SimpleName#resolveBinding()

The following examples show how to use org.eclipse.jdt.core.dom.SimpleName#resolveBinding() . 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: IntroduceParameterObjectProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private void updateSimpleName(ASTRewrite rewriter, ParameterInfo pi, SimpleName node, List<SingleVariableDeclaration> enclosingParameters, IJavaProject project) {
	AST ast= rewriter.getAST();
	IBinding binding= node.resolveBinding();
	Expression replacementNode= fParameterObjectFactory.createFieldReadAccess(pi, getParameterName(), ast, project, false, null);
	if (binding instanceof IVariableBinding) {
		IVariableBinding variable= (IVariableBinding) binding;
		if (variable.isParameter() && variable.getName().equals(getNameInScope(pi, enclosingParameters))) {
			rewriter.replace(node, replacementNode, null);
		}
	} else {
		ASTNode parent= node.getParent();
		if (!(parent instanceof QualifiedName || parent instanceof FieldAccess || parent instanceof SuperFieldAccess)) {
			if (node.getIdentifier().equals(getNameInScope(pi, enclosingParameters))) {
				rewriter.replace(node, replacementNode, null);
			}
		}
	}
}
 
Example 2
Source File: TempOccurrenceAnalyzer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public boolean visit(SimpleName node){
	if (node.getParent() instanceof VariableDeclaration){
		if (((VariableDeclaration)node.getParent()).getName() == node)
		 {
			return true; //don't include declaration
		}
	}

	if (fTempBinding != null && fTempBinding == node.resolveBinding()) {
		if (fIsInJavadoc) {
			fJavadocNodes.add(node);
		} else {
			fReferenceNodes.add(node);
		}
	}

	return true;
}
 
Example 3
Source File: RenameTypeParameterProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public boolean visit(SimpleName node) {
	IBinding binding= node.resolveBinding();
	if (fBinding == binding) {
		String groupDescription= null;
		if (node != fName) {
			if (fUpdateReferences) {
				groupDescription= RefactoringCoreMessages.RenameTypeParameterRefactoring_update_type_parameter_reference;
			}
		} else {
			groupDescription= RefactoringCoreMessages.RenameTypeParameterRefactoring_update_type_parameter_declaration;
		}
		if (groupDescription != null) {
			fRewrite.getASTRewrite().set(node, SimpleName.IDENTIFIER_PROPERTY, getNewElementName(), fRewrite.createGroupDescription(groupDescription));
		}
	}
	return true;
}
 
Example 4
Source File: FlowAnalyzer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void endVisit(SimpleName node) {
	if (skipNode(node) || node.isDeclaration()) {
		return;
	}
	IBinding binding = node.resolveBinding();
	if (binding instanceof IVariableBinding) {
		IVariableBinding variable = (IVariableBinding) binding;
		if (!variable.isField()) {
			setFlowInfo(node, new LocalFlowInfo(variable, FlowInfo.READ, fFlowContext));
		}
	} else if (binding instanceof ITypeBinding) {
		ITypeBinding type = (ITypeBinding) binding;
		if (type.isTypeVariable()) {
			setFlowInfo(node, new TypeVariableFlowInfo(type, fFlowContext));
		}
	}
}
 
Example 5
Source File: VariableDeclarationFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public boolean visit(VariableDeclarationExpression node) {
	if (fAddFinalLocals && node.fragments().size() == 1) {
		SimpleName name= ((VariableDeclarationFragment)node.fragments().get(0)).getName();

		IBinding binding= name.resolveBinding();
		if (binding == null)
			return false;

		if (fWrittenVariables.containsKey(binding))
			return false;

		ModifierChangeOperation op= createAddFinalOperation(name, node);
		if (op == null)
			return false;

		fResult.add(op);
		return false;
	}
	return false;
}
 
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: JdtASTMatcher.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public boolean match(SimpleName node, Object other) {
	boolean isomorphic= super.match(node, other);
	if (! isomorphic || !(other instanceof SimpleName))
		return false;
	SimpleName name= (SimpleName)other;
	IBinding nodeBinding= node.resolveBinding();
	IBinding otherBinding= name.resolveBinding();
	if (nodeBinding == null) {
		if (otherBinding != null) {
			return false;
		}
	} else {
		if (nodeBinding != otherBinding) {
			return false;
		}
	}
	if (node.resolveTypeBinding() != name.resolveTypeBinding())
		return false;
	return true;
}
 
Example 8
Source File: VariableDeclarationFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public boolean visit(SimpleName node) {
	if (node.getParent() instanceof VariableDeclarationFragment)
		return super.visit(node);
	if (node.getParent() instanceof SingleVariableDeclaration)
		return super.visit(node);

	IBinding binding= node.resolveBinding();
	if (!(binding instanceof IVariableBinding))
		return super.visit(node);

	binding= ((IVariableBinding)binding).getVariableDeclaration();
	if (ASTResolving.isWriteAccess(node)) {
		List<SimpleName> list;
		if (fResult.containsKey(binding)) {
			list= fResult.get(binding);
		} else {
			list= new ArrayList<SimpleName>();
		}
		list.add(node);
		fResult.put(binding, list);
	}

	return super.visit(node);
}
 
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) {
	IBinding binding = node.resolveBinding();
	if (binding != null && fForInitializerVariables.contains(binding)) {
		fReferringToForVariable = true;
	}
	return false;
}
 
Example 10
Source File: StyledStringVisitor.java    From JDeodorant with MIT License 5 votes vote down vote up
private boolean isStaticMethodCall(ASTNode node) {
	if (node instanceof SimpleName){
		SimpleName simpleName = (SimpleName) node;
		IBinding binding = simpleName.resolveBinding();
		if(binding != null && binding.getKind() == IBinding.METHOD) {
			IMethodBinding methodBinding = (IMethodBinding)binding;
			if((methodBinding.getModifiers() & Modifier.STATIC) != 0) 
				return true;
		}
	}
	return false;
}
 
Example 11
Source File: BindingSignatureVisitor.java    From JDeodorant with MIT License 5 votes vote down vote up
public boolean visit(SimpleName expr) {
	IBinding binding = expr.resolveBinding();
	if(binding != null) {
		bindingKeys.add(binding.getKey());
	}
	else {
		bindingKeys.add(expr.toString());
	}
	return false;
}
 
Example 12
Source File: MoveInnerToTopRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean isStaticFieldName(SimpleName name) {
	IBinding binding= name.resolveBinding();
	if (!(binding instanceof IVariableBinding))
		return false;
	IVariableBinding variableBinding= (IVariableBinding) binding;
	if (!variableBinding.isField())
		return false;
	return JdtFlags.isStatic(variableBinding);
}
 
Example 13
Source File: MoveInnerToTopRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean visit(SimpleName node) {
	IBinding binding= node.resolveBinding();
	if (binding instanceof IVariableBinding) {
		IVariableBinding variableBinding= (IVariableBinding) binding;
		if (variableBinding.isField()) {
			if (isAccessToOuter(variableBinding.getDeclaringClass())) {
				fSimpleNames.add(node);
			}
		}
	}
	return false;
}
 
Example 14
Source File: ExtractTempRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean visit(SimpleName node) {
	IBinding binding= node.resolveBinding();
	if (binding != null && fForInitializerVariables.contains(binding)) {
		fReferringToForVariable= true;
	}
	return false;
}
 
Example 15
Source File: FullConstraintCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public ITypeConstraint[] create(FieldAccess access){
	Expression expression= access.getExpression();
	SimpleName name= access.getName();
	IBinding binding= name.resolveBinding();
	if (! (binding instanceof IVariableBinding))
		return new ITypeConstraint[0];
	IVariableBinding vb= (IVariableBinding)binding;
	return createConstraintsForAccessToField(vb, expression, access);
}
 
Example 16
Source File: SourceAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean visit(SimpleName node) {
	addReferencesToName(node);
	IBinding binding= node.resolveBinding();
	if (binding instanceof ITypeBinding) {
		ITypeBinding type= (ITypeBinding)binding;
		if (type.isTypeVariable()) {
			addTypeVariableReference(type, node);
		}
	} else if (binding instanceof IVariableBinding) {
		IVariableBinding vb= (IVariableBinding)binding;
		if (vb.isField() && ! isStaticallyImported(node)) {
			Name topName= ASTNodes.getTopMostName(node);
			if (node == topName || node == ASTNodes.getLeftMostSimpleName(topName)) {
				StructuralPropertyDescriptor location= node.getLocationInParent();
				if (location != SingleVariableDeclaration.NAME_PROPERTY
					&& location != VariableDeclarationFragment.NAME_PROPERTY) {
					fImplicitReceivers.add(node);
				}
			}
		} else if (!vb.isField()) {
			// we have a local. Check if it is a parameter.
			ParameterData data= fParameters.get(binding);
			if (data != null) {
				ASTNode parent= node.getParent();
				if (parent instanceof Expression) {
					int precedence= OperatorPrecedence.getExpressionPrecedence((Expression)parent);
					if (precedence != Integer.MAX_VALUE) {
						data.setOperatorPrecedence(precedence);
					}
				}
			}
		}
	}
	return true;
}
 
Example 17
Source File: MoveInstanceMethodProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public final boolean visit(final SimpleName node) {
	Assert.isNotNull(node);
	final IBinding binding= node.resolveBinding();
	if (binding != null)
		if (isFieldAccess(node) && !isQualifiedEntity(node)) {
			final IVariableBinding variable= (IVariableBinding) binding;
			final String key= variable.getKey();
			if (!fFound.contains(key)) {
				fFound.add(key);
				fBindings.add(variable);
			}
		}
	return false;
}
 
Example 18
Source File: JDTUtils.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static IBinding resolveBinding(ASTNode node) {
	if (node instanceof SimpleName) {
		SimpleName simpleName = (SimpleName) node;
		// workaround for https://bugs.eclipse.org/62605 (constructor name resolves to type, not method)
		ASTNode normalized = ASTNodes.getNormalizedNode(simpleName);
		if (normalized.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY) {
			ClassInstanceCreation cic = (ClassInstanceCreation) normalized.getParent();
			IMethodBinding constructorBinding = cic.resolveConstructorBinding();
			if (constructorBinding == null) {
				return null;
			}
			ITypeBinding declaringClass = constructorBinding.getDeclaringClass();
			if (!declaringClass.isAnonymous()) {
				return constructorBinding;
			}
			ITypeBinding superTypeDeclaration = declaringClass.getSuperclass().getTypeDeclaration();
			return resolveSuperclassConstructor(superTypeDeclaration, constructorBinding);
		}
		return simpleName.resolveBinding();

	} else if (node instanceof SuperConstructorInvocation) {
		return ((SuperConstructorInvocation) node).resolveConstructorBinding();
	} else if (node instanceof ConstructorInvocation) {
		return ((ConstructorInvocation) node).resolveConstructorBinding();
	} else if (node instanceof LambdaExpression) {
		return ((LambdaExpression) node).resolveMethodBinding();
	} else {
		return null;
	}
}
 
Example 19
Source File: AbstractLoopUtilities.java    From JDeodorant with MIT License 4 votes vote down vote up
public static boolean isVariableLeftOperand(SimpleName variable, InfixExpression infixExpression)
{
	boolean leftOperandIsVariable     = false;
	Expression leftOperand            = infixExpression.getLeftOperand();
	//Expression rightOperand           = infixExpression.getRightOperand();
	IBinding infixVariableBinding     = variable.resolveBinding();
	if (leftOperand instanceof SimpleName)
	{
		SimpleName leftOperandSimpleName = (SimpleName) leftOperand;
		IBinding leftOperandBinding      = leftOperandSimpleName.resolveBinding();
		if (leftOperandBinding.isEqualTo(infixVariableBinding))
		{
			leftOperandIsVariable = true;
		}
	}
	else if (leftOperand instanceof InfixExpression)
	{
		InfixExpression infixLeftOperand = (InfixExpression) leftOperand;
		boolean left = isVariableLeftOperand(variable, infixLeftOperand);
		boolean right = isVariableRightOperand(variable, infixLeftOperand);
		List<Expression> extendedOperands = infixLeftOperand.extendedOperands();
		boolean variableFoundInExtendedOperands = false;
		for (Expression expression : extendedOperands)
		{
			if (expression instanceof SimpleName)
			{
				SimpleName simpleName = (SimpleName) expression;
				IBinding simpleNameBinding = simpleName.resolveBinding();
				if (simpleNameBinding.isEqualTo(infixVariableBinding))
				{
					variableFoundInExtendedOperands = true;
					break;
				}
			}
		}
		if (left || right || variableFoundInExtendedOperands)
		{
			leftOperandIsVariable = true;
		}
	}
	return leftOperandIsVariable;
}
 
Example 20
Source File: MoveInstanceMethodProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public final boolean visit(final SimpleName node) {
	Assert.isNotNull(node);
	final AST ast= node.getAST();
	final ASTRewrite rewrite= fRewrite;
	final IBinding binding= node.resolveBinding();
	if (binding instanceof ITypeBinding) {
		ITypeBinding type= (ITypeBinding) binding;
		String name= fTargetRewrite.getImportRewrite().addImport(type.getTypeDeclaration());
		if (name != null && name.indexOf('.') != -1) {
			fRewrite.replace(node, ASTNodeFactory.newName(ast, name), null);
			return false;
		}
	}
	if (Bindings.equals(fTarget, binding))
		if (fAnonymousClass > 0) {
			final ThisExpression target= ast.newThisExpression();
			target.setQualifier(ast.newSimpleName(fTargetType.getElementName()));
			fRewrite.replace(node, target, null);
		} else
			rewrite.replace(node, ast.newThisExpression(), null);
	else if (binding instanceof IVariableBinding) {
		final IVariableBinding variable= (IVariableBinding) binding;
		final IMethodBinding method= fDeclaration.resolveBinding();
		ITypeBinding declaring= variable.getDeclaringClass();
		if (method != null) {
			if (declaring != null && Bindings.isSuperType(declaring, method.getDeclaringClass(), false)) {
				declaring= declaring.getTypeDeclaration();
				if (JdtFlags.isStatic(variable))
					rewrite.replace(node, ast.newQualifiedName(ASTNodeFactory.newName(ast, fTargetRewrite.getImportRewrite().addImport(declaring)), ast.newSimpleName(node.getFullyQualifiedName())), null);
				else {
					final FieldAccess access= ast.newFieldAccess();
					access.setExpression(ast.newSimpleName(fTargetName));
					access.setName(ast.newSimpleName(node.getFullyQualifiedName()));
					rewrite.replace(node, access, null);
				}
			} else if (!(node.getParent() instanceof QualifiedName) && JdtFlags.isStatic(variable) && !fStaticImports.contains(variable) && !Checks.isEnumCase(node.getParent())) {
				rewrite.replace(node, ast.newQualifiedName(ASTNodeFactory.newName(ast, fTargetRewrite.getImportRewrite().addImport(declaring)), ast.newSimpleName(node.getFullyQualifiedName())), null);
			}
		}
	}
	return false;
}