use of org.eclipse.jdt.core.dom.Assignment in project AutoRefactor by JnRouvignac.
the class BooleanRefactoring method noThenReturnStmt.
private boolean noThenReturnStmt(final IfStatement node) {
final Assignment thenA = asExpression(node.getThenStatement(), Assignment.class);
if (hasOperator(thenA, ASSIGN) && asList(node.getElseStatement()).isEmpty() && (thenA.getLeftHandSide() instanceof Name || thenA.getLeftHandSide() instanceof FieldAccess)) {
final Statement previousSibling = getPreviousSibling(node);
if (previousSibling instanceof VariableDeclarationStatement) {
final VariableDeclarationStatement vds = (VariableDeclarationStatement) previousSibling;
VariableDeclarationFragment vdf = getVariableDeclarationFragment(vds, thenA.getLeftHandSide());
if (vdf != null) {
final VariableDefinitionsUsesVisitor variableUseVisitor = new VariableDefinitionsUsesVisitor(vdf.resolveBinding(), node.getExpression()).find();
if (variableUseVisitor.getUses().isEmpty()) {
final ITypeBinding typeBinding = vds.getType().resolveBinding();
return maybeReplace(node, thenA, typeBinding, vdf.getInitializer());
}
}
} else if (previousSibling instanceof ExpressionStatement) {
final Assignment elseA = asExpression(previousSibling, Assignment.class);
if (hasOperator(elseA, ASSIGN) && isSameVariable(thenA.getLeftHandSide(), elseA.getLeftHandSide())) {
final ITypeBinding typeBinding = elseA.resolveTypeBinding();
return maybeReplace(node, thenA, typeBinding, elseA.getRightHandSide());
}
}
}
return VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.Assignment in project AutoRefactor by JnRouvignac.
the class ForLoopHelper method decomposeInitializer.
/**
* Decomposes an initializer into a {@link Pair} with the name of the initialized variable
* and the initializing expression.
*
* @param init
* the initializer to decompose
* @return a {@link Pair} with the name of the initialized variable and the initializing
* expression, or {@code null} if the initializer could not be decomposed
*/
public static Pair<Name, Expression> decomposeInitializer(Expression init) {
if (init instanceof VariableDeclarationExpression) {
final VariableDeclarationExpression vde = (VariableDeclarationExpression) init;
final List<VariableDeclarationFragment> fragments = fragments(vde);
if (fragments.size() == 1) {
final VariableDeclarationFragment fragment = fragments.get(0);
return Pair.of((Name) fragment.getName(), fragment.getInitializer());
}
} else if (init instanceof Assignment) {
final Assignment as = (Assignment) init;
if (hasOperator(as, ASSIGN) && as.getLeftHandSide() instanceof Name) {
return Pair.of((Name) as.getLeftHandSide(), as.getRightHandSide());
}
}
return Pair.empty();
}
use of org.eclipse.jdt.core.dom.Assignment in project AutoRefactor by JnRouvignac.
the class ASTBuilder method assign.
/**
* Builds a new {@link Assignment} instance.
*
* @param lhs the left hand side expression
* @param operator the assignment operator
* @param rhs the right hand side expression
* @return a new Block
*/
public Assignment assign(final Expression lhs, final Assignment.Operator operator, final Expression rhs) {
final Assignment assign = ast.newAssignment();
assign.setLeftHandSide(lhs);
assign.setOperator(operator);
assign.setRightHandSide(rhs);
return assign;
}
use of org.eclipse.jdt.core.dom.Assignment in project eclipse.jdt.ls by eclipse.
the class UnresolvedElementsSubProcessor method addSimilarVariableProposals.
private static void addSimilarVariableProposals(ICompilationUnit cu, CompilationUnit astRoot, ITypeBinding binding, IVariableBinding resolvedField, SimpleName node, boolean isWriteAccess, Collection<CUCorrectionProposal> proposals) {
int kind = ScopeAnalyzer.VARIABLES | ScopeAnalyzer.CHECK_VISIBILITY;
if (!isWriteAccess) {
// also try to find similar methods
kind |= ScopeAnalyzer.METHODS;
}
IBinding[] varsAndMethodsInScope = (new ScopeAnalyzer(astRoot)).getDeclarationsInScope(node, kind);
if (varsAndMethodsInScope.length > 0) {
// avoid corrections like int i= i;
String otherNameInAssign = null;
// help with x.getString() -> y.getString()
String methodSenderName = null;
String fieldSenderName = null;
ASTNode parent = node.getParent();
switch(parent.getNodeType()) {
case ASTNode.VARIABLE_DECLARATION_FRAGMENT:
// node must be initializer
otherNameInAssign = ((VariableDeclarationFragment) parent).getName().getIdentifier();
break;
case ASTNode.ASSIGNMENT:
Assignment assignment = (Assignment) parent;
if (isWriteAccess && assignment.getRightHandSide() instanceof SimpleName) {
otherNameInAssign = ((SimpleName) assignment.getRightHandSide()).getIdentifier();
} else if (!isWriteAccess && assignment.getLeftHandSide() instanceof SimpleName) {
otherNameInAssign = ((SimpleName) assignment.getLeftHandSide()).getIdentifier();
}
break;
case ASTNode.METHOD_INVOCATION:
MethodInvocation inv = (MethodInvocation) parent;
if (inv.getExpression() == node) {
methodSenderName = inv.getName().getIdentifier();
}
break;
case ASTNode.QUALIFIED_NAME:
QualifiedName qualName = (QualifiedName) parent;
if (qualName.getQualifier() == node) {
fieldSenderName = qualName.getName().getIdentifier();
}
break;
}
ITypeBinding guessedType = ASTResolving.guessBindingForReference(node);
// $NON-NLS-1$
ITypeBinding objectBinding = astRoot.getAST().resolveWellKnownType("java.lang.Object");
String identifier = node.getIdentifier();
boolean isInStaticContext = ASTResolving.isInStaticContext(node);
ArrayList<CUCorrectionProposal> newProposals = new ArrayList<>(51);
loop: for (int i = 0; i < varsAndMethodsInScope.length && newProposals.size() <= 50; i++) {
IBinding varOrMeth = varsAndMethodsInScope[i];
if (varOrMeth instanceof IVariableBinding) {
IVariableBinding curr = (IVariableBinding) varOrMeth;
String currName = curr.getName();
if (currName.equals(otherNameInAssign)) {
continue loop;
}
if (resolvedField != null && Bindings.equals(resolvedField, curr)) {
continue loop;
}
boolean isFinal = Modifier.isFinal(curr.getModifiers());
if (isFinal && curr.isField() && isWriteAccess) {
continue loop;
}
if (isInStaticContext && !Modifier.isStatic(curr.getModifiers()) && curr.isField()) {
continue loop;
}
int relevance = IProposalRelevance.SIMILAR_VARIABLE_PROPOSAL;
if (NameMatcher.isSimilarName(currName, identifier)) {
// variable with a similar name than the unresolved variable
relevance += 3;
}
if (currName.equalsIgnoreCase(identifier)) {
relevance += 5;
}
ITypeBinding varType = curr.getType();
if (varType != null) {
if (guessedType != null && guessedType != objectBinding) {
// variable type is compatible with the guessed type
if (!isWriteAccess && canAssign(varType, guessedType) || isWriteAccess && canAssign(guessedType, varType)) {
// unresolved variable can be assign to this variable
relevance += 2;
}
}
if (methodSenderName != null && hasMethodWithName(varType, methodSenderName)) {
relevance += 2;
}
if (fieldSenderName != null && hasFieldWithName(varType, fieldSenderName)) {
relevance += 2;
}
}
if (relevance > 0) {
String label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_changevariable_description, BasicElementLabels.getJavaElementName(currName));
newProposals.add(new RenameNodeCorrectionProposal(label, cu, node.getStartPosition(), node.getLength(), currName, relevance));
}
} else if (varOrMeth instanceof IMethodBinding) {
IMethodBinding curr = (IMethodBinding) varOrMeth;
if (!curr.isConstructor() && guessedType != null && canAssign(curr.getReturnType(), guessedType)) {
if (NameMatcher.isSimilarName(curr.getName(), identifier)) {
AST ast = astRoot.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
String label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_changetomethod_description, org.eclipse.jdt.ls.core.internal.corrections.ASTResolving.getMethodSignature(curr));
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.CHANGE_TO_METHOD);
newProposals.add(proposal);
MethodInvocation newInv = ast.newMethodInvocation();
newInv.setName(ast.newSimpleName(curr.getName()));
ITypeBinding[] parameterTypes = curr.getParameterTypes();
for (int k = 0; k < parameterTypes.length; k++) {
ASTNode arg = ASTNodeFactory.newDefaultExpression(ast, parameterTypes[k]);
newInv.arguments().add(arg);
}
rewrite.replace(node, newInv, null);
}
}
}
}
if (newProposals.size() <= 50) {
proposals.addAll(newProposals);
}
}
if (binding != null && binding.isArray()) {
// $NON-NLS-1$
String idLength = "length";
String label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_changevariable_description, idLength);
proposals.add(new RenameNodeCorrectionProposal(label, cu, node.getStartPosition(), node.getLength(), idLength, IProposalRelevance.CHANGE_VARIABLE));
}
}
use of org.eclipse.jdt.core.dom.Assignment in project eclipse.jdt.ls 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<>();
node.accept(new SideEffectFinder(sideEffects));
return sideEffects.size() > 0;
}
Aggregations