use of org.eclipse.jdt.core.dom.Assignment 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;
}
use of org.eclipse.jdt.core.dom.Assignment in project AutoRefactor by JnRouvignac.
the class TryWithResourceRefactoring method newFragment.
private VariableDeclarationFragment newFragment(List<Statement> tryStmts, VariableDeclarationFragment existingFragment, List<ASTNode> nodesToRemove) {
final VariableDefinitionsUsesVisitor visitor = new VariableDefinitionsUsesVisitor(existingFragment).find();
final List<SimpleName> definitions = visitor.getDefinitions();
final ASTBuilder b = ctx.getASTBuilder();
if (!tryStmts.isEmpty()) {
final Statement tryStmt = tryStmts.get(0);
final Assignment assignResource = asExpression(tryStmt, Assignment.class);
if (assignResource != null && isSameVariable(existingFragment, assignResource.getLeftHandSide())) {
nodesToRemove.add(tryStmt);
if (containsOnly(definitions, assignResource.getLeftHandSide(), existingFragment.getName())) {
return b.declareFragment(b.move(existingFragment.getName()), b.move(assignResource.getRightHandSide()));
}
return null;
}
}
return containsOnly(definitions, existingFragment.getName()) ? b.move(existingFragment) : null;
}
use of org.eclipse.jdt.core.dom.Assignment in project AutoRefactor by JnRouvignac.
the class RemoveUnnecessaryCastRefactoring method canRemoveCast.
private boolean canRemoveCast(CastExpression node) {
final ASTNode parent = node.getParent();
switch(parent.getNodeType()) {
case RETURN_STATEMENT:
final MethodDeclaration md = getAncestor(parent, MethodDeclaration.class);
return isAssignmentCompatible(node.getExpression(), md.getReturnType2());
case ASSIGNMENT:
final Assignment as = (Assignment) parent;
return isAssignmentCompatible(node.getExpression(), as) || isConstantExpressionAssignmentConversion(node);
case VARIABLE_DECLARATION_FRAGMENT:
final VariableDeclarationFragment vdf = (VariableDeclarationFragment) parent;
return isAssignmentCompatible(node.getExpression(), resolveTypeBinding(vdf)) || isConstantExpressionAssignmentConversion(node);
case INFIX_EXPRESSION:
final InfixExpression ie = (InfixExpression) parent;
final Expression lo = ie.getLeftOperand();
final Expression ro = ie.getRightOperand();
if (node.equals(lo)) {
return (isStringConcat(ie) || isAssignmentCompatible(node.getExpression(), ro)) && !isPrimitiveTypeNarrowing(node) && !hasOperator(ie, DIVIDE) && !hasOperator(ie, PLUS) && !hasOperator(ie, MINUS);
} else {
final boolean integralDivision = isIntegralDivision(ie);
return ((isNotRefactored(lo) && isStringConcat(ie)) || (!integralDivision && isAssignmentCompatibleInInfixExpression(node, ie)) || (integralDivision && canRemoveCastInIntegralDivision(node, ie))) && !isPrimitiveTypeNarrowing(node) && !isIntegralDividedByFloatingPoint(node, ie);
}
}
return false;
}
use of org.eclipse.jdt.core.dom.Assignment in project AutoRefactor by JnRouvignac.
the class HotSpotIntrinsicedAPIsRefactoring method visit.
@Override
public boolean visit(ForStatement node) {
final SystemArrayCopyParams params = new SystemArrayCopyParams();
collectUniqueIndex(node, params);
final IVariableBinding incrementedIdx = getUniqueIncrementedVariable(node);
final List<Statement> stmts = asList(node.getBody());
if (equalNotNull(params.indexVarBinding, incrementedIdx) && stmts.size() == 1) {
collectLength(node.getExpression(), incrementedIdx, params);
final Assignment as = asExpression(stmts.get(0), Assignment.class);
if (hasOperator(as, ASSIGN)) {
final Expression lhs = as.getLeftHandSide();
final Expression rhs = as.getRightHandSide();
if (lhs instanceof ArrayAccess && rhs instanceof ArrayAccess) {
final ArrayAccess aaLHS = (ArrayAccess) lhs;
final ArrayAccess aaRHS = (ArrayAccess) rhs;
params.destArrayExpr = aaLHS.getArray();
params.srcArrayExpr = aaRHS.getArray();
if (haveSameType(params.srcArrayExpr, params.destArrayExpr)) {
params.destPos = calcIndex(aaLHS.getIndex(), params);
params.srcPos = calcIndex(aaRHS.getIndex(), params);
return replaceWithSystemArrayCopyCloneAll(node, params);
}
}
}
}
return VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.Assignment in project AutoRefactor by JnRouvignac.
the class ReduceVariableScopeRefactoring method copy.
private Block copy(Statement stmtToCopy, Name varName) {
if (stmtToCopy != null && !(stmtToCopy instanceof Block)) {
final Block b = this.ctx.getAST().newBlock();
final Assignment a = asExpression(stmtToCopy, Assignment.class);
if (a != null) {
final VariableDeclarationFragment vdf = getVariableDeclarationFragment(a, varName);
statements(b).add(this.ctx.getAST().newVariableDeclarationStatement(vdf));
} else {
throw new NotImplementedException(stmtToCopy);
}
return b;
}
// We should never come here if we had a Block statement, see the replace() method
throw new NotImplementedException(stmtToCopy);
}
Aggregations