use of org.eclipse.jdt.core.dom.StringLiteral in project flux by eclipse.
the class StubUtility method getBaseNameFromExpression.
private static String getBaseNameFromExpression(IJavaProject project, Expression assignedExpression, int variableKind) {
String name = null;
if (assignedExpression instanceof CastExpression) {
assignedExpression = ((CastExpression) assignedExpression).getExpression();
}
if (assignedExpression instanceof Name) {
Name simpleNode = (Name) assignedExpression;
IBinding binding = simpleNode.resolveBinding();
if (binding instanceof IVariableBinding)
return getBaseName((IVariableBinding) binding, project);
return ASTNodes.getSimpleNameIdentifier(simpleNode);
} else if (assignedExpression instanceof MethodInvocation) {
name = ((MethodInvocation) assignedExpression).getName().getIdentifier();
} else if (assignedExpression instanceof SuperMethodInvocation) {
name = ((SuperMethodInvocation) assignedExpression).getName().getIdentifier();
} else if (assignedExpression instanceof FieldAccess) {
return ((FieldAccess) assignedExpression).getName().getIdentifier();
} else if (variableKind == NamingConventions.VK_STATIC_FINAL_FIELD && (assignedExpression instanceof StringLiteral || assignedExpression instanceof NumberLiteral)) {
String string = assignedExpression instanceof StringLiteral ? ((StringLiteral) assignedExpression).getLiteralValue() : ((NumberLiteral) assignedExpression).getToken();
StringBuffer res = new StringBuffer();
boolean needsUnderscore = false;
for (int i = 0; i < string.length(); i++) {
char ch = string.charAt(i);
if (Character.isJavaIdentifierPart(ch)) {
if (res.length() == 0 && !Character.isJavaIdentifierStart(ch) || needsUnderscore) {
res.append('_');
}
res.append(ch);
needsUnderscore = false;
} else {
needsUnderscore = res.length() > 0;
}
}
if (res.length() > 0) {
return res.toString();
}
}
if (name != null) {
for (int i = 0; i < KNOWN_METHOD_NAME_PREFIXES.length; i++) {
String curr = KNOWN_METHOD_NAME_PREFIXES[i];
if (name.startsWith(curr)) {
if (name.equals(curr)) {
// don't suggest 'get' as variable name
return null;
} else if (Character.isUpperCase(name.charAt(curr.length()))) {
return name.substring(curr.length());
}
}
}
}
return name;
}
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 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;
}
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 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;
}
Aggregations