Search in sources :

Example 21 with ArrayType

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

the class ASTNodeFactory method newType.

/**
	 * Returns the new type node corresponding to the type of the given declaration
	 * including the extra dimensions. If the type is a {@link UnionType}, use the LUB type.
	 * If the <code>importRewrite</code> is <code>null</code>, the type may be fully-qualified.
	 * 
	 * @param ast The AST to create the resulting type with.
	 * @param declaration The variable declaration to get the type from
	 * @param importRewrite the import rewrite to use, or <code>null</code>
	 * @param context the import rewrite context, or <code>null</code>
	 * @return a new type node created with the given AST.
	 * 
	 * @since 3.7.1
	 */
public static Type newType(AST ast, VariableDeclaration declaration, ImportRewrite importRewrite, ImportRewriteContext context) {
    if (declaration instanceof VariableDeclarationFragment && declaration.getParent() instanceof LambdaExpression) {
        return newType((LambdaExpression) declaration.getParent(), (VariableDeclarationFragment) declaration, ast, importRewrite, context);
    }
    Type type = ASTNodes.getType(declaration);
    if (declaration instanceof SingleVariableDeclaration) {
        Type type2 = ((SingleVariableDeclaration) declaration).getType();
        if (type2 instanceof UnionType) {
            ITypeBinding typeBinding = type2.resolveBinding();
            if (typeBinding != null) {
                if (importRewrite != null) {
                    type = importRewrite.addImport(typeBinding, ast, context);
                    return type;
                } else {
                    String qualifiedName = typeBinding.getQualifiedName();
                    if (qualifiedName.length() > 0) {
                        type = ast.newSimpleType(ast.newName(qualifiedName));
                        return type;
                    }
                }
            }
            // XXX: fallback for intersection types or unresolved types: take first type of union
            type = (Type) ((UnionType) type2).types().get(0);
            return type;
        }
    }
    type = (Type) ASTNode.copySubtree(ast, type);
    List<Dimension> extraDimensions = declaration.extraDimensions();
    if (!extraDimensions.isEmpty()) {
        ArrayType arrayType;
        if (type instanceof ArrayType) {
            arrayType = (ArrayType) type;
        } else {
            arrayType = ast.newArrayType(type, 0);
            type = arrayType;
        }
        arrayType.dimensions().addAll(ASTNode.copySubtrees(ast, extraDimensions));
    }
    return type;
}
Also used : ArrayType(org.eclipse.jdt.core.dom.ArrayType) UnionType(org.eclipse.jdt.core.dom.UnionType) 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) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) Dimension(org.eclipse.jdt.core.dom.Dimension) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression)

Example 22 with ArrayType

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

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

the class DimensionRewrite method copyTypeAndAddDimensions.

/**
	 * Creates a {@link ASTRewrite#createCopyTarget(ASTNode) copy} of <code>type</code>
	 * and adds <code>extraDimensions</code> to it.
	 * 
	 * @param type the type to copy
	 * @param extraDimensions the dimensions to add
	 * @param rewrite the ASTRewrite with which to create new nodes
	 * @return the copy target with added dimensions
	 */
public static Type copyTypeAndAddDimensions(Type type, List<Dimension> extraDimensions, ASTRewrite rewrite) {
    AST ast = rewrite.getAST();
    if (extraDimensions.isEmpty()) {
        return (Type) rewrite.createCopyTarget(type);
    }
    ArrayType result;
    if (type instanceof ArrayType) {
        ArrayType arrayType = (ArrayType) type;
        Type varElementType = (Type) rewrite.createCopyTarget(arrayType.getElementType());
        result = ast.newArrayType(varElementType, 0);
        result.dimensions().addAll(copyDimensions(extraDimensions, rewrite));
        result.dimensions().addAll(copyDimensions(arrayType.dimensions(), rewrite));
    } else {
        Type elementType = (Type) rewrite.createCopyTarget(type);
        result = ast.newArrayType(elementType, 0);
        result.dimensions().addAll(copyDimensions(extraDimensions, rewrite));
    }
    return result;
}
Also used : ArrayType(org.eclipse.jdt.core.dom.ArrayType) AST(org.eclipse.jdt.core.dom.AST) Type(org.eclipse.jdt.core.dom.Type) ArrayType(org.eclipse.jdt.core.dom.ArrayType)

Example 24 with ArrayType

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

the class StubUtility2 method createParameters.

private static List<SingleVariableDeclaration> createParameters(IJavaProject project, ImportRewrite imports, ImportRewriteContext context, AST ast, IMethodBinding binding, String[] paramNames, MethodDeclaration decl) {
    boolean is50OrHigher = JavaModelUtil.is50OrHigher(project);
    List<SingleVariableDeclaration> parameters = decl.parameters();
    ITypeBinding[] params = binding.getParameterTypes();
    if (paramNames == null || paramNames.length < params.length) {
        paramNames = StubUtility.suggestArgumentNames(project, binding);
    }
    for (int i = 0; i < params.length; i++) {
        SingleVariableDeclaration var = ast.newSingleVariableDeclaration();
        ITypeBinding type = params[i];
        if (type.isWildcardType()) {
            ITypeBinding bound = type.getBound();
            type = (bound != null) ? bound : type.getErasure();
        }
        if (!is50OrHigher) {
            type = type.getErasure();
            var.setType(imports.addImport(type, ast, context));
        } else if (binding.isVarargs() && type.isArray() && i == params.length - 1) {
            var.setVarargs(true);
            /*
				 * Varargs annotations are special.
				 * Example:
				 *     foo(@O Object @A [] @B ... arg)
				 * => @B is not an annotation on the array dimension that constitutes the vararg.
				 * It's the type annotation of the *innermost* array dimension.
				 */
            int dimensions = type.getDimensions();
            @SuppressWarnings("unchecked") List<Annotation>[] dimensionAnnotations = (List<Annotation>[]) new List<?>[dimensions];
            for (int dim = 0; dim < dimensions; dim++) {
                dimensionAnnotations[dim] = new ArrayList<Annotation>();
                for (IAnnotationBinding annotation : type.getTypeAnnotations()) {
                    dimensionAnnotations[dim].add(imports.addAnnotation(annotation, ast, context));
                }
                type = type.getComponentType();
            }
            Type elementType = imports.addImport(type, ast, context);
            if (dimensions == 1) {
                var.setType(elementType);
            } else {
                ArrayType arrayType = ast.newArrayType(elementType, dimensions - 1);
                List<Dimension> dimensionNodes = arrayType.dimensions();
                for (int dim = 0; dim < dimensions - 1; dim++) {
                    // all except the innermost dimension
                    Dimension dimension = dimensionNodes.get(dim);
                    dimension.annotations().addAll(dimensionAnnotations[dim]);
                }
                var.setType(arrayType);
            }
            List<Annotation> varargTypeAnnotations = dimensionAnnotations[dimensions - 1];
            var.varargsAnnotations().addAll(varargTypeAnnotations);
        } else {
            var.setType(imports.addImport(type, ast, context));
        }
        var.setName(ast.newSimpleName(paramNames[i]));
        IAnnotationBinding[] annotations = binding.getParameterAnnotations(i);
        for (IAnnotationBinding annotation : annotations) {
            if (StubUtility2.isCopyOnInheritAnnotation(annotation.getAnnotationType(), project))
                var.modifiers().add(imports.addAnnotation(annotation, ast, context));
        }
        parameters.add(var);
    }
    return parameters;
}
Also used : SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) ArrayList(java.util.ArrayList) Dimension(org.eclipse.jdt.core.dom.Dimension) Annotation(org.eclipse.jdt.core.dom.Annotation) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) Type(org.eclipse.jdt.core.dom.Type) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) IAnnotationBinding(org.eclipse.jdt.core.dom.IAnnotationBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) List(java.util.List) ArrayList(java.util.ArrayList)

Example 25 with ArrayType

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

the class ASTNodeFactory method newArrayType.

/**
	 * Returns an {@link ArrayType} that adds one dimension to the given type node.
	 * If the given node is already an ArrayType, then a new {@link Dimension}
	 * without annotations is inserted at the first position.
	 * 
	 * @param type the type to be wrapped
	 * @return the array type
	 * @since 3.10
	 */
public static ArrayType newArrayType(Type type) {
    if (type instanceof ArrayType) {
        Dimension dimension = type.getAST().newDimension();
        ArrayType arrayType = (ArrayType) type;
        // first dimension is outermost
        arrayType.dimensions().add(0, dimension);
        return arrayType;
    } else {
        return type.getAST().newArrayType(type);
    }
}
Also used : ArrayType(org.eclipse.jdt.core.dom.ArrayType) Dimension(org.eclipse.jdt.core.dom.Dimension)

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