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;
}
use of org.eclipse.jdt.core.dom.ITypeBinding in project che by eclipse.
the class NewCUUsingWizardProposal method getPossibleSuperTypeBinding.
// private NewTypeWizardPage getPage(NewElementWizard wizard) {
// IWizardPage[] pages= wizard.getPages();
// Assert.isTrue(pages.length > 0 && pages[0] instanceof NewTypeWizardPage);
// return (NewTypeWizardPage) pages[0];
// }
//
// private NewElementWizard createWizard(StructuredSelection selection) {
// switch (fTypeKind) {
// case K_CLASS: {
// NewClassWizardPage page= new NewClassWizardPage();
// page.init(selection);
// configureWizardPage(page);
// return new NewClassCreationWizard(page, true);
// }
// case K_INTERFACE: {
// NewInterfaceWizardPage page= new NewInterfaceWizardPage();
// page.init(selection);
// configureWizardPage(page);
// return new NewInterfaceCreationWizard(page, true);
// }
// case K_ENUM: {
// NewEnumWizardPage page= new NewEnumWizardPage();
// page.init(selection);
// configureWizardPage(page);
// return new NewEnumCreationWizard(page, true);
// }
// case K_ANNOTATION: {
// NewAnnotationWizardPage page= new NewAnnotationWizardPage();
// page.init(selection);
// configureWizardPage(page);
// return new NewAnnotationCreationWizard(page, true);
// }
// }
// throw new IllegalArgumentException();
// }
//
// private void configureWizardPage(NewTypeWizardPage page) {
// fillInWizardPageName(page);
// fillInWizardPageSuperTypes(page);
// }
//
// /**
// * Fill-in the "Package" and "Name" fields.
// * @param page the wizard page.
// */
// private void fillInWizardPageName(NewTypeWizardPage page) {
// // allow to edit when there are type parameters
// page.setTypeName(fTypeNameWithParameters, fTypeNameWithParameters.indexOf('<') != -1);
//
// boolean isInEnclosingType= fTypeContainer instanceof IType;
// if (isInEnclosingType) {
// page.setEnclosingType((IType) fTypeContainer, true);
// } else {
// page.setPackageFragment((IPackageFragment) fTypeContainer, true);
// }
// page.setEnclosingTypeSelection(isInEnclosingType, true);
// }
//
// /**
// * Fill-in the "Super Class" and "Super Interfaces" fields.
// * @param page the wizard page.
// */
// private void fillInWizardPageSuperTypes(NewTypeWizardPage page) {
// ITypeBinding type= getPossibleSuperTypeBinding(fNode);
// type= Bindings.normalizeTypeBinding(type);
// if (type != null) {
// if (type.isArray()) {
// type= type.getElementType();
// }
// if (type.isTopLevel() || type.isMember()) {
// if (type.isClass() && (fTypeKind == K_CLASS)) {
// page.setSuperClass(type.getQualifiedName(), true);
// } else if (type.isInterface()) {
// List<String> superInterfaces= new ArrayList<String>();
// superInterfaces.add(type.getQualifiedName());
// page.setSuperInterfaces(superInterfaces, true);
// }
// }
// }
// }
private ITypeBinding getPossibleSuperTypeBinding(ASTNode node) {
if (fTypeKind == K_ANNOTATION) {
return null;
}
AST ast = node.getAST();
node = ASTNodes.getNormalizedNode(node);
ASTNode parent = node.getParent();
switch(parent.getNodeType()) {
case ASTNode.METHOD_DECLARATION:
if (node.getLocationInParent() == MethodDeclaration.THROWN_EXCEPTION_TYPES_PROPERTY) {
//$NON-NLS-1$
return ast.resolveWellKnownType("java.lang.Exception");
}
break;
case ASTNode.THROW_STATEMENT:
//$NON-NLS-1$
return ast.resolveWellKnownType("java.lang.Exception");
case ASTNode.SINGLE_VARIABLE_DECLARATION:
if (parent.getLocationInParent() == CatchClause.EXCEPTION_PROPERTY) {
//$NON-NLS-1$
return ast.resolveWellKnownType("java.lang.Exception");
}
break;
case ASTNode.VARIABLE_DECLARATION_STATEMENT:
case ASTNode.FIELD_DECLARATION:
// no guessing for LHS types, cannot be a supertype of a known type
return null;
case ASTNode.PARAMETERIZED_TYPE:
// Inheritance doesn't help: A<X> z= new A<String>(); ->
return null;
}
ITypeBinding binding = ASTResolving.guessBindingForTypeReference(node);
if (binding != null && !binding.isRecovered()) {
return binding;
}
return null;
}
Aggregations