Search in sources :

Example 1 with Message

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

the class SurroundWithAnalyzer method endVisit.

@Override
public void endVisit(CompilationUnit node) {
    postProcessSelectedNodes(internalGetSelectedNodes());
    BodyDeclaration enclosingNode = null;
    superCall: {
        if (getStatus().hasFatalError())
            break superCall;
        if (!hasSelectedNodes()) {
            ASTNode coveringNode = getLastCoveringNode();
            if (coveringNode instanceof Block) {
                Block block = (Block) coveringNode;
                Message[] messages = ASTNodes.getMessages(block, ASTNodes.NODE_ONLY);
                if (messages.length > 0) {
                    invalidSelection(RefactoringCoreMessages.SurroundWithTryCatchAnalyzer_compile_errors, JavaStatusContext.create(getCompilationUnit(), block));
                    break superCall;
                }
            }
            invalidSelection(RefactoringCoreMessages.SurroundWithTryCatchAnalyzer_doesNotCover);
            break superCall;
        }
        enclosingNode = (BodyDeclaration) ASTNodes.getParent(getFirstSelectedNode(), BodyDeclaration.class);
        if (!(enclosingNode instanceof MethodDeclaration) && !(enclosingNode instanceof Initializer)) {
            invalidSelection(RefactoringCoreMessages.SurroundWithTryCatchAnalyzer_doesNotContain);
            break superCall;
        }
        if (!onlyStatements()) {
            invalidSelection(RefactoringCoreMessages.SurroundWithTryCatchAnalyzer_onlyStatements);
        }
        fLocals = LocalDeclarationAnalyzer.perform(enclosingNode, getSelection());
    }
    super.endVisit(node);
}
Also used : Message(org.eclipse.jdt.core.dom.Message) Initializer(org.eclipse.jdt.core.dom.Initializer) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration)

Example 2 with Message

use of org.eclipse.jdt.core.dom.Message in project flux by eclipse.

the class ASTNodes method getMessages.

public static Message[] getMessages(ASTNode node, int flags) {
    ASTNode root = node.getRoot();
    if (!(root instanceof CompilationUnit))
        return EMPTY_MESSAGES;
    Message[] messages = ((CompilationUnit) root).getMessages();
    if (root == node)
        return messages;
    final int iterations = computeIterations(flags);
    List<Message> result = new ArrayList<Message>(5);
    for (int i = 0; i < messages.length; i++) {
        Message message = messages[i];
        ASTNode temp = node;
        int count = iterations;
        do {
            int nodeOffset = temp.getStartPosition();
            int messageOffset = message.getStartPosition();
            if (nodeOffset <= messageOffset && messageOffset < nodeOffset + temp.getLength()) {
                result.add(message);
                count = 0;
            } else {
                count--;
            }
        } while ((temp = temp.getParent()) != null && count > 0);
    }
    return result.toArray(new Message[result.size()]);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) Message(org.eclipse.jdt.core.dom.Message) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ArrayList(java.util.ArrayList)

Example 3 with Message

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

the class ExtractMethodAnalyzer method endVisit.

@Override
public void endVisit(CompilationUnit node) {
    RefactoringStatus status = getStatus();
    superCall: {
        if (status.hasFatalError())
            break superCall;
        if (!hasSelectedNodes()) {
            ASTNode coveringNode = getLastCoveringNode();
            if (coveringNode instanceof Block && coveringNode.getParent() instanceof MethodDeclaration) {
                MethodDeclaration methodDecl = (MethodDeclaration) coveringNode.getParent();
                Message[] messages = ASTNodes.getMessages(methodDecl, ASTNodes.NODE_ONLY);
                if (messages.length > 0) {
                    status.addFatalError(Messages.format(RefactoringCoreMessages.ExtractMethodAnalyzer_compile_errors, BasicElementLabels.getJavaElementName(methodDecl.getName().getIdentifier())), JavaStatusContext.create(fCUnit, methodDecl));
                    break superCall;
                }
            }
            status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_invalid_selection);
            break superCall;
        }
        fEnclosingBodyDeclaration = (BodyDeclaration) ASTNodes.getParent(getFirstSelectedNode(), BodyDeclaration.class);
        if (fEnclosingBodyDeclaration == null || (fEnclosingBodyDeclaration.getNodeType() != ASTNode.METHOD_DECLARATION && fEnclosingBodyDeclaration.getNodeType() != ASTNode.FIELD_DECLARATION && fEnclosingBodyDeclaration.getNodeType() != ASTNode.INITIALIZER)) {
            status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_invalid_selection);
            break superCall;
        } else if (ASTNodes.getEnclosingType(fEnclosingBodyDeclaration) == null) {
            status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_compile_errors_no_parent_binding);
            break superCall;
        } else if (fEnclosingBodyDeclaration.getNodeType() == ASTNode.METHOD_DECLARATION) {
            fEnclosingMethodBinding = ((MethodDeclaration) fEnclosingBodyDeclaration).resolveBinding();
        }
        if (!isSingleExpressionOrStatementSet()) {
            status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_single_expression_or_set);
            break superCall;
        }
        if (isExpressionSelected()) {
            ASTNode expression = getFirstSelectedNode();
            if (expression instanceof Name) {
                Name name = (Name) expression;
                if (name.resolveBinding() instanceof ITypeBinding) {
                    status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_type_reference);
                    break superCall;
                }
                if (name.resolveBinding() instanceof IMethodBinding) {
                    status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_method_name_reference);
                    break superCall;
                }
                if (name.resolveBinding() instanceof IVariableBinding) {
                    StructuralPropertyDescriptor locationInParent = name.getLocationInParent();
                    if (locationInParent == QualifiedName.NAME_PROPERTY || (locationInParent == FieldAccess.NAME_PROPERTY && !(((FieldAccess) name.getParent()).getExpression() instanceof ThisExpression))) {
                        status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_part_of_qualified_name);
                        break superCall;
                    }
                }
                if (name.isSimpleName() && ((SimpleName) name).isDeclaration()) {
                    status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_name_in_declaration);
                    break superCall;
                }
            }
            fForceStatic = ASTNodes.getParent(expression, ASTNode.SUPER_CONSTRUCTOR_INVOCATION) != null || ASTNodes.getParent(expression, ASTNode.CONSTRUCTOR_INVOCATION) != null;
        }
        status.merge(LocalTypeAnalyzer.perform(fEnclosingBodyDeclaration, getSelection()));
        computeLastStatementSelected();
    }
    super.endVisit(node);
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) Message(org.eclipse.jdt.core.dom.Message) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Name(org.eclipse.jdt.core.dom.Name) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) StructuralPropertyDescriptor(org.eclipse.jdt.core.dom.StructuralPropertyDescriptor)

Example 4 with Message

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

the class ExtractMethodAnalyzer method endVisit.

@Override
public void endVisit(CompilationUnit node) {
    RefactoringStatus status = getStatus();
    superCall: {
        if (status.hasFatalError()) {
            break superCall;
        }
        if (!hasSelectedNodes()) {
            ASTNode coveringNode = getLastCoveringNode();
            if (coveringNode instanceof Block && coveringNode.getParent() instanceof MethodDeclaration) {
                MethodDeclaration methodDecl = (MethodDeclaration) coveringNode.getParent();
                Message[] messages = ASTNodes.getMessages(methodDecl, ASTNodes.NODE_ONLY);
                if (messages.length > 0) {
                    status.addFatalError(Messages.format(RefactoringCoreMessages.ExtractMethodAnalyzer_compile_errors, BasicElementLabels.getJavaElementName(methodDecl.getName().getIdentifier())), JavaStatusContext.create(fCUnit, methodDecl));
                    break superCall;
                }
            }
            status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_invalid_selection);
            break superCall;
        }
        fEnclosingBodyDeclaration = (BodyDeclaration) ASTNodes.getParent(getFirstSelectedNode(), BodyDeclaration.class);
        if (fEnclosingBodyDeclaration == null || (fEnclosingBodyDeclaration.getNodeType() != ASTNode.METHOD_DECLARATION && fEnclosingBodyDeclaration.getNodeType() != ASTNode.FIELD_DECLARATION && fEnclosingBodyDeclaration.getNodeType() != ASTNode.INITIALIZER)) {
            status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_invalid_selection);
            break superCall;
        } else if (ASTNodes.getEnclosingType(fEnclosingBodyDeclaration) == null) {
            status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_compile_errors_no_parent_binding);
            break superCall;
        } else if (fEnclosingBodyDeclaration.getNodeType() == ASTNode.METHOD_DECLARATION) {
            fEnclosingMethodBinding = ((MethodDeclaration) fEnclosingBodyDeclaration).resolveBinding();
        }
        if (!isSingleExpressionOrStatementSet()) {
            status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_single_expression_or_set);
            break superCall;
        }
        if (isExpressionSelected()) {
            ASTNode expression = getFirstSelectedNode();
            if (expression instanceof Name) {
                Name name = (Name) expression;
                if (name.resolveBinding() instanceof ITypeBinding) {
                    status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_type_reference);
                    break superCall;
                }
                if (name.resolveBinding() instanceof IMethodBinding) {
                    status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_method_name_reference);
                    break superCall;
                }
                if (name.resolveBinding() instanceof IVariableBinding) {
                    StructuralPropertyDescriptor locationInParent = name.getLocationInParent();
                    boolean isPartOfQualifiedName = false;
                    boolean isPartOfQualifier = false;
                    if (locationInParent == QualifiedName.NAME_PROPERTY) {
                        isPartOfQualifiedName = true;
                        QualifiedName qualifiedName = (QualifiedName) name.getParent();
                        QualifiedName currParent = qualifiedName;
                        while (true) {
                            ASTNode parent = currParent.getParent();
                            if (parent instanceof QualifiedName) {
                                currParent = (QualifiedName) parent;
                            } else {
                                break;
                            }
                        }
                        if (!qualifiedName.equals(currParent)) {
                            isPartOfQualifier = true;
                        }
                    }
                    if ((isPartOfQualifiedName && !isPartOfQualifier) || (locationInParent == FieldAccess.NAME_PROPERTY && !(((FieldAccess) name.getParent()).getExpression() instanceof ThisExpression))) {
                        status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_part_of_qualified_name);
                        break superCall;
                    }
                }
                if (name.isSimpleName() && ((SimpleName) name).isDeclaration()) {
                    status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_name_in_declaration);
                    break superCall;
                }
            }
            fForceStatic = ASTNodes.getParent(expression, ASTNode.SUPER_CONSTRUCTOR_INVOCATION) != null || ASTNodes.getParent(expression, ASTNode.CONSTRUCTOR_INVOCATION) != null;
        }
        status.merge(LocalTypeAnalyzer.perform(fEnclosingBodyDeclaration, getSelection()));
        computeLastStatementSelected();
    }
    super.endVisit(node);
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) Message(org.eclipse.jdt.core.dom.Message) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Name(org.eclipse.jdt.core.dom.Name) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) StructuralPropertyDescriptor(org.eclipse.jdt.core.dom.StructuralPropertyDescriptor)

Example 5 with Message

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

the class SurroundWithAnalyzer method endVisit.

@Override
public void endVisit(CompilationUnit node) {
    postProcessSelectedNodes(internalGetSelectedNodes());
    ASTNode enclosingNode = null;
    superCall: {
        if (getStatus().hasFatalError()) {
            break superCall;
        }
        if (!hasSelectedNodes()) {
            ASTNode coveringNode = getLastCoveringNode();
            if (coveringNode instanceof Block) {
                Block block = (Block) coveringNode;
                Message[] messages = ASTNodes.getMessages(block, ASTNodes.NODE_ONLY);
                if (messages.length > 0) {
                    invalidSelection(RefactoringCoreMessages.SurroundWithTryCatchAnalyzer_compile_errors, JavaStatusContext.create(getCompilationUnit(), block));
                    break superCall;
                }
            }
            invalidSelection(RefactoringCoreMessages.SurroundWithTryCatchAnalyzer_doesNotCover);
            break superCall;
        }
        enclosingNode = getEnclosingNode(getFirstSelectedNode());
        boolean isValidEnclosingNode = enclosingNode instanceof MethodDeclaration || enclosingNode instanceof Initializer;
        if (fSurroundWithTryCatch) {
            isValidEnclosingNode = isValidEnclosingNode || enclosingNode instanceof MethodReference || enclosingNode.getLocationInParent() == LambdaExpression.BODY_PROPERTY;
        }
        if (!isValidEnclosingNode) {
            invalidSelection(RefactoringCoreMessages.SurroundWithTryCatchAnalyzer_doesNotContain);
            break superCall;
        }
        if (!validSelectedNodes()) {
            invalidSelection(RefactoringCoreMessages.SurroundWithTryCatchAnalyzer_onlyStatements);
        }
        fLocals = LocalDeclarationAnalyzer.perform(enclosingNode, getSelection());
    }
    super.endVisit(node);
}
Also used : Message(org.eclipse.jdt.core.dom.Message) Initializer(org.eclipse.jdt.core.dom.Initializer) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block) MethodReference(org.eclipse.jdt.core.dom.MethodReference)

Aggregations

ASTNode (org.eclipse.jdt.core.dom.ASTNode)5 Message (org.eclipse.jdt.core.dom.Message)5 Block (org.eclipse.jdt.core.dom.Block)4 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)4 FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)2 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)2 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)2 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)2 Initializer (org.eclipse.jdt.core.dom.Initializer)2 Name (org.eclipse.jdt.core.dom.Name)2 QualifiedName (org.eclipse.jdt.core.dom.QualifiedName)2 SimpleName (org.eclipse.jdt.core.dom.SimpleName)2 StructuralPropertyDescriptor (org.eclipse.jdt.core.dom.StructuralPropertyDescriptor)2 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)2 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)2 ArrayList (java.util.ArrayList)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)1 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)1 MethodReference (org.eclipse.jdt.core.dom.MethodReference)1