use of org.eclipse.jdt.internal.ui.text.correction.proposals.ModifierChangeCorrectionProposal in project che by eclipse.
the class ModifierCorrectionSubProcessor method addNonFinalLocalProposal.
public static void addNonFinalLocalProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> 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_TO_FINAL, image));
}
}
use of org.eclipse.jdt.internal.ui.text.correction.proposals.ModifierChangeCorrectionProposal in project che 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, image));
}
}
if (kind == TO_VISIBLE && bindingDecl.getKind() == IBinding.VARIABLE) {
UnresolvedElementsSubProcessor.getVariableProposals(context, problem, (IVariableBinding) bindingDecl, proposals);
}
}
use of org.eclipse.jdt.internal.ui.text.correction.proposals.ModifierChangeCorrectionProposal in project che by eclipse.
the class ModifierCorrectionSubProcessor method addMethodRequiresBodyProposals.
public static void addMethodRequiresBodyProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
ICompilationUnit cu = context.getCompilationUnit();
AST ast = context.getASTRoot().getAST();
ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
if (!(selectedNode instanceof MethodDeclaration)) {
return;
}
MethodDeclaration decl = (MethodDeclaration) selectedNode;
Modifier modifierNode;
{
ASTRewrite rewrite = ASTRewrite.create(ast);
modifierNode = removeModifier(decl, rewrite, Modifier.ABSTRACT);
Block body = ast.newBlock();
rewrite.set(decl, MethodDeclaration.BODY_PROPERTY, body, null);
if (!decl.isConstructor()) {
Type returnType = decl.getReturnType2();
if (returnType != null) {
Expression expression = ASTNodeFactory.newDefaultExpression(ast, returnType, decl.getExtraDimensions());
if (expression != null) {
ReturnStatement returnStatement = ast.newReturnStatement();
returnStatement.setExpression(expression);
body.statements().add(returnStatement);
}
}
}
String label = CorrectionMessages.ModifierCorrectionSubProcessor_addmissingbody_description;
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.ADD_MISSING_BODY, image);
proposals.add(proposal);
}
IMethodBinding binding = decl.resolveBinding();
if (modifierNode == null && binding != null) {
String label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifiertoabstract_description, getMethodLabel(binding));
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
int included = binding.getDeclaringClass().isInterface() ? Modifier.NONE : Modifier.ABSTRACT;
int excluded = Modifier.STATIC | Modifier.DEFAULT;
ModifierChangeCorrectionProposal proposal = new ModifierChangeCorrectionProposal(label, cu, binding, decl, included, excluded, IProposalRelevance.ADD_ABSTRACT_MODIFIER, image);
proposals.add(proposal);
}
}
use of org.eclipse.jdt.internal.ui.text.correction.proposals.ModifierChangeCorrectionProposal 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.internal.ui.text.correction.proposals.ModifierChangeCorrectionProposal 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));
}
}
}
}
}
Aggregations