use of org.eclipse.jdt.ls.core.internal.corrections.proposals.ChangeMethodSignatureProposal.SwapDescription in project eclipse.jdt.ls by eclipse.
the class UnresolvedElementsSubProcessor method doEqualNumberOfParameters.
private static void doEqualNumberOfParameters(IInvocationContext context, ASTNode invocationNode, IProblemLocation problem, List<Expression> arguments, ITypeBinding[] argTypes, IMethodBinding methodBinding, Collection<CUCorrectionProposal> proposals) throws CoreException {
ITypeBinding[] paramTypes = methodBinding.getParameterTypes();
int[] indexOfDiff = new int[paramTypes.length];
int nDiffs = 0;
for (int n = 0; n < argTypes.length; n++) {
if (!canAssign(argTypes[n], paramTypes[n])) {
indexOfDiff[nDiffs++] = n;
}
}
ITypeBinding declaringTypeDecl = methodBinding.getDeclaringClass().getTypeDeclaration();
ICompilationUnit cu = context.getCompilationUnit();
CompilationUnit astRoot = context.getASTRoot();
ASTNode nameNode = problem.getCoveringNode(astRoot);
if (nameNode == null) {
return;
}
if (nDiffs == 0) {
if (nameNode.getParent() instanceof MethodInvocation) {
MethodInvocation inv = (MethodInvocation) nameNode.getParent();
if (inv.getExpression() == null) {
addQualifierToOuterProposal(context, inv, methodBinding, proposals);
}
}
return;
}
if (nDiffs == 1) {
// one argument mismatching: try to fix
int idx = indexOfDiff[0];
Expression nodeToCast = arguments.get(idx);
ITypeBinding castType = paramTypes[idx];
castType = Bindings.normalizeTypeBinding(castType);
if (castType.isWildcardType()) {
castType = ASTResolving.normalizeWildcardType(castType, false, nodeToCast.getAST());
}
if (castType != null) {
ITypeBinding binding = nodeToCast.resolveTypeBinding();
ITypeBinding castFixType = null;
if (binding == null || castType.isCastCompatible(binding)) {
castFixType = castType;
} else if (JavaModelUtil.is50OrHigher(cu.getJavaProject())) {
ITypeBinding boxUnboxedTypeBinding = TypeMismatchSubProcessor.boxUnboxPrimitives(castType, binding, nodeToCast.getAST());
if (boxUnboxedTypeBinding != castType && boxUnboxedTypeBinding.isCastCompatible(binding)) {
castFixType = boxUnboxedTypeBinding;
}
}
if (castFixType != null) {
ASTRewriteCorrectionProposal proposal = TypeMismatchSubProcessor.createCastProposal(context, castFixType, nodeToCast, IProposalRelevance.CAST_ARGUMENT_1);
String castTypeName = BindingLabelProvider.getBindingLabel(castFixType, JavaElementLabels.ALL_DEFAULT);
String[] arg = new String[] { getArgumentName(arguments, idx), castTypeName };
proposal.setDisplayName(Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_addargumentcast_description, arg));
proposals.add(proposal);
}
TypeMismatchSubProcessor.addChangeSenderTypeProposals(context, nodeToCast, castType, false, IProposalRelevance.CAST_ARGUMENT_2, proposals);
}
}
if (nDiffs == 2) {
// try to swap
int idx1 = indexOfDiff[0];
int idx2 = indexOfDiff[1];
boolean canSwap = canAssign(argTypes[idx1], paramTypes[idx2]) && canAssign(argTypes[idx2], paramTypes[idx1]);
if (canSwap) {
Expression arg1 = arguments.get(idx1);
Expression arg2 = arguments.get(idx2);
ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST());
rewrite.replace(arg1, rewrite.createCopyTarget(arg2), null);
rewrite.replace(arg2, rewrite.createCopyTarget(arg1), null);
{
String[] arg = new String[] { getArgumentName(arguments, idx1), getArgumentName(arguments, idx2) };
String label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_swaparguments_description, arg);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.SWAP_ARGUMENTS);
proposals.add(proposal);
}
if (declaringTypeDecl.isFromSource()) {
ICompilationUnit targetCU = ASTResolving.findCompilationUnitForBinding(cu, astRoot, declaringTypeDecl);
if (targetCU != null) {
ChangeDescription[] changeDesc = new ChangeDescription[paramTypes.length];
for (int i = 0; i < nDiffs; i++) {
changeDesc[idx1] = new SwapDescription(idx2);
}
IMethodBinding methodDecl = methodBinding.getMethodDeclaration();
ITypeBinding[] declParamTypes = methodDecl.getParameterTypes();
ITypeBinding[] swappedTypes = new ITypeBinding[] { declParamTypes[idx1], declParamTypes[idx2] };
String[] args = new String[] { org.eclipse.jdt.ls.core.internal.corrections.ASTResolving.getMethodSignature(methodDecl), getTypeNames(swappedTypes) };
String label;
if (methodDecl.isConstructor()) {
label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_swapparams_constr_description, args);
} else {
label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_swapparams_description, args);
}
ChangeMethodSignatureProposal proposal = new ChangeMethodSignatureProposal(label, targetCU, invocationNode, methodDecl, changeDesc, null, IProposalRelevance.CHANGE_METHOD_SWAP_PARAMETERS);
proposals.add(proposal);
}
}
return;
}
}
if (declaringTypeDecl.isFromSource()) {
ICompilationUnit targetCU = ASTResolving.findCompilationUnitForBinding(cu, astRoot, declaringTypeDecl);
if (targetCU != null) {
ChangeDescription[] changeDesc = createSignatureChangeDescription(indexOfDiff, nDiffs, paramTypes, arguments, argTypes);
if (changeDesc != null) {
IMethodBinding methodDecl = methodBinding.getMethodDeclaration();
ITypeBinding[] declParamTypes = methodDecl.getParameterTypes();
ITypeBinding[] newParamTypes = new ITypeBinding[changeDesc.length];
for (int i = 0; i < newParamTypes.length; i++) {
newParamTypes[i] = changeDesc[i] == null ? declParamTypes[i] : ((EditDescription) changeDesc[i]).type;
}
boolean isVarArgs = methodDecl.isVarargs() && newParamTypes.length > 0 && newParamTypes[newParamTypes.length - 1].isArray();
String[] args = new String[] { org.eclipse.jdt.ls.core.internal.corrections.ASTResolving.getMethodSignature(methodDecl), org.eclipse.jdt.ls.core.internal.corrections.ASTResolving.getMethodSignature(methodDecl.getName(), newParamTypes, isVarArgs) };
String label;
if (methodDecl.isConstructor()) {
label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_changeparamsignature_constr_description, args);
} else {
label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_changeparamsignature_description, args);
}
ChangeMethodSignatureProposal proposal = new ChangeMethodSignatureProposal(label, targetCU, invocationNode, methodDecl, changeDesc, null, IProposalRelevance.CHANGE_METHOD_SIGNATURE);
proposals.add(proposal);
}
}
}
}
use of org.eclipse.jdt.ls.core.internal.corrections.proposals.ChangeMethodSignatureProposal.SwapDescription in project eclipse.jdt.ls by eclipse.
the class UnresolvedElementsSubProcessor method doEqualNumberOfParameters.
private static void doEqualNumberOfParameters(IInvocationContext context, ASTNode invocationNode, IProblemLocationCore problem, List<Expression> arguments, ITypeBinding[] argTypes, IMethodBinding methodBinding, Collection<ChangeCorrectionProposal> proposals) throws CoreException {
ITypeBinding[] paramTypes = methodBinding.getParameterTypes();
int[] indexOfDiff = new int[paramTypes.length];
int nDiffs = 0;
for (int n = 0; n < argTypes.length; n++) {
if (!canAssign(argTypes[n], paramTypes[n])) {
indexOfDiff[nDiffs++] = n;
}
}
ITypeBinding declaringTypeDecl = methodBinding.getDeclaringClass().getTypeDeclaration();
ICompilationUnit cu = context.getCompilationUnit();
CompilationUnit astRoot = context.getASTRoot();
ASTNode nameNode = problem.getCoveringNode(astRoot);
if (nameNode == null) {
return;
}
if (nDiffs == 0) {
if (nameNode.getParent() instanceof MethodInvocation) {
MethodInvocation inv = (MethodInvocation) nameNode.getParent();
if (inv.getExpression() == null) {
addQualifierToOuterProposal(context, inv, methodBinding, proposals);
}
}
return;
}
if (nDiffs == 1) {
// one argument mismatching: try to fix
int idx = indexOfDiff[0];
Expression nodeToCast = arguments.get(idx);
ITypeBinding castType = paramTypes[idx];
castType = Bindings.normalizeTypeBinding(castType);
if (castType.isWildcardType()) {
castType = ASTResolving.normalizeWildcardType(castType, false, nodeToCast.getAST());
}
if (castType != null) {
ITypeBinding binding = nodeToCast.resolveTypeBinding();
ITypeBinding castFixType = null;
if (binding == null || castType.isCastCompatible(binding)) {
castFixType = castType;
} else if (JavaModelUtil.is50OrHigher(cu.getJavaProject())) {
ITypeBinding boxUnboxedTypeBinding = TypeMismatchSubProcessor.boxUnboxPrimitives(castType, binding, nodeToCast.getAST());
if (boxUnboxedTypeBinding != castType && boxUnboxedTypeBinding.isCastCompatible(binding)) {
castFixType = boxUnboxedTypeBinding;
}
}
if (castFixType != null) {
ASTRewriteCorrectionProposal proposal = TypeMismatchSubProcessor.createCastProposal(context, castFixType, nodeToCast, IProposalRelevance.CAST_ARGUMENT_1);
String castTypeName = BindingLabelProviderCore.getBindingLabel(castFixType, JavaElementLabels.ALL_DEFAULT);
String[] arg = new String[] { getArgumentName(arguments, idx), castTypeName };
proposal.setDisplayName(Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_addargumentcast_description, arg));
proposals.add(proposal);
}
TypeMismatchSubProcessor.addChangeSenderTypeProposals(context, nodeToCast, castType, false, IProposalRelevance.CAST_ARGUMENT_2, proposals);
}
}
if (nDiffs == 2) {
// try to swap
int idx1 = indexOfDiff[0];
int idx2 = indexOfDiff[1];
boolean canSwap = canAssign(argTypes[idx1], paramTypes[idx2]) && canAssign(argTypes[idx2], paramTypes[idx1]);
if (canSwap) {
Expression arg1 = arguments.get(idx1);
Expression arg2 = arguments.get(idx2);
ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST());
rewrite.replace(arg1, rewrite.createCopyTarget(arg2), null);
rewrite.replace(arg2, rewrite.createCopyTarget(arg1), null);
{
String[] arg = new String[] { getArgumentName(arguments, idx1), getArgumentName(arguments, idx2) };
String label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_swaparguments_description, arg);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, CodeActionKind.QuickFix, context.getCompilationUnit(), rewrite, IProposalRelevance.SWAP_ARGUMENTS);
proposals.add(proposal);
}
if (declaringTypeDecl.isFromSource()) {
ICompilationUnit targetCU = ASTResolving.findCompilationUnitForBinding(cu, astRoot, declaringTypeDecl);
if (targetCU != null) {
ChangeDescription[] changeDesc = new ChangeDescription[paramTypes.length];
for (int i = 0; i < nDiffs; i++) {
changeDesc[idx1] = new SwapDescription(idx2);
}
IMethodBinding methodDecl = methodBinding.getMethodDeclaration();
ITypeBinding[] declParamTypes = methodDecl.getParameterTypes();
ITypeBinding[] swappedTypes = new ITypeBinding[] { declParamTypes[idx1], declParamTypes[idx2] };
String[] args = new String[] { org.eclipse.jdt.ls.core.internal.corrections.ASTResolving.getMethodSignature(methodDecl), getTypeNames(swappedTypes) };
String label;
if (methodDecl.isConstructor()) {
label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_swapparams_constr_description, args);
} else {
label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_swapparams_description, args);
}
ChangeMethodSignatureProposal proposal = new ChangeMethodSignatureProposal(label, targetCU, invocationNode, methodDecl, changeDesc, null, IProposalRelevance.CHANGE_METHOD_SWAP_PARAMETERS);
proposals.add(proposal);
}
}
return;
}
}
if (declaringTypeDecl.isFromSource()) {
ICompilationUnit targetCU = ASTResolving.findCompilationUnitForBinding(cu, astRoot, declaringTypeDecl);
if (targetCU != null) {
ChangeDescription[] changeDesc = createSignatureChangeDescription(indexOfDiff, nDiffs, paramTypes, arguments, argTypes);
if (changeDesc != null) {
IMethodBinding methodDecl = methodBinding.getMethodDeclaration();
ITypeBinding[] declParamTypes = methodDecl.getParameterTypes();
ITypeBinding[] newParamTypes = new ITypeBinding[changeDesc.length];
for (int i = 0; i < newParamTypes.length; i++) {
newParamTypes[i] = changeDesc[i] == null ? declParamTypes[i] : ((EditDescription) changeDesc[i]).type;
}
boolean isVarArgs = methodDecl.isVarargs() && newParamTypes.length > 0 && newParamTypes[newParamTypes.length - 1].isArray();
String[] args = new String[] { org.eclipse.jdt.ls.core.internal.corrections.ASTResolving.getMethodSignature(methodDecl), org.eclipse.jdt.ls.core.internal.corrections.ASTResolving.getMethodSignature(methodDecl.getName(), newParamTypes, isVarArgs) };
String label;
if (methodDecl.isConstructor()) {
label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_changeparamsignature_constr_description, args);
} else {
label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_changeparamsignature_description, args);
}
ChangeMethodSignatureProposal proposal = new ChangeMethodSignatureProposal(label, targetCU, invocationNode, methodDecl, changeDesc, null, IProposalRelevance.CHANGE_METHOD_SIGNATURE);
proposals.add(proposal);
}
}
}
}
Aggregations