use of org.eclipse.jdt.core.dom.StringLiteral 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.StringLiteral in project AutoRefactor by JnRouvignac.
the class LogParametersRatherThanLogMessageCleanUp method maybeReplaceConcatenation.
private boolean maybeReplaceConcatenation(final MethodInvocation visited, final String methodName, final InfixExpression message) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
StringBuilder messageBuilder = new StringBuilder();
List<Expression> allOperands = ASTNodes.allOperands(message);
List<Expression> params = new ArrayList<>(allOperands.size());
boolean hasLiteral = false;
boolean hasObjects = false;
for (Expression string : allOperands) {
if (string instanceof StringLiteral) {
hasLiteral = true;
String literal = (String) string.resolveConstantExpressionValue();
// Due to a bug in JDT Core, the literal may be null
if (literal == null || literal.contains("{") || literal.contains("}")) {
// $NON-NLS-1$ //$NON-NLS-2$
return true;
}
messageBuilder.append(literal);
} else {
hasObjects = true;
// $NON-NLS-1$
messageBuilder.append("{}");
if (ASTNodes.hasType(string, Throwable.class.getCanonicalName())) {
MethodInvocation valueOfMethod = ast.newMethodInvocation();
valueOfMethod.setExpression(ASTNodeFactory.newName(ast, String.class.getSimpleName()));
// $NON-NLS-1$
valueOfMethod.setName(ast.newSimpleName("valueOf"));
valueOfMethod.arguments().add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(string)));
MethodInvocation newMethodInvocation = valueOfMethod;
params.add(newMethodInvocation);
} else {
params.add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(string)));
}
}
}
if (hasLiteral && hasObjects) {
replaceConcatenation(visited, methodName, messageBuilder, params);
return false;
}
return true;
}
use of org.eclipse.jdt.core.dom.StringLiteral in project AutoRefactor by JnRouvignac.
the class ObsoleteStringRatherThanNewStringCleanUp method visit.
@Override
public boolean visit(final ClassInstanceCreation node) {
if (ASTNodes.hasType(node, String.class.getCanonicalName()) && node.arguments().size() == 1) {
Expression arg0 = (Expression) node.arguments().get(0);
if (ASTNodes.hasType(arg0, String.class.getCanonicalName()) && (arg0 instanceof StringLiteral || arg0 instanceof InfixExpression)) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteStringRatherThanNewStringCleanUp_description);
ASTNodes.replaceButKeepComment(rewrite, node, ASTNodeFactory.parenthesizeIfNeeded(ast, ASTNodes.createMoveTarget(rewrite, arg0)), group);
return false;
}
}
return true;
}
use of org.eclipse.jdt.core.dom.StringLiteral in project AutoRefactor by JnRouvignac.
the class StringCleanUp method visit.
@Override
public boolean visit(final MethodInvocation visited) {
ASTNode parent = visited.getParent();
boolean isStringValueOf = isStringValueOf(visited);
if (ASTNodes.usesGivenSignature(visited, Object.class.getCanonicalName(), "toString")) {
// $NON-NLS-1$
Expression stringExpression = visited.getExpression();
if (ASTNodes.hasType(stringExpression, String.class.getCanonicalName())) {
// If node is already a String, no need to call toString()
removeToString(visited);
return false;
}
if (parent instanceof InfixExpression && ASTNodes.hasOperator((InfixExpression) parent, InfixExpression.Operator.PLUS)) {
// If node is in a String context, no need to call toString()
InfixExpression infixExpression = (InfixExpression) parent;
Expression leftOperand = infixExpression.getLeftOperand();
Expression rightOperand = infixExpression.getRightOperand();
boolean leftOperandIsString = ASTNodes.hasType(leftOperand, String.class.getCanonicalName());
boolean rightOperandIsString = ASTNodes.hasType(rightOperand, String.class.getCanonicalName());
MethodInvocation lmi = ASTNodes.as(leftOperand, MethodInvocation.class);
MethodInvocation rmi = ASTNodes.as(rightOperand, MethodInvocation.class);
if ((leftOperandIsString || rightOperandIsString) && visited.getLocationInParent() != InfixExpression.LEFT_OPERAND_PROPERTY && visited.getLocationInParent() != InfixExpression.RIGHT_OPERAND_PROPERTY) {
// Node is in the extended operands
removeToString(visited);
return false;
}
if (leftOperandIsString && ASTNodes.usesGivenSignature(rmi, Object.class.getCanonicalName(), "toString")) {
// $NON-NLS-1$
removeToString(rmi);
return false;
}
if (rightOperandIsString && visited.getLocationInParent() == InfixExpression.LEFT_OPERAND_PROPERTY) {
removeToString(lmi);
return false;
}
}
} else if (isStringValueOf && ASTNodes.hasType((Expression) visited.arguments().get(0), String.class.getCanonicalName()) && (visited.arguments().get(0) instanceof StringLiteral || visited.arguments().get(0) instanceof InfixExpression)) {
removeValueOf(visited);
return false;
}
return true;
}
use of org.eclipse.jdt.core.dom.StringLiteral in project AutoRefactor by JnRouvignac.
the class ASTNodeFactory method newStringLiteral.
/**
* Builds a new {@link StringLiteral} instance.
*
* @param s the string literal value
* @return a new string literal
*/
public StringLiteral newStringLiteral(final String s) {
StringLiteral sl = ast.newStringLiteral();
sl.setLiteralValue(s);
return sl;
}
Aggregations