use of org.eclipse.jdt.core.dom.ITypeBinding 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.ITypeBinding in project che by eclipse.
the class ReturnTypeSubProcessor method addMissingReturnTypeProposals.
public static void addMissingReturnTypeProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
ICompilationUnit cu = context.getCompilationUnit();
CompilationUnit astRoot = context.getASTRoot();
ASTNode selectedNode = problem.getCoveringNode(astRoot);
if (selectedNode == null) {
return;
}
BodyDeclaration decl = ASTResolving.findParentBodyDeclaration(selectedNode);
if (decl instanceof MethodDeclaration) {
MethodDeclaration methodDeclaration = (MethodDeclaration) decl;
ReturnStatementCollector eval = new ReturnStatementCollector();
decl.accept(eval);
AST ast = astRoot.getAST();
ITypeBinding typeBinding = eval.getTypeBinding(decl.getAST());
typeBinding = Bindings.normalizeTypeBinding(typeBinding);
if (typeBinding == null) {
//$NON-NLS-1$
typeBinding = ast.resolveWellKnownType("void");
}
if (typeBinding.isWildcardType()) {
typeBinding = ASTResolving.normalizeWildcardType(typeBinding, true, ast);
}
ASTRewrite rewrite = ASTRewrite.create(ast);
String label = Messages.format(CorrectionMessages.ReturnTypeSubProcessor_missingreturntype_description, BindingLabelProvider.getBindingLabel(typeBinding, BindingLabelProvider.DEFAULT_TEXTFLAGS));
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, cu, rewrite, IProposalRelevance.MISSING_RETURN_TYPE, image);
ImportRewrite imports = proposal.createImportRewrite(astRoot);
ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(decl, imports);
Type type = imports.addImport(typeBinding, ast, importRewriteContext);
rewrite.set(methodDeclaration, MethodDeclaration.RETURN_TYPE2_PROPERTY, type, null);
rewrite.set(methodDeclaration, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.FALSE, null);
Javadoc javadoc = methodDeclaration.getJavadoc();
if (javadoc != null && typeBinding != null) {
TagElement newTag = ast.newTagElement();
newTag.setTagName(TagElement.TAG_RETURN);
TextElement commentStart = ast.newTextElement();
newTag.fragments().add(commentStart);
JavadocTagsSubProcessor.insertTag(rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY), newTag, null);
//$NON-NLS-1$
proposal.addLinkedPosition(rewrite.track(commentStart), false, "comment_start");
}
//$NON-NLS-1$
String key = "return_type";
proposal.addLinkedPosition(rewrite.track(type), true, key);
if (typeBinding != null) {
ITypeBinding[] bindings = ASTResolving.getRelaxingTypes(ast, typeBinding);
for (int i = 0; i < bindings.length; i++) {
proposal.addLinkedPositionProposal(key, bindings[i]);
}
}
proposals.add(proposal);
// change to constructor
ASTNode parentType = ASTResolving.findParentType(decl);
if (parentType instanceof AbstractTypeDeclaration) {
boolean isInterface = parentType instanceof TypeDeclaration && ((TypeDeclaration) parentType).isInterface();
if (!isInterface) {
String constructorName = ((AbstractTypeDeclaration) parentType).getName().getIdentifier();
ASTNode nameNode = methodDeclaration.getName();
label = Messages.format(CorrectionMessages.ReturnTypeSubProcessor_wrongconstructorname_description, BasicElementLabels.getJavaElementName(constructorName));
proposals.add(new ReplaceCorrectionProposal(label, cu, nameNode.getStartPosition(), nameNode.getLength(), constructorName, IProposalRelevance.CHANGE_TO_CONSTRUCTOR));
}
}
}
}
use of org.eclipse.jdt.core.dom.ITypeBinding in project che by eclipse.
the class GenerateForLoopAssistProposal method getIndexBasedForBodyAssignment.
/**
* Creates an {@link Assignment} as first expression appearing in an index based
* <code>for</code> loop's body. This Assignment declares a local variable and initializes it
* using the {@link List}'s current element identified by the loop index.
*
* @param rewrite the current {@link ASTRewrite} instance
* @param loopVariableName the name of the index variable in String representation
* @return a completed {@link Assignment} containing the mentioned declaration and
* initialization
*/
private Expression getIndexBasedForBodyAssignment(ASTRewrite rewrite, SimpleName loopVariableName) {
AST ast = rewrite.getAST();
ITypeBinding loopOverType = extractElementType(ast);
Assignment assignResolvedVariable = ast.newAssignment();
// left hand side
SimpleName resolvedVariableName = resolveLinkedVariableNameWithProposals(rewrite, loopOverType.getName(), loopVariableName.getIdentifier(), false);
VariableDeclarationFragment resolvedVariableDeclarationFragment = ast.newVariableDeclarationFragment();
resolvedVariableDeclarationFragment.setName(resolvedVariableName);
VariableDeclarationExpression resolvedVariableDeclaration = ast.newVariableDeclarationExpression(resolvedVariableDeclarationFragment);
resolvedVariableDeclaration.setType(getImportRewrite().addImport(loopOverType, ast, new ContextSensitiveImportRewriteContext(fCurrentNode, getImportRewrite())));
assignResolvedVariable.setLeftHandSide(resolvedVariableDeclaration);
// right hand side
MethodInvocation invokeGetExpression = ast.newMethodInvocation();
//$NON-NLS-1$
invokeGetExpression.setName(ast.newSimpleName("get"));
SimpleName indexVariableName = ast.newSimpleName(loopVariableName.getIdentifier());
addLinkedPosition(rewrite.track(indexVariableName), LinkedPositionGroup.NO_STOP, indexVariableName.getIdentifier());
invokeGetExpression.arguments().add(indexVariableName);
invokeGetExpression.setExpression((Expression) rewrite.createCopyTarget(fCurrentExpression));
assignResolvedVariable.setRightHandSide(invokeGetExpression);
assignResolvedVariable.setOperator(Assignment.Operator.ASSIGN);
return assignResolvedVariable;
}
use of org.eclipse.jdt.core.dom.ITypeBinding in project che by eclipse.
the class MissingReturnTypeCorrectionProposal method getRewrite.
/*(non-Javadoc)
* @see org.eclipse.jdt.internal.ui.text.correction.ASTRewriteCorrectionProposal#getRewrite()
*/
@Override
protected ASTRewrite getRewrite() {
AST ast = getAST();
ITypeBinding returnBinding = getReturnTypeBinding();
if (fExistingReturn != null) {
ASTRewrite rewrite = ASTRewrite.create(ast);
Expression expression = evaluateReturnExpressions(ast, returnBinding, fExistingReturn.getStartPosition());
if (expression != null) {
rewrite.set(fExistingReturn, ReturnStatement.EXPRESSION_PROPERTY, expression, null);
addLinkedPosition(rewrite.track(expression), true, RETURN_EXPRESSION_KEY);
}
return rewrite;
} else {
ASTRewrite rewrite = ASTRewrite.create(ast);
ASTNode body = getBody();
// For lambda the body can be a block or an expression.
if (body instanceof Block) {
Block block = (Block) body;
List<Statement> statements = block.statements();
int nStatements = statements.size();
ASTNode lastStatement = null;
if (nStatements > 0) {
lastStatement = statements.get(nStatements - 1);
}
if (returnBinding != null && lastStatement instanceof ExpressionStatement && lastStatement.getNodeType() != ASTNode.ASSIGNMENT) {
Expression expression = ((ExpressionStatement) lastStatement).getExpression();
ITypeBinding binding = expression.resolveTypeBinding();
if (binding != null && binding.isAssignmentCompatible(returnBinding)) {
Expression placeHolder = (Expression) rewrite.createMoveTarget(expression);
ReturnStatement returnStatement = ast.newReturnStatement();
returnStatement.setExpression(placeHolder);
rewrite.replace(lastStatement, returnStatement, null);
return rewrite;
}
}
int offset;
if (lastStatement == null) {
offset = block.getStartPosition() + 1;
} else {
offset = lastStatement.getStartPosition() + lastStatement.getLength();
}
ReturnStatement returnStatement = ast.newReturnStatement();
Expression expression = evaluateReturnExpressions(ast, returnBinding, offset);
returnStatement.setExpression(expression);
rewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY).insertLast(returnStatement, null);
addLinkedPosition(rewrite.track(returnStatement.getExpression()), true, RETURN_EXPRESSION_KEY);
}
return rewrite;
}
}
use of org.eclipse.jdt.core.dom.ITypeBinding in project che by eclipse.
the class NewAnnotationMemberProposal method getNewType.
private Type getNewType(ASTRewrite rewrite) {
AST ast = rewrite.getAST();
Type newTypeNode = null;
ITypeBinding binding = null;
if (fInvocationNode.getLocationInParent() == MemberValuePair.NAME_PROPERTY) {
Expression value = ((MemberValuePair) fInvocationNode.getParent()).getValue();
binding = value.resolveTypeBinding();
} else if (fInvocationNode instanceof Expression) {
binding = ((Expression) fInvocationNode).resolveTypeBinding();
}
if (binding != null) {
ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(fInvocationNode, getImportRewrite());
newTypeNode = getImportRewrite().addImport(binding, ast, importRewriteContext);
}
if (newTypeNode == null) {
//$NON-NLS-1$
newTypeNode = ast.newSimpleType(ast.newSimpleName("String"));
}
addLinkedPosition(rewrite.track(newTypeNode), false, KEY_TYPE);
return newTypeNode;
}
Aggregations