Search in sources :

Example 71 with BodyDeclaration

use of org.eclipse.jdt.core.dom.BodyDeclaration in project eclipse-pmd by acanda.

the class SuppressWarningsQuickFix method findBodyDeclaration.

private BodyDeclaration findBodyDeclaration(final ASTNode node) {
    final BodyDeclaration[] bodyDeclaration = new BodyDeclaration[1];
    node.accept(new ASTVisitor() {

        @Override
        public boolean visit(final EnumDeclaration node) {
            bodyDeclaration[0] = node;
            return false;
        }

        @Override
        public boolean visit(final TypeDeclaration node) {
            bodyDeclaration[0] = node;
            return false;
        }

        @Override
        public boolean visit(final AnnotationTypeDeclaration node) {
            bodyDeclaration[0] = node;
            return false;
        }
    });
    return bodyDeclaration[0];
}
Also used : AnnotationTypeDeclaration(org.eclipse.jdt.core.dom.AnnotationTypeDeclaration) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) AnnotationTypeDeclaration(org.eclipse.jdt.core.dom.AnnotationTypeDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor) EnumDeclaration(org.eclipse.jdt.core.dom.EnumDeclaration)

Example 72 with BodyDeclaration

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

the class ExtractConstantRefactoring method computeConstantDeclarationLocation.

private void computeConstantDeclarationLocation() throws JavaModelException {
    if (isDeclarationLocationComputed()) {
        return;
    }
    BodyDeclaration lastStaticDependency = null;
    Iterator<BodyDeclaration> decls = getContainingTypeDeclarationNode().bodyDeclarations().iterator();
    while (decls.hasNext()) {
        BodyDeclaration decl = decls.next();
        int modifiers;
        if (decl instanceof FieldDeclaration) {
            modifiers = ((FieldDeclaration) decl).getModifiers();
        } else if (decl instanceof Initializer) {
            modifiers = ((Initializer) decl).getModifiers();
        } else {
            continue;
        /* this declaration is not a field declaration
							    or initializer, so the placement of the constant
							    declaration relative to it does not matter */
        }
        if (Modifier.isStatic(modifiers) && depends(getSelectedExpression(), decl)) {
            lastStaticDependency = decl;
        }
    }
    if (lastStaticDependency == null) {
        fInsertFirst = true;
    } else {
        fToInsertAfter = lastStaticDependency;
    }
}
Also used : Initializer(org.eclipse.jdt.core.dom.Initializer) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Example 73 with BodyDeclaration

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

the class ExtractMethodRefactoring method findValidDuplicates.

private List<SnippetFinder.Match> findValidDuplicates(ASTNode startNode) {
    List<Match> duplicates = SnippetFinder.perform(startNode, fAnalyzer.getSelectedNodes());
    List<SnippetFinder.Match> validDuplicates = new ArrayList<>();
    for (Match duplicate : duplicates) {
        if (duplicate != null && !duplicate.isInvalidNode()) {
            try {
                ASTNode[] nodes = duplicate.getNodes();
                int duplicateStart = nodes[0].getStartPosition();
                ASTNode lastNode = nodes[nodes.length - 1];
                int duplicateEnd = lastNode.getStartPosition() + lastNode.getLength();
                int duplicateLength = duplicateEnd - duplicateStart;
                ExtractMethodAnalyzer analyzer = new ExtractMethodAnalyzer(fCUnit, Selection.createFromStartLength(duplicateStart, duplicateLength));
                fRoot.accept(analyzer);
                RefactoringStatus result = new RefactoringStatus();
                result.merge(analyzer.checkInitialConditions(fImportRewriter));
                if (!result.hasFatalError()) {
                    ITypeBinding originalReturnTypeBinding = fAnalyzer.getReturnTypeBinding();
                    ITypeBinding duplicateReturnTypeBinding = analyzer.getReturnTypeBinding();
                    if (originalReturnTypeBinding == null && duplicateReturnTypeBinding == null) {
                        validDuplicates.add(duplicate);
                    } else if (originalReturnTypeBinding != null && duplicateReturnTypeBinding != null) {
                        if (!originalReturnTypeBinding.equals(duplicateReturnTypeBinding)) {
                            if (duplicateReturnTypeBinding.equals(startNode.getAST().resolveWellKnownType("void"))) {
                                // $NON-NLS-1$
                                // extracted snippet returns non-void and duplicate snippet returns void => OK
                                validDuplicates.add(duplicate);
                            }
                        } else {
                            IVariableBinding originalReturnValBinding = fAnalyzer.getReturnValue();
                            IVariableBinding duplicateReturnValBinding = analyzer.getReturnValue();
                            if (originalReturnValBinding == null && duplicateReturnValBinding == null) {
                                validDuplicates.add(duplicate);
                            } else if (originalReturnValBinding != null && duplicateReturnValBinding != null) {
                                BodyDeclaration originalEnclosingBodyDeclaration = fAnalyzer.getEnclosingBodyDeclaration();
                                BodyDeclaration duplicateEnclosingBodyDeclaration = analyzer.getEnclosingBodyDeclaration();
                                VariableDeclaration originalReturnNode = ASTNodes.findVariableDeclaration(originalReturnValBinding, originalEnclosingBodyDeclaration);
                                VariableDeclaration duplicateReturnNode = ASTNodes.findVariableDeclaration(duplicateReturnValBinding, duplicateEnclosingBodyDeclaration);
                                if (originalReturnNode != null && duplicateReturnNode != null) {
                                    boolean matches;
                                    if (!fAnalyzer.getSelection().covers(originalReturnNode) && !analyzer.getSelection().covers(duplicateReturnNode)) {
                                        // returned variables are defined outside of the selection => always OK
                                        matches = true;
                                    } else {
                                        matches = matchesLocationInEnclosingBodyDecl(originalEnclosingBodyDeclaration, duplicateEnclosingBodyDeclaration, originalReturnNode, duplicateReturnNode);
                                    }
                                    if (matches) {
                                        validDuplicates.add(duplicate);
                                    }
                                }
                            }
                        }
                    }
                }
            } catch (CoreException e) {
            // consider as invalid duplicate
            }
        }
    }
    return validDuplicates;
}
Also used : ArrayList(java.util.ArrayList) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) Match(org.eclipse.jdt.ls.core.internal.corext.refactoring.code.SnippetFinder.Match) CoreException(org.eclipse.core.runtime.CoreException) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration)

Example 74 with BodyDeclaration

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

the class ExtractMethodRefactoring method checkFinalConditions.

@Override
public RefactoringStatus checkFinalConditions(IProgressMonitor pm) throws CoreException {
    pm.beginTask(RefactoringCoreMessages.ExtractMethodRefactoring_checking_new_name, 2);
    pm.subTask(EMPTY);
    RefactoringStatus result = checkMethodName();
    result.merge(checkParameterNames());
    result.merge(checkVarargOrder());
    pm.worked(1);
    if (pm.isCanceled()) {
        throw new OperationCanceledException();
    }
    BodyDeclaration node = fAnalyzer.getEnclosingBodyDeclaration();
    if (node != null) {
        fAnalyzer.checkInput(result, fMethodName, fDestination);
        pm.worked(1);
    }
    pm.done();
    return result;
}
Also used : OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration)

Example 75 with BodyDeclaration

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

the class ExtractMethodRefactoring method getRefactoringDescriptor.

private ExtractMethodDescriptor getRefactoringDescriptor() {
    final Map<String, String> arguments = new HashMap<>();
    String project = null;
    IJavaProject javaProject = fCUnit.getJavaProject();
    if (javaProject != null) {
        project = javaProject.getElementName();
    }
    ITypeBinding type = null;
    if (fDestination instanceof AbstractTypeDeclaration) {
        final AbstractTypeDeclaration decl = (AbstractTypeDeclaration) fDestination;
        type = decl.resolveBinding();
    } else if (fDestination instanceof AnonymousClassDeclaration) {
        final AnonymousClassDeclaration decl = (AnonymousClassDeclaration) fDestination;
        type = decl.resolveBinding();
    }
    IMethodBinding method = null;
    final BodyDeclaration enclosing = fAnalyzer.getEnclosingBodyDeclaration();
    if (enclosing instanceof MethodDeclaration) {
        final MethodDeclaration node = (MethodDeclaration) enclosing;
        method = node.resolveBinding();
    }
    final int flags = RefactoringDescriptor.STRUCTURAL_CHANGE | JavaRefactoringDescriptor.JAR_REFACTORING | JavaRefactoringDescriptor.JAR_SOURCE_ATTACHMENT;
    final String description = Messages.format(RefactoringCoreMessages.ExtractMethodRefactoring_descriptor_description_short, BasicElementLabels.getJavaElementName(fMethodName));
    final String label = method != null ? BindingLabelProvider.getBindingLabel(method, JavaElementLabels.ALL_FULLY_QUALIFIED) : '{' + JavaElementLabels.ELLIPSIS_STRING + '}';
    final String header = Messages.format(RefactoringCoreMessages.ExtractMethodRefactoring_descriptor_description, new String[] { BasicElementLabels.getJavaElementName(getSignature()), label, BindingLabelProvider.getBindingLabel(type, JavaElementLabels.ALL_FULLY_QUALIFIED) });
    final JDTRefactoringDescriptorComment comment = new JDTRefactoringDescriptorComment(project, this, header);
    comment.addSetting(Messages.format(RefactoringCoreMessages.ExtractMethodRefactoring_name_pattern, BasicElementLabels.getJavaElementName(fMethodName)));
    comment.addSetting(Messages.format(RefactoringCoreMessages.ExtractMethodRefactoring_destination_pattern, BindingLabelProvider.getBindingLabel(type, JavaElementLabels.ALL_FULLY_QUALIFIED)));
    String visibility = JdtFlags.getVisibilityString(fVisibility);
    if ("".equals(visibility)) {
        visibility = RefactoringCoreMessages.ExtractMethodRefactoring_default_visibility;
    }
    comment.addSetting(Messages.format(RefactoringCoreMessages.ExtractMethodRefactoring_visibility_pattern, visibility));
    if (fThrowRuntimeExceptions) {
        comment.addSetting(RefactoringCoreMessages.ExtractMethodRefactoring_declare_thrown_exceptions);
    }
    if (fReplaceDuplicates) {
        comment.addSetting(RefactoringCoreMessages.ExtractMethodRefactoring_replace_occurrences);
    }
    if (fGenerateJavadoc) {
        comment.addSetting(RefactoringCoreMessages.ExtractMethodRefactoring_generate_comment);
    }
    final ExtractMethodDescriptor descriptor = RefactoringSignatureDescriptorFactory.createExtractMethodDescriptor(project, description, comment.asString(), arguments, flags);
    arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT, JavaRefactoringDescriptorUtil.elementToHandle(project, fCUnit));
    arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_NAME, fMethodName);
    // $NON-NLS-1$
    arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION, String.valueOf(fSelectionStart) + " " + String.valueOf(fSelectionLength));
    arguments.put(ATTRIBUTE_VISIBILITY, new Integer(fVisibility).toString());
    arguments.put(ATTRIBUTE_DESTINATION, new Integer(fDestinationIndex).toString());
    arguments.put(ATTRIBUTE_EXCEPTIONS, Boolean.valueOf(fThrowRuntimeExceptions).toString());
    arguments.put(ATTRIBUTE_COMMENTS, Boolean.valueOf(fGenerateJavadoc).toString());
    arguments.put(ATTRIBUTE_REPLACE, Boolean.valueOf(fReplaceDuplicates).toString());
    return descriptor;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) HashMap(java.util.HashMap) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) JDTRefactoringDescriptorComment(org.eclipse.jdt.ls.core.internal.corext.refactoring.JDTRefactoringDescriptorComment) ExtractMethodDescriptor(org.eclipse.jdt.core.refactoring.descriptors.ExtractMethodDescriptor) IJavaProject(org.eclipse.jdt.core.IJavaProject) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

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