use of org.eclipse.jdt.core.dom.Assignment in project che by eclipse.
the class UnusedCodeFix method hasSideEffect.
private static boolean hasSideEffect(SimpleName reference) {
ASTNode parent = reference.getParent();
while (parent instanceof QualifiedName) {
parent = parent.getParent();
}
if (parent instanceof FieldAccess) {
parent = parent.getParent();
}
ASTNode node = null;
int nameParentType = parent.getNodeType();
if (nameParentType == ASTNode.ASSIGNMENT) {
Assignment assignment = (Assignment) parent;
node = assignment.getRightHandSide();
} else if (nameParentType == ASTNode.SINGLE_VARIABLE_DECLARATION) {
SingleVariableDeclaration decl = (SingleVariableDeclaration) parent;
node = decl.getInitializer();
if (node == null)
return false;
} else if (nameParentType == ASTNode.VARIABLE_DECLARATION_FRAGMENT) {
node = parent;
} else {
return false;
}
ArrayList<Expression> sideEffects = new ArrayList<Expression>();
node.accept(new SideEffectFinder(sideEffects));
return sideEffects.size() > 0;
}
use of org.eclipse.jdt.core.dom.Assignment in project che by eclipse.
the class CallInliner method crossCheckArguments.
/**
* Checks whether arguments are passed to the method which do some assignments
* inside the expression. If so these arguments can't be inlined into the
* calling method since the assignments might be reorder. An example is:
* <code>
* add((field=args).length,field.hashCode());
* </code>
* Field might not be initialized when the arguments are reorder in the called
* method.
* @param arguments the arguments
* @return all arguments that cannot be inlined
*/
private Set<Expression> crossCheckArguments(List<Expression> arguments) {
final Set<IBinding> assigned = new HashSet<IBinding>();
final Set<Expression> result = new HashSet<Expression>();
for (Iterator<Expression> iter = arguments.iterator(); iter.hasNext(); ) {
final Expression expression = iter.next();
expression.accept(new ASTVisitor() {
@Override
public boolean visit(Assignment node) {
Expression lhs = node.getLeftHandSide();
if (lhs instanceof Name) {
IBinding binding = ((Name) lhs).resolveBinding();
if (binding instanceof IVariableBinding) {
assigned.add(binding);
result.add(expression);
}
}
return true;
}
});
}
for (Iterator<Expression> iter = arguments.iterator(); iter.hasNext(); ) {
final Expression expression = iter.next();
if (!result.contains(expression)) {
expression.accept(new HierarchicalASTVisitor() {
@Override
public boolean visit(Name node) {
IBinding binding = node.resolveBinding();
if (binding != null && assigned.contains(binding))
result.add(expression);
return false;
}
});
}
}
return result;
}
use of org.eclipse.jdt.core.dom.Assignment in project che by eclipse.
the class RemoveDeclarationCorrectionProposal method removeVariableReferences.
/**
* Remove the field or variable declaration including the initializer.
* @param rewrite the ast rewrite
* @param reference the reference
*/
private void removeVariableReferences(ASTRewrite rewrite, SimpleName reference) {
ASTNode parent = reference.getParent();
while (parent instanceof QualifiedName) {
parent = parent.getParent();
}
if (parent instanceof FieldAccess) {
parent = parent.getParent();
}
int nameParentType = parent.getNodeType();
if (nameParentType == ASTNode.ASSIGNMENT) {
Assignment assignment = (Assignment) parent;
Expression rightHand = assignment.getRightHandSide();
ASTNode assignParent = assignment.getParent();
if (assignParent.getNodeType() == ASTNode.EXPRESSION_STATEMENT && rightHand.getNodeType() != ASTNode.ASSIGNMENT) {
removeVariableWithInitializer(rewrite, rightHand, assignParent);
} else {
rewrite.replace(assignment, rewrite.createCopyTarget(rightHand), null);
}
} else if (nameParentType == ASTNode.SINGLE_VARIABLE_DECLARATION) {
rewrite.remove(parent, null);
} else if (nameParentType == ASTNode.VARIABLE_DECLARATION_FRAGMENT) {
VariableDeclarationFragment frag = (VariableDeclarationFragment) parent;
ASTNode varDecl = frag.getParent();
List<VariableDeclarationFragment> fragments;
if (varDecl instanceof VariableDeclarationExpression) {
fragments = ((VariableDeclarationExpression) varDecl).fragments();
} else if (varDecl instanceof FieldDeclaration) {
fragments = ((FieldDeclaration) varDecl).fragments();
} else {
fragments = ((VariableDeclarationStatement) varDecl).fragments();
}
if (fragments.size() == 1) {
rewrite.remove(varDecl, null);
} else {
// don't try to preserve
rewrite.remove(frag, null);
}
}
}
use of org.eclipse.jdt.core.dom.Assignment in project che by eclipse.
the class InlineTempRefactoring method getModifiedInitializerSource.
private Expression getModifiedInitializerSource(CompilationUnitRewrite rewrite, SimpleName reference) throws JavaModelException {
VariableDeclaration varDecl = getVariableDeclaration();
Expression initializer = varDecl.getInitializer();
ASTNode referenceContext = reference.getParent();
if (Invocations.isResolvedTypeInferredFromExpectedType(initializer)) {
if (!(referenceContext instanceof VariableDeclarationFragment || referenceContext instanceof SingleVariableDeclaration || referenceContext instanceof Assignment)) {
ITypeBinding[] typeArguments = Invocations.getInferredTypeArguments(initializer);
if (typeArguments != null) {
String newSource = createParameterizedInvocation(initializer, typeArguments, rewrite);
return (Expression) rewrite.getASTRewrite().createStringPlaceholder(newSource, initializer.getNodeType());
}
}
}
Expression copy = (Expression) rewrite.getASTRewrite().createCopyTarget(initializer);
AST ast = rewrite.getAST();
if (NecessaryParenthesesChecker.needsParentheses(initializer, reference.getParent(), reference.getLocationInParent())) {
ParenthesizedExpression parenthesized = ast.newParenthesizedExpression();
parenthesized.setExpression(copy);
copy = parenthesized;
}
ITypeBinding explicitCast = ASTNodes.getExplicitCast(initializer, reference);
if (explicitCast != null) {
CastExpression cast = ast.newCastExpression();
if (NecessaryParenthesesChecker.needsParentheses(copy, cast, CastExpression.EXPRESSION_PROPERTY)) {
ParenthesizedExpression parenthesized = ast.newParenthesizedExpression();
parenthesized.setExpression(copy);
copy = parenthesized;
}
cast.setExpression(copy);
ImportRewriteContext context = new ContextSensitiveImportRewriteContext(reference, rewrite.getImportRewrite());
cast.setType(rewrite.getImportRewrite().addImport(explicitCast, ast, context));
copy = cast;
} else if (initializer instanceof ArrayInitializer && ASTNodes.getDimensions(varDecl) > 0) {
ArrayType newType = (ArrayType) ASTNodeFactory.newType(ast, varDecl);
ArrayCreation newArrayCreation = ast.newArrayCreation();
newArrayCreation.setType(newType);
newArrayCreation.setInitializer((ArrayInitializer) copy);
return newArrayCreation;
}
return copy;
}
use of org.eclipse.jdt.core.dom.Assignment in project che by eclipse.
the class ExtractMethodRefactoring method createCallNodes.
//---- Code generation -----------------------------------------------------------------------
private ASTNode[] createCallNodes(SnippetFinder.Match duplicate, int modifiers) {
List<ASTNode> result = new ArrayList<ASTNode>(2);
IVariableBinding[] locals = fAnalyzer.getCallerLocals();
for (int i = 0; i < locals.length; i++) {
result.add(createDeclaration(locals[i], null));
}
MethodInvocation invocation = fAST.newMethodInvocation();
invocation.setName(fAST.newSimpleName(fMethodName));
ASTNode typeNode = ASTResolving.findParentType(fAnalyzer.getEnclosingBodyDeclaration());
RefactoringStatus status = new RefactoringStatus();
while (fDestination != typeNode) {
fAnalyzer.checkInput(status, fMethodName, typeNode);
if (!status.isOK()) {
SimpleName destinationTypeName = fAST.newSimpleName(ASTNodes.getEnclosingType(fDestination).getName());
if ((modifiers & Modifier.STATIC) == 0) {
ThisExpression thisExpression = fAST.newThisExpression();
thisExpression.setQualifier(destinationTypeName);
invocation.setExpression(thisExpression);
} else {
invocation.setExpression(destinationTypeName);
}
break;
}
typeNode = typeNode.getParent();
}
List<Expression> arguments = invocation.arguments();
for (int i = 0; i < fParameterInfos.size(); i++) {
ParameterInfo parameter = fParameterInfos.get(i);
arguments.add(ASTNodeFactory.newName(fAST, getMappedName(duplicate, parameter)));
}
if (fLinkedProposalModel != null) {
LinkedProposalPositionGroup nameGroup = fLinkedProposalModel.getPositionGroup(KEY_NAME, true);
nameGroup.addPosition(fRewriter.track(invocation.getName()), false);
}
ASTNode call;
int returnKind = fAnalyzer.getReturnKind();
switch(returnKind) {
case ExtractMethodAnalyzer.ACCESS_TO_LOCAL:
IVariableBinding binding = fAnalyzer.getReturnLocal();
if (binding != null) {
VariableDeclarationStatement decl = createDeclaration(getMappedBinding(duplicate, binding), invocation);
call = decl;
} else {
Assignment assignment = fAST.newAssignment();
assignment.setLeftHandSide(ASTNodeFactory.newName(fAST, getMappedBinding(duplicate, fAnalyzer.getReturnValue()).getName()));
assignment.setRightHandSide(invocation);
call = assignment;
}
break;
case ExtractMethodAnalyzer.RETURN_STATEMENT_VALUE:
ReturnStatement rs = fAST.newReturnStatement();
rs.setExpression(invocation);
call = rs;
break;
default:
call = invocation;
}
if (call instanceof Expression && !fAnalyzer.isExpressionSelected()) {
call = fAST.newExpressionStatement((Expression) call);
}
result.add(call);
// return;
if (returnKind == ExtractMethodAnalyzer.RETURN_STATEMENT_VOID && !fAnalyzer.isLastStatementSelected()) {
result.add(fAST.newReturnStatement());
}
return result.toArray(new ASTNode[result.size()]);
}
Aggregations