use of org.eclipse.jdt.core.dom.ASTNode in project che by eclipse.
the class InferTypeArgumentsRefactoring method rewriteCastVariable.
private static ASTNode rewriteCastVariable(CastVariable2 castCv, CompilationUnitRewrite rewrite, InferTypeArgumentsTCModel tCModel) {
//, List positionGroups) {
ASTNode node = castCv.getRange().getNode(rewrite.getRoot());
ConstraintVariable2 expressionVariable = castCv.getExpressionVariable();
ConstraintVariable2 methodReceiverCv = tCModel.getMethodReceiverCv(expressionVariable);
if (methodReceiverCv != null) {
TType chosenReceiverType = InferTypeArgumentsConstraintsSolver.getChosenType(methodReceiverCv);
if (chosenReceiverType == null)
return null;
else if (!InferTypeArgumentsTCModel.isAGenericType(chosenReceiverType))
return null;
else if (hasUnboundElement(methodReceiverCv, tCModel))
return null;
}
CastExpression castExpression = (CastExpression) node;
Expression expression = castExpression.getExpression();
ASTNode nodeToReplace;
if (castExpression.getParent() instanceof ParenthesizedExpression)
nodeToReplace = castExpression.getParent();
else
nodeToReplace = castExpression;
Expression newExpression = (Expression) rewrite.getASTRewrite().createMoveTarget(expression);
rewrite.getASTRewrite().replace(nodeToReplace, newExpression, rewrite.createGroupDescription(RefactoringCoreMessages.InferTypeArgumentsRefactoring_removeCast));
rewrite.getImportRemover().registerRemovedNode(nodeToReplace);
return newExpression;
}
use of org.eclipse.jdt.core.dom.ASTNode in project che by eclipse.
the class SourceProvider method replaceParameterWithExpression.
private void replaceParameterWithExpression(ASTRewrite rewriter, CallContext context, ImportRewrite importRewrite) throws CoreException {
Expression[] arguments = context.arguments;
try {
ITextFileBuffer buffer = RefactoringFileBuffers.acquire(context.compilationUnit);
for (int i = 0; i < arguments.length; i++) {
Expression expression = arguments[i];
String expressionString = null;
if (expression instanceof SimpleName) {
expressionString = ((SimpleName) expression).getIdentifier();
} else {
try {
expressionString = buffer.getDocument().get(expression.getStartPosition(), expression.getLength());
} catch (BadLocationException exception) {
JavaPlugin.log(exception);
continue;
}
}
ParameterData parameter = getParameterData(i);
List<SimpleName> references = parameter.references();
for (Iterator<SimpleName> iter = references.iterator(); iter.hasNext(); ) {
ASTNode element = iter.next();
Expression newExpression = (Expression) rewriter.createStringPlaceholder(expressionString, expression.getNodeType());
AST ast = rewriter.getAST();
ITypeBinding explicitCast = ASTNodes.getExplicitCast(expression, (Expression) element);
if (explicitCast != null) {
CastExpression cast = ast.newCastExpression();
if (NecessaryParenthesesChecker.needsParentheses(expression, cast, CastExpression.EXPRESSION_PROPERTY)) {
newExpression = createParenthesizedExpression(newExpression, ast);
}
cast.setExpression(newExpression);
ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(expression, importRewrite);
cast.setType(importRewrite.addImport(explicitCast, ast, importRewriteContext));
expression = newExpression = cast;
}
if (NecessaryParenthesesChecker.needsParentheses(expression, element.getParent(), element.getLocationInParent())) {
newExpression = createParenthesizedExpression(newExpression, ast);
}
rewriter.replace(element, newExpression, null);
}
}
} finally {
RefactoringFileBuffers.release(context.compilationUnit);
}
}
use of org.eclipse.jdt.core.dom.ASTNode in project che by eclipse.
the class SourceProvider method isDangligIf.
public boolean isDangligIf() {
List<Statement> statements = fDeclaration.getBody().statements();
if (statements.size() != 1)
return false;
ASTNode p = statements.get(0);
while (true) {
if (p instanceof IfStatement) {
return ((IfStatement) p).getElseStatement() == null;
} else {
ChildPropertyDescriptor childD;
if (p instanceof WhileStatement) {
childD = WhileStatement.BODY_PROPERTY;
} else if (p instanceof ForStatement) {
childD = ForStatement.BODY_PROPERTY;
} else if (p instanceof EnhancedForStatement) {
childD = EnhancedForStatement.BODY_PROPERTY;
} else if (p instanceof DoStatement) {
childD = DoStatement.BODY_PROPERTY;
} else if (p instanceof LabeledStatement) {
childD = LabeledStatement.BODY_PROPERTY;
} else {
return false;
}
Statement body = (Statement) p.getStructuralProperty(childD);
if (body instanceof Block) {
return false;
} else {
p = body;
}
}
}
}
use of org.eclipse.jdt.core.dom.ASTNode in project che by eclipse.
the class SourceProvider method createRange.
private IRegion createRange(List<Statement> statements, int end) {
ASTNode first = statements.get(0);
ASTNode last = statements.get(end);
return createRange(first, last);
}
use of org.eclipse.jdt.core.dom.ASTNode in project che by eclipse.
the class SourceProvider method getExpressionRanges.
private List<IRegion> getExpressionRanges() {
fMarkerMode = EXPRESSION_MODE;
List<IRegion> result = new ArrayList<IRegion>(2);
List<Statement> statements = fDeclaration.getBody().statements();
ReturnStatement rs = null;
int size = statements.size();
ASTNode node;
switch(size) {
case 0:
return result;
case 1:
node = statements.get(0);
if (node.getNodeType() == ASTNode.RETURN_STATEMENT) {
rs = (ReturnStatement) node;
} else {
result.add(createRange(node, node));
}
break;
default:
{
node = statements.get(size - 1);
if (node.getNodeType() == ASTNode.RETURN_STATEMENT) {
result.add(createRange(statements, size - 2));
rs = (ReturnStatement) node;
} else {
result.add(createRange(statements, size - 1));
}
break;
}
}
if (rs != null) {
Expression exp = rs.getExpression();
result.add(createRange(exp, exp));
}
return result;
}
Aggregations