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

The following examples show how to use org.eclipse.jdt.core.dom.VariableDeclaration#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: 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 2
Source File: PromoteTempToFieldRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Creates a new promote temp to field refactoring.
 * @param declaration the variable declaration node to convert to a field
 */
public PromoteTempToFieldRefactoring(VariableDeclaration declaration) {
	Assert.isTrue(declaration != null);
	fTempDeclarationNode= declaration;
	IVariableBinding resolveBinding= declaration.resolveBinding();
	Assert.isTrue(resolveBinding != null && !resolveBinding.isParameter() && !resolveBinding.isField());

	ASTNode root= declaration.getRoot();
	Assert.isTrue(root instanceof CompilationUnit);
	fCompilationUnitNode= (CompilationUnit) root;

	IJavaElement input= fCompilationUnitNode.getJavaElement();
	Assert.isTrue(input instanceof ICompilationUnit);
	fCu= (ICompilationUnit) input;

	fSelectionStart= declaration.getStartPosition();
	fSelectionLength= declaration.getLength();

       fFieldName= ""; //$NON-NLS-1$
       fVisibility= Modifier.PRIVATE;
       fDeclareStatic= false;
       fDeclareFinal= false;
       fInitializeIn= INITIALIZE_IN_METHOD;
       fLinkedProposalModel= null;
}
 
Example 3
Source File: TempOccurrenceAnalyzer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public TempOccurrenceAnalyzer(VariableDeclaration tempDeclaration, boolean analyzeJavadoc){
	Assert.isNotNull(tempDeclaration);
	fReferenceNodes= new HashSet<>();
	fJavadocNodes= new HashSet<>();
	fAnalyzeJavadoc= analyzeJavadoc;
	fTempDeclaration= tempDeclaration;
	fTempBinding= tempDeclaration.resolveBinding();
	fIsInJavadoc= false;
}
 
Example 4
Source File: RenameAnalyzeUtil.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean visit(SimpleName node) {
	VariableDeclaration decl= getVariableDeclaration(node);
	if (decl == null) {
		return super.visit(node);
	}

	IVariableBinding binding= decl.resolveBinding();
	if (binding == null) {
		return super.visit(node);
	}

	boolean keysEqual= fKey.equals(binding.getKey());
	boolean rangeInSet= fRanges.contains(new Region(node.getStartPosition(), node.getLength()));

	if (keysEqual && !rangeInSet) {
		fProblemNodes.add(node);
	}

	if (!keysEqual && rangeInSet) {
		fProblemNodes.add(node);
	}

	/*
	 * if (!keyEquals && !rangeInSet)
	 * 		ok, different local variable.
	 *
	 * if (keyEquals && rangeInSet)
	 * 		ok, renamed local variable & has been renamed.
	 */

	return super.visit(node);
}
 
Example 5
Source File: CompilationUnitBuilder.java    From j2cl with Apache License 2.0 5 votes vote down vote up
private Variable createVariable(VariableDeclaration variableDeclaration) {
  IVariableBinding variableBinding = variableDeclaration.resolveBinding();
  Variable variable =
      JdtUtils.createVariable(
          getSourcePosition(variableBinding.getName(), variableDeclaration.getName()),
          variableBinding);
  variableByJdtBinding.put(variableBinding, variable);
  recordEnclosingType(variable, getCurrentType());
  return variable;
}
 
Example 6
Source File: ASTFlattenerUtils.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
public Iterable<Expression> findAssignmentsInBlock(final Block scope, final VariableDeclaration varDecl) {
  final Function1<Expression, Boolean> _function = (Expression it) -> {
    Expression name = null;
    boolean _matched = false;
    if (it instanceof Assignment) {
      _matched=true;
      name = ((Assignment)it).getLeftHandSide();
    }
    if (!_matched) {
      if (it instanceof PrefixExpression) {
        _matched=true;
        name = ((PrefixExpression)it).getOperand();
      }
    }
    if (!_matched) {
      if (it instanceof PostfixExpression) {
        _matched=true;
        name = ((PostfixExpression)it).getOperand();
      }
    }
    if ((name instanceof Name)) {
      final IBinding binding = ((Name)name).resolveBinding();
      if ((binding instanceof IVariableBinding)) {
        final IVariableBinding declBinding = varDecl.resolveBinding();
        return Boolean.valueOf((varDecl.getName().getIdentifier().equals(this.toSimpleName(((Name)name))) && ((IVariableBinding)binding).equals(declBinding)));
      }
    }
    return Boolean.valueOf(false);
  };
  return this.findAssignmentsInBlock(scope, _function);
}
 
Example 7
Source File: MoveInstanceMethodProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the bindings of the method arguments of the specified
 * declaration.
 *
 * @param declaration
 *            the method declaration
 * @return the array of method argument variable bindings
 */
protected static IVariableBinding[] getArgumentBindings(final MethodDeclaration declaration) {
	Assert.isNotNull(declaration);
	final List<IVariableBinding> parameters= new ArrayList<IVariableBinding>(declaration.parameters().size());
	for (final Iterator<SingleVariableDeclaration> iterator= declaration.parameters().iterator(); iterator.hasNext();) {
		VariableDeclaration variable= iterator.next();
		IVariableBinding binding= variable.resolveBinding();
		if (binding == null)
			return new IVariableBinding[0];
		parameters.add(binding);
	}
	final IVariableBinding[] result= new IVariableBinding[parameters.size()];
	parameters.toArray(result);
	return result;
}
 
Example 8
Source File: TempOccurrenceAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public TempOccurrenceAnalyzer(VariableDeclaration tempDeclaration, boolean analyzeJavadoc){
	Assert.isNotNull(tempDeclaration);
	fReferenceNodes= new HashSet<SimpleName>();
	fJavadocNodes= new HashSet<SimpleName>();
	fAnalyzeJavadoc= analyzeJavadoc;
	fTempDeclaration= tempDeclaration;
	fTempBinding= tempDeclaration.resolveBinding();
	fIsInJavadoc= false;
}
 
Example 9
Source File: RenameAnalyzeUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean visit(SimpleName node) {
	VariableDeclaration decl= getVariableDeclaration(node);
	if (decl == null)
		return super.visit(node);

	IVariableBinding binding= decl.resolveBinding();
	if (binding == null)
		return super.visit(node);

	boolean keysEqual= fKey.equals(binding.getKey());
	boolean rangeInSet= fRanges.contains(new Region(node.getStartPosition(), node.getLength()));

	if (keysEqual && !rangeInSet)
		fProblemNodes.add(node);

	if (!keysEqual && rangeInSet)
		fProblemNodes.add(node);

	/*
	 * if (!keyEquals && !rangeInSet)
	 * 		ok, different local variable.
	 *
	 * if (keyEquals && rangeInSet)
	 * 		ok, renamed local variable & has been renamed.
	 */

	return super.visit(node);
}
 
Example 10
Source File: AbstractVariable.java    From JDeodorant with MIT License 5 votes vote down vote up
public AbstractVariable(VariableDeclaration name) {
	IVariableBinding variableBinding = name.resolveBinding();
	this.variableBindingKey = variableBinding.getKey();
	this.variableName = variableBinding.getName();
	this.variableType = variableBinding.getType().getQualifiedName();
	this.isField = variableBinding.isField();
	this.isParameter = variableBinding.isParameter();
	this.isStatic = (variableBinding.getModifiers() & Modifier.STATIC) != 0;
}
 
Example 11
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 12
Source File: PreconditionExaminer.java    From JDeodorant with MIT License 5 votes vote down vote up
private IVariableBinding getVariableBinding(AbstractVariable variable, Set<VariableDeclaration> variableDeclarations) {
	for(VariableDeclaration variableDeclaration : variableDeclarations) {
		if(variableDeclaration.resolveBinding().getKey().equals(variable.getVariableBindingKey())) {
			return variableDeclaration.resolveBinding();
		}
	}
	return null;
}
 
Example 13
Source File: ExtractMethodFragmentRefactoring.java    From JDeodorant with MIT License 4 votes vote down vote up
protected ITypeBinding extractTypeBinding(VariableDeclaration variableDeclaration) {
	IVariableBinding variableBinding = variableDeclaration.resolveBinding();
	return variableBinding.getType();
}
 
Example 14
Source File: PreconditionExaminer.java    From JDeodorant with MIT License 4 votes vote down vote up
private ITypeBinding extractTypeBinding(VariableDeclaration variableDeclaration) {
	IVariableBinding variableBinding = variableDeclaration.resolveBinding();
	return variableBinding.getType();
}
 
Example 15
Source File: ExtractMethodAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private void initReturnType(ImportRewrite rewriter) {
	AST ast= fEnclosingBodyDeclaration.getAST();
	fReturnType= null;
	fReturnTypeBinding= null;
	switch (fReturnKind) {
		case ACCESS_TO_LOCAL:
			VariableDeclaration declaration= ASTNodes.findVariableDeclaration(fReturnValue, fEnclosingBodyDeclaration);
			fReturnType= ASTNodeFactory.newType(ast, declaration, rewriter, new ContextSensitiveImportRewriteContext(declaration, rewriter));
			if (declaration.resolveBinding() != null) {
				fReturnTypeBinding= declaration.resolveBinding().getType();
			}
			break;
		case EXPRESSION:
			Expression expression= (Expression)getFirstSelectedNode();
			if (expression.getNodeType() == ASTNode.CLASS_INSTANCE_CREATION) {
				fExpressionBinding= ((ClassInstanceCreation)expression).getType().resolveBinding();
			} else {
				fExpressionBinding= expression.resolveTypeBinding();
			}
			if (fExpressionBinding != null) {
				if (fExpressionBinding.isNullType()) {
					getStatus().addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_null_type, JavaStatusContext.create(fCUnit, expression));
				} else {
					ITypeBinding normalizedBinding= Bindings.normalizeForDeclarationUse(fExpressionBinding, ast);
					if (normalizedBinding != null) {
						ImportRewriteContext context= new ContextSensitiveImportRewriteContext(fEnclosingBodyDeclaration, rewriter);
						fReturnType= rewriter.addImport(normalizedBinding, ast, context);
						fReturnTypeBinding= normalizedBinding;
					}
				}
			} else {
				fReturnType= ast.newPrimitiveType(PrimitiveType.VOID);
				fReturnTypeBinding= ast.resolveWellKnownType("void"); //$NON-NLS-1$
				getStatus().addError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_determine_return_type, JavaStatusContext.create(fCUnit, expression));
			}
			break;
		case RETURN_STATEMENT_VALUE:
			LambdaExpression enclosingLambdaExpr= ASTResolving.findEnclosingLambdaExpression(getFirstSelectedNode());
			if (enclosingLambdaExpr != null) {
				fReturnType= ASTNodeFactory.newReturnType(enclosingLambdaExpr, ast, rewriter, null);
				IMethodBinding methodBinding= enclosingLambdaExpr.resolveMethodBinding();
				fReturnTypeBinding= methodBinding != null ? methodBinding.getReturnType() : null;
			} else if (fEnclosingBodyDeclaration.getNodeType() == ASTNode.METHOD_DECLARATION) {
				fReturnType= ((MethodDeclaration) fEnclosingBodyDeclaration).getReturnType2();
				fReturnTypeBinding= fReturnType != null ? fReturnType.resolveBinding() : null;
			}
			break;
		default:
			fReturnType= ast.newPrimitiveType(PrimitiveType.VOID);
			fReturnTypeBinding= ast.resolveWellKnownType("void"); //$NON-NLS-1$
	}
	if (fReturnType == null) {
		fReturnType= ast.newPrimitiveType(PrimitiveType.VOID);
		fReturnTypeBinding= ast.resolveWellKnownType("void"); //$NON-NLS-1$
	}
}
 
Example 16
Source File: TempAssignmentFinder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
TempAssignmentFinder(VariableDeclaration tempDeclaration){
	fTempBinding= tempDeclaration.resolveBinding();
}
 
Example 17
Source File: ExtractMethodAnalyzer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private void initReturnType(ImportRewrite rewriter) {
	AST ast = fEnclosingBodyDeclaration.getAST();
	fReturnType = null;
	fReturnTypeBinding = null;
	switch (fReturnKind) {
		case ACCESS_TO_LOCAL:
			VariableDeclaration declaration = ASTNodes.findVariableDeclaration(fReturnValue, fEnclosingBodyDeclaration);
			fReturnType = ASTNodeFactory.newType(ast, declaration, rewriter, new ContextSensitiveImportRewriteContext(declaration, rewriter));
			if (declaration.resolveBinding() != null) {
				fReturnTypeBinding = declaration.resolveBinding().getType();
			}
			break;
		case EXPRESSION:
			Expression expression = (Expression) getFirstSelectedNode();
			if (expression.getNodeType() == ASTNode.CLASS_INSTANCE_CREATION) {
				fExpressionBinding = ((ClassInstanceCreation) expression).getType().resolveBinding();
			} else {
				fExpressionBinding = expression.resolveTypeBinding();
			}
			if (fExpressionBinding != null) {
				if (fExpressionBinding.isNullType()) {
					getStatus().addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_null_type, JavaStatusContext.create(fCUnit, expression));
				} else {
					ITypeBinding normalizedBinding = Bindings.normalizeForDeclarationUse(fExpressionBinding, ast);
					if (normalizedBinding != null) {
						ImportRewriteContext context = new ContextSensitiveImportRewriteContext(fEnclosingBodyDeclaration, rewriter);
						fReturnType = rewriter.addImport(normalizedBinding, ast, context, TypeLocation.RETURN_TYPE);
						fReturnTypeBinding = normalizedBinding;
					}
				}
			} else {
				fReturnType = ast.newPrimitiveType(PrimitiveType.VOID);
				fReturnTypeBinding = ast.resolveWellKnownType("void"); //$NON-NLS-1$
				getStatus().addError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_determine_return_type, JavaStatusContext.create(fCUnit, expression));
			}
			break;
		case RETURN_STATEMENT_VALUE:
			LambdaExpression enclosingLambdaExpr = ASTResolving.findEnclosingLambdaExpression(getFirstSelectedNode());
			if (enclosingLambdaExpr != null) {
				fReturnType = ASTNodeFactory.newReturnType(enclosingLambdaExpr, ast, rewriter, null);
				IMethodBinding methodBinding = enclosingLambdaExpr.resolveMethodBinding();
				fReturnTypeBinding = methodBinding != null ? methodBinding.getReturnType() : null;
			} else if (fEnclosingBodyDeclaration.getNodeType() == ASTNode.METHOD_DECLARATION) {
				fReturnType = ((MethodDeclaration) fEnclosingBodyDeclaration).getReturnType2();
				fReturnTypeBinding = fReturnType != null ? fReturnType.resolveBinding() : null;
			}
			break;
		default:
			fReturnType = ast.newPrimitiveType(PrimitiveType.VOID);
			fReturnTypeBinding = ast.resolveWellKnownType("void"); //$NON-NLS-1$
	}
	if (fReturnType == null) {
		fReturnType = ast.newPrimitiveType(PrimitiveType.VOID);
		fReturnTypeBinding = ast.resolveWellKnownType("void"); //$NON-NLS-1$
	}
}