use of org.eclipse.jdt.core.dom.MethodInvocation in project che by eclipse.
the class GenerateForLoopAssistProposal method generateIndexBasedForRewrite.
/**
* Helper to generate an index based <code>for</code> loop to iterate over a {@link List}
* implementation.
*
* @param ast the current {@link AST} instance to generate the {@link ASTRewrite} for
* @return an applicable {@link ASTRewrite} instance
*/
private ASTRewrite generateIndexBasedForRewrite(AST ast) {
ASTRewrite rewrite = ASTRewrite.create(ast);
ForStatement loopStatement = ast.newForStatement();
//$NON-NLS-1$
SimpleName loopVariableName = resolveLinkedVariableNameWithProposals(rewrite, "int", null, true);
loopStatement.initializers().add(getForInitializer(ast, loopVariableName));
MethodInvocation listSizeExpression = ast.newMethodInvocation();
//$NON-NLS-1$
listSizeExpression.setName(ast.newSimpleName("size"));
Expression listExpression = (Expression) rewrite.createCopyTarget(fCurrentExpression);
listSizeExpression.setExpression(listExpression);
loopStatement.setExpression(getLinkedInfixExpression(rewrite, loopVariableName.getIdentifier(), listSizeExpression, InfixExpression.Operator.LESS));
loopStatement.updaters().add(getLinkedIncrementExpression(rewrite, loopVariableName.getIdentifier()));
Block forLoopBody = ast.newBlock();
forLoopBody.statements().add(ast.newExpressionStatement(getIndexBasedForBodyAssignment(rewrite, loopVariableName)));
forLoopBody.statements().add(createBlankLineStatementWithCursorPosition(rewrite));
loopStatement.setBody(forLoopBody);
rewrite.replace(fCurrentNode, loopStatement, null);
return rewrite;
}
use of org.eclipse.jdt.core.dom.MethodInvocation in project che by eclipse.
the class GenerateForLoopAssistProposal method getIteratorBasedForBodyAssignment.
/**
* Generates the Assignment in an iterator based for, used in the first statement of an iterator
* based <code>for</code> loop body, to retrieve the next element of the {@link Iterable}
* instance.
*
* @param rewrite the current instance of {@link ASTRewrite}
* @param loopOverType the {@link ITypeBinding} of the loop variable
* @param loopVariableName the name of the loop variable
* @return an {@link Assignment}, which retrieves the next element of the {@link Iterable} using
* the active {@link Iterator}
*/
private Assignment getIteratorBasedForBodyAssignment(ASTRewrite rewrite, ITypeBinding loopOverType, SimpleName loopVariableName) {
AST ast = rewrite.getAST();
Assignment assignResolvedVariable = ast.newAssignment();
// left hand side
SimpleName resolvedVariableName = resolveLinkedVariableNameWithProposals(rewrite, loopOverType.getName(), loopVariableName.getIdentifier(), false);
VariableDeclarationFragment resolvedVariableDeclarationFragment = ast.newVariableDeclarationFragment();
resolvedVariableDeclarationFragment.setName(resolvedVariableName);
VariableDeclarationExpression resolvedVariableDeclaration = ast.newVariableDeclarationExpression(resolvedVariableDeclarationFragment);
resolvedVariableDeclaration.setType(getImportRewrite().addImport(loopOverType, ast, new ContextSensitiveImportRewriteContext(fCurrentNode, getImportRewrite())));
assignResolvedVariable.setLeftHandSide(resolvedVariableDeclaration);
// right hand side
MethodInvocation invokeIteratorNextExpression = ast.newMethodInvocation();
//$NON-NLS-1$
invokeIteratorNextExpression.setName(ast.newSimpleName("next"));
SimpleName currentElementName = ast.newSimpleName(loopVariableName.getIdentifier());
addLinkedPosition(rewrite.track(currentElementName), LinkedPositionGroup.NO_STOP, currentElementName.getIdentifier());
invokeIteratorNextExpression.setExpression(currentElementName);
assignResolvedVariable.setRightHandSide(invokeIteratorNextExpression);
assignResolvedVariable.setOperator(Assignment.Operator.ASSIGN);
return assignResolvedVariable;
}
use of org.eclipse.jdt.core.dom.MethodInvocation in project che by eclipse.
the class NewVariableCorrectionProposal method evaluateVariableType.
private Type evaluateVariableType(AST ast, ImportRewrite imports, ImportRewriteContext importRewriteContext, IBinding targetContext) {
if (fOriginalNode.getParent() instanceof MethodInvocation) {
MethodInvocation parent = (MethodInvocation) fOriginalNode.getParent();
if (parent.getExpression() == fOriginalNode) {
// _x_.foo() -> guess qualifier type by looking for a type with method 'foo'
ITypeBinding[] bindings = ASTResolving.getQualifierGuess(fOriginalNode.getRoot(), parent.getName().getIdentifier(), parent.arguments(), targetContext);
if (bindings.length > 0) {
for (int i = 0; i < bindings.length; i++) {
addLinkedPositionProposal(KEY_TYPE, bindings[i]);
}
return imports.addImport(bindings[0], ast, importRewriteContext);
}
}
}
ITypeBinding binding = ASTResolving.guessBindingForReference(fOriginalNode);
if (binding != null) {
if (binding.isWildcardType()) {
binding = ASTResolving.normalizeWildcardType(binding, isVariableAssigned(), ast);
if (binding == null) {
// only null binding applies
//$NON-NLS-1$
binding = ast.resolveWellKnownType("java.lang.Object");
}
}
if (isVariableAssigned()) {
ITypeBinding[] typeProposals = ASTResolving.getRelaxingTypes(ast, binding);
for (int i = 0; i < typeProposals.length; i++) {
addLinkedPositionProposal(KEY_TYPE, typeProposals[i]);
}
}
return imports.addImport(binding, ast, importRewriteContext);
}
// no binding, find type AST node instead -> ABC a= x-> use 'ABC' as is
Type type = ASTResolving.guessTypeForReference(ast, fOriginalNode);
if (type != null) {
return type;
}
if (fVariableKind == CONST_FIELD) {
//$NON-NLS-1$
return ast.newSimpleType(ast.newSimpleName("String"));
}
//$NON-NLS-1$
return ast.newSimpleType(ast.newSimpleName("Object"));
}
use of org.eclipse.jdt.core.dom.MethodInvocation in project che by eclipse.
the class ConvertIterableLoopOperation method checkIteratorCondition.
private IStatus checkIteratorCondition() {
List<Expression> initializers = getForStatement().initializers();
if (initializers.size() != 1)
return SEMANTIC_CHANGE_WARNING_STATUS;
Expression expression = initializers.get(0);
if (!(expression instanceof VariableDeclarationExpression))
return SEMANTIC_CHANGE_WARNING_STATUS;
VariableDeclarationExpression declaration = (VariableDeclarationExpression) expression;
List<VariableDeclarationFragment> variableDeclarationFragments = declaration.fragments();
if (variableDeclarationFragments.size() != 1)
return SEMANTIC_CHANGE_WARNING_STATUS;
VariableDeclarationFragment declarationFragment = variableDeclarationFragments.get(0);
Expression initializer = declarationFragment.getInitializer();
if (!(initializer instanceof MethodInvocation))
return SEMANTIC_CHANGE_WARNING_STATUS;
MethodInvocation methodInvocation = (MethodInvocation) initializer;
String methodName = methodInvocation.getName().getIdentifier();
if (//$NON-NLS-1$
!"iterator".equals(methodName) || methodInvocation.arguments().size() != 0)
return SEMANTIC_CHANGE_WARNING_STATUS;
return StatusInfo.OK_STATUS;
}
use of org.eclipse.jdt.core.dom.MethodInvocation in project che by eclipse.
the class ConvertIterableLoopOperation method satisfiesPreconditions.
/**
* Is this proposal applicable?
*
* @return A status with severity <code>IStatus.Error</code> if not
* applicable
*/
@Override
public final IStatus satisfiesPreconditions() {
IStatus resultStatus = StatusInfo.OK_STATUS;
if (JavaModelUtil.is50OrHigher(getJavaProject())) {
resultStatus = checkExpressionCondition();
if (resultStatus.getSeverity() == IStatus.ERROR)
return resultStatus;
List<Expression> updateExpressions = getForStatement().updaters();
if (updateExpressions.size() == 1) {
resultStatus = new StatusInfo(IStatus.WARNING, Messages.format(FixMessages.ConvertIterableLoopOperation_RemoveUpdateExpression_Warning, BasicElementLabels.getJavaCodeString(updateExpressions.get(0).toString())));
} else if (updateExpressions.size() > 1) {
resultStatus = new StatusInfo(IStatus.WARNING, FixMessages.ConvertIterableLoopOperation_RemoveUpdateExpressions_Warning);
}
for (final Iterator<Expression> outer = getForStatement().initializers().iterator(); outer.hasNext(); ) {
final Expression initializer = outer.next();
if (initializer instanceof VariableDeclarationExpression) {
final VariableDeclarationExpression declaration = (VariableDeclarationExpression) initializer;
List<VariableDeclarationFragment> fragments = declaration.fragments();
if (fragments.size() != 1) {
//$NON-NLS-1$
return new StatusInfo(IStatus.ERROR, "");
} else {
final VariableDeclarationFragment fragment = fragments.get(0);
fragment.accept(new ASTVisitor() {
@Override
public final boolean visit(final MethodInvocation node) {
final IMethodBinding binding = node.resolveMethodBinding();
if (binding != null) {
final ITypeBinding type = binding.getReturnType();
if (type != null) {
final String qualified = type.getQualifiedName();
if (qualified.startsWith("java.util.Enumeration<") || qualified.startsWith("java.util.Iterator<")) {
//$NON-NLS-1$ //$NON-NLS-2$
final Expression qualifier = node.getExpression();
if (qualifier != null) {
final ITypeBinding resolved = qualifier.resolveTypeBinding();
if (resolved != null) {
//$NON-NLS-1$
final ITypeBinding iterable = getSuperType(resolved, "java.lang.Iterable");
if (iterable != null) {
fExpression = qualifier;
fIterable = resolved;
}
}
} else {
final ITypeBinding declaring = binding.getDeclaringClass();
if (declaring != null) {
//$NON-NLS-1$
final ITypeBinding superBinding = getSuperType(declaring, "java.lang.Iterable");
if (superBinding != null) {
fIterable = superBinding;
fThis = true;
}
}
}
}
}
}
return true;
}
@Override
public final boolean visit(final VariableDeclarationFragment node) {
final IVariableBinding binding = node.resolveBinding();
if (binding != null) {
final ITypeBinding type = binding.getType();
if (type != null) {
//$NON-NLS-1$
ITypeBinding iterator = getSuperType(type, "java.util.Iterator");
if (iterator != null)
fIteratorVariable = binding;
else {
//$NON-NLS-1$
iterator = getSuperType(type, "java.util.Enumeration");
if (iterator != null)
fIteratorVariable = binding;
}
}
}
return true;
}
});
}
}
}
final Statement statement = getForStatement().getBody();
final boolean[] otherInvocationThenNext = new boolean[] { false };
final int[] nextInvocationCount = new int[] { 0 };
if (statement != null && fIteratorVariable != null) {
final ITypeBinding elementType = getElementType(fIteratorVariable.getType());
statement.accept(new ASTVisitor() {
@Override
public boolean visit(SimpleName node) {
IBinding nodeBinding = node.resolveBinding();
if (fElementVariable != null && fElementVariable.equals(nodeBinding)) {
fMakeFinal = false;
}
if (nodeBinding == fIteratorVariable) {
if (node.getLocationInParent() == MethodInvocation.EXPRESSION_PROPERTY) {
MethodInvocation invocation = (MethodInvocation) node.getParent();
String name = invocation.getName().getIdentifier();
if (name.equals("next") || name.equals("nextElement")) {
//$NON-NLS-1$ //$NON-NLS-2$
nextInvocationCount[0]++;
Expression left = null;
if (invocation.getLocationInParent() == Assignment.RIGHT_HAND_SIDE_PROPERTY) {
left = ((Assignment) invocation.getParent()).getLeftHandSide();
} else if (invocation.getLocationInParent() == VariableDeclarationFragment.INITIALIZER_PROPERTY) {
left = ((VariableDeclarationFragment) invocation.getParent()).getName();
}
return visitElementVariable(left);
}
}
otherInvocationThenNext[0] = true;
}
return true;
}
private boolean visitElementVariable(final Expression node) {
if (node != null) {
final ITypeBinding binding = node.resolveTypeBinding();
if (binding != null && elementType.equals(binding)) {
if (node instanceof Name) {
final Name name = (Name) node;
final IBinding result = name.resolveBinding();
if (result != null) {
fOccurrences.add(node);
fElementVariable = result;
return false;
}
} else if (node instanceof FieldAccess) {
final FieldAccess access = (FieldAccess) node;
final IBinding result = access.resolveFieldBinding();
if (result != null) {
fOccurrences.add(node);
fElementVariable = result;
return false;
}
}
}
}
return true;
}
});
if (otherInvocationThenNext[0])
return ERROR_STATUS;
if (nextInvocationCount[0] > 1)
return ERROR_STATUS;
if (fElementVariable != null) {
statement.accept(new ASTVisitor() {
@Override
public final boolean visit(final VariableDeclarationFragment node) {
if (node.getInitializer() instanceof NullLiteral) {
SimpleName name = node.getName();
if (elementType.equals(name.resolveTypeBinding()) && fElementVariable.equals(name.resolveBinding())) {
fOccurrences.add(name);
}
}
return true;
}
});
}
}
final ASTNode root = getForStatement().getRoot();
if (root != null) {
root.accept(new ASTVisitor() {
@Override
public final boolean visit(final ForStatement node) {
return false;
}
@Override
public final boolean visit(final SimpleName node) {
final IBinding binding = node.resolveBinding();
if (binding != null && binding.equals(fElementVariable))
fAssigned = true;
return false;
}
});
}
}
if ((fExpression != null || fThis) && fIterable != null && fIteratorVariable != null && !fAssigned) {
return resultStatus;
} else {
return ERROR_STATUS;
}
}
Aggregations