Search in sources :

Example 66 with Block

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

the class TypeContextChecker method fillWithTypeStubs.

private static void fillWithTypeStubs(final StringBuffer bufBefore, final StringBuffer bufAfter, final int focalPosition, List<? extends BodyDeclaration> types) {
    StringBuffer buf;
    for (Iterator<? extends BodyDeclaration> iter = types.iterator(); iter.hasNext(); ) {
        BodyDeclaration bodyDeclaration = iter.next();
        if (!(bodyDeclaration instanceof AbstractTypeDeclaration)) {
            //account for local classes:
            if (!(bodyDeclaration instanceof MethodDeclaration))
                continue;
            int bodyStart = bodyDeclaration.getStartPosition();
            int bodyEnd = bodyDeclaration.getStartPosition() + bodyDeclaration.getLength();
            if (!(bodyStart < focalPosition && focalPosition < bodyEnd))
                continue;
            MethodDeclaration methodDeclaration = (MethodDeclaration) bodyDeclaration;
            buf = bufBefore;
            appendModifiers(buf, methodDeclaration.modifiers());
            appendTypeParameters(buf, methodDeclaration.typeParameters());
            //$NON-NLS-1$
            buf.append(" void ");
            buf.append(methodDeclaration.getName().getIdentifier());
            //$NON-NLS-1$
            buf.append("(){\n");
            Block body = methodDeclaration.getBody();
            body.accept(new HierarchicalASTVisitor() {

                @Override
                public boolean visit(AbstractTypeDeclaration node) {
                    fillWithTypeStubs(bufBefore, bufAfter, focalPosition, Collections.singletonList(node));
                    return false;
                }

                @Override
                public boolean visit(ClassInstanceCreation node) {
                    AnonymousClassDeclaration anonDecl = node.getAnonymousClassDeclaration();
                    if (anonDecl == null)
                        // could be in CIC parameter list
                        return true;
                    int anonStart = anonDecl.getStartPosition();
                    int anonEnd = anonDecl.getStartPosition() + anonDecl.getLength();
                    if (!(anonStart < focalPosition && focalPosition < anonEnd))
                        return false;
                    //$NON-NLS-1$
                    bufBefore.append(" new ");
                    bufBefore.append(node.getType().toString());
                    //$NON-NLS-1$
                    bufBefore.append("(){\n");
                    fillWithTypeStubs(bufBefore, bufAfter, focalPosition, anonDecl.bodyDeclarations());
                    //$NON-NLS-1$
                    bufAfter.append("};\n");
                    return false;
                }
            });
            buf = bufAfter;
            //$NON-NLS-1$
            buf.append("}\n");
            continue;
        }
        AbstractTypeDeclaration decl = (AbstractTypeDeclaration) bodyDeclaration;
        buf = decl.getStartPosition() < focalPosition ? bufBefore : bufAfter;
        appendModifiers(buf, decl.modifiers());
        if (decl instanceof TypeDeclaration) {
            TypeDeclaration type = (TypeDeclaration) decl;
            //$NON-NLS-1$//$NON-NLS-2$
            buf.append(type.isInterface() ? "interface " : "class ");
            buf.append(type.getName().getIdentifier());
            appendTypeParameters(buf, type.typeParameters());
            if (type.getSuperclassType() != null) {
                //$NON-NLS-1$
                buf.append(" extends ");
                buf.append(ASTNodes.asString(type.getSuperclassType()));
            }
            List<Type> superInterfaces = type.superInterfaceTypes();
            appendSuperInterfaces(buf, superInterfaces);
        } else if (decl instanceof AnnotationTypeDeclaration) {
            AnnotationTypeDeclaration annotation = (AnnotationTypeDeclaration) decl;
            //$NON-NLS-1$
            buf.append("@interface ");
            buf.append(annotation.getName().getIdentifier());
        } else if (decl instanceof EnumDeclaration) {
            EnumDeclaration enumDecl = (EnumDeclaration) decl;
            //$NON-NLS-1$
            buf.append("enum ");
            buf.append(enumDecl.getName().getIdentifier());
            List<Type> superInterfaces = enumDecl.superInterfaceTypes();
            appendSuperInterfaces(buf, superInterfaces);
        }
        //$NON-NLS-1$
        buf.append("{\n");
        if (decl instanceof EnumDeclaration)
            //$NON-NLS-1$
            buf.append(";\n");
        fillWithTypeStubs(bufBefore, bufAfter, focalPosition, decl.bodyDeclarations());
        buf = decl.getStartPosition() + decl.getLength() < focalPosition ? bufBefore : bufAfter;
        //$NON-NLS-1$
        buf.append("}\n");
    }
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) AnnotationTypeDeclaration(org.eclipse.jdt.core.dom.AnnotationTypeDeclaration) HierarchicalASTVisitor(org.eclipse.jdt.internal.corext.dom.HierarchicalASTVisitor) EnumDeclaration(org.eclipse.jdt.core.dom.EnumDeclaration) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) IType(org.eclipse.jdt.core.IType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) Type(org.eclipse.jdt.core.dom.Type) QualifiedType(org.eclipse.jdt.core.dom.QualifiedType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) Block(org.eclipse.jdt.core.dom.Block) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) AnnotationTypeDeclaration(org.eclipse.jdt.core.dom.AnnotationTypeDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 67 with Block

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

the class ExtractTempRefactoring method createAndInsertTempDeclaration.

private void createAndInsertTempDeclaration() throws CoreException {
    Expression initializer = getSelectedExpression().createCopyTarget(fCURewrite.getASTRewrite(), true);
    VariableDeclarationStatement vds = createTempDeclaration(initializer);
    if ((!fReplaceAllOccurrences) || (retainOnlyReplacableMatches(getMatchingFragments()).length <= 1)) {
        insertAt(getSelectedExpression().getAssociatedNode(), vds);
        return;
    }
    ASTNode[] firstReplaceNodeParents = getParents(getFirstReplacedExpression().getAssociatedNode());
    ASTNode[] commonPath = findDeepestCommonSuperNodePathForReplacedNodes();
    Assert.isTrue(commonPath.length <= firstReplaceNodeParents.length);
    ASTNode deepestCommonParent = firstReplaceNodeParents[commonPath.length - 1];
    if (deepestCommonParent instanceof Block)
        insertAt(firstReplaceNodeParents[commonPath.length], vds);
    else
        insertAt(deepestCommonParent, vds);
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) Block(org.eclipse.jdt.core.dom.Block)

Example 68 with Block

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

the class ExtractTempRefactoring method replaceSelectedExpressionWithTempDeclaration.

private void replaceSelectedExpressionWithTempDeclaration() throws CoreException {
    ASTRewrite rewrite = fCURewrite.getASTRewrite();
    // whole expression selected
    Expression selectedExpression = getSelectedExpression().getAssociatedExpression();
    Expression initializer = (Expression) rewrite.createMoveTarget(selectedExpression);
    // creates a VariableDeclarationStatement
    ASTNode replacement = createTempDeclaration(initializer);
    ExpressionStatement parent = (ExpressionStatement) selectedExpression.getParent();
    if (ASTNodes.isControlStatementBody(parent.getLocationInParent())) {
        Block block = rewrite.getAST().newBlock();
        block.statements().add(replacement);
        replacement = block;
    }
    rewrite.replace(parent, replacement, fCURewrite.createGroupDescription(RefactoringCoreMessages.ExtractTempRefactoring_declare_local_variable));
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Block(org.eclipse.jdt.core.dom.Block)

Example 69 with Block

use of org.eclipse.jdt.core.dom.Block 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 70 with Block

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

the class ExtractMethodAnalyzer method visit.

@Override
public boolean visit(MethodDeclaration node) {
    Block body = node.getBody();
    if (body == null)
        return false;
    Selection selection = getSelection();
    int nodeStart = body.getStartPosition();
    int nodeExclusiveEnd = nodeStart + body.getLength();
    // if selection node inside of the method body ignore method
    if (!(nodeStart < selection.getOffset() && selection.getExclusiveEnd() < nodeExclusiveEnd))
        return false;
    return super.visit(node);
}
Also used : Selection(org.eclipse.jdt.internal.corext.dom.Selection) Block(org.eclipse.jdt.core.dom.Block)

Aggregations

Block (org.eclipse.jdt.core.dom.Block)105 ASTNode (org.eclipse.jdt.core.dom.ASTNode)61 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)45 AST (org.eclipse.jdt.core.dom.AST)44 Statement (org.eclipse.jdt.core.dom.Statement)42 Expression (org.eclipse.jdt.core.dom.Expression)39 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)38 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)37 ForStatement (org.eclipse.jdt.core.dom.ForStatement)35 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)35 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)34 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)30 IfStatement (org.eclipse.jdt.core.dom.IfStatement)30 WhileStatement (org.eclipse.jdt.core.dom.WhileStatement)28 DoStatement (org.eclipse.jdt.core.dom.DoStatement)26 Type (org.eclipse.jdt.core.dom.Type)26 SimpleName (org.eclipse.jdt.core.dom.SimpleName)22 SwitchStatement (org.eclipse.jdt.core.dom.SwitchStatement)21 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)19 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)18