Search in sources :

Example 1 with WildcardType

use of org.eclipse.jdt.core.dom.WildcardType in project AutoRefactor by JnRouvignac.

the class ASTBuilder method toType.

/**
 * Converts a type binding into a type.
 *
 * @param typeBinding the type binding to convert
 * @param typeNameDecider decides on how the type should be referenced (simple name or qualified name)
 * @return a new type
 */
public Type toType(ITypeBinding typeBinding, TypeNameDecider typeNameDecider) {
    if (typeBinding == null) {
        throw new IllegalArgumentException(null, "typeBinding cannot be null");
    }
    if (typeBinding.isParameterizedType()) {
        final ParameterizedType type = ast.newParameterizedType(toType(typeBinding.getErasure(), typeNameDecider));
        final List<Type> typeArgs = typeArguments(type);
        for (ITypeBinding typeArg : typeBinding.getTypeArguments()) {
            typeArgs.add(toType(typeArg, typeNameDecider));
        }
        return type;
    } else if (typeBinding.isPrimitive()) {
        return type(typeBinding.getName());
    } else if (typeBinding.isClass() || typeBinding.isInterface() || typeBinding.isEnum() || typeBinding.isAnnotation() || typeBinding.isNullType() || typeBinding.isRawType()) {
        return type(typeNameDecider.useSimplestPossibleName(typeBinding));
    } else if (typeBinding.isArray()) {
        return ast.newArrayType(toType(typeBinding.getElementType(), typeNameDecider));
    } else if (typeBinding.isWildcardType()) {
        final WildcardType type = ast.newWildcardType();
        if (typeBinding.getBound() != null) {
            type.setBound(toType(typeBinding.getBound(), typeNameDecider), typeBinding.isUpperbound());
        }
        return type;
    } else if (typeBinding.isTypeVariable()) {
        return type(typeBinding.getName());
    } else if (typeBinding.isCapture()) {
        if (typeBinding.getTypeBounds().length > 1) {
            throw new NotImplementedException(null, "because it violates the javadoc of `ITypeBinding.getTypeBounds()`: " + "\"Note that per construction, it can only contain one class or array type, " + "at most, and then it is located in first position.\"");
        }
        return toType(typeBinding.getWildcard(), typeNameDecider);
    }
    throw new NotImplementedException(null, " for the type binding '" + typeBinding + "'");
}
Also used : ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) WildcardType(org.eclipse.jdt.core.dom.WildcardType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) WildcardType(org.eclipse.jdt.core.dom.WildcardType) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) NotImplementedException(org.autorefactor.util.NotImplementedException) IllegalArgumentException(org.autorefactor.util.IllegalArgumentException)

Example 2 with WildcardType

use of org.eclipse.jdt.core.dom.WildcardType 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)

Example 3 with WildcardType

use of org.eclipse.jdt.core.dom.WildcardType in project generator by mybatis.

the class TypeStringifier method visit.

@Override
public boolean visit(WildcardType node) {
    buffer.append('?');
    Type bound = node.getBound();
    if (bound != null) {
        if (node.isUpperBound()) {
            // $NON-NLS-1$
            buffer.append(" extends ");
        } else {
            // $NON-NLS-1$
            buffer.append(" super ");
        }
        bound.accept(this);
    }
    return false;
}
Also used : NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) IntersectionType(org.eclipse.jdt.core.dom.IntersectionType) WildcardType(org.eclipse.jdt.core.dom.WildcardType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) UnionType(org.eclipse.jdt.core.dom.UnionType) 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)

Example 4 with WildcardType

use of org.eclipse.jdt.core.dom.WildcardType in project flux by eclipse.

the class ASTResolving method internalGetPossibleTypeKinds.

private static int internalGetPossibleTypeKinds(ASTNode node) {
    int kind = SimilarElementsRequestor.ALL_TYPES;
    int mask = SimilarElementsRequestor.ALL_TYPES | SimilarElementsRequestor.VOIDTYPE;
    ASTNode parent = node.getParent();
    while (parent instanceof QualifiedName) {
        if (node.getLocationInParent() == QualifiedName.QUALIFIER_PROPERTY) {
            return SimilarElementsRequestor.REF_TYPES;
        }
        node = parent;
        parent = parent.getParent();
        mask = SimilarElementsRequestor.REF_TYPES;
    }
    while (parent instanceof Type) {
        if (parent instanceof QualifiedType) {
            if (node.getLocationInParent() == QualifiedType.QUALIFIER_PROPERTY) {
                return mask & (SimilarElementsRequestor.REF_TYPES);
            }
            mask &= SimilarElementsRequestor.REF_TYPES;
        } else if (parent instanceof NameQualifiedType) {
            if (node.getLocationInParent() == NameQualifiedType.QUALIFIER_PROPERTY) {
                return mask & (SimilarElementsRequestor.REF_TYPES);
            }
            mask &= SimilarElementsRequestor.REF_TYPES;
        } else if (parent instanceof ParameterizedType) {
            if (node.getLocationInParent() == ParameterizedType.TYPE_ARGUMENTS_PROPERTY) {
                return mask & SimilarElementsRequestor.REF_TYPES_AND_VAR;
            }
            mask &= SimilarElementsRequestor.CLASSES | SimilarElementsRequestor.INTERFACES;
        } else if (parent instanceof WildcardType) {
            if (node.getLocationInParent() == WildcardType.BOUND_PROPERTY) {
                return mask & SimilarElementsRequestor.REF_TYPES_AND_VAR;
            }
        }
        node = parent;
        parent = parent.getParent();
    }
    switch(parent.getNodeType()) {
        case ASTNode.TYPE_DECLARATION:
            if (node.getLocationInParent() == TypeDeclaration.SUPER_INTERFACE_TYPES_PROPERTY) {
                kind = SimilarElementsRequestor.INTERFACES;
            } else if (node.getLocationInParent() == TypeDeclaration.SUPERCLASS_TYPE_PROPERTY) {
                kind = SimilarElementsRequestor.CLASSES;
            }
            break;
        case ASTNode.ENUM_DECLARATION:
            kind = SimilarElementsRequestor.INTERFACES;
            break;
        case ASTNode.METHOD_DECLARATION:
            if (node.getLocationInParent() == MethodDeclaration.THROWN_EXCEPTION_TYPES_PROPERTY) {
                kind = SimilarElementsRequestor.CLASSES;
            } else if (node.getLocationInParent() == MethodDeclaration.RETURN_TYPE2_PROPERTY) {
                kind = SimilarElementsRequestor.ALL_TYPES | SimilarElementsRequestor.VOIDTYPE;
            }
            break;
        case ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION:
            kind = SimilarElementsRequestor.PRIMITIVETYPES | SimilarElementsRequestor.ANNOTATIONS | SimilarElementsRequestor.ENUMS;
            break;
        case ASTNode.INSTANCEOF_EXPRESSION:
            kind = SimilarElementsRequestor.REF_TYPES;
            break;
        case ASTNode.THROW_STATEMENT:
            kind = SimilarElementsRequestor.CLASSES;
            break;
        case ASTNode.CLASS_INSTANCE_CREATION:
            if (((ClassInstanceCreation) parent).getAnonymousClassDeclaration() == null) {
                kind = SimilarElementsRequestor.CLASSES;
            } else {
                kind = SimilarElementsRequestor.CLASSES | SimilarElementsRequestor.INTERFACES;
            }
            break;
        case ASTNode.SINGLE_VARIABLE_DECLARATION:
            int superParent = parent.getParent().getNodeType();
            if (superParent == ASTNode.CATCH_CLAUSE) {
                kind = SimilarElementsRequestor.CLASSES;
            } else if (superParent == ASTNode.ENHANCED_FOR_STATEMENT) {
                kind = SimilarElementsRequestor.REF_TYPES;
            }
            break;
        case ASTNode.TAG_ELEMENT:
            kind = SimilarElementsRequestor.REF_TYPES;
            break;
        case ASTNode.MARKER_ANNOTATION:
        case ASTNode.SINGLE_MEMBER_ANNOTATION:
        case ASTNode.NORMAL_ANNOTATION:
            kind = SimilarElementsRequestor.ANNOTATIONS;
            break;
        case ASTNode.TYPE_PARAMETER:
            if (((TypeParameter) parent).typeBounds().indexOf(node) > 0) {
                kind = SimilarElementsRequestor.INTERFACES;
            } else {
                kind = SimilarElementsRequestor.REF_TYPES_AND_VAR;
            }
            break;
        case ASTNode.TYPE_LITERAL:
            kind = SimilarElementsRequestor.REF_TYPES;
            break;
        default:
    }
    return kind & mask;
}
Also used : ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) WildcardType(org.eclipse.jdt.core.dom.WildcardType) QualifiedType(org.eclipse.jdt.core.dom.QualifiedType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) WildcardType(org.eclipse.jdt.core.dom.WildcardType) TypeParameter(org.eclipse.jdt.core.dom.TypeParameter) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) QualifiedType(org.eclipse.jdt.core.dom.QualifiedType) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType)

Example 5 with WildcardType

use of org.eclipse.jdt.core.dom.WildcardType in project AutoRefactor by JnRouvignac.

the class ASTNodeFactory method toType.

/**
 * Converts a type binding into a type.
 *
 * @param typeBinding     the type binding to convert
 * @param typeNameDecider decides on how the type should be referenced (simple
 *                        name or qualified name)
 * @return a new type
 */
public Type toType(final ITypeBinding typeBinding, final TypeNameDecider typeNameDecider) {
    if (typeBinding == null) {
        // $NON-NLS-1$
        throw new IllegalArgumentException(null, "typeBinding cannot be null");
    }
    if (typeBinding.isParameterizedType()) {
        ParameterizedType type = ast.newParameterizedType(toType(typeBinding.getErasure(), typeNameDecider));
        List<Type> typeArgs = type.typeArguments();
        for (ITypeBinding typeArg : typeBinding.getTypeArguments()) {
            typeArgs.add(toType(typeArg, typeNameDecider));
        }
        return type;
    }
    if (typeBinding.isPrimitive()) {
        return type(typeBinding.getName());
    }
    if (typeBinding.isClass() || typeBinding.isInterface() || typeBinding.isEnum() || typeBinding.isAnnotation() || typeBinding.isNullType() || typeBinding.isRawType()) {
        return type(typeNameDecider.useSimplestPossibleName(typeBinding));
    }
    if (typeBinding.isArray()) {
        return ast.newArrayType(toType(typeBinding.getElementType(), typeNameDecider), typeBinding.getDimensions());
    }
    if (typeBinding.isWildcardType()) {
        WildcardType type = ast.newWildcardType();
        if (typeBinding.getBound() != null) {
            type.setBound(toType(typeBinding.getBound(), typeNameDecider), typeBinding.isUpperbound());
        }
        return type;
    }
    if (typeBinding.isTypeVariable()) {
        return type(typeBinding.getName());
    }
    if (typeBinding.isCapture()) {
        if (typeBinding.getTypeBounds().length > 1) {
            throw new NotImplementedException(null, // $NON-NLS-1$
            "because it violates the javadoc of `ITypeBinding.getTypeBounds()`: " + // $NON-NLS-1$
            "\"Note that per construction, it can only contain one class or array type, " + // $NON-NLS-1$
            "at most, and then it is located in first position.\"");
        }
        return toType(typeBinding.getWildcard(), typeNameDecider);
    }
    // $NON-NLS-1$ //$NON-NLS-2$
    throw new NotImplementedException(null, " for the type binding '" + typeBinding + "'");
}
Also used : ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) WildcardType(org.eclipse.jdt.core.dom.WildcardType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) WildcardType(org.eclipse.jdt.core.dom.WildcardType) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) NotImplementedException(org.autorefactor.util.NotImplementedException) IllegalArgumentException(org.autorefactor.util.IllegalArgumentException)

Aggregations

ArrayType (org.eclipse.jdt.core.dom.ArrayType)6 ParameterizedType (org.eclipse.jdt.core.dom.ParameterizedType)6 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)6 SimpleType (org.eclipse.jdt.core.dom.SimpleType)6 Type (org.eclipse.jdt.core.dom.Type)6 WildcardType (org.eclipse.jdt.core.dom.WildcardType)6 QualifiedType (org.eclipse.jdt.core.dom.QualifiedType)4 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)3 IllegalArgumentException (org.autorefactor.util.IllegalArgumentException)2 NotImplementedException (org.autorefactor.util.NotImplementedException)2 NameQualifiedType (org.eclipse.jdt.core.dom.NameQualifiedType)2 Iterator (java.util.Iterator)1 JavaModelException (org.eclipse.jdt.core.JavaModelException)1 ASTNode (org.eclipse.jdt.core.dom.ASTNode)1 IntersectionType (org.eclipse.jdt.core.dom.IntersectionType)1 QualifiedName (org.eclipse.jdt.core.dom.QualifiedName)1 TypeParameter (org.eclipse.jdt.core.dom.TypeParameter)1 UnionType (org.eclipse.jdt.core.dom.UnionType)1 BinaryType (org.eclipse.jdt.internal.core.BinaryType)1 SourceType (org.eclipse.jdt.internal.core.SourceType)1