Search in sources :

Example 21 with ParameterizedType

use of org.eclipse.jdt.core.dom.ParameterizedType in project che by eclipse.

the class ASTNodeFactory method newCreationType.

public static Type newCreationType(AST ast, ITypeBinding typeBinding, ImportRewrite importRewrite, ImportRewriteContext importContext) {
    if (typeBinding.isParameterizedType()) {
        Type baseType = newCreationType(ast, typeBinding.getTypeDeclaration(), importRewrite, importContext);
        ParameterizedType parameterizedType = ast.newParameterizedType(baseType);
        for (ITypeBinding typeArgument : typeBinding.getTypeArguments()) {
            parameterizedType.typeArguments().add(newCreationType(ast, typeArgument, importRewrite, importContext));
        }
        return parameterizedType;
    } else if (typeBinding.isParameterizedType()) {
        Type elementType = newCreationType(ast, typeBinding.getElementType(), importRewrite, importContext);
        ArrayType arrayType = ast.newArrayType(elementType, 0);
        while (typeBinding.isArray()) {
            Dimension dimension = ast.newDimension();
            IAnnotationBinding[] typeAnnotations = typeBinding.getTypeAnnotations();
            for (IAnnotationBinding typeAnnotation : typeAnnotations) {
                dimension.annotations().add(importRewrite.addAnnotation(typeAnnotation, ast, importContext));
            }
            arrayType.dimensions().add(dimension);
            typeBinding = typeBinding.getComponentType();
        }
        return arrayType;
    } else if (typeBinding.isWildcardType()) {
        ITypeBinding bound = typeBinding.getBound();
        typeBinding = (bound != null) ? bound : typeBinding.getErasure();
        return newCreationType(ast, typeBinding, importRewrite, importContext);
    } else {
        return importRewrite.addImport(typeBinding, ast, importContext);
    }
}
Also used : ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) Type(org.eclipse.jdt.core.dom.Type) UnionType(org.eclipse.jdt.core.dom.UnionType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) IAnnotationBinding(org.eclipse.jdt.core.dom.IAnnotationBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) Dimension(org.eclipse.jdt.core.dom.Dimension)

Example 22 with ParameterizedType

use of org.eclipse.jdt.core.dom.ParameterizedType in project che by eclipse.

the class InferTypeArgumentsRefactoring method rewriteTypeVariable.

private static ParameterizedType rewriteTypeVariable(TypeVariable2 typeCv, CompilationUnitRewrite rewrite, InferTypeArgumentsTCModel tCModel, boolean leaveUnconstraindRaw, SimpleType[] types) {
    ASTNode node = typeCv.getRange().getNode(rewrite.getRoot());
    if (node instanceof Name && node.getParent() instanceof Type) {
        Type originalType = (Type) node.getParent();
        if (types != null && !has(types, originalType))
            return null;
        // Must rewrite all type arguments in one batch. Do the rewrite when the first one is encountered; skip the others.
        Object rewritten = originalType.getProperty(REWRITTEN);
        if (rewritten == REWRITTEN)
            return null;
        originalType.setProperty(REWRITTEN, REWRITTEN);
        ArrayList<CollectionElementVariable2> typeArgumentCvs = getTypeArgumentCvs(typeCv, tCModel);
        Type[] typeArguments = getTypeArguments(originalType, typeArgumentCvs, rewrite, tCModel, leaveUnconstraindRaw);
        if (typeArguments == null)
            return null;
        Type movingType = (Type) rewrite.getASTRewrite().createMoveTarget(originalType);
        ParameterizedType newType = rewrite.getAST().newParameterizedType(movingType);
        for (int i = 0; i < typeArguments.length; i++) {
            newType.typeArguments().add(typeArguments[i]);
        }
        rewrite.getASTRewrite().replace(originalType, newType, rewrite.createGroupDescription(RefactoringCoreMessages.InferTypeArgumentsRefactoring_addTypeArguments));
        return newType;
    } else {
        //TODO: other node types?
        return null;
    }
}
Also used : ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) TType(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.TType) CollectionElementVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.CollectionElementVariable2) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Name(org.eclipse.jdt.core.dom.Name)

Example 23 with ParameterizedType

use of org.eclipse.jdt.core.dom.ParameterizedType in project che by eclipse.

the class InferTypeArgumentsRefactoring method getTypeArguments.

/**
	 * @param baseType the base type
	 * @param typeArgumentCvs type argument constraint variables
	 * @param rewrite the cu rewrite
	 * @param tCModel the type constraints model
	 * @param leaveUnconstraindRaw <code>true</code> to keep unconstrained type references raw,
	 *            <code>false</code> to infer <code>&lt;?&gt;</code> if possible
	 * @return the new type arguments, or <code>null</code> iff an argument could not be inferred
	 */
private static Type[] getTypeArguments(Type baseType, ArrayList<CollectionElementVariable2> typeArgumentCvs, CompilationUnitRewrite rewrite, InferTypeArgumentsTCModel tCModel, boolean leaveUnconstraindRaw) {
    if (typeArgumentCvs.size() == 0)
        return null;
    Type[] typeArguments = new Type[typeArgumentCvs.size()];
    for (int i = 0; i < typeArgumentCvs.size(); i++) {
        CollectionElementVariable2 elementCv = typeArgumentCvs.get(i);
        Type typeArgument;
        TType chosenType = InferTypeArgumentsConstraintsSolver.getChosenType(elementCv);
        if (chosenType != null) {
            if (chosenType.isWildcardType() && !unboundedWildcardAllowed(baseType))
                // can't e.g. write "new ArrayList<?>()".
                return null;
            if (// workaround for bug 99124
            chosenType.isParameterizedType())
                chosenType = chosenType.getTypeDeclaration();
            BindingKey bindingKey = new BindingKey(chosenType.getBindingKey());
            typeArgument = rewrite.getImportRewrite().addImportFromSignature(bindingKey.toSignature(), rewrite.getAST());
            ArrayList<CollectionElementVariable2> nestedTypeArgumentCvs = getTypeArgumentCvs(elementCv, tCModel);
            //recursion
            Type[] nestedTypeArguments = getTypeArguments(typeArgument, nestedTypeArgumentCvs, rewrite, tCModel, leaveUnconstraindRaw);
            if (nestedTypeArguments != null) {
                ParameterizedType parameterizedType = rewrite.getAST().newParameterizedType(typeArgument);
                for (int j = 0; j < nestedTypeArguments.length; j++) parameterizedType.typeArguments().add(nestedTypeArguments[j]);
                typeArgument = parameterizedType;
            }
        } else {
            // couldn't infer an element type (no constraints)
            if (leaveUnconstraindRaw) {
                // every guess could be wrong => leave the whole thing raw
                return null;
            } else {
                if (unboundedWildcardAllowed(baseType)) {
                    typeArgument = rewrite.getAST().newWildcardType();
                } else {
                    //$NON-NLS-1$
                    String object = rewrite.getImportRewrite().addImport("java.lang.Object");
                    typeArgument = (Type) rewrite.getASTRewrite().createStringPlaceholder(object, ASTNode.SIMPLE_TYPE);
                }
            }
        //				ASTNode baseTypeParent= baseType.getParent();
        //				if (baseTypeParent instanceof ClassInstanceCreation) {
        //					//No ? allowed. Take java.lang.Object.
        //					typeArgument= rewrite.getAST().newSimpleType(rewrite.getAST().newName(rewrite.getImportRewrite().addImport("java.lang.Object"))); //$NON-NLS-1$
        //				} else if (baseTypeParent instanceof ArrayCreation || baseTypeParent instanceof InstanceofExpression) {
        //					//Only ? allowed.
        //					typeArgument= rewrite.getAST().newWildcardType();
        //				} else {
        //					//E.g. field type: can put anything. Choosing ? in order to be most constraining.
        //					typeArgument= rewrite.getAST().newWildcardType();
        //				}
        }
        typeArguments[i] = typeArgument;
    }
    return typeArguments;
}
Also used : ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) TType(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.TType) CollectionElementVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.CollectionElementVariable2) BindingKey(org.eclipse.jdt.core.BindingKey) TType(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.TType)

Example 24 with ParameterizedType

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

the class ASTNodes method getQualifiedTypeName.

/**
	 * Returns the (potentially qualified) name of a type, followed by array dimensions.
	 * Skips type arguments and type annotations.
	 * 
	 * @param type a type that has a name
	 * @return the name, followed by array dimensions
	 * @since 3.10
	 */
public static String getQualifiedTypeName(Type type) {
    final StringBuffer buffer = new StringBuffer();
    ASTVisitor visitor = new ASTVisitor() {

        @Override
        public boolean visit(SimpleType node) {
            buffer.append(node.getName().getFullyQualifiedName());
            return false;
        }

        @Override
        public boolean visit(QualifiedType node) {
            node.getQualifier().accept(this);
            buffer.append('.');
            buffer.append(node.getName().getIdentifier());
            return false;
        }

        @Override
        public boolean visit(NameQualifiedType node) {
            buffer.append(node.getQualifier().getFullyQualifiedName());
            buffer.append('.');
            buffer.append(node.getName().getIdentifier());
            return false;
        }

        @Override
        public boolean visit(ParameterizedType node) {
            node.getType().accept(this);
            return false;
        }

        @Override
        public void endVisit(ArrayType node) {
            for (int i = 0; i < node.dimensions().size(); i++) {
                //$NON-NLS-1$
                buffer.append("[]");
            }
        }
    };
    type.accept(visitor);
    return buffer.toString();
}
Also used : ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) QualifiedType(org.eclipse.jdt.core.dom.QualifiedType) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor)

Example 25 with ParameterizedType

use of org.eclipse.jdt.core.dom.ParameterizedType in project che by eclipse.

the class InferTypeArgumentsRefactoring method inferArguments.

public static ParameterizedType[] inferArguments(SimpleType[] types, InferTypeArgumentsUpdate update, InferTypeArgumentsTCModel model, CompilationUnitRewrite rewrite) {
    for (int i = 0; i < types.length; i++) {
        types[i].setProperty(REWRITTEN, null);
    }
    List<ParameterizedType> result = new ArrayList<ParameterizedType>();
    HashMap<ICompilationUnit, CuUpdate> updates = update.getUpdates();
    Set<Entry<ICompilationUnit, CuUpdate>> entrySet = updates.entrySet();
    for (Iterator<Entry<ICompilationUnit, CuUpdate>> iter = entrySet.iterator(); iter.hasNext(); ) {
        Entry<ICompilationUnit, CuUpdate> entry = iter.next();
        rewrite.setResolveBindings(false);
        CuUpdate cuUpdate = entry.getValue();
        for (Iterator<CollectionElementVariable2> cvIter = cuUpdate.getDeclarations().iterator(); cvIter.hasNext(); ) {
            ConstraintVariable2 cv = cvIter.next();
            ParameterizedType newNode = rewriteConstraintVariable(cv, rewrite, model, false, types);
            if (newNode != null)
                result.add(newNode);
        }
    }
    return result.toArray(new ParameterizedType[result.size()]);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CollectionElementVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.CollectionElementVariable2) ArrayList(java.util.ArrayList) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) Entry(java.util.Map.Entry) CuUpdate(org.eclipse.jdt.internal.corext.refactoring.generics.InferTypeArgumentsUpdate.CuUpdate) ConstraintVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ConstraintVariable2)

Aggregations

ParameterizedType (org.eclipse.jdt.core.dom.ParameterizedType)37 Type (org.eclipse.jdt.core.dom.Type)23 SimpleType (org.eclipse.jdt.core.dom.SimpleType)15 ArrayType (org.eclipse.jdt.core.dom.ArrayType)14 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)12 IType (org.eclipse.jdt.core.IType)9 ASTNode (org.eclipse.jdt.core.dom.ASTNode)9 WildcardType (java.lang.reflect.WildcardType)8 org.eclipse.jdt.core.dom (org.eclipse.jdt.core.dom)8 NameQualifiedType (org.eclipse.jdt.core.dom.NameQualifiedType)7 QualifiedType (org.eclipse.jdt.core.dom.QualifiedType)7 SimpleName (org.eclipse.jdt.core.dom.SimpleName)6 ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)5 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)5 Name (org.eclipse.jdt.core.dom.Name)5 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)4 ASTVisitor (org.eclipse.jdt.core.dom.ASTVisitor)4 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)4 UnionType (org.eclipse.jdt.core.dom.UnionType)4 WildcardType (org.eclipse.jdt.core.dom.WildcardType)4