Search in sources :

Example 36 with ArrayType

use of org.eclipse.jdt.core.dom.ArrayType 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 37 with ArrayType

use of org.eclipse.jdt.core.dom.ArrayType in project jbosstools-hibernate by jbosstools.

the class ProcessEntityInfo method isSimilarType.

// simple type name check
public boolean isSimilarType(Type type, String fullyQualifiedName) {
    String typeName = null;
    if (type.isSimpleType()) {
        SimpleType st = (SimpleType) type;
        typeName = st.getName().getFullyQualifiedName();
    } else if (type.isArrayType()) {
        ArrayType at = (ArrayType) type;
        Type elementType = at.getElementType();
        if (elementType.isSimpleType()) {
            SimpleType st = (SimpleType) elementType;
            typeName = st.getName().getFullyQualifiedName();
        }
    }
    if (typeName != null && fullyQualifiedName.indexOf(typeName) == -1) {
        return false;
    }
    return true;
}
Also used : ArrayType(org.eclipse.jdt.core.dom.ArrayType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) OwnerType(org.hibernate.eclipse.jdt.ui.internal.jpa.common.OwnerType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) RefType(org.hibernate.eclipse.jdt.ui.internal.jpa.common.RefType) FieldGetterType(org.hibernate.eclipse.jdt.ui.internal.jpa.common.EntityInfo.FieldGetterType) ArrayType(org.eclipse.jdt.core.dom.ArrayType)

Example 38 with ArrayType

use of org.eclipse.jdt.core.dom.ArrayType in project jbosstools-hibernate by jbosstools.

the class TypeVisitor method visit.

@Override
public boolean visit(ArrayType type) {
    IValue array = null;
    Type elementType = type.getElementType();
    ITypeBinding tb = elementType.resolveBinding();
    // Unresolved binding. Omit the property.
    if (tb == null)
        return false;
    if (tb.isPrimitive()) {
        array = service.newPrimitiveArray(rootClass);
        IValue value = buildSimpleValue(tb.getName());
        value.setTable(rootClass.getTable());
        array.setElement(value);
        // TODO what to set?
        array.setCollectionTable(rootClass.getTable());
    } else {
        IPersistentClass associatedClass = rootClasses.get(tb.getBinaryName());
        array = service.newArray(rootClass);
        array.setElementClassName(tb.getBinaryName());
        array.setCollectionTable(associatedClass.getTable());
        IValue oValue = service.newOneToMany(rootClass);
        oValue.setAssociatedClass(associatedClass);
        oValue.setReferencedEntityName(tb.getBinaryName());
        array.setElement(oValue);
    }
    IValue key = service.newSimpleValue();
    if (StringHelper.isNotEmpty(entityInfo.getPrimaryIdName())) {
        key.addColumn(service.newColumn(entityInfo.getPrimaryIdName().toUpperCase()));
    }
    array.setKey(key);
    array.setFetchModeJoin();
    IValue index = service.newSimpleValue();
    // add default index
    // index.addColumn(new Column(varName.toUpperCase()+"_POSITION"));
    array.setIndex(index);
    buildProperty(array);
    // $NON-NLS-1$
    prop.setCascade("none");
    // do not visit children
    return false;
}
Also used : SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) RefType(org.hibernate.eclipse.jdt.ui.internal.jpa.common.RefType) WildcardType(org.eclipse.jdt.core.dom.WildcardType) IType(org.eclipse.jdt.core.IType) QualifiedType(org.eclipse.jdt.core.dom.QualifiedType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) IValue(org.jboss.tools.hibernate.runtime.spi.IValue) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) IPersistentClass(org.jboss.tools.hibernate.runtime.spi.IPersistentClass)

Example 39 with ArrayType

use of org.eclipse.jdt.core.dom.ArrayType in project jbosstools-hibernate by jbosstools.

the class CollectEntityInfo method processFieldOrGetter.

public boolean processFieldOrGetter(Type type, List<String> list, boolean fieldFlag) {
    if (type == null) {
        return false;
    }
    if (type.isPrimitiveType()) {
        PrimitiveType pt = (PrimitiveType) type;
        if (!pt.getPrimitiveTypeCode().equals(PrimitiveType.BOOLEAN)) {
            // this is candidate for primary id
            Iterator<String> itVarNames = list.iterator();
            while (itVarNames.hasNext()) {
                String name = itVarNames.next();
                if ("version".equalsIgnoreCase(name)) {
                    // $NON-NLS-1$
                    FieldGetterType versionFieldGetter = updateFieldGetter(entityInfo.getVersionFieldGetter(), fieldFlag);
                    entityInfo.setVersionFieldGetter(versionFieldGetter);
                } else {
                    entityInfo.addPrimaryIdCandidate(name);
                }
            }
        }
    } else if (type.isSimpleType()) {
        SimpleType st = (SimpleType) type;
        ITypeBinding tb = st.resolveBinding();
        if (tb != null) {
            // $NON-NLS-1$
            String entityFullyQualifiedName = "";
            if (tb.getJavaElement() instanceof SourceType) {
                SourceType sourceT = (SourceType) tb.getJavaElement();
                entityFullyQualifiedName = sourceT.getFullyQualifiedName();
                entityInfo.addDependency(entityFullyQualifiedName);
                RefType refType2Use = tb.isEnum() ? RefType.ENUMERATED : RefType.MANY2ONE;
                Iterator<String> itVarNames = list.iterator();
                while (itVarNames.hasNext()) {
                    String name = itVarNames.next();
                    entityInfo.addReference(name, entityFullyQualifiedName, refType2Use);
                }
            } else if (tb.getJavaElement() instanceof BinaryType) {
                ITypeBinding tbParent = tb.getTypeDeclaration().getSuperclass();
                if (tbParent != null) {
                    if ("java.lang.Number".equals(tbParent.getBinaryName())) {
                        // $NON-NLS-1$
                        // this is candidate for primary id
                        Iterator<String> itVarNames = list.iterator();
                        while (itVarNames.hasNext()) {
                            String name = itVarNames.next();
                            if ("version".equalsIgnoreCase(name)) {
                                // $NON-NLS-1$
                                FieldGetterType versionFieldGetter = updateFieldGetter(entityInfo.getVersionFieldGetter(), fieldFlag);
                                entityInfo.setVersionFieldGetter(versionFieldGetter);
                            } else {
                                entityInfo.addPrimaryIdCandidate(name);
                            }
                        }
                    } else if ("java.util.Date".equals(tbParent.getBinaryName())) {
                        // $NON-NLS-1$
                        // this is candidate for version
                        Iterator<String> itVarNames = list.iterator();
                        while (itVarNames.hasNext()) {
                            String name = itVarNames.next();
                            if ("version".equalsIgnoreCase(name)) {
                                // $NON-NLS-1$
                                FieldGetterType versionFieldGetter = updateFieldGetter(entityInfo.getVersionFieldGetter(), fieldFlag);
                                entityInfo.setVersionFieldGetter(versionFieldGetter);
                            }
                        }
                    }
                }
                if ("java.lang.String".equals(tb.getBinaryName())) {
                    // $NON-NLS-1$
                    Iterator<String> itVarNames = list.iterator();
                    while (itVarNames.hasNext()) {
                        String name = itVarNames.next();
                        entityInfo.updateAnnotationColumn(name, null, false);
                        entityInfo.addPrimaryIdCandidate(name);
                    }
                }
            }
        }
    } else if (type.isArrayType()) {
        ArrayType at = (ArrayType) type;
        Type elementType = at.getElementType();
        ITypeBinding tb = elementType.resolveBinding();
        if (tb != null) {
            if (tb.getJavaElement() instanceof SourceType) {
                // $NON-NLS-1$
                String entityFullyQualifiedName = "";
                SourceType sourceT = (SourceType) tb.getJavaElement();
                try {
                    entityFullyQualifiedName = sourceT.getFullyQualifiedParameterizedName();
                } catch (JavaModelException e) {
                    // $NON-NLS-1$
                    HibernateConsolePlugin.getDefault().logErrorMessage("JavaModelException: ", e);
                }
                entityInfo.addDependency(entityFullyQualifiedName);
                Iterator<String> itVarNames = list.iterator();
                while (itVarNames.hasNext()) {
                    String name = itVarNames.next();
                    entityInfo.addReference(name, entityFullyQualifiedName, RefType.ONE2MANY);
                }
            }
        }
    } else if (type.isParameterizedType()) {
        ParameterizedType pt = (ParameterizedType) type;
        Type typeP = pt.getType();
        ITypeBinding tb = typeP.resolveBinding();
        if (tb != null) {
            ITypeBinding[] interfaces = Utils.getAllInterfaces(tb);
            // $NON-NLS-1$
            String fullyQualifiedNameTypeName = "";
            if (Utils.isImplementInterface(interfaces, "java.util.Collection")) {
                // $NON-NLS-1$
                // $NON-NLS-1$
                fullyQualifiedNameTypeName = "java.util.Collection";
            }
            if (Utils.isImplementInterface(interfaces, "java.util.Map")) {
                // $NON-NLS-1$
                // $NON-NLS-1$
                fullyQualifiedNameTypeName = "java.util.Map";
            }
            /*for (int i = 0; i < interfaces.length; i++) {
					if (interfaces[i].getJavaElement() instanceof BinaryType) {
						BinaryType binaryT = (BinaryType)interfaces[i].getJavaElement();
						String tmp = binaryT.getFullyQualifiedName('.');
						if (0 == "java.util.Collection".compareTo(tmp)) { //$NON-NLS-1$
							fullyQualifiedNameTypeName = tmp;
							break;
						}
					}
				}*/
            if (fullyQualifiedNameTypeName.length() > 0) {
                Iterator<Type> typeArgsIt = pt.typeArguments().iterator();
                while (typeArgsIt.hasNext()) {
                    typeP = typeArgsIt.next();
                    tb = typeP.resolveBinding();
                    // $NON-NLS-1$
                    String entityFullyQualifiedName = "";
                    if (tb.getJavaElement() instanceof SourceType) {
                        SourceType sourceT = (SourceType) tb.getJavaElement();
                        try {
                            entityFullyQualifiedName = sourceT.getFullyQualifiedParameterizedName();
                        } catch (JavaModelException e) {
                            // $NON-NLS-1$
                            HibernateConsolePlugin.getDefault().logErrorMessage("JavaModelException: ", e);
                        }
                        entityInfo.addDependency(entityFullyQualifiedName);
                        Iterator<String> itVarNames = list.iterator();
                        while (itVarNames.hasNext()) {
                            String name = itVarNames.next();
                            entityInfo.addReference(name, entityFullyQualifiedName, RefType.ONE2MANY);
                        }
                    }
                }
            }
        }
    } else if (type.isQualifiedType()) {
        QualifiedType qt = (QualifiedType) type;
        @SuppressWarnings("unused") ITypeBinding tb = qt.resolveBinding();
    } else if (type.isWildcardType()) {
        WildcardType wt = (WildcardType) type;
        @SuppressWarnings("unused") ITypeBinding tb = wt.resolveBinding();
    }
    return true;
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) BinaryType(org.eclipse.jdt.internal.core.BinaryType) SourceType(org.eclipse.jdt.internal.core.SourceType) FieldGetterType(org.hibernate.eclipse.jdt.ui.internal.jpa.common.EntityInfo.FieldGetterType) RefType(org.hibernate.eclipse.jdt.ui.internal.jpa.common.RefType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) BinaryType(org.eclipse.jdt.internal.core.BinaryType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) RefType(org.hibernate.eclipse.jdt.ui.internal.jpa.common.RefType) WildcardType(org.eclipse.jdt.core.dom.WildcardType) FieldGetterType(org.hibernate.eclipse.jdt.ui.internal.jpa.common.EntityInfo.FieldGetterType) SourceType(org.eclipse.jdt.internal.core.SourceType) QualifiedType(org.eclipse.jdt.core.dom.QualifiedType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) WildcardType(org.eclipse.jdt.core.dom.WildcardType) QualifiedType(org.eclipse.jdt.core.dom.QualifiedType) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) Iterator(java.util.Iterator) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType)

Aggregations

ArrayType (org.eclipse.jdt.core.dom.ArrayType)39 Type (org.eclipse.jdt.core.dom.Type)27 ParameterizedType (org.eclipse.jdt.core.dom.ParameterizedType)21 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)18 SimpleType (org.eclipse.jdt.core.dom.SimpleType)18 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)15 Dimension (org.eclipse.jdt.core.dom.Dimension)10 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)10 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)10 QualifiedType (org.eclipse.jdt.core.dom.QualifiedType)8 SimpleName (org.eclipse.jdt.core.dom.SimpleName)8 List (java.util.List)7 ASTNode (org.eclipse.jdt.core.dom.ASTNode)7 NameQualifiedType (org.eclipse.jdt.core.dom.NameQualifiedType)7 UnionType (org.eclipse.jdt.core.dom.UnionType)7 IType (org.eclipse.jdt.core.IType)6 ASTVisitor (org.eclipse.jdt.core.dom.ASTVisitor)6 Expression (org.eclipse.jdt.core.dom.Expression)6 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)6 ArrayList (java.util.ArrayList)5