use of org.eclipse.jdt.core.dom.NumberLiteral in project AutoRefactor by JnRouvignac.
the class BigNumberRefactoring method visit.
@Override
public boolean visit(MethodInvocation node) {
if (node.getExpression() == null) {
return VISIT_SUBTREE;
}
if (getJavaMinorVersion() >= 5 && (isMethod(node, "java.math.BigInteger", "valueOf", "long") || isMethod(node, "java.math.BigDecimal", "valueOf", "long") || isMethod(node, "java.math.BigDecimal", "valueOf", "double"))) {
final ITypeBinding typeBinding = node.getExpression().resolveTypeBinding();
final Expression arg0 = arg0(node);
if (arg0 instanceof NumberLiteral) {
final String token = ((NumberLiteral) arg0).getToken().replaceFirst("[lLfFdD]$", "");
if (token.contains(".") && hasType(typeBinding, "java.math.BigDecimal")) {
this.ctx.getRefactorings().replace(node, getClassInstanceCreatorNode((Name) node.getExpression(), token));
} else if (ZERO_LONG_LITERAL_RE.matcher(token).matches()) {
replaceWithQualifiedName(node, typeBinding, "ZERO");
} else if (ONE_LONG_LITERAL_RE.matcher(token).matches()) {
replaceWithQualifiedName(node, typeBinding, "ONE");
} else if (TEN_LONG_LITERAL_RE.matcher(token).matches()) {
replaceWithQualifiedName(node, typeBinding, "TEN");
} else {
return VISIT_SUBTREE;
}
return DO_NOT_VISIT_SUBTREE;
}
} else if (!(node.getParent() instanceof PrefixExpression) || !hasOperator((PrefixExpression) node.getParent(), NOT)) {
return maybeReplaceEquals(true, node, node);
}
return VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.NumberLiteral 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.NumberLiteral 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.NumberLiteral in project AutoRefactor by JnRouvignac.
the class ObsoleteRemoveUnnecessaryCastCleanUp method createPrimitive.
private void createPrimitive(final CastExpression visited, final PrefixExpression.Operator prefixOperator, final NumberLiteral literal, final char postfix) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteRemoveUnnecessaryCastCleanUp_description);
NumberLiteral numberLiteral = ast.newNumberLiteral(literal.getToken() + postfix);
if (prefixOperator != null) {
PrefixExpression prefixExpression = ast.newPrefixExpression();
prefixExpression.setOperator(prefixOperator);
prefixExpression.setOperand(numberLiteral);
ASTNodes.replaceButKeepComment(rewrite, visited, prefixExpression, group);
} else {
ASTNodes.replaceButKeepComment(rewrite, visited, numberLiteral, group);
}
}
use of org.eclipse.jdt.core.dom.NumberLiteral in project AutoRefactor by JnRouvignac.
the class ObsoleteRemoveUnnecessaryCastCleanUp method visit.
@Override
public boolean visit(final CastExpression visited) {
Expression expression = visited.getExpression();
PrefixExpression prefixExpression = ASTNodes.as(expression, PrefixExpression.class);
NumberLiteral literal = ASTNodes.as(expression, NumberLiteral.class);
PrefixExpression.Operator prefixOperator = null;
if (prefixExpression != null) {
if (ASTNodes.hasOperator(prefixExpression, PrefixExpression.Operator.MINUS)) {
prefixOperator = PrefixExpression.Operator.MINUS;
literal = ASTNodes.as(prefixExpression.getOperand(), NumberLiteral.class);
} else if (ASTNodes.hasOperator(prefixExpression, PrefixExpression.Operator.PLUS)) {
prefixOperator = PrefixExpression.Operator.PLUS;
literal = ASTNodes.as(prefixExpression.getOperand(), NumberLiteral.class);
} else {
literal = null;
}
}
if (literal != null && (literal.getToken().matches(".*[^lLdDfF]") || literal.getToken().matches("0x.*[^lL]"))) {
// $NON-NLS-1$ //$NON-NLS-2$
if (ASTNodes.hasType(visited.getType().resolveBinding(), long.class.getSimpleName())) {
createPrimitive(visited, prefixOperator, literal, 'L');
return false;
}
if (ASTNodes.hasType(visited.getType().resolveBinding(), float.class.getSimpleName())) {
createPrimitive(visited, prefixOperator, literal, 'F');
return false;
}
if (ASTNodes.hasType(visited.getType().resolveBinding(), double.class.getSimpleName())) {
createPrimitive(visited, prefixOperator, literal, 'D');
return false;
}
}
if (canRemoveCast(visited)) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteRemoveUnnecessaryCastCleanUp_description);
ASTNodes.replaceButKeepComment(rewrite, visited, ASTNodes.createMoveTarget(rewrite, visited.getExpression()), group);
return false;
}
return true;
}
Aggregations