use of org.eclipse.jdt.core.dom.IVariableBinding in project flux by eclipse.
the class ModifierCorrectionSubProcessor method addNonAccessibleReferenceProposal.
public static void addNonAccessibleReferenceProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals, int kind, int relevance) throws CoreException {
ICompilationUnit cu = context.getCompilationUnit();
ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
if (selectedNode == null) {
return;
}
IBinding binding = null;
switch(selectedNode.getNodeType()) {
case ASTNode.SIMPLE_NAME:
binding = ((SimpleName) selectedNode).resolveBinding();
break;
case ASTNode.QUALIFIED_NAME:
binding = ((QualifiedName) selectedNode).resolveBinding();
break;
case ASTNode.SIMPLE_TYPE:
binding = ((SimpleType) selectedNode).resolveBinding();
break;
case ASTNode.NAME_QUALIFIED_TYPE:
binding = ((NameQualifiedType) selectedNode).resolveBinding();
break;
case ASTNode.METHOD_INVOCATION:
binding = ((MethodInvocation) selectedNode).getName().resolveBinding();
break;
case ASTNode.SUPER_METHOD_INVOCATION:
binding = ((SuperMethodInvocation) selectedNode).getName().resolveBinding();
break;
case ASTNode.FIELD_ACCESS:
binding = ((FieldAccess) selectedNode).getName().resolveBinding();
break;
case ASTNode.SUPER_FIELD_ACCESS:
binding = ((SuperFieldAccess) selectedNode).getName().resolveBinding();
break;
case ASTNode.CLASS_INSTANCE_CREATION:
binding = ((ClassInstanceCreation) selectedNode).resolveConstructorBinding();
break;
case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
binding = ((SuperConstructorInvocation) selectedNode).resolveConstructorBinding();
break;
default:
return;
}
ITypeBinding typeBinding = null;
String name;
IBinding bindingDecl;
boolean isLocalVar = false;
if (binding instanceof IVariableBinding && problem.getProblemId() == IProblem.NotVisibleType) {
binding = ((IVariableBinding) binding).getType();
}
if (binding instanceof IMethodBinding && problem.getProblemId() == IProblem.NotVisibleType) {
binding = ((IMethodBinding) binding).getReturnType();
}
if (binding instanceof IMethodBinding) {
IMethodBinding methodDecl = (IMethodBinding) binding;
if (methodDecl.isDefaultConstructor()) {
//UnresolvedElementsSubProcessor.getConstructorProposals(context, problem, proposals);
return;
}
bindingDecl = methodDecl.getMethodDeclaration();
typeBinding = methodDecl.getDeclaringClass();
//$NON-NLS-1$
name = BasicElementLabels.getJavaElementName(methodDecl.getName() + "()");
} else if (binding instanceof IVariableBinding) {
IVariableBinding varDecl = (IVariableBinding) binding;
typeBinding = varDecl.getDeclaringClass();
name = BasicElementLabels.getJavaElementName(binding.getName());
isLocalVar = !varDecl.isField();
bindingDecl = varDecl.getVariableDeclaration();
} else if (binding instanceof ITypeBinding) {
typeBinding = (ITypeBinding) binding;
bindingDecl = typeBinding.getTypeDeclaration();
name = BasicElementLabels.getJavaElementName(binding.getName());
} else {
return;
}
if (typeBinding != null && typeBinding.isFromSource() || isLocalVar) {
int includedModifiers = 0;
int excludedModifiers = 0;
String label;
switch(kind) {
case TO_VISIBLE:
excludedModifiers = Modifier.PRIVATE | Modifier.PROTECTED | Modifier.PUBLIC;
includedModifiers = getNeededVisibility(selectedNode, typeBinding, binding);
label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changevisibility_description, new String[] { name, getVisibilityString(includedModifiers) });
break;
case TO_STATIC:
label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifiertostatic_description, name);
includedModifiers = Modifier.STATIC;
if (bindingDecl.getKind() == IBinding.METHOD) {
excludedModifiers = Modifier.DEFAULT | Modifier.ABSTRACT;
}
break;
case TO_NON_STATIC:
if (typeBinding != null && typeBinding.isInterface())
return;
label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifiertononstatic_description, name);
excludedModifiers = Modifier.STATIC;
break;
case TO_NON_PRIVATE:
int visibility;
if (cu.getParent().getElementName().equals(typeBinding.getPackage().getName())) {
visibility = Modifier.NONE;
excludedModifiers = Modifier.PRIVATE;
} else {
visibility = Modifier.PUBLIC;
includedModifiers = Modifier.PUBLIC;
excludedModifiers = Modifier.PRIVATE | Modifier.PROTECTED | Modifier.PUBLIC;
}
label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changevisibility_description, new String[] { name, getVisibilityString(visibility) });
break;
case TO_NON_FINAL:
if (typeBinding != null && typeBinding.isInterface())
return;
label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifiertononfinal_description, name);
excludedModifiers = Modifier.FINAL;
break;
default:
//$NON-NLS-1$
throw new IllegalArgumentException("not supported");
}
ICompilationUnit targetCU = isLocalVar ? cu : ASTResolving.findCompilationUnitForBinding(cu, context.getASTRoot(), typeBinding.getTypeDeclaration());
if (targetCU != null) {
//Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
proposals.add(new ModifierChangeCorrectionProposal(label, targetCU, bindingDecl, selectedNode, includedModifiers, excludedModifiers, relevance));
}
}
if (kind == TO_VISIBLE && bindingDecl.getKind() == IBinding.VARIABLE) {
// UnresolvedElementsSubProcessor.getVariableProposals(context, problem, (IVariableBinding) bindingDecl, proposals);
}
}
use of org.eclipse.jdt.core.dom.IVariableBinding in project flux by eclipse.
the class ModifierCorrectionSubProcessor method addRemoveInvalidModifiersProposal.
public static void addRemoveInvalidModifiersProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals, int relevance) {
ICompilationUnit cu = context.getCompilationUnit();
ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
if (selectedNode instanceof MethodDeclaration) {
selectedNode = ((MethodDeclaration) selectedNode).getName();
}
if (!(selectedNode instanceof SimpleName)) {
return;
}
IBinding binding = ((SimpleName) selectedNode).resolveBinding();
if (binding != null) {
String methodName = BasicElementLabels.getJavaElementName(binding.getName());
String label = null;
int problemId = problem.getProblemId();
int excludedModifiers = 0;
int includedModifiers = 0;
switch(problemId) {
case IProblem.CannotHideAnInstanceMethodWithAStaticMethod:
case IProblem.UnexpectedStaticModifierForMethod:
excludedModifiers = Modifier.STATIC;
label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemethodtononstatic_description, methodName);
break;
case IProblem.UnexpectedStaticModifierForField:
excludedModifiers = Modifier.STATIC;
label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changefieldmodifiertononstatic_description, methodName);
break;
case IProblem.IllegalModifierCombinationFinalVolatileForField:
excludedModifiers = Modifier.VOLATILE;
label = CorrectionMessages.ModifierCorrectionSubProcessor_removevolatile_description;
break;
case IProblem.IllegalModifierForInterfaceMethod:
excludedModifiers = ~(Modifier.PUBLIC | Modifier.ABSTRACT);
break;
case IProblem.IllegalModifierForInterfaceMethod18:
excludedModifiers = ~(Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.STRICTFP | Modifier.DEFAULT | Modifier.STATIC);
if (Modifier.isAbstract(binding.getModifiers())) {
excludedModifiers = excludedModifiers | Modifier.STRICTFP;
}
break;
case IProblem.IllegalModifierForInterface:
excludedModifiers = ~(Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.STRICTFP);
break;
case IProblem.IllegalModifierForClass:
excludedModifiers = ~(Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.FINAL | Modifier.STRICTFP);
break;
case IProblem.IllegalModifierForInterfaceField:
excludedModifiers = ~(Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL);
break;
case IProblem.IllegalModifierForMemberInterface:
case IProblem.IllegalVisibilityModifierForInterfaceMemberType:
excludedModifiers = ~(Modifier.PUBLIC | Modifier.STATIC | Modifier.STRICTFP);
break;
case IProblem.IllegalModifierForMemberClass:
excludedModifiers = ~(Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE | Modifier.STATIC | Modifier.ABSTRACT | Modifier.FINAL | Modifier.STRICTFP);
break;
case IProblem.IllegalModifierForLocalClass:
excludedModifiers = ~(Modifier.ABSTRACT | Modifier.FINAL | Modifier.STRICTFP);
break;
case IProblem.IllegalModifierForArgument:
excludedModifiers = ~Modifier.FINAL;
break;
case IProblem.IllegalModifierForField:
excludedModifiers = ~(Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL | Modifier.VOLATILE | Modifier.TRANSIENT);
break;
case IProblem.IllegalModifierForMethod:
excludedModifiers = ~(Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE | Modifier.STATIC | Modifier.ABSTRACT | Modifier.FINAL | Modifier.NATIVE | Modifier.STRICTFP | Modifier.SYNCHRONIZED);
break;
case IProblem.IllegalModifierForConstructor:
excludedModifiers = ~(Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE);
break;
case IProblem.IllegalModifierForVariable:
excludedModifiers = ~Modifier.FINAL;
break;
case IProblem.IllegalModifierForEnum:
excludedModifiers = ~(Modifier.PUBLIC | Modifier.STRICTFP);
break;
case IProblem.IllegalModifierForEnumConstant:
excludedModifiers = ~Modifier.NONE;
break;
case IProblem.IllegalModifierForEnumConstructor:
excludedModifiers = ~Modifier.PRIVATE;
break;
case IProblem.IllegalModifierForMemberEnum:
excludedModifiers = ~(Modifier.PUBLIC | Modifier.PRIVATE | Modifier.PROTECTED | Modifier.STATIC | Modifier.STRICTFP);
break;
default:
//$NON-NLS-1$
Assert.isTrue(false, "not supported");
return;
}
if (label == null)
label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_removeinvalidmodifiers_description, methodName);
// Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
proposals.add(new ModifierChangeCorrectionProposal(label, cu, binding, selectedNode, includedModifiers, excludedModifiers, relevance));
if (problemId == IProblem.IllegalModifierCombinationFinalVolatileForField) {
proposals.add(new ModifierChangeCorrectionProposal(CorrectionMessages.ModifierCorrectionSubProcessor_removefinal_description, cu, binding, selectedNode, 0, Modifier.FINAL, relevance + 1));
}
if (problemId == IProblem.UnexpectedStaticModifierForField && binding instanceof IVariableBinding) {
ITypeBinding declClass = ((IVariableBinding) binding).getDeclaringClass();
if (declClass.isMember()) {
proposals.add(new ModifierChangeCorrectionProposal(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifiertostaticfinal_description, cu, binding, selectedNode, Modifier.FINAL, Modifier.VOLATILE, relevance + 1));
ASTNode parentType = context.getASTRoot().findDeclaringNode(declClass);
if (parentType != null) {
proposals.add(new ModifierChangeCorrectionProposal(CorrectionMessages.ModifierCorrectionSubProcessor_addstatictoparenttype_description, cu, declClass, parentType, Modifier.STATIC, 0, relevance - 1));
}
}
}
if (problemId == IProblem.UnexpectedStaticModifierForMethod && binding instanceof IMethodBinding) {
ITypeBinding declClass = ((IMethodBinding) binding).getDeclaringClass();
if (declClass.isMember()) {
ASTNode parentType = context.getASTRoot().findDeclaringNode(declClass);
if (parentType != null) {
proposals.add(new ModifierChangeCorrectionProposal(CorrectionMessages.ModifierCorrectionSubProcessor_addstatictoparenttype_description, cu, declClass, parentType, Modifier.STATIC, 0, relevance - 1));
}
}
}
}
}
use of org.eclipse.jdt.core.dom.IVariableBinding in project flux by eclipse.
the class ModifierCorrectionSubProcessor method addNeedToEmulateProposal.
public static void addNeedToEmulateProposal(IInvocationContext context, IProblemLocation problem, Collection<ModifierChangeCorrectionProposal> proposals) {
ICompilationUnit cu = context.getCompilationUnit();
ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
if (!(selectedNode instanceof SimpleName)) {
return;
}
IBinding binding = ((SimpleName) selectedNode).resolveBinding();
if (binding instanceof IVariableBinding) {
binding = ((IVariableBinding) binding).getVariableDeclaration();
// Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
String label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifiertofinal_description, BasicElementLabels.getJavaElementName(binding.getName()));
proposals.add(new ModifierChangeCorrectionProposal(label, cu, binding, selectedNode, Modifier.FINAL, 0, IProposalRelevance.CHANGE_MODIFIER_OF_VARIABLE_TO_FINAL));
}
}
use of org.eclipse.jdt.core.dom.IVariableBinding in project flux by eclipse.
the class AdvancedQuickAssistProcessor method getReplaceIfElseWithConditionalProposals.
private static boolean getReplaceIfElseWithConditionalProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
if (!(node instanceof IfStatement)) {
return false;
}
IfStatement ifStatement = (IfStatement) node;
Statement thenStatement = getSingleStatement(ifStatement.getThenStatement());
Statement elseStatement = getSingleStatement(ifStatement.getElseStatement());
if (thenStatement == null || elseStatement == null) {
return false;
}
Expression assigned = null;
Expression thenExpression = null;
Expression elseExpression = null;
ITypeBinding exprBinding = null;
if (thenStatement instanceof ReturnStatement && elseStatement instanceof ReturnStatement) {
thenExpression = ((ReturnStatement) thenStatement).getExpression();
elseExpression = ((ReturnStatement) elseStatement).getExpression();
MethodDeclaration declaration = ASTResolving.findParentMethodDeclaration(node);
if (declaration == null || declaration.isConstructor()) {
return false;
}
exprBinding = declaration.getReturnType2().resolveBinding();
} else if (thenStatement instanceof ExpressionStatement && elseStatement instanceof ExpressionStatement) {
Expression inner1 = ((ExpressionStatement) thenStatement).getExpression();
Expression inner2 = ((ExpressionStatement) elseStatement).getExpression();
if (inner1 instanceof Assignment && inner2 instanceof Assignment) {
Assignment assign1 = (Assignment) inner1;
Assignment assign2 = (Assignment) inner2;
Expression left1 = assign1.getLeftHandSide();
Expression left2 = assign2.getLeftHandSide();
if (left1 instanceof Name && left2 instanceof Name && assign1.getOperator() == assign2.getOperator()) {
IBinding bind1 = ((Name) left1).resolveBinding();
IBinding bind2 = ((Name) left2).resolveBinding();
if (bind1 == bind2 && bind1 instanceof IVariableBinding) {
assigned = left1;
exprBinding = ((IVariableBinding) bind1).getType();
thenExpression = assign1.getRightHandSide();
elseExpression = assign2.getRightHandSide();
}
}
}
}
if (thenExpression == null || elseExpression == null) {
return false;
}
// we could produce quick assist
if (resultingCollections == null) {
return true;
}
//
AST ast = node.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
// TightSourceRangeComputer sourceRangeComputer= new TightSourceRangeComputer();
// sourceRangeComputer.addTightSourceNode(ifStatement);
// rewrite.setTargetSourceRangeComputer(sourceRangeComputer);
String label = CorrectionMessages.AdvancedQuickAssistProcessor_replaceIfWithConditional;
// Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.REPLACE_IF_ELSE_WITH_CONDITIONAL);
// prepare conditional expression
ConditionalExpression conditionalExpression = ast.newConditionalExpression();
Expression conditionCopy = (Expression) rewrite.createCopyTarget(ifStatement.getExpression());
conditionalExpression.setExpression(conditionCopy);
Expression thenCopy = (Expression) rewrite.createCopyTarget(thenExpression);
Expression elseCopy = (Expression) rewrite.createCopyTarget(elseExpression);
IJavaProject project = context.getCompilationUnit().getJavaProject();
if (!JavaModelUtil.is50OrHigher(project)) {
ITypeBinding thenBinding = thenExpression.resolveTypeBinding();
ITypeBinding elseBinding = elseExpression.resolveTypeBinding();
if (thenBinding != null && elseBinding != null && exprBinding != null && !elseBinding.isAssignmentCompatible(thenBinding)) {
CastExpression castException = ast.newCastExpression();
ImportRewrite importRewrite = proposal.createImportRewrite(context.getASTRoot());
ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(node, importRewrite);
castException.setType(importRewrite.addImport(exprBinding, ast, importRewriteContext));
castException.setExpression(elseCopy);
elseCopy = castException;
}
} else if (JavaModelUtil.is17OrHigher(project)) {
addExplicitTypeArgumentsIfNecessary(rewrite, proposal, thenExpression);
addExplicitTypeArgumentsIfNecessary(rewrite, proposal, elseExpression);
}
conditionalExpression.setThenExpression(thenCopy);
conditionalExpression.setElseExpression(elseCopy);
// replace 'if' statement with conditional expression
if (assigned == null) {
ReturnStatement returnStatement = ast.newReturnStatement();
returnStatement.setExpression(conditionalExpression);
rewrite.replace(ifStatement, returnStatement, null);
} else {
Assignment assignment = ast.newAssignment();
assignment.setLeftHandSide((Expression) rewrite.createCopyTarget(assigned));
assignment.setRightHandSide(conditionalExpression);
assignment.setOperator(((Assignment) assigned.getParent()).getOperator());
ExpressionStatement expressionStatement = ast.newExpressionStatement(assignment);
rewrite.replace(ifStatement, expressionStatement, null);
}
// add correction proposal
resultingCollections.add(proposal);
return true;
}
use of org.eclipse.jdt.core.dom.IVariableBinding in project flux by eclipse.
the class QuickAssistProcessor method getJoinVariableProposals.
private static boolean getJoinVariableProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
ASTNode parent = node.getParent();
VariableDeclarationFragment fragment = null;
boolean onFirstAccess = false;
if (node instanceof SimpleName && node.getLocationInParent() == Assignment.LEFT_HAND_SIDE_PROPERTY) {
onFirstAccess = true;
SimpleName name = (SimpleName) node;
IBinding binding = name.resolveBinding();
if (!(binding instanceof IVariableBinding)) {
return false;
}
ASTNode declaring = context.getASTRoot().findDeclaringNode(binding);
if (declaring instanceof VariableDeclarationFragment) {
fragment = (VariableDeclarationFragment) declaring;
} else {
return false;
}
} else if (parent instanceof VariableDeclarationFragment) {
fragment = (VariableDeclarationFragment) parent;
} else {
return false;
}
IVariableBinding binding = fragment.resolveBinding();
Expression initializer = fragment.getInitializer();
if ((initializer != null && initializer.getNodeType() != ASTNode.NULL_LITERAL) || binding == null || binding.isField()) {
return false;
}
if (!(fragment.getParent() instanceof VariableDeclarationStatement)) {
return false;
}
VariableDeclarationStatement statement = (VariableDeclarationStatement) fragment.getParent();
SimpleName[] names = LinkedNodeFinder.findByBinding(statement.getParent(), binding);
if (names.length <= 1 || names[0] != fragment.getName()) {
return false;
}
SimpleName firstAccess = names[1];
if (onFirstAccess) {
if (firstAccess != node) {
return false;
}
} else {
if (firstAccess.getLocationInParent() != Assignment.LEFT_HAND_SIDE_PROPERTY) {
return false;
}
}
Assignment assignment = (Assignment) firstAccess.getParent();
if (assignment.getLocationInParent() != ExpressionStatement.EXPRESSION_PROPERTY) {
return false;
}
ExpressionStatement assignParent = (ExpressionStatement) assignment.getParent();
if (resultingCollections == null) {
return true;
}
AST ast = statement.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
// TightSourceRangeComputer sourceRangeComputer= new TightSourceRangeComputer();
// sourceRangeComputer.addTightSourceNode(assignParent);
// rewrite.setTargetSourceRangeComputer(sourceRangeComputer);
String label = CorrectionMessages.QuickAssistProcessor_joindeclaration_description;
// Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_LOCAL);
LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.JOIN_VARIABLE_DECLARATION);
proposal.setCommandId(SPLIT_JOIN_VARIABLE_DECLARATION_ID);
Expression placeholder = (Expression) rewrite.createMoveTarget(assignment.getRightHandSide());
rewrite.set(fragment, VariableDeclarationFragment.INITIALIZER_PROPERTY, placeholder, null);
if (onFirstAccess) {
// replace assignment with variable declaration
rewrite.replace(assignParent, rewrite.createMoveTarget(statement), null);
} else {
// different scopes -> remove assignments, set variable initializer
if (ASTNodes.isControlStatementBody(assignParent.getLocationInParent())) {
Block block = ast.newBlock();
rewrite.replace(assignParent, block, null);
} else {
rewrite.remove(assignParent, null);
}
}
proposal.setEndPosition(rewrite.track(fragment.getName()));
resultingCollections.add(proposal);
return true;
}
Aggregations