Search in sources :

Example 16 with ArrayAccess

use of org.eclipse.jdt.core.dom.ArrayAccess in project xtext-xtend by eclipse.

the class JavaASTFlattener method visit.

@Override
public boolean visit(final Assignment node) {
    final Expression leftSide = node.getLeftHandSide();
    Type type = null;
    if ((leftSide instanceof ArrayAccess)) {
        Expression _array = ((ArrayAccess) leftSide).getArray();
        if ((_array instanceof SimpleName)) {
            Expression _array_1 = ((ArrayAccess) leftSide).getArray();
            type = this._aSTFlattenerUtils.findDeclaredType(((SimpleName) _array_1));
        }
        this.handleAssignment(node, ((ArrayAccess) leftSide), type);
    } else {
        if ((leftSide instanceof SimpleName)) {
            type = this._aSTFlattenerUtils.findDeclaredType(((SimpleName) leftSide));
        }
        this.handleAssignment(node, leftSide, type);
    }
    return false;
}
Also used : ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) WildcardType(org.eclipse.jdt.core.dom.WildcardType) QualifiedType(org.eclipse.jdt.core.dom.QualifiedType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) ArrayAccess(org.eclipse.jdt.core.dom.ArrayAccess) InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) SimpleName(org.eclipse.jdt.core.dom.SimpleName)

Example 17 with ArrayAccess

use of org.eclipse.jdt.core.dom.ArrayAccess in project xtext-xtend by eclipse.

the class JavaASTFlattener method computeArrayName.

public String computeArrayName(final ArrayAccess node) {
    String _switchResult = null;
    Expression _array = node.getArray();
    final Expression array = _array;
    boolean _matched = false;
    if (array instanceof SimpleName) {
        _matched = true;
        _switchResult = ((SimpleName) array).getIdentifier();
    }
    if (!_matched) {
        if (array instanceof MethodInvocation) {
            _matched = true;
            _switchResult = ((MethodInvocation) array).getName().getIdentifier();
        }
    }
    if (!_matched) {
        if (array instanceof ArrayAccess) {
            _matched = true;
            String _computeArrayName = this.computeArrayName(((ArrayAccess) array));
            _switchResult = ("_" + _computeArrayName);
        }
    }
    if (!_matched) {
        _switchResult = "tmpNode";
    }
    return _switchResult;
}
Also used : ArrayAccess(org.eclipse.jdt.core.dom.ArrayAccess) InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) SimpleName(org.eclipse.jdt.core.dom.SimpleName) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation)

Example 18 with ArrayAccess

use of org.eclipse.jdt.core.dom.ArrayAccess 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!");
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) BooleanLiteral(org.eclipse.jdt.core.dom.BooleanLiteral) SimpleName(org.eclipse.jdt.core.dom.SimpleName) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) ArrayAccess(org.eclipse.jdt.core.dom.ArrayAccess) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) PrimitiveExpression(org.evosuite.testcase.PrimitiveExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) GenericClass(org.evosuite.utils.GenericClass) NumberLiteral(org.eclipse.jdt.core.dom.NumberLiteral)

Example 19 with ArrayAccess

use of org.eclipse.jdt.core.dom.ArrayAccess in project eclipse.jdt.ls by eclipse.

the class ASTResolving method guessTypeForReference.

public static Type guessTypeForReference(AST ast, ASTNode node) {
    ASTNode parent = node.getParent();
    while (parent != null) {
        switch(parent.getNodeType()) {
            case ASTNode.VARIABLE_DECLARATION_FRAGMENT:
                if (((VariableDeclarationFragment) parent).getInitializer() == node) {
                    return ASTNodeFactory.newType(ast, (VariableDeclaration) parent);
                }
                return null;
            case ASTNode.SINGLE_VARIABLE_DECLARATION:
                if (((VariableDeclarationFragment) parent).getInitializer() == node) {
                    return ASTNodeFactory.newType(ast, (VariableDeclaration) parent);
                }
                return null;
            case ASTNode.ARRAY_ACCESS:
                if (!((ArrayAccess) parent).getIndex().equals(node)) {
                    Type type = guessTypeForReference(ast, parent);
                    if (type != null) {
                        return ASTNodeFactory.newArrayType(type);
                    }
                }
                return null;
            case ASTNode.FIELD_ACCESS:
                if (node.equals(((FieldAccess) parent).getName())) {
                    node = parent;
                    parent = parent.getParent();
                } else {
                    return null;
                }
                break;
            case ASTNode.SUPER_FIELD_ACCESS:
            case ASTNode.PARENTHESIZED_EXPRESSION:
                node = parent;
                parent = parent.getParent();
                break;
            case ASTNode.QUALIFIED_NAME:
                if (node.equals(((QualifiedName) parent).getName())) {
                    node = parent;
                    parent = parent.getParent();
                } else {
                    return null;
                }
                break;
            default:
                return null;
        }
    }
    return null;
}
Also used : ArrayAccess(org.eclipse.jdt.core.dom.ArrayAccess) Type(org.eclipse.jdt.core.dom.Type) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Aggregations

ArrayAccess (org.eclipse.jdt.core.dom.ArrayAccess)19 Expression (org.eclipse.jdt.core.dom.Expression)11 SimpleName (org.eclipse.jdt.core.dom.SimpleName)11 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)10 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)10 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)9 Assignment (org.eclipse.jdt.core.dom.Assignment)8 CastExpression (org.eclipse.jdt.core.dom.CastExpression)8 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)7 ASTNode (org.eclipse.jdt.core.dom.ASTNode)6 ArrayType (org.eclipse.jdt.core.dom.ArrayType)6 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)6 PostfixExpression (org.eclipse.jdt.core.dom.PostfixExpression)6 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)6 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)6 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)6 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)5 InstanceofExpression (org.eclipse.jdt.core.dom.InstanceofExpression)5 ParameterizedType (org.eclipse.jdt.core.dom.ParameterizedType)5 AST (org.eclipse.jdt.core.dom.AST)4