use of org.eclipse.jdt.core.dom.IMethodBinding in project che by eclipse.
the class StubUtility2 method getOverridableMethods.
public static IMethodBinding[] getOverridableMethods(AST ast, ITypeBinding typeBinding, boolean isSubType) {
List<IMethodBinding> allMethods = new ArrayList<IMethodBinding>();
IMethodBinding[] typeMethods = typeBinding.getDeclaredMethods();
for (int index = 0; index < typeMethods.length; index++) {
final int modifiers = typeMethods[index].getModifiers();
if (!typeMethods[index].isConstructor() && !Modifier.isStatic(modifiers) && !Modifier.isPrivate(modifiers))
allMethods.add(typeMethods[index]);
}
ITypeBinding clazz = typeBinding.getSuperclass();
while (clazz != null) {
IMethodBinding[] methods = clazz.getDeclaredMethods();
for (int offset = 0; offset < methods.length; offset++) {
final int modifiers = methods[offset].getModifiers();
if (!methods[offset].isConstructor() && !Modifier.isStatic(modifiers) && !Modifier.isPrivate(modifiers)) {
if (findOverridingMethod(methods[offset], allMethods) == null)
allMethods.add(methods[offset]);
}
}
clazz = clazz.getSuperclass();
}
clazz = typeBinding;
while (clazz != null) {
ITypeBinding[] superInterfaces = clazz.getInterfaces();
for (int index = 0; index < superInterfaces.length; index++) {
getOverridableMethods(ast, superInterfaces[index], allMethods);
}
clazz = clazz.getSuperclass();
}
if (typeBinding.isInterface())
//$NON-NLS-1$
getOverridableMethods(ast, ast.resolveWellKnownType("java.lang.Object"), allMethods);
if (!isSubType)
allMethods.removeAll(Arrays.asList(typeMethods));
int modifiers = 0;
if (!typeBinding.isInterface()) {
for (int index = allMethods.size() - 1; index >= 0; index--) {
IMethodBinding method = allMethods.get(index);
modifiers = method.getModifiers();
if (Modifier.isFinal(modifiers))
allMethods.remove(index);
}
}
return allMethods.toArray(new IMethodBinding[allMethods.size()]);
}
use of org.eclipse.jdt.core.dom.IMethodBinding in project che by eclipse.
the class StubUtility2 method getVisibleConstructors.
public static IMethodBinding[] getVisibleConstructors(ITypeBinding binding, boolean accountExisting, boolean proposeDefault) {
List<IMethodBinding> constructorMethods = new ArrayList<IMethodBinding>();
List<IMethodBinding> existingConstructors = null;
ITypeBinding superType = binding.getSuperclass();
if (superType == null)
return new IMethodBinding[0];
if (accountExisting) {
IMethodBinding[] methods = binding.getDeclaredMethods();
existingConstructors = new ArrayList<IMethodBinding>(methods.length);
for (int index = 0; index < methods.length; index++) {
IMethodBinding method = methods[index];
if (method.isConstructor() && !method.isDefaultConstructor())
existingConstructors.add(method);
}
}
if (existingConstructors != null)
constructorMethods.addAll(existingConstructors);
IMethodBinding[] methods = binding.getDeclaredMethods();
IMethodBinding[] superMethods = superType.getDeclaredMethods();
for (int index = 0; index < superMethods.length; index++) {
IMethodBinding method = superMethods[index];
if (method.isConstructor()) {
if (Bindings.isVisibleInHierarchy(method, binding.getPackage()) && (!accountExisting || !Bindings.containsSignatureEquivalentConstructor(methods, method)))
constructorMethods.add(method);
}
}
if (existingConstructors != null)
constructorMethods.removeAll(existingConstructors);
if (constructorMethods.isEmpty()) {
superType = binding;
while (superType.getSuperclass() != null) superType = superType.getSuperclass();
//$NON-NLS-1$
IMethodBinding method = Bindings.findMethodInType(superType, "Object", new ITypeBinding[0]);
if (method != null) {
if ((proposeDefault || !accountExisting || existingConstructors == null || existingConstructors.isEmpty()) && (!accountExisting || !Bindings.containsSignatureEquivalentConstructor(methods, method)))
constructorMethods.add(method);
}
}
return constructorMethods.toArray(new IMethodBinding[constructorMethods.size()]);
}
use of org.eclipse.jdt.core.dom.IMethodBinding in project che by eclipse.
the class Bindings method findOverriddenMethodInHierarchy.
/**
* Finds a method in the hierarchy of <code>type</code> that is overridden by </code>binding</code>.
* Returns <code>null</code> if no such method exists. If the method is defined in more than one super type only the first match is
* returned. First the super class is examined and then the implemented interfaces.
* @param type The type to search the method in
* @param binding The method that overrides
* @return the method binding overridden the method
*/
public static IMethodBinding findOverriddenMethodInHierarchy(ITypeBinding type, IMethodBinding binding) {
IMethodBinding method = findOverriddenMethodInType(type, binding);
if (method != null)
return method;
ITypeBinding superClass = type.getSuperclass();
if (superClass != null) {
method = findOverriddenMethodInHierarchy(superClass, binding);
if (method != null)
return method;
}
ITypeBinding[] interfaces = type.getInterfaces();
for (int i = 0; i < interfaces.length; i++) {
method = findOverriddenMethodInHierarchy(interfaces[i], binding);
if (method != null)
return method;
}
return null;
}
use of org.eclipse.jdt.core.dom.IMethodBinding 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.IMethodBinding 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);
}
}
}
Aggregations