use of org.eclipse.jdt.core.dom.ExpressionStatement 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.ExpressionStatement in project che by eclipse.
the class NewMethodCorrectionProposal method getNewMethodType.
/* (non-Javadoc)
* @see org.eclipse.jdt.internal.ui.text.correction.proposals.AbstractMethodCorrectionProposal#getNewMethodType(org.eclipse.jdt.core.dom.rewrite.ASTRewrite)
*/
@Override
protected Type getNewMethodType(ASTRewrite rewrite) throws CoreException {
ASTNode node = getInvocationNode();
AST ast = rewrite.getAST();
Type newTypeNode = null;
ITypeBinding[] otherProposals = null;
ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(node, getImportRewrite());
if (node.getParent() instanceof MethodInvocation) {
MethodInvocation parent = (MethodInvocation) node.getParent();
if (parent.getExpression() == node) {
ITypeBinding[] bindings = ASTResolving.getQualifierGuess(node.getRoot(), parent.getName().getIdentifier(), parent.arguments(), getSenderBinding());
if (bindings.length > 0) {
newTypeNode = getImportRewrite().addImport(bindings[0], ast, importRewriteContext);
otherProposals = bindings;
}
}
}
if (newTypeNode == null) {
ITypeBinding binding = ASTResolving.guessBindingForReference(node);
if (binding != null && binding.isWildcardType()) {
binding = ASTResolving.normalizeWildcardType(binding, false, ast);
}
if (binding != null) {
newTypeNode = getImportRewrite().addImport(binding, ast, importRewriteContext);
} else {
ASTNode parent = node.getParent();
if (parent instanceof ExpressionStatement) {
newTypeNode = ast.newPrimitiveType(PrimitiveType.VOID);
} else {
newTypeNode = ASTResolving.guessTypeForReference(ast, node);
if (newTypeNode == null) {
//$NON-NLS-1$
newTypeNode = ast.newSimpleType(ast.newSimpleName("Object"));
}
}
}
}
addLinkedPosition(rewrite.track(newTypeNode), false, KEY_TYPE);
if (otherProposals != null) {
for (int i = 0; i < otherProposals.length; i++) {
addLinkedPositionProposal(KEY_TYPE, otherProposals[i]);
}
}
return newTypeNode;
}
use of org.eclipse.jdt.core.dom.ExpressionStatement in project che by eclipse.
the class AssignToVariableAssistProposal method doAddLocal.
private ASTRewrite doAddLocal() {
Expression expression = ((ExpressionStatement) fNodeToAssign).getExpression();
AST ast = fNodeToAssign.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
createImportRewrite((CompilationUnit) fNodeToAssign.getRoot());
String[] varNames = suggestLocalVariableNames(fTypeBinding, expression);
for (int i = 0; i < varNames.length; i++) {
addLinkedPositionProposal(KEY_NAME, varNames[i], null);
}
VariableDeclarationFragment newDeclFrag = ast.newVariableDeclarationFragment();
newDeclFrag.setName(ast.newSimpleName(varNames[0]));
newDeclFrag.setInitializer((Expression) rewrite.createCopyTarget(expression));
Type type = evaluateType(ast);
if (ASTNodes.isControlStatementBody(fNodeToAssign.getLocationInParent())) {
Block block = ast.newBlock();
block.statements().add(rewrite.createMoveTarget(fNodeToAssign));
rewrite.replace(fNodeToAssign, block, null);
}
if (needsSemicolon(expression)) {
VariableDeclarationStatement varStatement = ast.newVariableDeclarationStatement(newDeclFrag);
varStatement.setType(type);
rewrite.replace(expression, varStatement, null);
} else {
// trick for bug 43248: use an VariableDeclarationExpression and keep the ExpressionStatement
VariableDeclarationExpression varExpression = ast.newVariableDeclarationExpression(newDeclFrag);
varExpression.setType(type);
rewrite.replace(expression, varExpression, null);
}
addLinkedPosition(rewrite.track(newDeclFrag.getName()), true, KEY_NAME);
addLinkedPosition(rewrite.track(type), false, KEY_TYPE);
// set cursor after expression statement
setEndPosition(rewrite.track(fNodeToAssign));
return rewrite;
}
use of org.eclipse.jdt.core.dom.ExpressionStatement in project che by eclipse.
the class IntroduceFactoryRefactoring method getCtorCallAt.
/**
* Look "in the vicinity" of the given range to find the <code>ClassInstanceCreation</code>
* node that this search hit identified. Necessary because the <code>SearchEngine</code>
* doesn't always cough up text extents that <code>NodeFinder.perform()</code> agrees with.
* @param start
* @param length
* @param unitAST
* @return return a {@link ClassInstanceCreation} or a {@link MethodRef} or <code>null</code> if this is really a constructor->constructor call (e.g. "this(...)")
* @throws CoreException
*/
private ASTNode getCtorCallAt(int start, int length, CompilationUnit unitAST) throws CoreException {
ICompilationUnit unitHandle = ASTCreator.getCu(unitAST);
ASTNode node = NodeFinder.perform(unitAST, start, length);
if (node == null)
throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, Messages.format(RefactoringCoreMessages.IntroduceFactory_noASTNodeForConstructorSearchHit, new Object[] { Integer.toString(start), Integer.toString(start + length), BasicElementLabels.getJavaCodeString(unitHandle.getSource().substring(start, start + length)), BasicElementLabels.getFileName(unitHandle) }), null));
if (node instanceof ClassInstanceCreation) {
if (((ClassInstanceCreation) node).getAnonymousClassDeclaration() != null) {
// Cannot replace anonymous inner class, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=250660
fConstructorVisibility = Modifier.PROTECTED;
return null;
}
return node;
} else if (node instanceof VariableDeclaration) {
Expression init = ((VariableDeclaration) node).getInitializer();
if (init instanceof ClassInstanceCreation) {
return init;
} else if (init != null)
throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, Messages.format(RefactoringCoreMessages.IntroduceFactory_unexpectedInitializerNodeType, new Object[] { BasicElementLabels.getJavaCodeString(init.toString()), BasicElementLabels.getFileName(unitHandle) }), null));
else
throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, Messages.format(RefactoringCoreMessages.IntroduceFactory_noConstructorCallNodeInsideFoundVarbleDecl, BasicElementLabels.getJavaCodeString(node.toString())), null));
} else if (node instanceof ConstructorInvocation) {
// to another flavor on the same class.
return null;
} else if (node instanceof SuperConstructorInvocation) {
// This is a call we can bypass; it's from one constructor flavor
// to another flavor on the same class.
fConstructorVisibility = Modifier.PROTECTED;
return null;
} else if (node instanceof ExpressionStatement) {
Expression expr = ((ExpressionStatement) node).getExpression();
if (expr instanceof ClassInstanceCreation)
return expr;
else
throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, Messages.format(RefactoringCoreMessages.IntroduceFactory_unexpectedASTNodeTypeForConstructorSearchHit, new Object[] { BasicElementLabels.getJavaCodeString(expr.toString()), BasicElementLabels.getFileName(unitHandle) }), null));
} else if (node instanceof SimpleName && (node.getParent() instanceof MethodDeclaration || node.getParent() instanceof AbstractTypeDeclaration)) {
// We seem to have been given a hit for an implicit call to the base-class constructor.
// Do nothing with this (implicit) call, but have to make sure we make the derived class
// doesn't lose access to the base-class constructor (so make it 'protected', not 'private').
fConstructorVisibility = Modifier.PROTECTED;
return null;
} else if (node instanceof MethodRef) {
return node;
} else
throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, Messages.format(RefactoringCoreMessages.IntroduceFactory_unexpectedASTNodeTypeForConstructorSearchHit, new Object[] { BasicElementLabels.getJavaElementName(node.getClass().getName() + "('" + node.toString() + "')"), BasicElementLabels.getFileName(unitHandle) }), //$NON-NLS-1$ //$NON-NLS-2$
null));
}
use of org.eclipse.jdt.core.dom.ExpressionStatement in project che by eclipse.
the class ExtractTempRefactoring method getSelectedExpression.
private IExpressionFragment getSelectedExpression() throws JavaModelException {
if (fSelectedExpression != null)
return fSelectedExpression;
IASTFragment selectedFragment = ASTFragmentFactory.createFragmentForSourceRange(new SourceRange(fSelectionStart, fSelectionLength), fCompilationUnitNode, fCu);
if (selectedFragment instanceof IExpressionFragment && !Checks.isInsideJavadoc(selectedFragment.getAssociatedNode())) {
fSelectedExpression = (IExpressionFragment) selectedFragment;
} else if (selectedFragment != null) {
if (selectedFragment.getAssociatedNode() instanceof ExpressionStatement) {
ExpressionStatement exprStatement = (ExpressionStatement) selectedFragment.getAssociatedNode();
Expression expression = exprStatement.getExpression();
fSelectedExpression = (IExpressionFragment) ASTFragmentFactory.createFragmentForFullSubtree(expression);
} else if (selectedFragment.getAssociatedNode() instanceof Assignment) {
Assignment assignment = (Assignment) selectedFragment.getAssociatedNode();
fSelectedExpression = (IExpressionFragment) ASTFragmentFactory.createFragmentForFullSubtree(assignment);
}
}
if (fSelectedExpression != null && Checks.isEnumCase(fSelectedExpression.getAssociatedExpression().getParent())) {
fSelectedExpression = null;
}
return fSelectedExpression;
}
Aggregations