Search in sources :

Example 26 with ArrayType

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

the class UnresolvedElementsSubProcessor method getTypeProposals.

public static void getTypeProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) throws CoreException {
    ICompilationUnit cu = context.getCompilationUnit();
    ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
    if (selectedNode == null) {
        return;
    }
    int kind = evauateTypeKind(selectedNode, cu.getJavaProject());
    if (kind == SimilarElementsRequestor.REF_TYPES) {
        addEnhancedForWithoutTypeProposals(cu, selectedNode, proposals);
    }
    while (selectedNode.getLocationInParent() == QualifiedName.NAME_PROPERTY) {
        selectedNode = selectedNode.getParent();
    }
    Name node = null;
    if (selectedNode instanceof SimpleType) {
        node = ((SimpleType) selectedNode).getName();
    } else if (selectedNode instanceof NameQualifiedType) {
        node = ((NameQualifiedType) selectedNode).getName();
    } else if (selectedNode instanceof ArrayType) {
        Type elementType = ((ArrayType) selectedNode).getElementType();
        if (elementType.isSimpleType()) {
            node = ((SimpleType) elementType).getName();
        } else if (elementType.isNameQualifiedType()) {
            node = ((NameQualifiedType) elementType).getName();
        } else {
            return;
        }
    } else if (selectedNode instanceof Name) {
        node = (Name) selectedNode;
    } else {
        return;
    }
    // change to similar type proposals
    addSimilarTypeProposals(kind, cu, node, IProposalRelevance.SIMILAR_TYPE, proposals);
    while (node.getParent() instanceof QualifiedName) {
        node = (Name) node.getParent();
    }
    if (selectedNode != node) {
        kind = evauateTypeKind(node, cu.getJavaProject());
    }
    if ((kind & (SimilarElementsRequestor.CLASSES | SimilarElementsRequestor.INTERFACES)) != 0) {
        // only propose annotations when there are no other suggestions
        kind &= ~SimilarElementsRequestor.ANNOTATIONS;
    }
    addNewTypeProposals(cu, node, kind, IProposalRelevance.NEW_TYPE, proposals);
    ReorgCorrectionsSubProcessor.addProjectSetupFixProposal(context, problem, node.getFullyQualifiedName(), proposals);
}
Also used : ArrayType(org.eclipse.jdt.core.dom.ArrayType) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SimpleType(org.eclipse.jdt.core.dom.SimpleType) IType(org.eclipse.jdt.core.IType) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Name(org.eclipse.jdt.core.dom.Name)

Example 27 with ArrayType

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

the class CompletionGenerator method resolveExpression3rdParty.

/**
   * Finds the type of the expression in foo.bar().a().b, this would give me the
   * type of b if it exists in return type of a(). If noCompare is true,
   * it'll return type of a()
   * @param nearestNode
   * @param astNode
   * @return
   */
public static ClassMember resolveExpression3rdParty(PreprocessedSketch ps, ASTNode nearestNode, ASTNode astNode, boolean noCompare) {
    log("Resolve 3rdParty expr-- " + getNodeAsString(astNode) + " nearest node " + getNodeAsString(nearestNode));
    if (astNode == null)
        return null;
    ClassMember scopeParent;
    SimpleType stp;
    if (astNode instanceof SimpleName) {
        ASTNode decl = findDeclaration2(((SimpleName) astNode), nearestNode);
        if (decl != null) {
            // see if locally defined
            log(getNodeAsString(astNode) + " found decl -> " + getNodeAsString(decl));
            {
                if (decl.getNodeType() == ASTNode.TYPE_DECLARATION) {
                    TypeDeclaration td = (TypeDeclaration) decl;
                    return new ClassMember(ps, td);
                }
            }
            {
                // Handle "array." x "array[1]."
                Type type = extracTypeInfo2(decl);
                if (type != null && type.isArrayType() && astNode.getParent().getNodeType() != ASTNode.ARRAY_ACCESS) {
                    // No array access, we want members of the array itself
                    Type elementType = ((ArrayType) type).getElementType();
                    // Get name of the element class
                    String name = "";
                    if (elementType.isSimpleType()) {
                        Class<?> c = findClassIfExists(ps, elementType.toString());
                        if (c != null)
                            name = c.getName();
                    } else if (elementType.isPrimitiveType()) {
                        name = ((PrimitiveType) elementType).getPrimitiveTypeCode().toString();
                    }
                    // Convert element class to array class
                    Class<?> arrayClass = getArrayClass(name, ps.classLoader);
                    return arrayClass == null ? null : new ClassMember(arrayClass);
                }
            }
            return new ClassMember(ps, extracTypeInfo(decl));
        } else {
            // or in a predefined class?
            Class<?> tehClass = findClassIfExists(ps, astNode.toString());
            if (tehClass != null) {
                return new ClassMember(tehClass);
            }
        }
        astNode = astNode.getParent();
    }
    switch(astNode.getNodeType()) {
        //TODO: Notice the redundancy in the 3 cases, you can simplify things even more.
        case ASTNode.FIELD_ACCESS:
            FieldAccess fa = (FieldAccess) astNode;
            if (fa.getExpression() == null) {
                // TODO: Check for existence of 'new' keyword. Could be a ClassInstanceCreation
                // Local code or belongs to super class
                log("FA,Not implemented.");
                return null;
            } else {
                if (fa.getExpression() instanceof SimpleName) {
                    stp = extracTypeInfo(findDeclaration2((SimpleName) fa.getExpression(), nearestNode));
                    if (stp == null) {
                        /*The type wasn't found in local code, so it might be something like
             * log(), or maybe belonging to super class, etc.
             */
                        Class<?> tehClass = findClassIfExists(ps, fa.getExpression().toString());
                        if (tehClass != null) {
                            // so look for method in this class.
                            return definedIn3rdPartyClass(ps, new ClassMember(tehClass), fa.getName().toString());
                        }
                        log("FA resolve 3rd par, Can't resolve " + fa.getExpression());
                        return null;
                    }
                    log("FA, SN Type " + getNodeAsString(stp));
                    scopeParent = definedIn3rdPartyClass(ps, stp.getName().toString(), "THIS");
                } else {
                    scopeParent = resolveExpression3rdParty(ps, nearestNode, fa.getExpression(), noCompare);
                }
                log("FA, ScopeParent " + scopeParent);
                return definedIn3rdPartyClass(ps, scopeParent, fa.getName().toString());
            }
        case ASTNode.METHOD_INVOCATION:
            MethodInvocation mi = (MethodInvocation) astNode;
            ASTNode temp = findDeclaration2(mi.getName(), nearestNode);
            if (temp instanceof MethodDeclaration) {
                // method is locally defined
                log(mi.getName() + " was found locally," + getNodeAsString(extracTypeInfo(temp)));
                {
                    // Handle "array." x "array[1]."
                    Type type = extracTypeInfo2(temp);
                    if (type != null && type.isArrayType() && astNode.getParent().getNodeType() != ASTNode.ARRAY_ACCESS) {
                        // No array access, we want members of the array itself
                        Type elementType = ((ArrayType) type).getElementType();
                        // Get name of the element class
                        String name = "";
                        if (elementType.isSimpleType()) {
                            Class<?> c = findClassIfExists(ps, elementType.toString());
                            if (c != null)
                                name = c.getName();
                        } else if (elementType.isPrimitiveType()) {
                            name = ((PrimitiveType) elementType).getPrimitiveTypeCode().toString();
                        }
                        // Convert element class to array class
                        Class<?> arrayClass = getArrayClass(name, ps.classLoader);
                        return arrayClass == null ? null : new ClassMember(arrayClass);
                    }
                }
                return new ClassMember(ps, extracTypeInfo(temp));
            }
            if (mi.getExpression() == null) {
                //        if()
                //Local code or belongs to super class
                log("MI,Not implemented.");
                return null;
            } else {
                if (mi.getExpression() instanceof SimpleName) {
                    ASTNode decl = findDeclaration2((SimpleName) mi.getExpression(), nearestNode);
                    if (decl != null) {
                        if (decl.getNodeType() == ASTNode.TYPE_DECLARATION) {
                            TypeDeclaration td = (TypeDeclaration) decl;
                            return new ClassMember(ps, td);
                        }
                        stp = extracTypeInfo(decl);
                        if (stp == null) {
                            /*The type wasn't found in local code, so it might be something like
             * System.console()., or maybe belonging to super class, etc.
             */
                            Class<?> tehClass = findClassIfExists(ps, mi.getExpression().toString());
                            if (tehClass != null) {
                                // so look for method in this class.
                                return definedIn3rdPartyClass(ps, new ClassMember(tehClass), mi.getName().toString());
                            }
                            log("MI resolve 3rd par, Can't resolve " + mi.getExpression());
                            return null;
                        }
                        log("MI, SN Type " + getNodeAsString(stp));
                        ASTNode typeDec = findDeclaration2(stp.getName(), nearestNode);
                        if (typeDec == null) {
                            log(stp.getName() + " couldn't be found locally..");
                            Class<?> tehClass = findClassIfExists(ps, stp.getName().toString());
                            if (tehClass != null) {
                                // so look for method in this class.
                                return definedIn3rdPartyClass(ps, new ClassMember(tehClass), mi.getName().toString());
                            }
                        //return new ClassMember(findClassIfExists(stp.getName().toString()));
                        }
                        //scopeParent = definedIn3rdPartyClass(stp.getName().toString(), "THIS");
                        return definedIn3rdPartyClass(ps, new ClassMember(ps, typeDec), mi.getName().toString());
                    }
                } else {
                    log("MI EXP.." + getNodeAsString(mi.getExpression()));
                    //          return null;
                    scopeParent = resolveExpression3rdParty(ps, nearestNode, mi.getExpression(), noCompare);
                    log("MI, ScopeParent " + scopeParent);
                    return definedIn3rdPartyClass(ps, scopeParent, mi.getName().toString());
                }
            }
            break;
        case ASTNode.QUALIFIED_NAME:
            QualifiedName qn = (QualifiedName) astNode;
            ASTNode temp2 = findDeclaration2(qn.getName(), nearestNode);
            if (temp2 instanceof FieldDeclaration) {
                // field is locally defined
                log(qn.getName() + " was found locally," + getNodeAsString(extracTypeInfo(temp2)));
                return new ClassMember(ps, extracTypeInfo(temp2));
            }
            if (qn.getQualifier() == null) {
                log("QN,Not implemented.");
                return null;
            } else {
                if (qn.getQualifier() instanceof SimpleName) {
                    stp = extracTypeInfo(findDeclaration2(qn.getQualifier(), nearestNode));
                    if (stp == null) {
                        /*The type wasn't found in local code, so it might be something like
             * log(), or maybe belonging to super class, etc.
             */
                        Class<?> tehClass = findClassIfExists(ps, qn.getQualifier().toString());
                        if (tehClass != null) {
                            // note how similar thing is called on line 690. Check check.
                            return definedIn3rdPartyClass(ps, new ClassMember(tehClass), qn.getName().toString());
                        }
                        log("QN resolve 3rd par, Can't resolve " + qn.getQualifier());
                        return null;
                    }
                    log("QN, SN Local Type " + getNodeAsString(stp));
                    //scopeParent = definedIn3rdPartyClass(stp.getName().toString(), "THIS");
                    ASTNode typeDec = findDeclaration2(stp.getName(), nearestNode);
                    if (typeDec == null) {
                        log(stp.getName() + " couldn't be found locally..");
                        Class<?> tehClass = findClassIfExists(ps, stp.getName().toString());
                        if (tehClass != null) {
                            // note how similar thing is called on line 690. Check check.
                            return definedIn3rdPartyClass(ps, new ClassMember(tehClass), qn.getName().toString());
                        }
                        log("QN resolve 3rd par, Can't resolve " + qn.getQualifier());
                        return null;
                    }
                    return definedIn3rdPartyClass(ps, new ClassMember(ps, typeDec), qn.getName().toString());
                } else {
                    scopeParent = resolveExpression3rdParty(ps, nearestNode, qn.getQualifier(), noCompare);
                    log("QN, ScopeParent " + scopeParent);
                    return definedIn3rdPartyClass(ps, scopeParent, qn.getName().toString());
                }
            }
        case ASTNode.ARRAY_ACCESS:
            ArrayAccess arac = (ArrayAccess) astNode;
            return resolveExpression3rdParty(ps, nearestNode, arac.getArray(), noCompare);
        default:
            log("Unaccounted type " + getNodeAsString(astNode));
            break;
    }
    return null;
}
Also used : MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) SimpleType(org.eclipse.jdt.core.dom.SimpleType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) ArrayAccess(org.eclipse.jdt.core.dom.ArrayAccess) ASTNode(org.eclipse.jdt.core.dom.ASTNode) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration)

Example 28 with ArrayType

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

the class QuickAssistProcessor method convertMethodRefernceToLambda.

/**
 * Converts and replaces the given method reference with corresponding lambda
 * expression in the given ASTRewrite.
 *
 * @param methodReference
 *            the method reference to convert
 * @param functionalMethod
 *            the non-generic functional interface method to be implemented by
 *            the lambda expression
 * @param astRoot
 *            the AST root
 * @param rewrite
 *            the ASTRewrite
 * @param linkedProposalModel
 *            to create linked proposals for lambda's parameters or
 *            <code>null</code> if linked proposals are not required
 * @param createBlockBody
 *            <code>true</code> if lambda expression's body should be a block
 *
 * @return lambda expression used to replace the method reference in the given
 *         ASTRewrite
 * @throws JavaModelException
 *             if an exception occurs while accessing the Java element
 *             corresponding to the <code>functionalMethod</code>
 */
public static LambdaExpression convertMethodRefernceToLambda(MethodReference methodReference, IMethodBinding functionalMethod, CompilationUnit astRoot, ASTRewrite rewrite, LinkedProposalModel linkedProposalModel, boolean createBlockBody) throws JavaModelException {
    AST ast = astRoot.getAST();
    LambdaExpression lambda = ast.newLambdaExpression();
    String[] lambdaParamNames = getUniqueParameterNames(methodReference, functionalMethod);
    List<VariableDeclaration> lambdaParameters = lambda.parameters();
    for (int i = 0; i < lambdaParamNames.length; i++) {
        String paramName = lambdaParamNames[i];
        VariableDeclarationFragment lambdaParameter = ast.newVariableDeclarationFragment();
        SimpleName name = ast.newSimpleName(paramName);
        lambdaParameter.setName(name);
        lambdaParameters.add(lambdaParameter);
        if (linkedProposalModel != null) {
            linkedProposalModel.getPositionGroup(name.getIdentifier(), true).addPosition(rewrite.track(name), i == 0);
        }
    }
    int noOfLambdaParameters = lambdaParamNames.length;
    lambda.setParentheses(noOfLambdaParameters != 1);
    ITypeBinding returnTypeBinding = functionalMethod.getReturnType();
    // too often null, see bug 440000, bug 440344, bug 333665
    IMethodBinding referredMethodBinding = methodReference.resolveMethodBinding();
    if (methodReference instanceof CreationReference) {
        CreationReference creationRef = (CreationReference) methodReference;
        Type type = creationRef.getType();
        if (type instanceof ArrayType) {
            ArrayCreation arrayCreation = ast.newArrayCreation();
            if (createBlockBody) {
                Block blockBody = getBlockBodyForLambda(arrayCreation, returnTypeBinding, ast);
                lambda.setBody(blockBody);
            } else {
                lambda.setBody(arrayCreation);
            }
            ArrayType arrayType = (ArrayType) type;
            Type copiedElementType = (Type) rewrite.createCopyTarget(arrayType.getElementType());
            arrayCreation.setType(ast.newArrayType(copiedElementType, arrayType.getDimensions()));
            SimpleName name = ast.newSimpleName(lambdaParamNames[0]);
            arrayCreation.dimensions().add(name);
            if (linkedProposalModel != null) {
                linkedProposalModel.getPositionGroup(name.getIdentifier(), false).addPosition(rewrite.track(name), LinkedPositionGroup.NO_STOP);
            }
        } else {
            ClassInstanceCreation cic = ast.newClassInstanceCreation();
            if (createBlockBody) {
                Block blockBody = getBlockBodyForLambda(cic, returnTypeBinding, ast);
                lambda.setBody(blockBody);
            } else {
                lambda.setBody(cic);
            }
            ITypeBinding typeBinding = type.resolveBinding();
            if (!(type instanceof ParameterizedType) && typeBinding != null && typeBinding.getTypeDeclaration().isGenericType()) {
                cic.setType(ast.newParameterizedType((Type) rewrite.createCopyTarget(type)));
            } else {
                cic.setType((Type) rewrite.createCopyTarget(type));
            }
            List<SimpleName> invocationArgs = getInvocationArguments(ast, 0, noOfLambdaParameters, lambdaParamNames);
            cic.arguments().addAll(invocationArgs);
            if (linkedProposalModel != null) {
                for (SimpleName name : invocationArgs) {
                    linkedProposalModel.getPositionGroup(name.getIdentifier(), false).addPosition(rewrite.track(name), LinkedPositionGroup.NO_STOP);
                }
            }
            cic.typeArguments().addAll(getCopiedTypeArguments(rewrite, methodReference.typeArguments()));
        }
    } else if (referredMethodBinding != null && Modifier.isStatic(referredMethodBinding.getModifiers())) {
        MethodInvocation methodInvocation = ast.newMethodInvocation();
        if (createBlockBody) {
            Block blockBody = getBlockBodyForLambda(methodInvocation, returnTypeBinding, ast);
            lambda.setBody(blockBody);
        } else {
            lambda.setBody(methodInvocation);
        }
        Expression expr = null;
        boolean hasConflict = hasConflict(methodReference.getStartPosition(), referredMethodBinding, ScopeAnalyzer.METHODS | ScopeAnalyzer.CHECK_VISIBILITY, astRoot);
        if (hasConflict || !Bindings.isSuperType(referredMethodBinding.getDeclaringClass(), ASTNodes.getEnclosingType(methodReference)) || methodReference.typeArguments().size() != 0) {
            if (methodReference instanceof ExpressionMethodReference) {
                ExpressionMethodReference expressionMethodReference = (ExpressionMethodReference) methodReference;
                expr = (Expression) rewrite.createCopyTarget(expressionMethodReference.getExpression());
            } else if (methodReference instanceof TypeMethodReference) {
                Type type = ((TypeMethodReference) methodReference).getType();
                ITypeBinding typeBinding = type.resolveBinding();
                if (typeBinding != null) {
                    ImportRewrite importRewrite = CodeStyleConfiguration.createImportRewrite(astRoot, true);
                    expr = ast.newName(importRewrite.addImport(typeBinding));
                }
            }
        }
        methodInvocation.setExpression(expr);
        SimpleName methodName = getMethodInvocationName(methodReference);
        methodInvocation.setName((SimpleName) rewrite.createCopyTarget(methodName));
        List<SimpleName> invocationArgs = getInvocationArguments(ast, 0, noOfLambdaParameters, lambdaParamNames);
        methodInvocation.arguments().addAll(invocationArgs);
        if (linkedProposalModel != null) {
            for (SimpleName name : invocationArgs) {
                linkedProposalModel.getPositionGroup(name.getIdentifier(), false).addPosition(rewrite.track(name), LinkedPositionGroup.NO_STOP);
            }
        }
        methodInvocation.typeArguments().addAll(getCopiedTypeArguments(rewrite, methodReference.typeArguments()));
    } else if (methodReference instanceof SuperMethodReference) {
        SuperMethodInvocation superMethodInvocation = ast.newSuperMethodInvocation();
        if (createBlockBody) {
            Block blockBody = getBlockBodyForLambda(superMethodInvocation, returnTypeBinding, ast);
            lambda.setBody(blockBody);
        } else {
            lambda.setBody(superMethodInvocation);
        }
        Name superQualifier = ((SuperMethodReference) methodReference).getQualifier();
        if (superQualifier != null) {
            superMethodInvocation.setQualifier((Name) rewrite.createCopyTarget(superQualifier));
        }
        SimpleName methodName = getMethodInvocationName(methodReference);
        superMethodInvocation.setName((SimpleName) rewrite.createCopyTarget(methodName));
        List<SimpleName> invocationArgs = getInvocationArguments(ast, 0, noOfLambdaParameters, lambdaParamNames);
        superMethodInvocation.arguments().addAll(invocationArgs);
        if (linkedProposalModel != null) {
            for (SimpleName name : invocationArgs) {
                linkedProposalModel.getPositionGroup(name.getIdentifier(), false).addPosition(rewrite.track(name), LinkedPositionGroup.NO_STOP);
            }
        }
        superMethodInvocation.typeArguments().addAll(getCopiedTypeArguments(rewrite, methodReference.typeArguments()));
    } else {
        MethodInvocation methodInvocation = ast.newMethodInvocation();
        if (createBlockBody) {
            Block blockBody = getBlockBodyForLambda(methodInvocation, returnTypeBinding, ast);
            lambda.setBody(blockBody);
        } else {
            lambda.setBody(methodInvocation);
        }
        boolean isTypeReference = isTypeReferenceToInstanceMethod(methodReference);
        if (isTypeReference) {
            SimpleName name = ast.newSimpleName(lambdaParamNames[0]);
            methodInvocation.setExpression(name);
            if (linkedProposalModel != null) {
                linkedProposalModel.getPositionGroup(name.getIdentifier(), false).addPosition(rewrite.track(name), LinkedPositionGroup.NO_STOP);
            }
        } else {
            Expression expr = ((ExpressionMethodReference) methodReference).getExpression();
            if (!(expr instanceof ThisExpression && methodReference.typeArguments().size() == 0)) {
                methodInvocation.setExpression((Expression) rewrite.createCopyTarget(expr));
            }
        }
        SimpleName methodName = getMethodInvocationName(methodReference);
        methodInvocation.setName((SimpleName) rewrite.createCopyTarget(methodName));
        List<SimpleName> invocationArgs = getInvocationArguments(ast, isTypeReference ? 1 : 0, noOfLambdaParameters, lambdaParamNames);
        methodInvocation.arguments().addAll(invocationArgs);
        if (linkedProposalModel != null) {
            for (SimpleName name : invocationArgs) {
                linkedProposalModel.getPositionGroup(name.getIdentifier(), false).addPosition(rewrite.track(name), LinkedPositionGroup.NO_STOP);
            }
        }
        methodInvocation.typeArguments().addAll(getCopiedTypeArguments(rewrite, methodReference.typeArguments()));
    }
    rewrite.replace(methodReference, lambda, null);
    return lambda;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) SimpleName(org.eclipse.jdt.core.dom.SimpleName) CreationReference(org.eclipse.jdt.core.dom.CreationReference) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) TypeMethodReference(org.eclipse.jdt.core.dom.TypeMethodReference) List(java.util.List) ArrayList(java.util.ArrayList) SuperMethodReference(org.eclipse.jdt.core.dom.SuperMethodReference) ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) AST(org.eclipse.jdt.core.dom.AST) ExpressionMethodReference(org.eclipse.jdt.core.dom.ExpressionMethodReference) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) IType(org.eclipse.jdt.core.IType) UnionType(org.eclipse.jdt.core.dom.UnionType) 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) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) ArrayCreation(org.eclipse.jdt.core.dom.ArrayCreation) Block(org.eclipse.jdt.core.dom.Block) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression)

Example 29 with ArrayType

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

the class UnresolvedElementsSubProcessor method getTypeProposals.

public static void getTypeProposals(IInvocationContext context, IProblemLocation problem, Collection<CUCorrectionProposal> proposals) throws CoreException {
    ICompilationUnit cu = context.getCompilationUnit();
    ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
    if (selectedNode == null) {
        return;
    }
    int kind = evauateTypeKind(selectedNode, cu.getJavaProject());
    if (kind == TypeKinds.REF_TYPES) {
        addEnhancedForWithoutTypeProposals(cu, selectedNode, proposals);
    }
    while (selectedNode.getLocationInParent() == QualifiedName.NAME_PROPERTY) {
        selectedNode = selectedNode.getParent();
    }
    Name node = null;
    if (selectedNode instanceof SimpleType) {
        node = ((SimpleType) selectedNode).getName();
    } else if (selectedNode instanceof NameQualifiedType) {
        node = ((NameQualifiedType) selectedNode).getName();
    } else if (selectedNode instanceof ArrayType) {
        Type elementType = ((ArrayType) selectedNode).getElementType();
        if (elementType.isSimpleType()) {
            node = ((SimpleType) elementType).getName();
        } else if (elementType.isNameQualifiedType()) {
            node = ((NameQualifiedType) elementType).getName();
        } else {
            return;
        }
    } else if (selectedNode instanceof Name) {
        node = (Name) selectedNode;
    } else {
        return;
    }
    // change to similar type proposals
    addSimilarTypeProposals(kind, cu, node, IProposalRelevance.SIMILAR_TYPE, proposals);
    while (node.getParent() instanceof QualifiedName) {
        node = (Name) node.getParent();
    }
    if (selectedNode != node) {
        kind = evauateTypeKind(node, cu.getJavaProject());
    }
    if ((kind & (TypeKinds.CLASSES | TypeKinds.INTERFACES)) != 0) {
        // only propose annotations when there are no other suggestions
        kind &= ~TypeKinds.ANNOTATIONS;
    }
    addNewTypeProposals(cu, node, kind, IProposalRelevance.NEW_TYPE, proposals);
}
Also used : ArrayType(org.eclipse.jdt.core.dom.ArrayType) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SimpleType(org.eclipse.jdt.core.dom.SimpleType) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) IType(org.eclipse.jdt.core.IType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Name(org.eclipse.jdt.core.dom.Name)

Example 30 with ArrayType

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