use of org.eclipse.jdt.core.dom.ReturnStatement in project AutoRefactor by JnRouvignac.
the class ObsoleteLambdaCleanUp method removeReturnAndBrackets.
private void removeReturnAndBrackets(final LambdaExpression node, final List<Statement> statements) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteLambdaCleanUp_description);
ReturnStatement returnStatement = (ReturnStatement) statements.get(0);
ASTNodes.replaceButKeepComment(rewrite, node.getBody(), ASTNodeFactory.parenthesizeIfNeeded(ast, ASTNodes.createMoveTarget(rewrite, returnStatement.getExpression())), group);
}
use of org.eclipse.jdt.core.dom.ReturnStatement in project AutoRefactor by JnRouvignac.
the class ObsoleteLambdaCleanUp method visit.
@Override
public boolean visit(final LambdaExpression node) {
if (node.hasParentheses() && node.parameters().size() == 1 && node.parameters().get(0) instanceof VariableDeclarationFragment) {
// TODO it should also be possible to deal with a SingleVariableDeclaration
// when the type matches the expected inferred type
// To do this, we should visit the whole block and check the target type
removeParamParentheses(node);
return false;
}
if (node.getBody() instanceof Block) {
List<Statement> statements = ASTNodes.asList((Block) node.getBody());
if (statements.size() == 1 && statements.get(0) instanceof ReturnStatement) {
removeReturnAndBrackets(node, statements);
return false;
}
} else if (node.getBody() instanceof ClassInstanceCreation) {
ClassInstanceCreation ci = (ClassInstanceCreation) node.getBody();
List<Expression> arguments = ci.arguments();
if (ci.resolveTypeBinding() != null && ci.getAnonymousClassDeclaration() == null && node.parameters().size() == arguments.size() && areSameIdentifiers(node, arguments)) {
replaceByCreationReference(node, ci);
return false;
}
} else if (node.getBody() instanceof SuperMethodInvocation) {
SuperMethodInvocation smi = (SuperMethodInvocation) node.getBody();
List<Expression> arguments = smi.arguments();
if (node.parameters().size() == arguments.size() && areSameIdentifiers(node, arguments)) {
replaceBySuperMethodReference(node, smi);
return false;
}
} else if (node.getBody() instanceof MethodInvocation) {
MethodInvocation methodInvocation = (MethodInvocation) node.getBody();
Expression calledExpression = methodInvocation.getExpression();
ITypeBinding calledType = ASTNodes.getCalledType(methodInvocation);
List<Expression> arguments = methodInvocation.arguments();
if (node.parameters().size() == arguments.size()) {
if (!areSameIdentifiers(node, arguments)) {
return true;
}
if (isStaticMethod(methodInvocation)) {
if (!arguments.isEmpty()) {
String[] remainingParams = new String[arguments.size() - 1];
for (int i = 0; i < arguments.size() - 1; i++) {
ITypeBinding argumentBinding = arguments.get(i + 1).resolveTypeBinding();
if (argumentBinding == null) {
return true;
}
remainingParams[i] = argumentBinding.getQualifiedName();
}
for (IMethodBinding methodBinding : calledType.getDeclaredMethods()) {
if ((methodBinding.getModifiers() & Modifier.STATIC) == 0 && ASTNodes.usesGivenSignature(methodBinding, calledType.getQualifiedName(), methodInvocation.getName().getIdentifier(), remainingParams)) {
return true;
}
}
}
replaceByTypeReference(node, methodInvocation);
return false;
}
if (calledExpression == null || calledExpression instanceof StringLiteral || calledExpression instanceof NumberLiteral || calledExpression instanceof ThisExpression) {
replaceByMethodReference(node, methodInvocation);
return false;
}
if (calledExpression instanceof FieldAccess) {
FieldAccess fieldAccess = (FieldAccess) calledExpression;
if (fieldAccess.resolveFieldBinding().isEffectivelyFinal()) {
replaceByMethodReference(node, methodInvocation);
return false;
}
} else if (calledExpression instanceof SuperFieldAccess) {
SuperFieldAccess fieldAccess = (SuperFieldAccess) calledExpression;
if (fieldAccess.resolveFieldBinding().isEffectivelyFinal()) {
replaceByMethodReference(node, methodInvocation);
return false;
}
}
} else if (calledExpression instanceof SimpleName && node.parameters().size() == arguments.size() + 1) {
SimpleName calledObject = (SimpleName) calledExpression;
if (isSameIdentifier(node, 0, calledObject)) {
for (int i = 0; i < arguments.size(); i++) {
SimpleName expression = ASTNodes.as(arguments.get(i), SimpleName.class);
if (expression == null || !isSameIdentifier(node, i + 1, expression)) {
return true;
}
}
ITypeBinding klass = calledExpression.resolveTypeBinding();
if (klass == null) {
return true;
}
String[] remainingParams = new String[arguments.size() + 1];
remainingParams[0] = klass.getQualifiedName();
for (int i = 0; i < arguments.size(); i++) {
ITypeBinding argumentBinding = arguments.get(i).resolveTypeBinding();
if (argumentBinding == null) {
return true;
}
remainingParams[i + 1] = argumentBinding.getQualifiedName();
}
for (IMethodBinding methodBinding : klass.getDeclaredMethods()) {
if ((methodBinding.getModifiers() & Modifier.STATIC) != 0 && ASTNodes.usesGivenSignature(methodBinding, klass.getQualifiedName(), methodInvocation.getName().getIdentifier(), remainingParams)) {
return true;
}
}
replaceByTypeReference(node, methodInvocation);
return false;
}
}
}
return true;
}
use of org.eclipse.jdt.core.dom.ReturnStatement in project AutoRefactor by JnRouvignac.
the class ObsoleteRedundantComparatorCleanUp method isClassToRemove.
private boolean isClassToRemove(final ClassInstanceCreation classInstanceCreation, final boolean isForward) {
AnonymousClassDeclaration anonymousClassDecl = classInstanceCreation.getAnonymousClassDeclaration();
Type type = classInstanceCreation.getType();
if (type != null && type.resolveBinding() != null && type.resolveBinding().getTypeArguments() != null && type.resolveBinding().getTypeArguments().length == 1 && ASTNodes.hasType(type.resolveBinding(), Comparator.class.getCanonicalName()) && classInstanceCreation.arguments().isEmpty() && anonymousClassDecl != null) {
List<BodyDeclaration> bodies = anonymousClassDecl.bodyDeclarations();
ITypeBinding typeArgument = type.resolveBinding().getTypeArguments()[0];
if (bodies != null && bodies.size() == 1 && typeArgument != null) {
BodyDeclaration body = bodies.get(0);
if (body instanceof MethodDeclaration) {
MethodDeclaration methodDecl = (MethodDeclaration) body;
ReturnStatement returnStatement = ASTNodes.as(methodDecl.getBody(), ReturnStatement.class);
if (returnStatement != null && returnStatement.getExpression() != null && // $NON-NLS-1$
ASTNodes.usesGivenSignature(// $NON-NLS-1$
methodDecl, // $NON-NLS-1$
Comparator.class.getCanonicalName(), // $NON-NLS-1$
"compare", typeArgument.getQualifiedName(), typeArgument.getQualifiedName())) {
VariableDeclaration object1 = (VariableDeclaration) methodDecl.parameters().get(0);
VariableDeclaration object2 = (VariableDeclaration) methodDecl.parameters().get(1);
return isReturnedExpressionToRemove(object1.getName(), object2.getName(), returnStatement.getExpression(), isForward);
}
}
}
}
return false;
}
use of org.eclipse.jdt.core.dom.ReturnStatement in project AutoRefactor by JnRouvignac.
the class ObsoleteJava7HashRatherThanEclipseJava6HashCleanUp method isStmtValid.
private boolean isStmtValid(final CollectedData data) {
Statement statement = data.getStmtIterator().next();
ExpressionStatement exprStatement = ASTNodes.as(statement, ExpressionStatement.class);
if (exprStatement != null) {
return isAssignmentValid(data, exprStatement);
}
ReturnStatement returnStatement = ASTNodes.as(statement, ReturnStatement.class);
if (returnStatement != null) {
data.setHasReturnStatement(true);
Expression expression = returnStatement.getExpression();
return returnStatement != null && (isGivenVariable(expression, data.getResultId()) || isHashValid(data, expression));
}
VariableDeclarationStatement varStatement = ASTNodes.as(statement, VariableDeclarationStatement.class);
if (varStatement != null && data.getTempVar() == null) {
VariableDeclarationFragment fragment = ASTNodes.getUniqueFragment(varStatement);
if (ASTNodes.hasType(varStatement.getType().resolveBinding(), long.class.getSimpleName()) && fragment != null) {
data.setTempVar(fragment.getName());
Expression initializer = fragment.getInitializer();
if (fragment.getExtraDimensions() == 0) {
if (initializer != null) {
SimpleName fieldToFind = isDoubleToLongBitsMethod(data, initializer);
data.setTempValueUsed(false);
if (fieldToFind != null && data.getStmtIterator().hasNext()) {
boolean assignmentValid = isStmtValid(data);
if (assignmentValid) {
data.getFields().add(ASTNodes.getUnparenthesedExpression(fieldToFind));
return true;
}
}
} else if (data.getStmtIterator().hasNext()) {
return isStmtValid(data);
}
}
}
}
return false;
}
use of org.eclipse.jdt.core.dom.ReturnStatement in project AutoRefactor by JnRouvignac.
the class ControlWorkflowMatcher method buildActualNodes.
private ControlWorkflowNode buildActualNodes(final List<Statement> actualStatements) {
if (Utils.isEmpty(actualStatements)) {
throw new AbortSearchException();
}
IfStatement ifStatement = ASTNodes.as(actualStatements.get(0), IfStatement.class);
ReturnStatement returnStatement = ASTNodes.as(actualStatements.get(0), ReturnStatement.class);
if (ifStatement != null) {
ControlWorkflowNode node = new ControlWorkflowNode();
node.setCondition(ifStatement.getExpression());
node.setThenNode(buildActualNodes(ifStatement.getThenStatement()));
if (ifStatement.getElseStatement() != null && actualStatements.size() == 1) {
node.setElseNode(buildActualNodes(ifStatement.getElseStatement()));
return node;
}
if (ifStatement.getElseStatement() != null || actualStatements.size() == 1 || !ASTNodes.fallsThrough(ifStatement.getThenStatement())) {
throw new AbortSearchException();
}
List<Statement> elseStmts = new ArrayList<>(actualStatements);
elseStmts.remove(0);
node.setElseNode(buildActualNodes(elseStmts));
return node;
}
if (returnStatement != null) {
Expression condition = returnStatement.getExpression();
return buildActualNodes(condition);
}
if (actualStatements.size() == 1) {
ControlWorkflowNode node = new ControlWorkflowNode();
node.setFinalStatement(actualStatements.get(0));
return node;
}
throw new AbortSearchException();
}
Aggregations