Java Code Examples for org.eclipse.jdt.internal.corext.dom.ASTNodes#findVariableDeclaration()

The following examples show how to use org.eclipse.jdt.internal.corext.dom.ASTNodes#findVariableDeclaration() . 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: ExtractMethodRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private void initializeParameterInfos() {
	IVariableBinding[] arguments = fAnalyzer.getArguments();
	fParameterInfos = new ArrayList<>(arguments.length);
	ASTNode root = fAnalyzer.getEnclosingBodyDeclaration();
	ParameterInfo vararg = null;
	for (int i = 0; i < arguments.length; i++) {
		IVariableBinding argument = arguments[i];
		if (argument == null) {
			continue;
		}
		VariableDeclaration declaration = ASTNodes.findVariableDeclaration(argument, root);
		boolean isVarargs = declaration instanceof SingleVariableDeclaration ? ((SingleVariableDeclaration) declaration).isVarargs() : false;
		ParameterInfo info = new ParameterInfo(argument, getType(declaration, isVarargs), argument.getName(), i);
		if (isVarargs) {
			vararg = info;
		} else {
			fParameterInfos.add(info);
		}
	}
	if (vararg != null) {
		fParameterInfos.add(vararg);
	}
}
 
Example 2
Source File: ExtractMethodRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private void initializeParameterInfos() {
	IVariableBinding[] arguments= fAnalyzer.getArguments();
	fParameterInfos= new ArrayList<ParameterInfo>(arguments.length);
	ASTNode root= fAnalyzer.getEnclosingBodyDeclaration();
	ParameterInfo vararg= null;
	for (int i= 0; i < arguments.length; i++) {
		IVariableBinding argument= arguments[i];
		if (argument == null)
			continue;
		VariableDeclaration declaration= ASTNodes.findVariableDeclaration(argument, root);
		boolean isVarargs= declaration instanceof SingleVariableDeclaration
			? ((SingleVariableDeclaration)declaration).isVarargs()
			: false;
		ParameterInfo info= new ParameterInfo(argument, getType(declaration, isVarargs), argument.getName(), i);
		if (isVarargs) {
			vararg= info;
		} else {
			fParameterInfos.add(info);
		}
	}
	if (vararg != null) {
		fParameterInfos.add(vararg);
	}
}
 
Example 3
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 4
Source File: ExtractMethodRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private VariableDeclarationStatement createDeclaration(IVariableBinding binding, Expression intilizer) {
	VariableDeclaration original = ASTNodes.findVariableDeclaration(binding, fAnalyzer.getEnclosingBodyDeclaration());
	VariableDeclarationFragment fragment = fAST.newVariableDeclarationFragment();
	fragment.setName((SimpleName) ASTNode.copySubtree(fAST, original.getName()));
	fragment.setInitializer(intilizer);
	VariableDeclarationStatement result = fAST.newVariableDeclarationStatement(fragment);
	result.modifiers().addAll(ASTNode.copySubtrees(fAST, ASTNodes.getModifiers(original)));
	result.setType(ASTNodeFactory.newType(fAST, original, fImportRewriter, new ContextSensitiveImportRewriteContext(original, fImportRewriter)));
	return result;
}
 
Example 5
Source File: RenameAnalyzeUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.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= (CompilationUnit) ASTNodes.getParent(node, CompilationUnit.class);
		return ASTNodes.findVariableDeclaration( ((IVariableBinding) binding), cu);
	}
	return null;
}
 
Example 6
Source File: ExtractMethodRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private VariableDeclarationStatement createDeclaration(IVariableBinding binding, Expression intilizer) {
	VariableDeclaration original= ASTNodes.findVariableDeclaration(binding, fAnalyzer.getEnclosingBodyDeclaration());
	VariableDeclarationFragment fragment= fAST.newVariableDeclarationFragment();
	fragment.setName((SimpleName)ASTNode.copySubtree(fAST, original.getName()));
	fragment.setInitializer(intilizer);
	VariableDeclarationStatement result= fAST.newVariableDeclarationStatement(fragment);
	result.modifiers().addAll(ASTNode.copySubtrees(fAST, ASTNodes.getModifiers(original)));
	result.setType(ASTNodeFactory.newType(fAST, original, fImportRewriter, new ContextSensitiveImportRewriteContext(original, fImportRewriter)));
	return result;
}
 
Example 7
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$
	}
}
 
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: ExtractMethodRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private VariableDeclaration getVariableDeclaration(ParameterInfo parameter) {
	return ASTNodes.findVariableDeclaration(parameter.getOldBinding(), fAnalyzer.getEnclosingBodyDeclaration());
}
 
Example 10
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 11
Source File: ExtractMethodRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private VariableDeclaration getVariableDeclaration(ParameterInfo parameter) {
	return ASTNodes.findVariableDeclaration(parameter.getOldBinding(), fAnalyzer.getEnclosingBodyDeclaration());
}
 
Example 12
Source File: LocalDeclarationAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private void handleReferenceToLocal(SimpleName node, IVariableBinding binding) {
	VariableDeclaration declaration= ASTNodes.findVariableDeclaration(binding, node);
	if (declaration != null && fSelection.covers(declaration))
		addLocalDeclaration(declaration);
}