use of org.eclipse.jdt.core.dom.Expression in project che by eclipse.
the class MissingReturnTypeCorrectionProposal method evaluateReturnExpressions.
/*
* Evaluates possible return expressions. The favourite expression is returned.
*/
private Expression evaluateReturnExpressions(AST ast, ITypeBinding returnBinding, int returnOffset) {
CompilationUnit root = getCU();
Expression result = null;
if (returnBinding != null) {
result = computeProposals(ast, returnBinding, returnOffset, root, result);
}
Expression defaultExpression = createDefaultExpression(ast);
addLinkedPositionProposal(RETURN_EXPRESSION_KEY, ASTNodes.asString(defaultExpression), null);
if (result == null) {
return defaultExpression;
}
return result;
}
use of org.eclipse.jdt.core.dom.Expression 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.Expression 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.Expression in project che by eclipse.
the class NewMethodCorrectionProposal method evaluateModifiers.
private int evaluateModifiers(ASTNode targetTypeDecl) {
if (getSenderBinding().isAnnotation()) {
return 0;
}
if (getSenderBinding().isInterface()) {
// for interface and annotation members copy the modifiers from an existing field
MethodDeclaration[] methodDecls = ((TypeDeclaration) targetTypeDecl).getMethods();
if (methodDecls.length > 0) {
return methodDecls[0].getModifiers();
}
return 0;
}
ASTNode invocationNode = getInvocationNode();
if (invocationNode instanceof MethodInvocation) {
int modifiers = 0;
Expression expression = ((MethodInvocation) invocationNode).getExpression();
if (expression != null) {
if (expression instanceof Name && ((Name) expression).resolveBinding().getKind() == IBinding.TYPE) {
modifiers |= Modifier.STATIC;
}
} else if (ASTResolving.isInStaticContext(invocationNode)) {
modifiers |= Modifier.STATIC;
}
ASTNode node = ASTResolving.findParentType(invocationNode);
if (targetTypeDecl.equals(node)) {
modifiers |= Modifier.PRIVATE;
} else if (node instanceof AnonymousClassDeclaration && ASTNodes.isParent(node, targetTypeDecl)) {
modifiers |= Modifier.PROTECTED;
if (ASTResolving.isInStaticContext(node) && expression == null) {
modifiers |= Modifier.STATIC;
}
} else {
modifiers |= Modifier.PUBLIC;
}
return modifiers;
}
return Modifier.PUBLIC;
}
use of org.eclipse.jdt.core.dom.Expression in project che by eclipse.
the class NewMethodCorrectionProposal method addNewParameters.
/* (non-Javadoc)
* @see org.eclipse.jdt.internal.ui.text.correction.proposals.AbstractMethodCorrectionProposal#addNewParameters(org.eclipse.jdt.core.dom.rewrite.ASTRewrite, java.util.List, java.util.List)
*/
@Override
protected void addNewParameters(ASTRewrite rewrite, List<String> takenNames, List<SingleVariableDeclaration> params) throws CoreException {
AST ast = rewrite.getAST();
List<Expression> arguments = fArguments;
ImportRewriteContext context = new ContextSensitiveImportRewriteContext(ASTResolving.findParentBodyDeclaration(getInvocationNode()), getImportRewrite());
for (int i = 0; i < arguments.size(); i++) {
Expression elem = arguments.get(i);
SingleVariableDeclaration param = ast.newSingleVariableDeclaration();
// argument type
//$NON-NLS-1$
String argTypeKey = "arg_type_" + i;
Type type = evaluateParameterType(ast, elem, argTypeKey, context);
param.setType(type);
// argument name
//$NON-NLS-1$
String argNameKey = "arg_name_" + i;
String name = evaluateParameterName(takenNames, elem, type, argNameKey);
param.setName(ast.newSimpleName(name));
params.add(param);
addLinkedPosition(rewrite.track(param.getType()), false, argTypeKey);
addLinkedPosition(rewrite.track(param.getName()), false, argNameKey);
}
}
Aggregations