use of org.eclipse.jdt.core.dom.ITypeBinding in project che 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<ICommandAccess> 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);
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.SWAP_ARGUMENTS, image);
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[] { 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);
}
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ChangeMethodSignatureProposal proposal = new ChangeMethodSignatureProposal(label, targetCU, invocationNode, methodDecl, changeDesc, null, IProposalRelevance.CHANGE_METHOD_SWAP_PARAMETERS, image);
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[] { ASTResolving.getMethodSignature(methodDecl), 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);
}
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ChangeMethodSignatureProposal proposal = new ChangeMethodSignatureProposal(label, targetCU, invocationNode, methodDecl, changeDesc, null, IProposalRelevance.CHANGE_METHOD_SIGNATURE, image);
proposals.add(proposal);
}
}
}
}
use of org.eclipse.jdt.core.dom.ITypeBinding in project che by eclipse.
the class UnresolvedElementsSubProcessor method addNewMethodProposals.
private static void addNewMethodProposals(ICompilationUnit cu, CompilationUnit astRoot, Expression sender, List<Expression> arguments, boolean isSuperInvocation, ASTNode invocationNode, String methodName, Collection<ICommandAccess> proposals) throws JavaModelException {
ITypeBinding nodeParentType = Bindings.getBindingOfParentType(invocationNode);
ITypeBinding binding = null;
if (sender != null) {
binding = sender.resolveTypeBinding();
} else {
binding = nodeParentType;
if (isSuperInvocation && binding != null) {
binding = binding.getSuperclass();
}
}
if (binding != null && binding.isFromSource()) {
ITypeBinding senderDeclBinding = binding.getTypeDeclaration();
ICompilationUnit targetCU = ASTResolving.findCompilationUnitForBinding(cu, astRoot, senderDeclBinding);
if (targetCU != null) {
String label;
Image image;
ITypeBinding[] parameterTypes = getParameterTypes(arguments);
if (parameterTypes != null) {
String sig = ASTResolving.getMethodSignature(methodName, parameterTypes, false);
if (ASTResolving.isUseableTypeInContext(parameterTypes, senderDeclBinding, false)) {
if (nodeParentType == senderDeclBinding) {
label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_createmethod_description, sig);
image = JavaPluginImages.get(JavaPluginImages.IMG_MISC_PRIVATE);
} else {
label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_createmethod_other_description, new Object[] { sig, BasicElementLabels.getJavaElementName(senderDeclBinding.getName()) });
image = JavaPluginImages.get(JavaPluginImages.IMG_MISC_PUBLIC);
}
proposals.add(new NewMethodCorrectionProposal(label, targetCU, invocationNode, arguments, senderDeclBinding, IProposalRelevance.CREATE_METHOD, image));
}
if (senderDeclBinding.isNested() && cu.equals(targetCU) && sender == null && Bindings.findMethodInHierarchy(senderDeclBinding, methodName, (ITypeBinding[]) null) == null) {
// no covering method
ASTNode anonymDecl = astRoot.findDeclaringNode(senderDeclBinding);
if (anonymDecl != null) {
senderDeclBinding = Bindings.getBindingOfParentType(anonymDecl.getParent());
if (!senderDeclBinding.isAnonymous() && ASTResolving.isUseableTypeInContext(parameterTypes, senderDeclBinding, false)) {
String[] args = new String[] { sig, ASTResolving.getTypeSignature(senderDeclBinding) };
label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_createmethod_other_description, args);
image = JavaPluginImages.get(JavaPluginImages.IMG_MISC_PROTECTED);
proposals.add(new NewMethodCorrectionProposal(label, targetCU, invocationNode, arguments, senderDeclBinding, IProposalRelevance.CREATE_METHOD, image));
}
}
}
}
}
}
}
use of org.eclipse.jdt.core.dom.ITypeBinding in project che by eclipse.
the class UnresolvedElementsSubProcessor method addSimilarTypeProposals.
private static void addSimilarTypeProposals(int kind, ICompilationUnit cu, Name node, int relevance, Collection<ICommandAccess> proposals) throws CoreException {
SimilarElement[] elements = SimilarElementsRequestor.findSimilarElement(cu, node, kind);
// try to resolve type in context -> highest severity
String resolvedTypeName = null;
ITypeBinding binding = ASTResolving.guessBindingForTypeReference(node);
if (binding != null) {
ITypeBinding simpleBinding = binding;
if (simpleBinding.isArray()) {
simpleBinding = simpleBinding.getElementType();
}
simpleBinding = simpleBinding.getTypeDeclaration();
if (!simpleBinding.isRecovered()) {
resolvedTypeName = simpleBinding.getQualifiedName();
CUCorrectionProposal proposal = createTypeRefChangeProposal(cu, resolvedTypeName, node, relevance + 2, elements.length);
proposals.add(proposal);
if (proposal instanceof AddImportCorrectionProposal)
proposal.setRelevance(relevance + elements.length + 2);
if (binding.isParameterizedType() && (node.getParent() instanceof SimpleType || node.getParent() instanceof NameQualifiedType) && !(node.getParent().getParent() instanceof Type)) {
proposals.add(createTypeRefChangeFullProposal(cu, binding, node, relevance + 5));
}
}
} else {
ASTNode normalizedNode = ASTNodes.getNormalizedNode(node);
if (!(normalizedNode.getParent() instanceof Type) && node.getParent() != normalizedNode) {
ITypeBinding normBinding = ASTResolving.guessBindingForTypeReference(normalizedNode);
if (normBinding != null && !normBinding.isRecovered()) {
proposals.add(createTypeRefChangeFullProposal(cu, normBinding, normalizedNode, relevance + 5));
}
}
}
// add all similar elements
for (int i = 0; i < elements.length; i++) {
SimilarElement elem = elements[i];
if ((elem.getKind() & SimilarElementsRequestor.ALL_TYPES) != 0) {
String fullName = elem.getName();
if (!fullName.equals(resolvedTypeName)) {
proposals.add(createTypeRefChangeProposal(cu, fullName, node, relevance, elements.length));
}
}
}
}
use of org.eclipse.jdt.core.dom.ITypeBinding in project che by eclipse.
the class UnresolvedElementsSubProcessor method addParameterMissmatchProposals.
private static void addParameterMissmatchProposals(IInvocationContext context, IProblemLocation problem, List<IMethodBinding> similarElements, ASTNode invocationNode, List<Expression> arguments, Collection<ICommandAccess> proposals) throws CoreException {
int nSimilarElements = similarElements.size();
ITypeBinding[] argTypes = getArgumentTypes(arguments);
if (argTypes == null || nSimilarElements == 0) {
return;
}
for (int i = 0; i < nSimilarElements; i++) {
IMethodBinding elem = similarElements.get(i);
int diff = elem.getParameterTypes().length - argTypes.length;
if (diff == 0) {
int nProposals = proposals.size();
doEqualNumberOfParameters(context, invocationNode, problem, arguments, argTypes, elem, proposals);
if (nProposals != proposals.size()) {
// only suggest for one method (avoid duplicated proposals)
return;
}
} else if (diff > 0) {
doMoreParameters(context, invocationNode, argTypes, elem, proposals);
} else {
doMoreArguments(context, invocationNode, arguments, argTypes, elem, proposals);
}
}
}
use of org.eclipse.jdt.core.dom.ITypeBinding in project che by eclipse.
the class UnresolvedElementsSubProcessor method doMoreArguments.
private static void doMoreArguments(IInvocationContext context, ASTNode invocationNode, List<Expression> arguments, ITypeBinding[] argTypes, IMethodBinding methodRef, Collection<ICommandAccess> proposals) throws CoreException {
ITypeBinding[] paramTypes = methodRef.getParameterTypes();
int k = 0, nSkipped = 0;
int diff = argTypes.length - paramTypes.length;
int[] indexSkipped = new int[diff];
for (int i = 0; i < argTypes.length; i++) {
if (k < paramTypes.length && canAssign(argTypes[i], paramTypes[k])) {
// match
k++;
} else {
if (nSkipped >= diff) {
// too different
return;
}
indexSkipped[nSkipped++] = i;
}
}
ICompilationUnit cu = context.getCompilationUnit();
CompilationUnit astRoot = context.getASTRoot();
// remove arguments
{
ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST());
for (int i = diff - 1; i >= 0; i--) {
rewrite.remove(arguments.get(indexSkipped[i]), null);
}
String[] arg = new String[] { ASTResolving.getMethodSignature(methodRef) };
String label;
if (diff == 1) {
label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_removeargument_description, arg);
} else {
label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_removearguments_description, arg);
}
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_REMOVE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.REMOVE_ARGUMENTS, image);
proposals.add(proposal);
}
IMethodBinding methodDecl = methodRef.getMethodDeclaration();
ITypeBinding declaringType = methodDecl.getDeclaringClass();
// add parameters
if (!declaringType.isFromSource()) {
return;
}
ICompilationUnit targetCU = ASTResolving.findCompilationUnitForBinding(cu, astRoot, declaringType);
if (targetCU != null) {
if (isImplicitConstructor(methodDecl)) {
return;
}
ChangeDescription[] changeDesc = new ChangeDescription[argTypes.length];
ITypeBinding[] changeTypes = new ITypeBinding[diff];
for (int i = diff - 1; i >= 0; i--) {
int idx = indexSkipped[i];
Expression arg = arguments.get(idx);
String name = getExpressionBaseName(arg);
ITypeBinding newType = Bindings.normalizeTypeBinding(argTypes[idx]);
if (newType == null) {
//$NON-NLS-1$
newType = astRoot.getAST().resolveWellKnownType("java.lang.Object");
}
if (newType.isWildcardType()) {
newType = ASTResolving.normalizeWildcardType(newType, true, astRoot.getAST());
}
if (!ASTResolving.isUseableTypeInContext(newType, methodDecl, false)) {
return;
}
changeDesc[idx] = new InsertDescription(newType, name);
changeTypes[i] = newType;
}
String[] arg = new String[] { ASTResolving.getMethodSignature(methodDecl), getTypeNames(changeTypes) };
String label;
if (methodDecl.isConstructor()) {
if (diff == 1) {
label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_addparam_constr_description, arg);
} else {
label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_addparams_constr_description, arg);
}
} else {
if (diff == 1) {
label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_addparam_description, arg);
} else {
label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_addparams_description, arg);
}
}
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_ADD);
ChangeMethodSignatureProposal proposal = new ChangeMethodSignatureProposal(label, targetCU, invocationNode, methodDecl, changeDesc, null, IProposalRelevance.CHANGE_METHOD_ADD_PARAMETER, image);
proposals.add(proposal);
}
}
Aggregations