use of org.eclipse.jdt.core.dom.IMethodBinding 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);
}
}
use of org.eclipse.jdt.core.dom.IMethodBinding in project che by eclipse.
the class UnresolvedElementsSubProcessor method doMoreParameters.
private static void doMoreParameters(IInvocationContext context, ASTNode invocationNode, ITypeBinding[] argTypes, IMethodBinding methodBinding, Collection<ICommandAccess> proposals) throws CoreException {
ITypeBinding[] paramTypes = methodBinding.getParameterTypes();
int k = 0, nSkipped = 0;
int diff = paramTypes.length - argTypes.length;
int[] indexSkipped = new int[diff];
for (int i = 0; i < paramTypes.length; i++) {
if (k < argTypes.length && canAssign(argTypes[k], paramTypes[i])) {
// match
k++;
} else {
if (nSkipped >= diff) {
// too different
return;
}
indexSkipped[nSkipped++] = i;
}
}
ITypeBinding declaringType = methodBinding.getDeclaringClass();
ICompilationUnit cu = context.getCompilationUnit();
CompilationUnit astRoot = context.getASTRoot();
// add arguments
{
String[] arg = new String[] { ASTResolving.getMethodSignature(methodBinding) };
String label;
if (diff == 1) {
label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_addargument_description, arg);
} else {
label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_addarguments_description, arg);
}
AddArgumentCorrectionProposal proposal = new AddArgumentCorrectionProposal(label, context.getCompilationUnit(), invocationNode, indexSkipped, paramTypes, IProposalRelevance.ADD_ARGUMENTS);
proposal.setImage(JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_ADD));
proposals.add(proposal);
}
// remove parameters
if (!declaringType.isFromSource()) {
return;
}
ICompilationUnit targetCU = ASTResolving.findCompilationUnitForBinding(cu, astRoot, declaringType);
if (targetCU != null) {
IMethodBinding methodDecl = methodBinding.getMethodDeclaration();
ITypeBinding[] declParameterTypes = methodDecl.getParameterTypes();
ChangeDescription[] changeDesc = new ChangeDescription[declParameterTypes.length];
ITypeBinding[] changedTypes = new ITypeBinding[diff];
for (int i = diff - 1; i >= 0; i--) {
int idx = indexSkipped[i];
changeDesc[idx] = new RemoveDescription();
changedTypes[i] = declParameterTypes[idx];
}
String[] arg = new String[] { ASTResolving.getMethodSignature(methodDecl), getTypeNames(changedTypes) };
String label;
if (methodDecl.isConstructor()) {
if (diff == 1) {
label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_removeparam_constr_description, arg);
} else {
label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_removeparams_constr_description, arg);
}
} else {
if (diff == 1) {
label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_removeparam_description, arg);
} else {
label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_removeparams_description, arg);
}
}
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_REMOVE);
ChangeMethodSignatureProposal proposal = new ChangeMethodSignatureProposal(label, targetCU, invocationNode, methodDecl, changeDesc, null, IProposalRelevance.CHANGE_METHOD_REMOVE_PARAMETER, image);
proposals.add(proposal);
}
}
use of org.eclipse.jdt.core.dom.IMethodBinding in project che by eclipse.
the class GenerateForLoopAssistProposal method getIteratorBasedForInitializer.
/**
* Generates the initializer for an iterator based <code>for</code> loop, which declares and
* initializes the variable to loop over.
*
* @param rewrite the instance of {@link ASTRewrite}
* @param loopVariableName the proposed name of the loop variable
* @return a {@link VariableDeclarationExpression} to use as initializer
*/
private VariableDeclarationExpression getIteratorBasedForInitializer(ASTRewrite rewrite, SimpleName loopVariableName) {
AST ast = rewrite.getAST();
//$NON-NLS-1$
IMethodBinding iteratorMethodBinding = Bindings.findMethodInHierarchy(fExpressionType, "iterator", new ITypeBinding[] {});
// initializing fragment
VariableDeclarationFragment varDeclarationFragment = ast.newVariableDeclarationFragment();
varDeclarationFragment.setName(loopVariableName);
MethodInvocation iteratorExpression = ast.newMethodInvocation();
iteratorExpression.setName(ast.newSimpleName(iteratorMethodBinding.getName()));
iteratorExpression.setExpression((Expression) rewrite.createCopyTarget(fCurrentExpression));
varDeclarationFragment.setInitializer(iteratorExpression);
// declaration
VariableDeclarationExpression varDeclarationExpression = ast.newVariableDeclarationExpression(varDeclarationFragment);
varDeclarationExpression.setType(getImportRewrite().addImport(iteratorMethodBinding.getReturnType(), ast, new ContextSensitiveImportRewriteContext(fCurrentNode, getImportRewrite())));
return varDeclarationExpression;
}
use of org.eclipse.jdt.core.dom.IMethodBinding in project che by eclipse.
the class GenerateForLoopAssistProposal method extractElementType.
/**
* Extracts the type parameter of the variable contained in fCurrentExpression or the elements
* type to iterate over an array using <code>foreach</code>.
*
* @param ast the current {@link AST} instance
* @return the {@link ITypeBinding} of the elements to iterate over
*/
private ITypeBinding extractElementType(AST ast) {
if (fExpressionType.isArray()) {
return Bindings.normalizeForDeclarationUse(fExpressionType.getElementType(), ast);
}
// extract elements type directly out of the bindings
//$NON-NLS-1$
IMethodBinding iteratorMethodBinding = Bindings.findMethodInHierarchy(fExpressionType, "iterator", new ITypeBinding[] {});
IMethodBinding iteratorNextMethodBinding = Bindings.findMethodInHierarchy(iteratorMethodBinding.getReturnType(), "next", //$NON-NLS-1$
new ITypeBinding[] {});
ITypeBinding currentElementBinding = iteratorNextMethodBinding.getReturnType();
return Bindings.normalizeForDeclarationUse(currentElementBinding, ast);
}
use of org.eclipse.jdt.core.dom.IMethodBinding in project che by eclipse.
the class TypeMismatchSubProcessor method addChangeSenderTypeProposals.
public static void addChangeSenderTypeProposals(IInvocationContext context, Expression nodeToCast, ITypeBinding castTypeBinding, boolean isAssignedNode, int relevance, Collection<ICommandAccess> proposals) throws JavaModelException {
IBinding callerBinding = Bindings.resolveExpressionBinding(nodeToCast, false);
ICompilationUnit cu = context.getCompilationUnit();
CompilationUnit astRoot = context.getASTRoot();
ICompilationUnit targetCu = null;
ITypeBinding declaringType = null;
IBinding callerBindingDecl = callerBinding;
if (callerBinding instanceof IVariableBinding) {
IVariableBinding variableBinding = (IVariableBinding) callerBinding;
if (variableBinding.isEnumConstant()) {
return;
}
if (!variableBinding.isField()) {
targetCu = cu;
} else {
callerBindingDecl = variableBinding.getVariableDeclaration();
ITypeBinding declaringClass = variableBinding.getDeclaringClass();
if (declaringClass == null) {
// array length
return;
}
declaringType = declaringClass.getTypeDeclaration();
}
} else if (callerBinding instanceof IMethodBinding) {
IMethodBinding methodBinding = (IMethodBinding) callerBinding;
if (!methodBinding.isConstructor()) {
declaringType = methodBinding.getDeclaringClass().getTypeDeclaration();
callerBindingDecl = methodBinding.getMethodDeclaration();
}
} else if (callerBinding instanceof ITypeBinding && nodeToCast.getLocationInParent() == SingleMemberAnnotation.TYPE_NAME_PROPERTY) {
declaringType = (ITypeBinding) callerBinding;
//$NON-NLS-1$
callerBindingDecl = Bindings.findMethodInType(declaringType, "value", (String[]) null);
if (callerBindingDecl == null) {
return;
}
}
if (declaringType != null && declaringType.isFromSource()) {
targetCu = ASTResolving.findCompilationUnitForBinding(cu, astRoot, declaringType);
}
if (targetCu != null && ASTResolving.isUseableTypeInContext(castTypeBinding, callerBindingDecl, false)) {
proposals.add(new TypeChangeCorrectionProposal(targetCu, callerBindingDecl, astRoot, castTypeBinding, isAssignedNode, relevance));
}
// add interface to resulting type
if (!isAssignedNode) {
ITypeBinding nodeType = nodeToCast.resolveTypeBinding();
if (castTypeBinding.isInterface() && nodeType != null && nodeType.isClass() && !nodeType.isAnonymous() && nodeType.isFromSource()) {
ITypeBinding typeDecl = nodeType.getTypeDeclaration();
ICompilationUnit nodeCu = ASTResolving.findCompilationUnitForBinding(cu, astRoot, typeDecl);
if (nodeCu != null && ASTResolving.isUseableTypeInContext(castTypeBinding, typeDecl, true)) {
proposals.add(new ImplementInterfaceProposal(nodeCu, typeDecl, astRoot, castTypeBinding, relevance - 1));
}
}
}
}
Aggregations