Search in sources :

Example 11 with ParameterizedType

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

the class IntroduceFactoryRefactoring method setCtorTypeArguments.

/**
	 * Sets the type being instantiated in the given constructor call, including
     * specifying any necessary type arguments.
	 * @param newCtorCall the constructor call to modify
	 * @param ctorTypeName the simple name of the type being instantiated
	 * @param ctorOwnerTypeParameters the formal type parameters of the type being
	 * instantiated
	 * @param ast utility object used to create AST nodes
	 */
private void setCtorTypeArguments(ClassInstanceCreation newCtorCall, String ctorTypeName, ITypeBinding[] ctorOwnerTypeParameters, AST ast) {
    if (// easy, just a simple type
    ctorOwnerTypeParameters.length == 0)
        newCtorCall.setType(ASTNodeFactory.newType(ast, ctorTypeName));
    else {
        Type baseType = ast.newSimpleType(ast.newSimpleName(ctorTypeName));
        ParameterizedType newInstantiatedType = ast.newParameterizedType(baseType);
        List<Type> newInstTypeArgs = newInstantiatedType.typeArguments();
        for (int i = 0; i < ctorOwnerTypeParameters.length; i++) {
            Type typeArg = ASTNodeFactory.newType(ast, ctorOwnerTypeParameters[i].getName());
            newInstTypeArgs.add(typeArg);
        }
        newCtorCall.setType(newInstantiatedType);
    }
}
Also used : ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) IType(org.eclipse.jdt.core.IType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) Type(org.eclipse.jdt.core.dom.Type)

Example 12 with ParameterizedType

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

the class IntroduceFactoryRefactoring method setMethodReturnType.

/**
	 * Sets the return type of the factory method, including any necessary type
	 * arguments. E.g., for constructor <code>Foo()</code> in <code>Foo&lt;T&gt;</code>,
	 * the factory method defines a method type parameter <code>&lt;T&gt;</code> and
	 * returns a <code>Foo&lt;T&gt;</code>.
	 * @param newMethod the method whose return type is to be set
	 * @param retTypeName the simple name of the return type (without type parameters)
	 * @param ctorOwnerTypeParameters the formal type parameters of the type that the
	 * factory method instantiates (whose constructor is being encapsulated)
	 * @param ast utility object used to create AST nodes
	 */
private void setMethodReturnType(MethodDeclaration newMethod, String retTypeName, ITypeBinding[] ctorOwnerTypeParameters, AST ast) {
    if (ctorOwnerTypeParameters.length == 0)
        newMethod.setReturnType2(ast.newSimpleType(ast.newSimpleName(retTypeName)));
    else {
        Type baseType = ast.newSimpleType(ast.newSimpleName(retTypeName));
        ParameterizedType newRetType = ast.newParameterizedType(baseType);
        List<Type> newRetTypeArgs = newRetType.typeArguments();
        for (int i = 0; i < ctorOwnerTypeParameters.length; i++) {
            Type retTypeArg = ASTNodeFactory.newType(ast, ctorOwnerTypeParameters[i].getName());
            newRetTypeArgs.add(retTypeArg);
        }
        newMethod.setReturnType2(newRetType);
    }
}
Also used : ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) IType(org.eclipse.jdt.core.IType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) Type(org.eclipse.jdt.core.dom.Type)

Example 13 with ParameterizedType

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

the class TypeArgumentMismatchSubProcessor method removeMismatchedArguments.

//	public static void getTypeParameterMismatchProposals(IInvocationContext context, IProblemLocation problem, Collection proposals) {
//	CompilationUnit astRoot= context.getASTRoot();
//	ASTNode selectedNode= problem.getCoveredNode(astRoot);
//	if (!(selectedNode instanceof SimpleName)) {
//	return;
//	}
//	ASTNode normalizedNode= ASTNodes.getNormalizedNode(selectedNode);
//	if (!(normalizedNode instanceof ParameterizedType)) {
//	return;
//	}
//	// waiting for result of https://bugs.eclipse.org/bugs/show_bug.cgi?id=81544
//	}
public static void removeMismatchedArguments(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
    ICompilationUnit cu = context.getCompilationUnit();
    ASTNode selectedNode = problem.getCoveredNode(context.getASTRoot());
    if (!(selectedNode instanceof SimpleName)) {
        return;
    }
    ASTNode normalizedNode = ASTNodes.getNormalizedNode(selectedNode);
    if (normalizedNode instanceof ParameterizedType) {
        ASTRewrite rewrite = ASTRewrite.create(normalizedNode.getAST());
        ParameterizedType pt = (ParameterizedType) normalizedNode;
        ASTNode mt = rewrite.createMoveTarget(pt.getType());
        rewrite.replace(pt, mt, null);
        String label = CorrectionMessages.TypeArgumentMismatchSubProcessor_removeTypeArguments;
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
        ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.REMOVE_TYPE_ARGUMENTS, image);
        proposals.add(proposal);
    }
}
Also used : ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Image(org.eclipse.swt.graphics.Image)

Example 14 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 15 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)

Aggregations

ParameterizedType (org.eclipse.jdt.core.dom.ParameterizedType)27 Type (org.eclipse.jdt.core.dom.Type)21 SimpleType (org.eclipse.jdt.core.dom.SimpleType)13 ArrayType (org.eclipse.jdt.core.dom.ArrayType)12 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)10 IType (org.eclipse.jdt.core.IType)9 ASTNode (org.eclipse.jdt.core.dom.ASTNode)9 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 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 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)4 UnionType (org.eclipse.jdt.core.dom.UnionType)4 AST (org.eclipse.jdt.core.dom.AST)3 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)3 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)3