Search in sources :

Example 96 with Statement

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

the class ControlStatementsFix method createRemoveBlockFix.

public static ControlStatementsFix[] createRemoveBlockFix(CompilationUnit compilationUnit, ASTNode node) {
    if (!(node instanceof Statement)) {
        return null;
    }
    Statement statement = (Statement) node;
    if (statement instanceof Block) {
        Block block = (Block) statement;
        if (block.statements().size() != 1)
            return null;
        ASTNode parent = block.getParent();
        if (!(parent instanceof Statement))
            return null;
        statement = (Statement) parent;
    }
    if (statement instanceof IfStatement) {
        List<ControlStatementsFix> result = new ArrayList<ControlStatementsFix>();
        List<RemoveBlockOperation> removeAllList = new ArrayList<RemoveBlockOperation>();
        IfElseIterator iter = new IfElseIterator((IfStatement) statement);
        IfStatement item = null;
        while (iter.hasNext()) {
            item = iter.next();
            if (RemoveBlockOperation.satisfiesQuickAssistPrecondition(item, IfStatement.THEN_STATEMENT_PROPERTY)) {
                RemoveBlockOperation op = new RemoveBlockOperation(item, IfStatement.THEN_STATEMENT_PROPERTY);
                removeAllList.add(op);
                if (item == statement)
                    result.add(new ControlStatementsFix(FixMessages.ControlStatementsFix_removeIfBlock_proposalDescription, compilationUnit, new CompilationUnitRewriteOperation[] { op }));
            }
        }
        if (RemoveBlockOperation.satisfiesQuickAssistPrecondition(item, IfStatement.ELSE_STATEMENT_PROPERTY)) {
            RemoveBlockOperation op = new RemoveBlockOperation(item, IfStatement.ELSE_STATEMENT_PROPERTY);
            removeAllList.add(op);
            if (item == statement)
                result.add(new ControlStatementsFix(FixMessages.ControlStatementsFix_removeElseBlock_proposalDescription, compilationUnit, new CompilationUnitRewriteOperation[] { op }));
        }
        if (removeAllList.size() > 1) {
            CompilationUnitRewriteOperation[] allConvert = removeAllList.toArray(new CompilationUnitRewriteOperation[removeAllList.size()]);
            result.add(new ControlStatementsFix(FixMessages.ControlStatementsFix_removeIfElseBlock_proposalDescription, compilationUnit, allConvert));
        }
        return result.toArray(new ControlStatementsFix[result.size()]);
    } else if (statement instanceof WhileStatement) {
        if (RemoveBlockOperation.satisfiesQuickAssistPrecondition(statement, WhileStatement.BODY_PROPERTY)) {
            RemoveBlockOperation op = new RemoveBlockOperation(statement, WhileStatement.BODY_PROPERTY);
            return new ControlStatementsFix[] { new ControlStatementsFix(FixMessages.ControlStatementsFix_removeBrackets_proposalDescription, compilationUnit, new CompilationUnitRewriteOperation[] { op }) };
        }
    } else if (statement instanceof ForStatement) {
        if (RemoveBlockOperation.satisfiesQuickAssistPrecondition(statement, ForStatement.BODY_PROPERTY)) {
            RemoveBlockOperation op = new RemoveBlockOperation(statement, ForStatement.BODY_PROPERTY);
            return new ControlStatementsFix[] { new ControlStatementsFix(FixMessages.ControlStatementsFix_removeBrackets_proposalDescription, compilationUnit, new CompilationUnitRewriteOperation[] { op }) };
        }
    } else if (statement instanceof EnhancedForStatement) {
        if (RemoveBlockOperation.satisfiesQuickAssistPrecondition(statement, EnhancedForStatement.BODY_PROPERTY)) {
            RemoveBlockOperation op = new RemoveBlockOperation(statement, EnhancedForStatement.BODY_PROPERTY);
            return new ControlStatementsFix[] { new ControlStatementsFix(FixMessages.ControlStatementsFix_removeBrackets_proposalDescription, compilationUnit, new CompilationUnitRewriteOperation[] { op }) };
        }
    } else if (statement instanceof DoStatement) {
        if (RemoveBlockOperation.satisfiesQuickAssistPrecondition(statement, DoStatement.BODY_PROPERTY)) {
            RemoveBlockOperation op = new RemoveBlockOperation(statement, DoStatement.BODY_PROPERTY);
            return new ControlStatementsFix[] { new ControlStatementsFix(FixMessages.ControlStatementsFix_removeBrackets_proposalDescription, compilationUnit, new CompilationUnitRewriteOperation[] { op }) };
        }
    }
    return null;
}
Also used : DoStatement(org.eclipse.jdt.core.dom.DoStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) ThrowStatement(org.eclipse.jdt.core.dom.ThrowStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) ArrayList(java.util.ArrayList) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement)

Example 97 with Statement

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

the class ConvertForLoopOperation method rewriteAST.

/**
	 * {@inheritDoc}
	 */
@Override
public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel positionGroups) throws CoreException {
    TextEditGroup group = createTextEditGroup(FixMessages.Java50Fix_ConvertToEnhancedForLoop_description, cuRewrite);
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    TightSourceRangeComputer rangeComputer;
    if (rewrite.getExtendedSourceRangeComputer() instanceof TightSourceRangeComputer) {
        rangeComputer = (TightSourceRangeComputer) rewrite.getExtendedSourceRangeComputer();
    } else {
        rangeComputer = new TightSourceRangeComputer();
    }
    rangeComputer.addTightSourceNode(getForStatement());
    rewrite.setTargetSourceRangeComputer(rangeComputer);
    Statement statement = convert(cuRewrite, group, positionGroups);
    rewrite.replace(getForStatement(), statement, group);
}
Also used : TightSourceRangeComputer(org.eclipse.jdt.internal.corext.refactoring.util.TightSourceRangeComputer) Statement(org.eclipse.jdt.core.dom.Statement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 98 with Statement

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

the class CodeFormatterUtil method format2.

/**
	 * Creates edits that describe how to format the given string.
	 * The given node is used to infer the kind to use to format the string.
	 * Consider to use {@link #format2(int, String, int, String, java.util.Map)} if the kind is already known.
	 * Returns <code>null</code> if the code could not be formatted for the given kind.
	 *
	 * @param node
	 *        Use to infer the kind of the code snippet to format.
	 * @param source
	 *        The source to format
	 * @param indentationLevel
	 *        The initial indentation level, used to shift left/right the entire source fragment.
	 *        An initial indentation level of zero or below has no effect.
	 * @param lineSeparator
	 *        The line separator to use in formatted source,
	 *        if set to <code>null</code>, then the platform default one will be used.
	 * @param options
	 *        The options map to use for formatting with the default code formatter.
	 *        Recognized options are documented on {@link org.eclipse.jdt.core.JavaCore#getDefaultOptions()}.
	 *        If set to <code>null</code>, then use the current settings from {@link org.eclipse.jdt.core.JavaCore#getOptions()}.
	 * @return an TextEdit describing the changes required to format source
	 * @throws IllegalArgumentException
	 *         If the offset and length are not inside the string, a IllegalArgumentException is thrown.
	 */
public static TextEdit format2(ASTNode node, String source, int indentationLevel, String lineSeparator, Map<String, String> options) {
    int code;
    //$NON-NLS-1$
    String prefix = "";
    //$NON-NLS-1$
    String suffix = "";
    if (node instanceof Statement) {
        code = CodeFormatter.K_STATEMENTS;
        if (node.getNodeType() == ASTNode.SWITCH_CASE) {
            //$NON-NLS-1$
            prefix = "switch(1) {";
            //$NON-NLS-1$
            suffix = "}";
            code = CodeFormatter.K_STATEMENTS;
        }
    } else if (node instanceof Expression && node.getNodeType() != ASTNode.VARIABLE_DECLARATION_EXPRESSION) {
        code = CodeFormatter.K_EXPRESSION;
    } else if (node instanceof BodyDeclaration) {
        code = CodeFormatter.K_CLASS_BODY_DECLARATIONS;
    } else {
        switch(node.getNodeType()) {
            case ASTNode.ARRAY_TYPE:
            case ASTNode.PARAMETERIZED_TYPE:
            case ASTNode.PRIMITIVE_TYPE:
            case ASTNode.QUALIFIED_TYPE:
            case ASTNode.SIMPLE_TYPE:
                //$NON-NLS-1$
                suffix = " x;";
                code = CodeFormatter.K_CLASS_BODY_DECLARATIONS;
                break;
            case ASTNode.WILDCARD_TYPE:
                //$NON-NLS-1$
                prefix = "A<";
                //$NON-NLS-1$
                suffix = "> x;";
                code = CodeFormatter.K_CLASS_BODY_DECLARATIONS;
                break;
            case ASTNode.COMPILATION_UNIT:
                code = CodeFormatter.K_COMPILATION_UNIT;
                break;
            case ASTNode.VARIABLE_DECLARATION_EXPRESSION:
            case ASTNode.SINGLE_VARIABLE_DECLARATION:
                //$NON-NLS-1$
                suffix = ";";
                code = CodeFormatter.K_STATEMENTS;
                break;
            case ASTNode.VARIABLE_DECLARATION_FRAGMENT:
                //$NON-NLS-1$
                prefix = "A ";
                //$NON-NLS-1$
                suffix = ";";
                code = CodeFormatter.K_STATEMENTS;
                break;
            case ASTNode.PACKAGE_DECLARATION:
            case ASTNode.IMPORT_DECLARATION:
                //$NON-NLS-1$
                suffix = "\nclass A {}";
                code = CodeFormatter.K_COMPILATION_UNIT;
                break;
            case ASTNode.JAVADOC:
                //$NON-NLS-1$
                suffix = "void foo();";
                code = CodeFormatter.K_CLASS_BODY_DECLARATIONS;
                break;
            case ASTNode.CATCH_CLAUSE:
                //$NON-NLS-1$
                prefix = "try {}";
                code = CodeFormatter.K_STATEMENTS;
                break;
            case ASTNode.ANONYMOUS_CLASS_DECLARATION:
                //$NON-NLS-1$
                prefix = "new A()";
                //$NON-NLS-1$
                suffix = ";";
                code = CodeFormatter.K_STATEMENTS;
                break;
            case ASTNode.MEMBER_VALUE_PAIR:
                //$NON-NLS-1$
                prefix = "@Author(";
                //$NON-NLS-1$
                suffix = ") class x {}";
                code = CodeFormatter.K_COMPILATION_UNIT;
                break;
            case ASTNode.MODIFIER:
                //$NON-NLS-1$
                suffix = " class x {}";
                code = CodeFormatter.K_COMPILATION_UNIT;
                break;
            case ASTNode.TYPE_PARAMETER:
                //$NON-NLS-1$
                prefix = "class X<";
                //$NON-NLS-1$
                suffix = "> {}";
                code = CodeFormatter.K_COMPILATION_UNIT;
                break;
            case ASTNode.MEMBER_REF:
            case ASTNode.METHOD_REF:
            case ASTNode.METHOD_REF_PARAMETER:
            case ASTNode.TAG_ELEMENT:
            case ASTNode.TEXT_ELEMENT:
                // Javadoc formatting not yet supported:
                return null;
            default:
                //Assert.isTrue(false, "Node type not covered: " + node.getClass().getName()); //$NON-NLS-1$
                return null;
        }
    }
    String concatStr = prefix + source + suffix;
    TextEdit edit = format2(code, concatStr, prefix.length(), source.length(), indentationLevel, lineSeparator, options);
    if (edit != null && prefix.length() > 0) {
        edit.moveTree(-prefix.length());
    }
    return edit;
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) Statement(org.eclipse.jdt.core.dom.Statement) TextEdit(org.eclipse.text.edits.TextEdit) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration)

Example 99 with Statement

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

the class IntroduceFactoryRefactoring method createFactoryMethod.

/**
	 * Creates and returns a new MethodDeclaration that represents the factory method to be used in
	 * place of direct calls to the constructor in question.
	 * 
	 * @param ast An AST used as a factory for various AST nodes
	 * @param ctorBinding binding for the constructor being wrapped
	 * @param unitRewriter the ASTRewrite to be used
	 * @return the new method declaration
	 * @throws CoreException if an exception occurs while accessing its corresponding resource
	 */
private MethodDeclaration createFactoryMethod(AST ast, IMethodBinding ctorBinding, ASTRewrite unitRewriter) throws CoreException {
    MethodDeclaration newMethod = ast.newMethodDeclaration();
    SimpleName newMethodName = ast.newSimpleName(fNewMethodName);
    ClassInstanceCreation newCtorCall = ast.newClassInstanceCreation();
    ReturnStatement ret = ast.newReturnStatement();
    Block body = ast.newBlock();
    List<Statement> stmts = body.statements();
    String retTypeName = ctorBinding.getName();
    createFactoryMethodSignature(ast, newMethod);
    newMethod.setName(newMethodName);
    newMethod.setBody(body);
    ITypeBinding declaringClass = fCtorBinding.getDeclaringClass();
    ITypeBinding[] ctorOwnerTypeParameters = declaringClass.getTypeParameters();
    setMethodReturnType(newMethod, retTypeName, ctorOwnerTypeParameters, ast);
    newMethod.modifiers().addAll(ASTNodeFactory.newModifiers(ast, Modifier.STATIC | Modifier.PUBLIC));
    setCtorTypeArguments(newCtorCall, retTypeName, ctorOwnerTypeParameters, ast);
    createFactoryMethodConstructorArgs(ast, newCtorCall);
    if (Modifier.isAbstract(declaringClass.getModifiers())) {
        AnonymousClassDeclaration decl = ast.newAnonymousClassDeclaration();
        IMethodBinding[] unimplementedMethods = getUnimplementedMethods(declaringClass);
        CodeGenerationSettings settings = JavaPreferencesSettings.getCodeGenerationSettings(fCUHandle.getJavaProject());
        ImportRewriteContext context = new ContextSensitiveImportRewriteContext(fFactoryCU, decl.getStartPosition(), fImportRewriter);
        for (int i = 0; i < unimplementedMethods.length; i++) {
            IMethodBinding unImplementedMethod = unimplementedMethods[i];
            MethodDeclaration newMethodDecl = StubUtility2.createImplementationStub(fCUHandle, unitRewriter, fImportRewriter, context, unImplementedMethod, unImplementedMethod.getDeclaringClass().getName(), settings, false);
            decl.bodyDeclarations().add(newMethodDecl);
        }
        newCtorCall.setAnonymousClassDeclaration(decl);
    }
    ret.setExpression(newCtorCall);
    stmts.add(ret);
    return newMethod;
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) CodeGenerationSettings(org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) Statement(org.eclipse.jdt.core.dom.Statement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) SimpleName(org.eclipse.jdt.core.dom.SimpleName) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) Block(org.eclipse.jdt.core.dom.Block)

Example 100 with Statement

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

the class SourceProvider method isSingleControlStatementWithoutBlock.

private boolean isSingleControlStatementWithoutBlock() {
    List<Statement> statements = fDeclaration.getBody().statements();
    int size = statements.size();
    if (size != 1)
        return false;
    Statement statement = statements.get(size - 1);
    int nodeType = statement.getNodeType();
    if (nodeType == ASTNode.IF_STATEMENT) {
        IfStatement ifStatement = (IfStatement) statement;
        return !(ifStatement.getThenStatement() instanceof Block) && !(ifStatement.getElseStatement() instanceof Block);
    } else if (nodeType == ASTNode.FOR_STATEMENT) {
        return !(((ForStatement) statement).getBody() instanceof Block);
    } else if (nodeType == ASTNode.WHILE_STATEMENT) {
        return !(((WhileStatement) statement).getBody() instanceof Block);
    }
    return false;
}
Also used : IfStatement(org.eclipse.jdt.core.dom.IfStatement) DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) LabeledStatement(org.eclipse.jdt.core.dom.LabeledStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) Block(org.eclipse.jdt.core.dom.Block) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement)

Aggregations

Statement (org.eclipse.jdt.core.dom.Statement)178 IfStatement (org.eclipse.jdt.core.dom.IfStatement)107 ForStatement (org.eclipse.jdt.core.dom.ForStatement)89 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)85 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)83 WhileStatement (org.eclipse.jdt.core.dom.WhileStatement)77 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)76 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)75 Block (org.eclipse.jdt.core.dom.Block)70 DoStatement (org.eclipse.jdt.core.dom.DoStatement)70 Expression (org.eclipse.jdt.core.dom.Expression)61 SwitchStatement (org.eclipse.jdt.core.dom.SwitchStatement)61 ASTNode (org.eclipse.jdt.core.dom.ASTNode)54 TryStatement (org.eclipse.jdt.core.dom.TryStatement)48 BreakStatement (org.eclipse.jdt.core.dom.BreakStatement)43 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)39 ArrayList (java.util.ArrayList)37 AST (org.eclipse.jdt.core.dom.AST)37 ContinueStatement (org.eclipse.jdt.core.dom.ContinueStatement)34 SimpleName (org.eclipse.jdt.core.dom.SimpleName)34