use of org.eclipse.jdt.core.dom.Assignment in project flux by eclipse.
the class ASTNodes method getTargetType.
/**
* Derives the target type defined at the location of the given expression if the target context
* supports poly expressions.
*
* @param expression the expression at whose location the target type is required
* @return the type binding of the target type defined at the location of the given expression
* if the target context supports poly expressions, or <code>null</code> if the target
* type could not be derived
*
* @since 3.10
*/
public static ITypeBinding getTargetType(Expression expression) {
ASTNode parent = expression.getParent();
StructuralPropertyDescriptor locationInParent = expression.getLocationInParent();
if (locationInParent == VariableDeclarationFragment.INITIALIZER_PROPERTY || locationInParent == SingleVariableDeclaration.INITIALIZER_PROPERTY) {
return ((VariableDeclaration) parent).getName().resolveTypeBinding();
} else if (locationInParent == Assignment.RIGHT_HAND_SIDE_PROPERTY) {
return ((Assignment) parent).getLeftHandSide().resolveTypeBinding();
} else if (locationInParent == ReturnStatement.EXPRESSION_PROPERTY) {
return getTargetTypeForReturnStmt((ReturnStatement) parent);
} else if (locationInParent == ArrayInitializer.EXPRESSIONS_PROPERTY) {
return getTargetTypeForArrayInitializer((ArrayInitializer) parent);
} else if (locationInParent == MethodInvocation.ARGUMENTS_PROPERTY) {
MethodInvocation methodInvocation = (MethodInvocation) parent;
IMethodBinding methodBinding = methodInvocation.resolveMethodBinding();
if (methodBinding != null) {
return getParameterTypeBinding(expression, methodInvocation.arguments(), methodBinding);
}
} else if (locationInParent == SuperMethodInvocation.ARGUMENTS_PROPERTY) {
SuperMethodInvocation superMethodInvocation = (SuperMethodInvocation) parent;
IMethodBinding superMethodBinding = superMethodInvocation.resolveMethodBinding();
if (superMethodBinding != null) {
return getParameterTypeBinding(expression, superMethodInvocation.arguments(), superMethodBinding);
}
} else if (locationInParent == ConstructorInvocation.ARGUMENTS_PROPERTY) {
ConstructorInvocation constructorInvocation = (ConstructorInvocation) parent;
IMethodBinding constructorBinding = constructorInvocation.resolveConstructorBinding();
if (constructorBinding != null) {
return getParameterTypeBinding(expression, constructorInvocation.arguments(), constructorBinding);
}
} else if (locationInParent == SuperConstructorInvocation.ARGUMENTS_PROPERTY) {
SuperConstructorInvocation superConstructorInvocation = (SuperConstructorInvocation) parent;
IMethodBinding superConstructorBinding = superConstructorInvocation.resolveConstructorBinding();
if (superConstructorBinding != null) {
return getParameterTypeBinding(expression, superConstructorInvocation.arguments(), superConstructorBinding);
}
} else if (locationInParent == ClassInstanceCreation.ARGUMENTS_PROPERTY) {
ClassInstanceCreation creation = (ClassInstanceCreation) parent;
IMethodBinding creationBinding = creation.resolveConstructorBinding();
if (creationBinding != null) {
return getParameterTypeBinding(expression, creation.arguments(), creationBinding);
}
} else if (locationInParent == EnumConstantDeclaration.ARGUMENTS_PROPERTY) {
EnumConstantDeclaration enumConstantDecl = (EnumConstantDeclaration) parent;
IMethodBinding enumConstructorBinding = enumConstantDecl.resolveConstructorBinding();
if (enumConstructorBinding != null) {
return getParameterTypeBinding(expression, enumConstantDecl.arguments(), enumConstructorBinding);
}
} else if (locationInParent == LambdaExpression.BODY_PROPERTY) {
IMethodBinding methodBinding = ((LambdaExpression) parent).resolveMethodBinding();
if (methodBinding != null) {
return methodBinding.getReturnType();
}
} else if (locationInParent == ConditionalExpression.THEN_EXPRESSION_PROPERTY || locationInParent == ConditionalExpression.ELSE_EXPRESSION_PROPERTY) {
return getTargetType((ConditionalExpression) parent);
} else if (locationInParent == CastExpression.EXPRESSION_PROPERTY) {
return ((CastExpression) parent).getType().resolveBinding();
} else if (locationInParent == ParenthesizedExpression.EXPRESSION_PROPERTY) {
return getTargetType((ParenthesizedExpression) parent);
}
return null;
}
use of org.eclipse.jdt.core.dom.Assignment in project AutoRefactor by JnRouvignac.
the class BooleanCleanUp method noThenReturnStatement.
private boolean noThenReturnStatement(final IfStatement visited) {
Assignment thenAssignment = ASTNodes.asExpression(visited.getThenStatement(), Assignment.class);
if (ASTNodes.hasOperator(thenAssignment, Assignment.Operator.ASSIGN) && ASTNodes.asList(visited.getElseStatement()).isEmpty() && (thenAssignment.getLeftHandSide() instanceof Name || thenAssignment.getLeftHandSide() instanceof FieldAccess || thenAssignment.getLeftHandSide() instanceof SuperFieldAccess)) {
Statement previousSibling = ASTNodes.getPreviousSibling(visited);
if (previousSibling instanceof VariableDeclarationStatement) {
VariableDeclarationStatement variableDeclarationStatement = (VariableDeclarationStatement) previousSibling;
VariableDeclarationFragment fragment = getVariableDeclarationFragment(variableDeclarationStatement, thenAssignment.getLeftHandSide());
if (fragment != null) {
VarDefinitionsUsesVisitor variableUseVisitor;
try {
variableUseVisitor = new VarDefinitionsUsesVisitor(fragment.resolveBinding(), visited.getExpression(), true);
} catch (Exception e) {
return true;
}
if (variableUseVisitor.getReads().isEmpty()) {
ITypeBinding typeBinding = variableDeclarationStatement.getType().resolveBinding();
return maybeReplace(visited, thenAssignment, typeBinding, fragment.getInitializer());
}
}
} else if (previousSibling instanceof ExpressionStatement) {
Assignment elseAssignment = ASTNodes.asExpression(previousSibling, Assignment.class);
if (ASTNodes.hasOperator(elseAssignment, Assignment.Operator.ASSIGN) && ASTNodes.isSameVariable(thenAssignment.getLeftHandSide(), elseAssignment.getLeftHandSide())) {
ITypeBinding typeBinding = elseAssignment.resolveTypeBinding();
return maybeReplace(visited, thenAssignment, typeBinding, elseAssignment.getRightHandSide());
}
}
}
return true;
}
use of org.eclipse.jdt.core.dom.Assignment in project AutoRefactor by JnRouvignac.
the class ObsoleteBreakRatherThanPassiveIterationsCleanUp method areAssignmentsValid.
private boolean areAssignmentsValid(final Set<SimpleName> allowedVars, final List<Statement> assignments) {
for (Statement statement : assignments) {
VariableDeclarationStatement variableDeclaration = ASTNodes.as(statement, VariableDeclarationStatement.class);
Assignment assignment = ASTNodes.asExpression(statement, Assignment.class);
if (variableDeclaration != null) {
for (Object obj : variableDeclaration.fragments()) {
VariableDeclarationFragment fragment = (VariableDeclarationFragment) obj;
if (!ASTNodes.isHardCoded(fragment.getInitializer())) {
return false;
}
}
} else if (assignment != null && ASTNodes.hasOperator(assignment, Assignment.Operator.ASSIGN) && ASTNodes.isHardCoded(assignment.getRightHandSide()) && ASTNodes.isPassive(assignment.getLeftHandSide())) {
VarConflictVisitor varOccurrenceVisitor = new VarConflictVisitor(allowedVars, true);
varOccurrenceVisitor.traverseNodeInterruptibly(assignment.getLeftHandSide());
if (varOccurrenceVisitor.isVarConflicting()) {
return false;
}
} else {
return false;
}
}
return true;
}
use of org.eclipse.jdt.core.dom.Assignment in project AutoRefactor by JnRouvignac.
the class CFGBuilder method addVariableAccess.
/**
* @return whether the current variable access can throw an exception.
*/
private boolean addVariableAccess(final CFGBasicBlock basicBlock, final Expression node, final int flags, final ThrowerBlocks throwers) {
if (node == null) {
return false;
}
switch(node.getNodeType()) {
case ASTNode.ARRAY_ACCESS:
ArrayAccess aa = (ArrayAccess) node;
addVariableAccess(basicBlock, aa.getArray(), flags, throwers);
addVariableAccess(basicBlock, aa.getIndex(), flags, throwers);
throwers.addThrow(aa, newException(node, ArrayIndexOutOfBoundsException.class.getCanonicalName()));
return true;
case ASTNode.ARRAY_CREATION:
ArrayCreation ac = (ArrayCreation) node;
boolean acMightThrow1 = addVariableAccess(basicBlock, ac.getInitializer(), flags, throwers);
boolean acMightThrow2 = addVariableAccesses(basicBlock, ac.dimensions(), flags, throwers);
return acMightThrow1 || acMightThrow2;
case ASTNode.ARRAY_INITIALIZER:
ArrayInitializer ai = (ArrayInitializer) node;
return addVariableAccesses(basicBlock, ai.expressions(), flags, throwers);
case ASTNode.ASSIGNMENT:
Assignment a = (Assignment) node;
boolean aMightThrow1 = addVariableAccess(basicBlock, a.getLeftHandSide(), VariableAccess.WRITE, throwers);
boolean aMightThrow2 = addVariableAccess(basicBlock, a.getRightHandSide(), VariableAccess.READ, throwers);
return aMightThrow1 || aMightThrow2;
case ASTNode.BOOLEAN_LITERAL:
case ASTNode.CHARACTER_LITERAL:
case ASTNode.NULL_LITERAL:
case ASTNode.NUMBER_LITERAL:
case ASTNode.STRING_LITERAL:
case ASTNode.TYPE_LITERAL:
// Nothing to do
return false;
case ASTNode.CAST_EXPRESSION:
CastExpression cae = (CastExpression) node;
return addVariableAccess(basicBlock, cae.getExpression(), flags, throwers);
case ASTNode.CLASS_INSTANCE_CREATION:
ClassInstanceCreation classInstanceCreation = (ClassInstanceCreation) node;
addVariableAccess(basicBlock, classInstanceCreation.getExpression(), flags, throwers);
addVariableAccesses(basicBlock, classInstanceCreation.arguments(), flags, throwers);
IMethodBinding cicBinding = classInstanceCreation.resolveConstructorBinding();
if (cicBinding != null) {
ITypeBinding[] declaredThrows = cicBinding.getExceptionTypes();
throwers.addThrow(classInstanceCreation, declaredThrows);
return declaredThrows.length > 0;
}
return false;
case ASTNode.CONDITIONAL_EXPRESSION:
ConditionalExpression coe = (ConditionalExpression) node;
boolean mightThrow1 = addVariableAccess(basicBlock, coe.getExpression(), flags, throwers);
boolean mightThrow2 = addVariableAccess(basicBlock, coe.getThenExpression(), flags, throwers);
boolean mightThrow3 = addVariableAccess(basicBlock, coe.getElseExpression(), flags, throwers);
return mightThrow1 || mightThrow2 || mightThrow3;
case ASTNode.FIELD_ACCESS:
FieldAccess fa = (FieldAccess) node;
boolean mightThrow = addVariableAccess(basicBlock, fa.getExpression(), flags, throwers);
basicBlock.addVariableAccess(new VariableAccess(fa, flags));
if (is(flags, VariableAccess.READ)) {
throwers.addThrow(fa, newException(node, NullPointerException.class.getCanonicalName()));
mightThrow = true;
}
return mightThrow;
case ASTNode.INFIX_EXPRESSION:
InfixExpression infixExpression = (InfixExpression) node;
boolean ieMightThrow1 = addVariableAccess(basicBlock, infixExpression.getLeftOperand(), flags, throwers);
boolean ieMightThrow2 = addVariableAccess(basicBlock, infixExpression.getRightOperand(), flags, throwers);
return ieMightThrow1 || ieMightThrow2;
case ASTNode.INSTANCEOF_EXPRESSION:
InstanceofExpression ioe = (InstanceofExpression) node;
return addVariableAccess(basicBlock, ioe.getLeftOperand(), flags, throwers);
case ASTNode.METHOD_INVOCATION:
MethodInvocation methodInvocation = (MethodInvocation) node;
addVariableAccess(basicBlock, methodInvocation.getExpression(), flags, throwers);
addVariableAccesses(basicBlock, methodInvocation.arguments(), flags, throwers);
IMethodBinding methodBinding = methodInvocation.resolveMethodBinding();
if (methodBinding != null) {
ITypeBinding[] declaredThrows = methodBinding.getExceptionTypes();
throwers.addThrow(methodInvocation, declaredThrows);
return declaredThrows.length > 0;
}
return false;
case ASTNode.SIMPLE_NAME:
SimpleName sn = (SimpleName) node;
basicBlock.addVariableAccess(new VariableAccess(sn, flags));
if (is(flags, VariableAccess.READ)) {
throwers.addThrow(sn, newException(node, NullPointerException.class.getCanonicalName()));
return true;
}
return false;
case ASTNode.QUALIFIED_NAME:
QualifiedName qn = (QualifiedName) node;
basicBlock.addVariableAccess(new VariableAccess(qn, flags));
throwers.addThrow(qn, newException(node, NullPointerException.class.getCanonicalName()));
return true;
case ASTNode.PARENTHESIZED_EXPRESSION:
ParenthesizedExpression pe = (ParenthesizedExpression) node;
return addVariableAccess(basicBlock, pe.getExpression(), flags, throwers);
case ASTNode.POSTFIX_EXPRESSION:
PostfixExpression poe = (PostfixExpression) node;
return addVariableAccess(basicBlock, poe.getOperand(), flags, throwers);
case ASTNode.PREFIX_EXPRESSION:
PrefixExpression pre = (PrefixExpression) node;
return addVariableAccess(basicBlock, pre.getOperand(), flags, throwers);
case ASTNode.SUPER_FIELD_ACCESS:
SuperFieldAccess sfa = (SuperFieldAccess) node;
boolean sfaMightThrow1 = addVariableAccess(basicBlock, sfa.getQualifier(), flags, throwers);
boolean sfaMightThrow2 = addVariableAccess(basicBlock, sfa.getName(), flags, throwers);
return sfaMightThrow1 || sfaMightThrow2;
case ASTNode.SUPER_METHOD_INVOCATION:
SuperMethodInvocation smi = (SuperMethodInvocation) node;
addVariableAccess(basicBlock, smi.getQualifier(), flags, throwers);
addVariableAccess(basicBlock, smi.getName(), flags, throwers);
IMethodBinding sMethodBinding = smi.resolveMethodBinding();
if (sMethodBinding != null) {
ITypeBinding[] declaredThrows = sMethodBinding.getExceptionTypes();
throwers.addThrow(smi, declaredThrows);
return declaredThrows.length > 0;
}
return false;
case ASTNode.THIS_EXPRESSION:
ThisExpression te = (ThisExpression) node;
// TODO JNR remember use of "this" here
return addVariableAccess(basicBlock, te.getQualifier(), flags, throwers);
case ASTNode.VARIABLE_DECLARATION_EXPRESSION:
return addDeclarations(basicBlock, (VariableDeclarationExpression) node, throwers);
default:
throw new NotImplementedException(node);
}
}
use of org.eclipse.jdt.core.dom.Assignment in project AutoRefactor by JnRouvignac.
the class ASTNodes 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<Expression, Expression> decomposeInitializer(final Expression init) {
if (init instanceof VariableDeclarationExpression) {
VariableDeclarationExpression variableDeclarationExpression = (VariableDeclarationExpression) init;
List<VariableDeclarationFragment> fragments = variableDeclarationExpression.fragments();
if (fragments.size() == 1) {
VariableDeclarationFragment fragment = fragments.get(0);
return Pair.of(fragment.getName(), fragment.getInitializer());
}
} else if (init instanceof Assignment) {
Assignment as = (Assignment) init;
if (hasOperator(as, Assignment.Operator.ASSIGN)) {
Name name = as(as.getLeftHandSide(), Name.class);
FieldAccess fieldAccess = as(as.getLeftHandSide(), FieldAccess.class);
if (name != null) {
return Pair.of(name, as.getRightHandSide());
}
if (fieldAccess != null) {
return Pair.of(fieldAccess, as.getRightHandSide());
}
}
}
return Pair.empty();
}
Aggregations