use of org.eclipse.jdt.core.dom.BooleanLiteral in project evosuite by EvoSuite.
the class TestExtractingVisitor method retrieveTypeClass.
/**
* <p>
* retrieveTypeClass
* </p>
*
* @param argument
* a {@link java.lang.Object} object.
* @return a {@link java.lang.Class} object.
*/
protected Class<?> retrieveTypeClass(Object argument) {
assert argument != null;
if (argument instanceof SimpleType) {
SimpleType simpleType = (SimpleType) argument;
return retrieveTypeClass(simpleType);
}
if (argument instanceof ITypeBinding) {
ITypeBinding binding = (ITypeBinding) argument;
return retrieveTypeClass(binding);
}
if (argument instanceof IVariableBinding) {
IVariableBinding variableBinding = (IVariableBinding) argument;
return retrieveTypeClass(variableBinding.getType());
}
if (argument instanceof SimpleName) {
SimpleName simpleName = (SimpleName) argument;
return retrieveTypeClass(simpleName.resolveBinding());
}
if (argument instanceof StringLiteral) {
return String.class;
}
if (argument instanceof NumberLiteral) {
return retrieveTypeClass((NumberLiteral) argument);
}
if (argument instanceof PrimitiveType) {
PrimitiveType primitiveType = (PrimitiveType) argument;
String typeCode = primitiveType.getPrimitiveTypeCode().toString();
Class<?> result = PRIMITIVE_TYPECODE_MAPPING.get(typeCode);
assert result != null : "Could not resolve typecode " + typeCode + ".";
return result;
}
if (argument instanceof ArrayType) {
ArrayType arrayType = (ArrayType) argument;
return retrieveTypeClass(arrayType);
}
if (argument instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) argument;
return retrieveTypeClass(parameterizedType.getType());
}
if (argument instanceof VariableDeclarationFragment) {
VariableDeclarationFragment varDeclFrgmnt = (VariableDeclarationFragment) argument;
return retrieveTypeClass(varDeclFrgmnt.resolveBinding());
}
if (argument instanceof InfixExpression) {
InfixExpression infixExpr = (InfixExpression) argument;
ITypeBinding refTypeBinding = infixExpr.resolveTypeBinding();
if (refTypeBinding != null) {
return retrieveTypeClass(refTypeBinding);
} else {
throw new RuntimeException("Could not determine type class of infix expression '" + infixExpr + "'.");
}
}
if (argument instanceof MethodInvocation) {
MethodInvocation methodInvocation = (MethodInvocation) argument;
ITypeBinding typeBinding = methodInvocation.resolveTypeBinding();
if (typeBinding != null) {
return retrieveTypeClass(typeBinding);
}
Expression typeExpression = methodInvocation.getExpression();
if (typeExpression instanceof MethodInvocation) {
MethodInvocation parentMethodInvocation = (MethodInvocation) typeExpression;
IMethodBinding parentMethodBinding = parentMethodInvocation.resolveMethodBinding();
return retrieveTypeClass(parentMethodBinding.getDeclaringClass());
} else {
return retrieveTypeClass(typeExpression);
}
}
if (argument instanceof ArrayAccess) {
ArrayAccess arrayAccess = (ArrayAccess) argument;
return retrieveTypeClass(arrayAccess.getArray());
}
if (argument instanceof Class<?>) {
return (Class<?>) argument;
}
if (argument instanceof ClassInstanceCreation) {
return retrieveTypeClass(((ClassInstanceCreation) argument).resolveTypeBinding());
}
if (argument instanceof BooleanLiteral) {
return Boolean.TYPE;
}
throw new UnsupportedOperationException("Retrieval of type " + argument.getClass() + " not implemented yet!");
}
use of org.eclipse.jdt.core.dom.BooleanLiteral in project flux by eclipse.
the class AdvancedQuickAssistProcessor method getInversedExpression.
private static Expression getInversedExpression(ASTRewrite rewrite, Expression expression, SimpleNameRenameProvider provider) {
AST ast = rewrite.getAST();
//
if (expression instanceof BooleanLiteral) {
return ast.newBooleanLiteral(!((BooleanLiteral) expression).booleanValue());
}
if (expression instanceof InfixExpression) {
InfixExpression infixExpression = (InfixExpression) expression;
InfixExpression.Operator operator = infixExpression.getOperator();
if (operator == InfixExpression.Operator.LESS) {
return getInversedInfixExpression(rewrite, infixExpression, InfixExpression.Operator.GREATER_EQUALS, provider);
}
if (operator == InfixExpression.Operator.GREATER) {
return getInversedInfixExpression(rewrite, infixExpression, InfixExpression.Operator.LESS_EQUALS, provider);
}
if (operator == InfixExpression.Operator.LESS_EQUALS) {
return getInversedInfixExpression(rewrite, infixExpression, InfixExpression.Operator.GREATER, provider);
}
if (operator == InfixExpression.Operator.GREATER_EQUALS) {
return getInversedInfixExpression(rewrite, infixExpression, InfixExpression.Operator.LESS, provider);
}
if (operator == InfixExpression.Operator.EQUALS) {
return getInversedInfixExpression(rewrite, infixExpression, InfixExpression.Operator.NOT_EQUALS, provider);
}
if (operator == InfixExpression.Operator.NOT_EQUALS) {
return getInversedInfixExpression(rewrite, infixExpression, InfixExpression.Operator.EQUALS, provider);
}
if (operator == InfixExpression.Operator.CONDITIONAL_AND) {
return getInversedAndOrExpression(rewrite, infixExpression, InfixExpression.Operator.CONDITIONAL_OR, provider);
}
if (operator == InfixExpression.Operator.CONDITIONAL_OR) {
return getInversedAndOrExpression(rewrite, infixExpression, InfixExpression.Operator.CONDITIONAL_AND, provider);
}
if (operator == InfixExpression.Operator.AND) {
return getInversedAndOrExpression(rewrite, infixExpression, InfixExpression.Operator.OR, provider);
}
if (operator == InfixExpression.Operator.OR) {
return getInversedAndOrExpression(rewrite, infixExpression, InfixExpression.Operator.AND, provider);
}
if (operator == InfixExpression.Operator.XOR) {
return getInversedNotExpression(rewrite, expression, ast);
}
}
if (expression instanceof PrefixExpression) {
PrefixExpression prefixExpression = (PrefixExpression) expression;
if (prefixExpression.getOperator() == PrefixExpression.Operator.NOT) {
Expression operand = prefixExpression.getOperand();
if ((operand instanceof ParenthesizedExpression) && NecessaryParenthesesChecker.canRemoveParentheses(operand, expression.getParent(), expression.getLocationInParent())) {
operand = ((ParenthesizedExpression) operand).getExpression();
}
Expression renamedNameCopy = getRenamedNameCopy(provider, rewrite, operand);
if (renamedNameCopy instanceof InfixExpression) {
InfixExpression infixExpression = (InfixExpression) renamedNameCopy;
infixExpression.setOperator(((InfixExpression) operand).getOperator());
}
return renamedNameCopy;
}
}
if (expression instanceof InstanceofExpression) {
return getInversedNotExpression(rewrite, expression, ast);
}
if (expression instanceof ParenthesizedExpression) {
ParenthesizedExpression parenthesizedExpression = (ParenthesizedExpression) expression;
Expression innerExpression = parenthesizedExpression.getExpression();
while (innerExpression instanceof ParenthesizedExpression) {
innerExpression = ((ParenthesizedExpression) innerExpression).getExpression();
}
if (innerExpression instanceof InstanceofExpression) {
return getInversedExpression(rewrite, innerExpression, provider);
}
parenthesizedExpression = getParenthesizedExpression(ast, getInversedExpression(rewrite, innerExpression, provider));
return parenthesizedExpression;
}
if (expression instanceof ConditionalExpression) {
ConditionalExpression conditionalExpression = (ConditionalExpression) expression;
ConditionalExpression newExpression = ast.newConditionalExpression();
newExpression.setExpression((Expression) rewrite.createCopyTarget(conditionalExpression.getExpression()));
newExpression.setThenExpression(getInversedExpression(rewrite, conditionalExpression.getThenExpression()));
newExpression.setElseExpression(getInversedExpression(rewrite, conditionalExpression.getElseExpression()));
return newExpression;
}
PrefixExpression prefixExpression = ast.newPrefixExpression();
prefixExpression.setOperator(PrefixExpression.Operator.NOT);
Expression renamedNameCopy = getRenamedNameCopy(provider, rewrite, expression);
if (NecessaryParenthesesChecker.needsParentheses(renamedNameCopy, prefixExpression, PrefixExpression.OPERAND_PROPERTY)) {
renamedNameCopy = getParenthesizedExpression(ast, renamedNameCopy);
}
prefixExpression.setOperand(renamedNameCopy);
return prefixExpression;
}
use of org.eclipse.jdt.core.dom.BooleanLiteral in project AutoRefactor by JnRouvignac.
the class ObsoleteLiteralRatherThanBooleanConstantCleanUp method replaceWithBooleanLiteral.
private void replaceWithBooleanLiteral(final QualifiedName visited, final boolean val) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteLiteralRatherThanBooleanConstantCleanUp_description);
BooleanLiteral booleanLiteral = ast.newBooleanLiteral(val);
ASTNodes.replaceButKeepComment(rewrite, visited, booleanLiteral, group);
}
Aggregations