Search in sources :

Example 21 with BodyDeclaration

use of org.eclipse.jdt.core.dom.BodyDeclaration in project AutoRefactor by JnRouvignac.

the class ASTHelper method getSibling.

private static BodyDeclaration getSibling(BodyDeclaration node, List<BodyDeclaration> bodyDeclarations, boolean lookForPrevious) {
    final TreeSet<BodyDeclaration> children = new TreeSet<BodyDeclaration>(new NodeStartPositionComparator());
    children.addAll(bodyDeclarations);
    BodyDeclaration previous = null;
    boolean returnNext = false;
    for (BodyDeclaration child : children) {
        if (lookForPrevious) {
            if (child.equals(node)) {
                return previous;
            }
        } else if (returnNext) {
            return child;
        }
        previous = child;
        returnNext = child.equals(node);
    }
    return null;
}
Also used : TreeSet(java.util.TreeSet) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration)

Example 22 with BodyDeclaration

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

the class RedundantNullnessTypeAnnotationsFilter method getNNBDAnnotation.

// based on org.eclipse.jdt.apt.core.internal.declaration.ASTBasedDeclarationImpl.getAnnotationInstancesFromAST()
private static /* @Nullable */
IAnnotationBinding getNNBDAnnotation(ASTNode astNode, String nonNullByDefaultName) {
    List<IExtendedModifier> extendsMods = null;
    switch(astNode.getNodeType()) {
        case ASTNode.COMPILATION_UNIT:
            {
                // special case: when reaching the root of the ast, check the package annotations.
                PackageDeclaration packageDeclaration = ((CompilationUnit) astNode).getPackage();
                if (packageDeclaration != null) {
                    IPackageBinding packageBinding = packageDeclaration.resolveBinding();
                    if (packageBinding != null) {
                        for (IAnnotationBinding annotationBinding : packageBinding.getAnnotations()) {
                            ITypeBinding annotationType = annotationBinding.getAnnotationType();
                            if (annotationType != null && annotationType.getQualifiedName().equals(nonNullByDefaultName)) {
                                return annotationBinding;
                            }
                        }
                    }
                }
                return null;
            }
        case ASTNode.TYPE_DECLARATION:
        case ASTNode.ANNOTATION_TYPE_DECLARATION:
        case ASTNode.ENUM_DECLARATION:
        case ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION:
        case ASTNode.METHOD_DECLARATION:
        case ASTNode.FIELD_DECLARATION:
        case ASTNode.ENUM_CONSTANT_DECLARATION:
            extendsMods = ((BodyDeclaration) astNode).modifiers();
            break;
        case ASTNode.VARIABLE_DECLARATION_STATEMENT:
            extendsMods = ((VariableDeclarationStatement) astNode).modifiers();
            break;
        case ASTNode.VARIABLE_DECLARATION_EXPRESSION:
            extendsMods = ((VariableDeclarationExpression) astNode).modifiers();
            break;
        case ASTNode.SINGLE_VARIABLE_DECLARATION:
            extendsMods = ((SingleVariableDeclaration) astNode).modifiers();
            break;
        case ASTNode.VARIABLE_DECLARATION_FRAGMENT:
            final ASTNode parent = ((VariableDeclarationFragment) astNode).getParent();
            if (parent instanceof BodyDeclaration) {
                extendsMods = ((BodyDeclaration) parent).modifiers();
            }
            break;
        default:
            return null;
    }
    if (extendsMods != null) {
        for (IExtendedModifier extMod : extendsMods) {
            if (extMod.isAnnotation()) {
                Annotation annotation = (Annotation) extMod;
                IAnnotationBinding annotationBinding = annotation.resolveAnnotationBinding();
                if (annotationBinding != null) {
                    ITypeBinding annotationType = annotationBinding.getAnnotationType();
                    if (annotationType != null && annotationType.getQualifiedName().equals(nonNullByDefaultName)) {
                        return annotationBinding;
                    }
                }
            }
        }
    }
    return null;
}
Also used : IAnnotationBinding(org.eclipse.jdt.core.dom.IAnnotationBinding) 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) PackageDeclaration(org.eclipse.jdt.core.dom.PackageDeclaration) Annotation(org.eclipse.jdt.core.dom.Annotation) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) IPackageBinding(org.eclipse.jdt.core.dom.IPackageBinding)

Example 23 with BodyDeclaration

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

the class LocalVariableIndex method internalPerform.

private static int internalPerform(BodyDeclaration methodOrInitializer) {
    // we have to find the outermost method/initializer/field declaration since a local or anonymous
    // type can reference final variables from the outer scope.
    BodyDeclaration target = methodOrInitializer;
    ASTNode parent = target.getParent();
    while (parent != null) {
        if (parent instanceof MethodDeclaration || parent instanceof Initializer || parent instanceof FieldDeclaration) {
            target = (BodyDeclaration) parent;
        }
        parent = parent.getParent();
    }
    return doPerform(target);
}
Also used : Initializer(org.eclipse.jdt.core.dom.Initializer) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Example 24 with BodyDeclaration

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

the class SelfEncapsulateFieldProposal method getRewrite.

@Override
protected ASTRewrite getRewrite() throws CoreException {
    CompilationUnit astRoot = ASTResolving.findParentCompilationUnit(fNode);
    ASTNode typeDecl = astRoot.findDeclaringNode(fSenderBinding);
    ASTNode newTypeDecl = null;
    if (typeDecl != null) {
        newTypeDecl = typeDecl;
    } else {
        astRoot = ASTResolving.createQuickFixAST(getCompilationUnit(), null);
        newTypeDecl = astRoot.findDeclaringNode(fSenderBinding.getKey());
    }
    createImportRewrite(astRoot);
    if (newTypeDecl != null) {
        ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST());
        ChildListPropertyDescriptor property = ASTNodes.getBodyDeclarationsProperty(newTypeDecl);
        List<BodyDeclaration> members = ASTNodes.getBodyDeclarations(newTypeDecl);
        int insertIndex = members.size();
        ListRewrite listRewriter = rewrite.getListRewrite(newTypeDecl, property);
        isGetter = true;
        MethodDeclaration newStub = getStub(rewrite, newTypeDecl);
        listRewriter.insertAt(newStub, insertIndex, null);
        isGetter = false;
        newStub = getStub(rewrite, newTypeDecl);
        listRewriter.insertAt(newStub, insertIndex + 1, null);
        return rewrite;
    }
    return null;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) ChildListPropertyDescriptor(org.eclipse.jdt.core.dom.ChildListPropertyDescriptor)

Example 25 with BodyDeclaration

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

the class JavadocTagsSubProcessor method getMissingJavadocTagProposals.

public static void getMissingJavadocTagProposals(IInvocationContext context, IProblemLocation problem, Collection<CUCorrectionProposal> proposals) {
    ASTNode node = problem.getCoveringNode(context.getASTRoot());
    if (node == null) {
        return;
    }
    node = ASTNodes.getNormalizedNode(node);
    BodyDeclaration bodyDeclaration = ASTResolving.findParentBodyDeclaration(node);
    if (bodyDeclaration == null) {
        return;
    }
    Javadoc javadoc = bodyDeclaration.getJavadoc();
    if (javadoc == null) {
        return;
    }
    String label;
    StructuralPropertyDescriptor location = node.getLocationInParent();
    if (location == SingleVariableDeclaration.NAME_PROPERTY) {
        label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_paramtag_description;
        if (node.getParent().getLocationInParent() != MethodDeclaration.PARAMETERS_PROPERTY) {
            // paranoia checks
            return;
        }
    } else if (location == TypeParameter.NAME_PROPERTY) {
        label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_paramtag_description;
        StructuralPropertyDescriptor parentLocation = node.getParent().getLocationInParent();
        if (parentLocation != MethodDeclaration.TYPE_PARAMETERS_PROPERTY && parentLocation != TypeDeclaration.TYPE_PARAMETERS_PROPERTY) {
            // paranoia checks
            return;
        }
    } else if (location == MethodDeclaration.RETURN_TYPE2_PROPERTY) {
        label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_returntag_description;
    } else if (location == MethodDeclaration.THROWN_EXCEPTION_TYPES_PROPERTY) {
        label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_throwstag_description;
    } else {
        return;
    }
    ASTRewriteCorrectionProposal proposal = new AddMissingJavadocTagProposal(label, context.getCompilationUnit(), bodyDeclaration, node, IProposalRelevance.ADD_MISSING_TAG);
    proposals.add(proposal);
    String label2 = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_allmissing_description;
    ASTRewriteCorrectionProposal addAllMissing = new AddAllMissingJavadocTagsProposal(label2, context.getCompilationUnit(), bodyDeclaration, IProposalRelevance.ADD_ALL_MISSING_TAGS);
    proposals.add(addAllMissing);
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode) Javadoc(org.eclipse.jdt.core.dom.Javadoc) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) StructuralPropertyDescriptor(org.eclipse.jdt.core.dom.StructuralPropertyDescriptor)

Aggregations

BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)98 ASTNode (org.eclipse.jdt.core.dom.ASTNode)61 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)53 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)28 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)27 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)26 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)26 Type (org.eclipse.jdt.core.dom.Type)25 AST (org.eclipse.jdt.core.dom.AST)23 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)18 ImportRewrite (org.eclipse.jdt.core.dom.rewrite.ImportRewrite)17 Expression (org.eclipse.jdt.core.dom.Expression)16 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)15 Initializer (org.eclipse.jdt.core.dom.Initializer)15 SimpleName (org.eclipse.jdt.core.dom.SimpleName)15 TypeDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration)15 ImportRewriteContext (org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext)14 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)14 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)13 ArrayList (java.util.ArrayList)12