use of org.eclipse.jdt.core.dom.IBinding in project che by eclipse.
the class TempDeclarationFinder method findTempDeclaration.
/**
* @return <code>null</code> if the selection is invalid or does not cover a temp
* declaration or reference.
*/
public static VariableDeclaration findTempDeclaration(CompilationUnit cu, int selectionOffset, int selectionLength) {
TempSelectionAnalyzer analyzer = new TempSelectionAnalyzer(selectionOffset, selectionLength);
cu.accept(analyzer);
ASTNode[] selected = analyzer.getSelectedNodes();
if (selected == null || selected.length != 1)
return null;
ASTNode selectedNode = selected[0];
if (selectedNode instanceof VariableDeclaration)
return (VariableDeclaration) selectedNode;
if (selectedNode instanceof Name) {
Name reference = (Name) selectedNode;
IBinding binding = reference.resolveBinding();
if (binding == null)
return null;
ASTNode declaringNode = cu.findDeclaringNode(binding);
if (declaringNode instanceof VariableDeclaration)
return (VariableDeclaration) declaringNode;
else
return null;
} else if (selectedNode instanceof VariableDeclarationStatement) {
VariableDeclarationStatement vds = (VariableDeclarationStatement) selectedNode;
if (vds.fragments().size() != 1)
return null;
return (VariableDeclaration) vds.fragments().get(0);
}
return null;
}
use of org.eclipse.jdt.core.dom.IBinding in project che by eclipse.
the class RemoveDeclarationCorrectionProposal method getRewrite.
/*(non-Javadoc)
* @see org.eclipse.jdt.internal.ui.text.correction.ASTRewriteCorrectionProposal#getRewrite()
*/
@Override
protected ASTRewrite getRewrite() {
IBinding binding = fName.resolveBinding();
CompilationUnit root = (CompilationUnit) fName.getRoot();
ASTRewrite rewrite;
if (binding.getKind() == IBinding.METHOD) {
IMethodBinding decl = ((IMethodBinding) binding).getMethodDeclaration();
ASTNode declaration = root.findDeclaringNode(decl);
rewrite = ASTRewrite.create(root.getAST());
rewrite.remove(declaration, null);
} else if (binding.getKind() == IBinding.TYPE) {
ITypeBinding decl = ((ITypeBinding) binding).getTypeDeclaration();
ASTNode declaration = root.findDeclaringNode(decl);
rewrite = ASTRewrite.create(root.getAST());
rewrite.remove(declaration, null);
} else if (binding.getKind() == IBinding.VARIABLE) {
// needs full AST
CompilationUnit completeRoot = SharedASTProvider.getAST(getCompilationUnit(), SharedASTProvider.WAIT_YES, null);
SimpleName nameNode = (SimpleName) NodeFinder.perform(completeRoot, fName.getStartPosition(), fName.getLength());
rewrite = ASTRewrite.create(completeRoot.getAST());
SimpleName[] references = LinkedNodeFinder.findByBinding(completeRoot, nameNode.resolveBinding());
for (int i = 0; i < references.length; i++) {
removeVariableReferences(rewrite, references[i]);
}
IVariableBinding bindingDecl = ((IVariableBinding) nameNode.resolveBinding()).getVariableDeclaration();
ASTNode declaringNode = completeRoot.findDeclaringNode(bindingDecl);
if (declaringNode instanceof SingleVariableDeclaration) {
removeParamTag(rewrite, (SingleVariableDeclaration) declaringNode);
}
} else {
//$NON-NLS-1$
throw new IllegalArgumentException("Unexpected binding");
}
return rewrite;
}
use of org.eclipse.jdt.core.dom.IBinding in project che by eclipse.
the class MissingReturnTypeCorrectionProposal method computeProposals.
protected Expression computeProposals(AST ast, ITypeBinding returnBinding, int returnOffset, CompilationUnit root, Expression result) {
ScopeAnalyzer analyzer = new ScopeAnalyzer(root);
IBinding[] bindings = analyzer.getDeclarationsInScope(returnOffset, ScopeAnalyzer.VARIABLES | ScopeAnalyzer.CHECK_VISIBILITY);
for (int i = 0; i < bindings.length; i++) {
IVariableBinding curr = (IVariableBinding) bindings[i];
ITypeBinding type = curr.getType();
if (type != null && type.isAssignmentCompatible(returnBinding) && testModifier(curr)) {
if (result == null) {
result = ast.newSimpleName(curr.getName());
}
addLinkedPositionProposal(RETURN_EXPRESSION_KEY, curr.getName(), null);
}
}
return result;
}
use of org.eclipse.jdt.core.dom.IBinding in project che by eclipse.
the class MissingReturnTypeInLambdaCorrectionProposal method computeProposals.
@Override
protected Expression computeProposals(AST ast, ITypeBinding returnBinding, int returnOffset, CompilationUnit root, Expression result) {
ScopeAnalyzer analyzer = new ScopeAnalyzer(root);
IBinding[] bindings = analyzer.getDeclarationsInScope(returnOffset, ScopeAnalyzer.VARIABLES | ScopeAnalyzer.CHECK_VISIBILITY);
org.eclipse.jdt.core.dom.NodeFinder finder = new org.eclipse.jdt.core.dom.NodeFinder(root, returnOffset, 0);
ASTNode varDeclFrag = ASTResolving.findAncestor(finder.getCoveringNode(), ASTNode.VARIABLE_DECLARATION_FRAGMENT);
IVariableBinding varDeclFragBinding = null;
if (varDeclFrag != null)
varDeclFragBinding = ((VariableDeclarationFragment) varDeclFrag).resolveBinding();
for (int i = 0; i < bindings.length; i++) {
IVariableBinding curr = (IVariableBinding) bindings[i];
ITypeBinding type = curr.getType();
// Bindings are compared to make sure that a lambda does not return a variable which is yet to be initialised.
if (type != null && type.isAssignmentCompatible(returnBinding) && testModifier(curr) && !Bindings.equals(curr, varDeclFragBinding)) {
if (result == null) {
result = ast.newSimpleName(curr.getName());
}
addLinkedPositionProposal(RETURN_EXPRESSION_KEY, curr.getName(), null);
}
}
return result;
}
use of org.eclipse.jdt.core.dom.IBinding in project che by eclipse.
the class NewVariableCorrectionProposal method evaluateFieldModifiers.
private int evaluateFieldModifiers(ASTNode newTypeDecl) {
if (fSenderBinding.isAnnotation()) {
return 0;
}
if (fSenderBinding.isInterface()) {
// for interface members copy the modifiers from an existing field
FieldDeclaration[] fieldDecls = ((TypeDeclaration) newTypeDecl).getFields();
if (fieldDecls.length > 0) {
return fieldDecls[0].getModifiers();
}
return 0;
}
int modifiers = 0;
if (fVariableKind == CONST_FIELD) {
modifiers |= Modifier.FINAL | Modifier.STATIC;
} else {
ASTNode parent = fOriginalNode.getParent();
if (parent instanceof QualifiedName) {
IBinding qualifierBinding = ((QualifiedName) parent).getQualifier().resolveBinding();
if (qualifierBinding instanceof ITypeBinding) {
modifiers |= Modifier.STATIC;
}
} else if (ASTResolving.isInStaticContext(fOriginalNode)) {
modifiers |= Modifier.STATIC;
}
}
ASTNode node = ASTResolving.findParentType(fOriginalNode, true);
if (newTypeDecl.equals(node)) {
modifiers |= Modifier.PRIVATE;
} else if (node instanceof AnonymousClassDeclaration) {
modifiers |= Modifier.PROTECTED;
} else {
modifiers |= Modifier.PUBLIC;
}
return modifiers;
}
Aggregations