Search in sources :

Example 96 with TypeDeclaration

use of org.eclipse.jdt.core.dom.TypeDeclaration in project sts4 by spring-projects.

the class WebfluxRouterSymbolProvider method extractNestedValue.

private void extractNestedValue(ASTNode node, Collection<WebfluxRouteElement> values, Function<MethodInvocation, WebfluxRouteElement> extractor) {
    if (node == null || node instanceof TypeDeclaration) {
        return;
    }
    if (node instanceof MethodInvocation) {
        MethodInvocation methodInvocation = (MethodInvocation) node;
        IMethodBinding methodBinding = methodInvocation.resolveMethodBinding();
        if (WebfluxUtils.ROUTER_FUNCTIONS_TYPE.equals(methodBinding.getDeclaringClass().getBinaryName())) {
            String name = methodBinding.getName();
            if (WebfluxUtils.REQUEST_PREDICATE_NEST_METHOD.equals(name)) {
                List<?> arguments = methodInvocation.arguments();
                for (Object argument : arguments) {
                    if (argument instanceof MethodInvocation) {
                        MethodInvocation nestedMethod = (MethodInvocation) argument;
                        WebfluxRouteElement value = extractor.apply(nestedMethod);
                        if (value != null) {
                            values.add(value);
                        }
                    }
                }
            }
        }
    }
    extractNestedValue(node.getParent(), values, extractor);
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration)

Example 97 with TypeDeclaration

use of org.eclipse.jdt.core.dom.TypeDeclaration in project sts4 by spring-projects.

the class ComponentInjectionsHoverProvider method addAutomaticallyWiredContructor.

@Override
protected void addAutomaticallyWiredContructor(StringBuilder hover, Annotation annotation, LiveBeansModel beans, LiveBean bean, IJavaProject project) {
    TypeDeclaration typeDecl = ASTUtils.findDeclaringType(annotation);
    if (typeDecl != null) {
        MethodDeclaration[] constructors = ASTUtils.findConstructors(typeDecl);
        if (constructors != null && constructors.length == 1 && !hasAutowiredAnnotation(constructors[0])) {
            String[] dependencies = bean.getDependencies();
            if (dependencies != null && dependencies.length > 0) {
                hover.append("\n\n");
                hover.append(LiveHoverUtils.showBean(bean) + " got autowired with:\n\n");
                boolean firstDependency = true;
                for (String injectedBean : dependencies) {
                    if (!firstDependency) {
                        hover.append("\n");
                    }
                    List<LiveBean> dependencyBeans = beans.getBeansOfName(injectedBean);
                    for (LiveBean dependencyBean : dependencyBeans) {
                        hover.append("- " + LiveHoverUtils.showBeanWithResource(server, dependencyBean, "  ", project));
                    }
                    firstDependency = false;
                }
            }
        }
    }
}
Also used : MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) LiveBean(org.springframework.ide.vscode.commons.boot.app.cli.livebean.LiveBean)

Example 98 with TypeDeclaration

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

the class UnresolvedElementsSubProcessor method addNewTypeProposals.

public static void addNewTypeProposals(ICompilationUnit cu, Name refNode, int kind, int relevance, Collection<ChangeCorrectionProposal> proposals) throws CoreException {
    Name node = refNode;
    do {
        String typeName = ASTNodes.getSimpleNameIdentifier(node);
        Name qualifier = null;
        // only propose to create types for qualifiers when the name starts with upper case
        boolean isPossibleName = isLikelyTypeName(typeName) || node == refNode;
        if (isPossibleName) {
            IPackageFragment enclosingPackage = null;
            IType enclosingType = null;
            if (node.isSimpleName()) {
                enclosingPackage = (IPackageFragment) cu.getParent();
            // don't suggest member type, user can select it in wizard
            } else {
                Name qualifierName = ((QualifiedName) node).getQualifier();
                IBinding binding = qualifierName.resolveBinding();
                if (binding != null && binding.isRecovered()) {
                    binding = null;
                }
                if (binding instanceof ITypeBinding) {
                    enclosingType = (IType) binding.getJavaElement();
                } else if (binding instanceof IPackageBinding) {
                    qualifier = qualifierName;
                    enclosingPackage = (IPackageFragment) binding.getJavaElement();
                } else {
                    IJavaElement[] res = cu.codeSelect(qualifierName.getStartPosition(), qualifierName.getLength());
                    if (res != null && res.length > 0 && res[0] instanceof IType) {
                        enclosingType = (IType) res[0];
                    } else {
                        qualifier = qualifierName;
                        enclosingPackage = JavaModelUtil.getPackageFragmentRoot(cu).getPackageFragment(ASTResolving.getFullName(qualifierName));
                    }
                }
            }
            int rel = relevance;
            if (enclosingPackage != null && isLikelyPackageName(enclosingPackage.getElementName())) {
                rel += 3;
            }
            if (enclosingPackage != null && !enclosingPackage.getCompilationUnit(typeName + JavaModelUtil.DEFAULT_CU_SUFFIX).exists() || enclosingType != null && !enclosingType.isReadOnly() && !enclosingType.getType(typeName).exists()) {
                // new member type
                IJavaElement enclosing = enclosingPackage != null ? (IJavaElement) enclosingPackage : enclosingType;
                if ((kind & TypeKinds.CLASSES) != 0) {
                    proposals.add(new NewCUProposal(cu, node, NewCUProposal.K_CLASS, enclosing, rel + 3));
                }
                if ((kind & TypeKinds.INTERFACES) != 0) {
                    proposals.add(new NewCUProposal(cu, node, NewCUProposal.K_INTERFACE, enclosing, rel + 2));
                }
                if ((kind & TypeKinds.ENUMS) != 0) {
                    proposals.add(new NewCUProposal(cu, node, NewCUProposal.K_ENUM, enclosing, rel));
                }
                if ((kind & TypeKinds.ANNOTATIONS) != 0) {
                    proposals.add(new NewCUProposal(cu, node, NewCUProposal.K_ANNOTATION, enclosing, rel + 1));
                // TODO: addNullityAnnotationTypesProposals(cu, node, proposals);
                }
            }
        }
        node = qualifier;
    } while (node != null);
    // type parameter proposals
    if (refNode.isSimpleName() && (kind & TypeKinds.VARIABLES) != 0) {
        CompilationUnit root = (CompilationUnit) refNode.getRoot();
        String name = ((SimpleName) refNode).getIdentifier();
        BodyDeclaration declaration = ASTResolving.findParentBodyDeclaration(refNode);
        int baseRel = relevance;
        if (isLikelyTypeParameterName(name)) {
            baseRel += 8;
        }
        while (declaration != null) {
            IBinding binding = null;
            int rel = baseRel;
            if (declaration instanceof MethodDeclaration) {
                binding = ((MethodDeclaration) declaration).resolveBinding();
                if (isLikelyMethodTypeParameterName(name)) {
                    rel += 2;
                }
            } else if (declaration instanceof TypeDeclaration) {
                binding = ((TypeDeclaration) declaration).resolveBinding();
                rel++;
            }
            if (binding != null) {
                AddTypeParameterProposal proposal = new AddTypeParameterProposal(cu, binding, root, name, null, rel);
                proposals.add(proposal);
            }
            if (!Modifier.isStatic(declaration.getModifiers())) {
                declaration = ASTResolving.findParentBodyDeclaration(declaration.getParent());
            } else {
                declaration = null;
            }
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) IJavaElement(org.eclipse.jdt.core.IJavaElement) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) IBinding(org.eclipse.jdt.core.dom.IBinding) SimpleName(org.eclipse.jdt.core.dom.SimpleName) SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Name(org.eclipse.jdt.core.dom.Name) IType(org.eclipse.jdt.core.IType) IPackageBinding(org.eclipse.jdt.core.dom.IPackageBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration)

Example 99 with TypeDeclaration

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

the class LocalCorrectionsSubProcessor method addConstructorFromSuperclassProposal.

public static void addConstructorFromSuperclassProposal(IInvocationContext context, IProblemLocationCore problem, Collection<ChangeCorrectionProposal> proposals) throws CoreException {
    ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
    if (selectedNode == null) {
        return;
    }
    TypeDeclaration typeDeclaration = null;
    if (selectedNode.getLocationInParent() == TypeDeclaration.NAME_PROPERTY) {
        typeDeclaration = (TypeDeclaration) selectedNode.getParent();
    } else {
        BodyDeclaration declaration = ASTResolving.findParentBodyDeclaration(selectedNode);
        if (declaration instanceof Initializer && problem.getProblemId() == IProblem.UnhandledExceptionInDefaultConstructor) {
            addUncaughtExceptionProposals(context, problem, proposals);
        }
        return;
    }
    ITypeBinding binding = typeDeclaration.resolveBinding();
    if (binding == null || binding.getSuperclass() == null) {
        return;
    }
    ICompilationUnit cu = context.getCompilationUnit();
    IMethodBinding[] methods = binding.getSuperclass().getDeclaredMethods();
    for (int i = 0; i < methods.length; i++) {
        IMethodBinding curr = methods[i];
        if (curr.isConstructor() && !Modifier.isPrivate(curr.getModifiers())) {
            proposals.add(new ConstructorFromSuperclassProposal(cu, typeDeclaration, curr, IProposalRelevance.ADD_CONSTRUCTOR_FROM_SUPER_CLASS));
        }
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) Initializer(org.eclipse.jdt.core.dom.Initializer) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration)

Example 100 with TypeDeclaration

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

the class JavadocTagsSubProcessor method getMissingJavadocCommentProposals.

public static void getMissingJavadocCommentProposals(IInvocationContext context, IProblemLocationCore problem, Collection<ChangeCorrectionProposal> proposals) throws CoreException {
    ASTNode node = problem.getCoveringNode(context.getASTRoot());
    if (node == null) {
        return;
    }
    BodyDeclaration declaration = ASTResolving.findParentBodyDeclaration(node);
    if (declaration == null) {
        return;
    }
    ICompilationUnit cu = context.getCompilationUnit();
    ITypeBinding binding = Bindings.getBindingOfParentType(declaration);
    if (binding == null) {
        return;
    }
    if (declaration instanceof MethodDeclaration) {
        MethodDeclaration methodDecl = (MethodDeclaration) declaration;
        IMethodBinding methodBinding = methodDecl.resolveBinding();
        IMethodBinding overridden = null;
        if (methodBinding != null) {
            overridden = Bindings.findOverriddenMethod(methodBinding, true);
        }
        String string = CodeGeneration.getMethodComment(cu, binding.getName(), methodDecl, overridden, String.valueOf('\n'));
        if (string != null) {
            String label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_method_description;
            proposals.add(new AddJavadocCommentProposal(label, cu, IProposalRelevance.ADD_JAVADOC_METHOD, declaration.getStartPosition(), string));
        }
    } else if (declaration instanceof AbstractTypeDeclaration) {
        String typeQualifiedName = Bindings.getTypeQualifiedName(binding);
        String[] typeParamNames;
        if (declaration instanceof TypeDeclaration) {
            List<TypeParameter> typeParams = ((TypeDeclaration) declaration).typeParameters();
            typeParamNames = new String[typeParams.size()];
            for (int i = 0; i < typeParamNames.length; i++) {
                typeParamNames[i] = (typeParams.get(i)).getName().getIdentifier();
            }
        } else {
            typeParamNames = new String[0];
        }
        String string = CodeGeneration.getTypeComment(cu, typeQualifiedName, typeParamNames, String.valueOf('\n'));
        if (string != null) {
            String label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_type_description;
            proposals.add(new AddJavadocCommentProposal(label, cu, IProposalRelevance.ADD_JAVADOC_TYPE, declaration.getStartPosition(), string));
        }
    } else if (declaration instanceof FieldDeclaration) {
        // $NON-NLS-1$
        String comment = "/**\n *\n */\n";
        List<VariableDeclarationFragment> fragments = ((FieldDeclaration) declaration).fragments();
        if (fragments != null && fragments.size() > 0) {
            VariableDeclaration decl = fragments.get(0);
            String fieldName = decl.getName().getIdentifier();
            String typeName = binding.getName();
            comment = CodeGeneration.getFieldComment(cu, typeName, fieldName, String.valueOf('\n'));
        }
        if (comment != null) {
            String label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_field_description;
            proposals.add(new AddJavadocCommentProposal(label, cu, IProposalRelevance.ADD_JAVADOC_FIELD, declaration.getStartPosition(), comment));
        }
    } else if (declaration instanceof EnumConstantDeclaration) {
        EnumConstantDeclaration enumDecl = (EnumConstantDeclaration) declaration;
        String id = enumDecl.getName().getIdentifier();
        String comment = CodeGeneration.getFieldComment(cu, binding.getName(), id, String.valueOf('\n'));
        String label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_enumconst_description;
        proposals.add(new AddJavadocCommentProposal(label, cu, IProposalRelevance.ADD_JAVADOC_ENUM, declaration.getStartPosition(), comment));
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) EnumConstantDeclaration(org.eclipse.jdt.core.dom.EnumConstantDeclaration) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) List(java.util.List) ArrayList(java.util.ArrayList) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Aggregations

TypeDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration)111 ASTNode (org.eclipse.jdt.core.dom.ASTNode)64 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)51 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)40 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)38 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)34 SimpleName (org.eclipse.jdt.core.dom.SimpleName)24 Type (org.eclipse.jdt.core.dom.Type)24 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)23 AST (org.eclipse.jdt.core.dom.AST)22 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)21 ArrayList (java.util.ArrayList)20 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)20 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)17 AnnotationTypeDeclaration (org.eclipse.jdt.core.dom.AnnotationTypeDeclaration)15 ASTVisitor (org.eclipse.jdt.core.dom.ASTVisitor)13 Block (org.eclipse.jdt.core.dom.Block)13 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)13 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)13 List (java.util.List)12