Search in sources :

Example 6 with BodyDeclaration

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

the class AbstractMethodCorrectionProposal method findConstructorInsertIndex.

private int findConstructorInsertIndex(List<BodyDeclaration> decls) {
    int nDecls = decls.size();
    int lastMethod = 0;
    for (int i = nDecls - 1; i >= 0; i--) {
        BodyDeclaration curr = decls.get(i);
        if (curr instanceof MethodDeclaration) {
            if (((MethodDeclaration) curr).isConstructor()) {
                return i + 1;
            }
            lastMethod = i;
        }
    }
    return lastMethod;
}
Also used : MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration)

Example 7 with BodyDeclaration

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

the class ReplaceInvocationsRefactoring method checkFinalConditions.

@Override
public RefactoringStatus checkFinalConditions(IProgressMonitor pm) throws CoreException {
    //$NON-NLS-1$
    pm.beginTask("", 20);
    fChangeManager = new TextChangeManager();
    RefactoringStatus result = new RefactoringStatus();
    fSourceProvider = resolveSourceProvider(fMethodBinding, result);
    if (result.hasFatalError())
        return result;
    result.merge(fSourceProvider.checkActivation());
    if (result.hasFatalError())
        return result;
    fSourceProvider.initialize();
    fTargetProvider.initialize();
    pm.setTaskName(RefactoringCoreMessages.InlineMethodRefactoring_searching);
    RefactoringStatus searchStatus = new RefactoringStatus();
    String binaryRefsDescription = Messages.format(RefactoringCoreMessages.ReferencesInBinaryContext_ref_in_binaries_description, BasicElementLabels.getJavaElementName(fSourceProvider.getMethodName()));
    ReferencesInBinaryContext binaryRefs = new ReferencesInBinaryContext(binaryRefsDescription);
    ICompilationUnit[] units = fTargetProvider.getAffectedCompilationUnits(searchStatus, binaryRefs, new SubProgressMonitor(pm, 1));
    binaryRefs.addErrorIfNecessary(searchStatus);
    if (searchStatus.hasFatalError()) {
        result.merge(searchStatus);
        return result;
    }
    IFile[] filesToBeModified = getFilesToBeModified(units);
    result.merge(Checks.validateModifiesFiles(filesToBeModified, getValidationContext()));
    if (result.hasFatalError())
        return result;
    result.merge(ResourceChangeChecker.checkFilesToBeChanged(filesToBeModified, new SubProgressMonitor(pm, 1)));
    checkOverridden(result, new SubProgressMonitor(pm, 4));
    IProgressMonitor sub = new SubProgressMonitor(pm, 15);
    //$NON-NLS-1$
    sub.beginTask("", units.length * 3);
    for (int c = 0; c < units.length; c++) {
        ICompilationUnit unit = units[c];
        sub.subTask(Messages.format(RefactoringCoreMessages.InlineMethodRefactoring_processing, BasicElementLabels.getFileName(unit)));
        CallInliner inliner = null;
        try {
            boolean added = false;
            MultiTextEdit root = new MultiTextEdit();
            CompilationUnitChange change = (CompilationUnitChange) fChangeManager.get(unit);
            change.setEdit(root);
            BodyDeclaration[] bodies = fTargetProvider.getAffectedBodyDeclarations(unit, new SubProgressMonitor(pm, 1));
            if (bodies.length == 0)
                continue;
            inliner = new CallInliner(unit, (CompilationUnit) bodies[0].getRoot(), fSourceProvider);
            for (int b = 0; b < bodies.length; b++) {
                BodyDeclaration body = bodies[b];
                inliner.initialize(body);
                RefactoringStatus nestedInvocations = new RefactoringStatus();
                ASTNode[] invocations = removeNestedCalls(nestedInvocations, unit, fTargetProvider.getInvocations(body, new SubProgressMonitor(sub, 2)));
                for (int i = 0; i < invocations.length; i++) {
                    ASTNode invocation = invocations[i];
                    result.merge(inliner.initialize(invocation, fTargetProvider.getStatusSeverity()));
                    if (result.hasFatalError())
                        break;
                    if (result.getSeverity() < fTargetProvider.getStatusSeverity()) {
                        added = true;
                        TextEditGroup group = new TextEditGroup(RefactoringCoreMessages.InlineMethodRefactoring_edit_inline);
                        change.addTextEditGroup(group);
                        result.merge(inliner.perform(group));
                    }
                }
                // do this after we have inlined the method calls. We still want
                // to generate the modifications.
                result.merge(nestedInvocations);
            }
            if (!added) {
                fChangeManager.remove(unit);
            } else {
                root.addChild(inliner.getModifications());
                ImportRewrite rewrite = inliner.getImportEdit();
                if (rewrite.hasRecordedChanges()) {
                    TextEdit edit = rewrite.rewriteImports(null);
                    if (edit instanceof MultiTextEdit ? ((MultiTextEdit) edit).getChildrenSize() > 0 : true) {
                        root.addChild(edit);
                        change.addTextEditGroup(new TextEditGroup(RefactoringCoreMessages.InlineMethodRefactoring_edit_import, new TextEdit[] { edit }));
                    }
                }
            }
        } finally {
            if (inliner != null)
                inliner.dispose();
        }
        sub.worked(1);
        if (sub.isCanceled())
            throw new OperationCanceledException();
    }
    result.merge(searchStatus);
    sub.done();
    pm.done();
    return result;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IFile(org.eclipse.core.resources.IFile) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) ReferencesInBinaryContext(org.eclipse.jdt.internal.corext.refactoring.base.ReferencesInBinaryContext) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) ASTNode(org.eclipse.jdt.core.dom.ASTNode) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) TextEditGroup(org.eclipse.text.edits.TextEditGroup) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextChangeManager(org.eclipse.jdt.internal.corext.refactoring.util.TextChangeManager) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange)

Example 8 with BodyDeclaration

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

the class Java50Fix method createAddDeprecatedAnnotationOperations.

private static void createAddDeprecatedAnnotationOperations(CompilationUnit compilationUnit, IProblemLocation[] locations, List<CompilationUnitRewriteOperation> result) {
    for (int i = 0; i < locations.length; i++) {
        IProblemLocation problem = locations[i];
        if (isMissingDeprecationProblem(problem.getProblemId())) {
            ASTNode selectedNode = problem.getCoveringNode(compilationUnit);
            if (selectedNode != null) {
                ASTNode declaringNode = getDeclaringNode(selectedNode);
                if (declaringNode instanceof BodyDeclaration) {
                    BodyDeclaration declaration = (BodyDeclaration) declaringNode;
                    AnnotationRewriteOperation operation = new AnnotationRewriteOperation(declaration, DEPRECATED);
                    result.add(operation);
                }
            }
        }
    }
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) IProblemLocation(org.eclipse.jdt.ui.text.java.IProblemLocation)

Example 9 with BodyDeclaration

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

the class LambdaExpressionsFix method isFunctionalAnonymous.

static boolean isFunctionalAnonymous(ClassInstanceCreation node) {
    ITypeBinding typeBinding = node.resolveTypeBinding();
    if (typeBinding == null)
        return false;
    ITypeBinding[] interfaces = typeBinding.getInterfaces();
    if (interfaces.length != 1)
        return false;
    if (interfaces[0].getFunctionalInterfaceMethod() == null)
        return false;
    AnonymousClassDeclaration anonymTypeDecl = node.getAnonymousClassDeclaration();
    if (anonymTypeDecl == null || anonymTypeDecl.resolveBinding() == null)
        return false;
    List<BodyDeclaration> bodyDeclarations = anonymTypeDecl.bodyDeclarations();
    // cannot convert if there are fields or additional methods
    if (bodyDeclarations.size() != 1)
        return false;
    BodyDeclaration bodyDeclaration = bodyDeclarations.get(0);
    if (!(bodyDeclaration instanceof MethodDeclaration))
        return false;
    MethodDeclaration methodDecl = (MethodDeclaration) bodyDeclaration;
    IMethodBinding methodBinding = methodDecl.resolveBinding();
    if (methodBinding == null)
        return false;
    // generic lambda expressions are not allowed
    if (methodBinding.isGenericMethod())
        return false;
    // lambda cannot refer to 'this'/'super' literals
    if (SuperThisReferenceFinder.hasReference(methodDecl))
        return false;
    if (!isInTargetTypeContext(node))
        return false;
    return true;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration)

Example 10 with BodyDeclaration

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

the class SelfEncapsulateFieldRefactoring method addGetterSetterChanges.

private List<TextEditGroup> addGetterSetterChanges(CompilationUnit root, ASTRewrite rewriter, String lineDelimiter, boolean usingLocalSetter, boolean usingLocalGetter) throws CoreException {
    List<TextEditGroup> result = new ArrayList<TextEditGroup>(2);
    AST ast = root.getAST();
    FieldDeclaration decl = (FieldDeclaration) ASTNodes.getParent(fFieldDeclaration, ASTNode.FIELD_DECLARATION);
    int position = 0;
    int numberOfMethods = 0;
    List<BodyDeclaration> members = ASTNodes.getBodyDeclarations(decl.getParent());
    for (Iterator<BodyDeclaration> iter = members.iterator(); iter.hasNext(); ) {
        BodyDeclaration element = iter.next();
        if (element.getNodeType() == ASTNode.METHOD_DECLARATION) {
            if (fInsertionIndex == -1) {
                break;
            } else if (fInsertionIndex == numberOfMethods) {
                position++;
                break;
            }
            numberOfMethods++;
        }
        position++;
    }
    TextEditGroup description;
    ListRewrite rewrite = fRewriter.getListRewrite(decl.getParent(), getBodyDeclarationsProperty(decl.getParent()));
    if (!usingLocalGetter) {
        description = new TextEditGroup(RefactoringCoreMessages.SelfEncapsulateField_add_getter);
        result.add(description);
        rewrite.insertAt(createGetterMethod(ast, rewriter, lineDelimiter), position++, description);
    }
    if (!JdtFlags.isFinal(fField) && !usingLocalSetter) {
        description = new TextEditGroup(RefactoringCoreMessages.SelfEncapsulateField_add_setter);
        result.add(description);
        rewrite.insertAt(createSetterMethod(ast, rewriter, lineDelimiter), position, description);
    }
    if (!JdtFlags.isPrivate(fField))
        result.add(makeDeclarationPrivate(rewriter, decl));
    return result;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) ArrayList(java.util.ArrayList) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

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