Search in sources :

Example 6 with ArrayType

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

the class StubUtility method getVariableNameSuggestions.

private static String[] getVariableNameSuggestions(int variableKind, IJavaProject project, Type expectedType, Collection<String> excluded, boolean evaluateDefault) {
    int dim = 0;
    if (expectedType.isArrayType()) {
        ArrayType arrayType = (ArrayType) expectedType;
        dim = arrayType.getDimensions();
        expectedType = arrayType.getElementType();
    }
    if (expectedType.isParameterizedType()) {
        expectedType = ((ParameterizedType) expectedType).getType();
    }
    String typeName = ASTNodes.getTypeName(expectedType);
    if (typeName.length() > 0) {
        return getVariableNameSuggestions(variableKind, project, typeName, dim, excluded, evaluateDefault);
    }
    return EMPTY;
}
Also used : ArrayType(org.eclipse.jdt.core.dom.ArrayType)

Example 7 with ArrayType

use of org.eclipse.jdt.core.dom.ArrayType in project eclipse.jdt.ls 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) {
// return createParameters(project, imports, context, ast, binding, paramNames, decl, null);
// }
private static List<SingleVariableDeclaration> createParameters(IJavaProject project, ImportRewrite imports, ImportRewriteContext context, AST ast, IMethodBinding binding, String[] paramNames, MethodDeclaration decl, IAnnotationBinding defaultNullness) {
    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];
        type = replaceWildcardsAndCaptures(type);
        if (!is50OrHigher) {
            type = type.getErasure();
            var.setType(imports.addImport(type, ast, context, TypeLocation.PARAMETER));
        } 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<>();
                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, TypeLocation.PARAMETER));
        }
        var.setName(ast.newSimpleName(paramNames[i]));
        IAnnotationBinding[] annotations = binding.getParameterAnnotations(i);
        for (IAnnotationBinding annotation : annotations) {
            if (StubUtility2.isCopyOnInheritAnnotation(annotation.getAnnotationType(), project, defaultNullness)) {
                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 8 with ArrayType

use of org.eclipse.jdt.core.dom.ArrayType in project eclipse.jdt.ls 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) AnnotatableType(org.eclipse.jdt.core.dom.AnnotatableType) 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 9 with ArrayType

use of org.eclipse.jdt.core.dom.ArrayType in project eclipse.jdt.ls 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 10 with ArrayType

use of org.eclipse.jdt.core.dom.ArrayType in project eclipse-cs by checkstyle.

the class ArrayTypeStyleQuickfix method handleGetCorrectingASTVisitor.

/**
 * {@inheritDoc}
 */
@Override
protected ASTVisitor handleGetCorrectingASTVisitor(final IRegion lineInfo, final int markerStartOffset) {
    return new ASTVisitor() {

        @Override
        public boolean visit(VariableDeclarationStatement node) {
            if (containsPosition(node, markerStartOffset)) {
                if (isCStyle(node.fragments())) {
                    int dimensions = 0;
                    List<?> fragments = node.fragments();
                    for (int i = 0, size = fragments.size(); i < size; i++) {
                        VariableDeclaration decl = (VariableDeclaration) fragments.get(i);
                        if (decl.getExtraDimensions() > dimensions) {
                            dimensions = decl.getExtraDimensions();
                        }
                        decl.setExtraDimensions(0);
                    }
                    // wrap current type into ArrayType
                    ArrayType arrayType = createArrayType(node.getType(), dimensions);
                    node.setType(arrayType);
                } else if (isJavaStyle(node.getType())) {
                    int dimensions = ((ArrayType) node.getType()).getDimensions();
                    List<?> fragments = node.fragments();
                    for (int i = 0, size = fragments.size(); i < size; i++) {
                        VariableDeclaration decl = (VariableDeclaration) fragments.get(i);
                        decl.setExtraDimensions(dimensions);
                    }
                    Type elementType = (Type) ASTNode.copySubtree(node.getAST(), ((ArrayType) node.getType()).getElementType());
                    node.setType(elementType);
                }
            }
            return true;
        }

        @Override
        public boolean visit(SingleVariableDeclaration node) {
            if (containsPosition(node, markerStartOffset)) {
                if (isCStyle(node)) {
                    // wrap the existing type into an array type
                    node.setType(createArrayType(node.getType(), node.getExtraDimensions()));
                    node.setExtraDimensions(0);
                } else if (isJavaStyle(node.getType())) {
                    ArrayType arrayType = (ArrayType) node.getType();
                    Type elementType = (Type) ASTNode.copySubtree(node.getAST(), arrayType.getElementType());
                    node.setType(elementType);
                    node.setExtraDimensions(arrayType.getDimensions());
                }
            }
            return true;
        }

        @Override
        public boolean visit(FieldDeclaration node) {
            if (containsPosition(node, markerStartOffset)) {
                if (isCStyle(node.fragments())) {
                    int dimensions = 0;
                    List<?> fragments = node.fragments();
                    for (int i = 0, size = fragments.size(); i < size; i++) {
                        VariableDeclaration decl = (VariableDeclaration) fragments.get(i);
                        if (decl.getExtraDimensions() > dimensions) {
                            dimensions = decl.getExtraDimensions();
                        }
                        decl.setExtraDimensions(0);
                    }
                    // wrap current type into ArrayType
                    ArrayType arrayType = createArrayType(node.getType(), dimensions);
                    node.setType(arrayType);
                } else if (isJavaStyle(node.getType())) {
                    int dimensions = ((ArrayType) node.getType()).getDimensions();
                    List<?> fragments = node.fragments();
                    for (int i = 0, size = fragments.size(); i < size; i++) {
                        VariableDeclaration decl = (VariableDeclaration) fragments.get(i);
                        decl.setExtraDimensions(dimensions);
                    }
                    Type elementType = (Type) ASTNode.copySubtree(node.getAST(), ((ArrayType) node.getType()).getElementType());
                    node.setType(elementType);
                }
            }
            return true;
        }

        private boolean isJavaStyle(Type type) {
            return type instanceof ArrayType;
        }

        private boolean isCStyle(VariableDeclaration decl) {
            return decl.getExtraDimensions() > 0;
        }

        private boolean isCStyle(List<?> fragments) {
            Iterator<?> it = fragments.iterator();
            while (it.hasNext()) {
                VariableDeclaration decl = (VariableDeclaration) it.next();
                if (isCStyle(decl)) {
                    return true;
                }
            }
            return false;
        }

        private ArrayType createArrayType(Type componentType, int dimensions) {
            Type type = (Type) ASTNode.copySubtree(componentType.getAST(), componentType);
            ArrayType arrayType = componentType.getAST().newArrayType(type, dimensions);
            return arrayType;
        }
    };
}
Also used : ArrayType(org.eclipse.jdt.core.dom.ArrayType) Type(org.eclipse.jdt.core.dom.Type) ArrayType(org.eclipse.jdt.core.dom.ArrayType) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) List(java.util.List) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor)

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