Search in sources :

Example 36 with BodyDeclaration

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

the class ExtractTempRefactoring method isReferringToLocalVariableFromFor.

private static boolean isReferringToLocalVariableFromFor(Expression expression) {
    ASTNode current = expression;
    ASTNode parent = current.getParent();
    while (parent != null && !(parent instanceof BodyDeclaration)) {
        if (parent instanceof ForStatement) {
            ForStatement forStmt = (ForStatement) parent;
            if (forStmt.initializers().contains(current) || forStmt.updaters().contains(current) || forStmt.getExpression() == current) {
                List<Expression> initializers = forStmt.initializers();
                if (initializers.size() == 1 && initializers.get(0) instanceof VariableDeclarationExpression) {
                    List<IVariableBinding> forInitializerVariables = getForInitializedVariables((VariableDeclarationExpression) initializers.get(0));
                    ForStatementChecker checker = new ForStatementChecker(forInitializerVariables);
                    expression.accept(checker);
                    if (checker.isReferringToForVariable()) {
                        return true;
                    }
                }
            }
        }
        current = parent;
        parent = current.getParent();
    }
    return false;
}
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) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Example 37 with BodyDeclaration

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

the class ExtractTempRefactoring method getEnclosingBodyNode.

private ASTNode getEnclosingBodyNode() throws JavaModelException {
    ASTNode node = getSelectedExpression().getAssociatedNode();
    // expression must be in a method, lambda or initializer body
    // make sure it is not in method or parameter annotation
    StructuralPropertyDescriptor location = null;
    while (node != null && !(node instanceof BodyDeclaration)) {
        location = node.getLocationInParent();
        node = node.getParent();
        if (node instanceof LambdaExpression) {
            break;
        }
    }
    if (location == MethodDeclaration.BODY_PROPERTY || location == Initializer.BODY_PROPERTY || (location == LambdaExpression.BODY_PROPERTY && ((LambdaExpression) node).resolveMethodBinding() != null)) {
        return (ASTNode) node.getStructuralProperty(location);
    }
    return null;
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) StructuralPropertyDescriptor(org.eclipse.jdt.core.dom.StructuralPropertyDescriptor)

Example 38 with BodyDeclaration

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

the class QuickAssistProcessor method getConvertStringConcatenationProposals.

private static boolean getConvertStringConcatenationProposals(IInvocationContext context, Collection<ICommandAccess> resultingCollections) {
    ASTNode node = context.getCoveringNode();
    BodyDeclaration parentDecl = ASTResolving.findParentBodyDeclaration(node);
    if (!(parentDecl instanceof MethodDeclaration || parentDecl instanceof Initializer))
        return false;
    AST ast = node.getAST();
    // $NON-NLS-1$
    ITypeBinding stringBinding = ast.resolveWellKnownType("java.lang.String");
    if (node instanceof Expression && !(node instanceof InfixExpression)) {
        node = node.getParent();
    }
    if (node instanceof VariableDeclarationFragment) {
        node = ((VariableDeclarationFragment) node).getInitializer();
    } else if (node instanceof Assignment) {
        node = ((Assignment) node).getRightHandSide();
    }
    InfixExpression oldInfixExpression = null;
    while (node instanceof InfixExpression) {
        InfixExpression curr = (InfixExpression) node;
        if (curr.resolveTypeBinding() == stringBinding && curr.getOperator() == InfixExpression.Operator.PLUS) {
            // is a infix expression we can use
            oldInfixExpression = curr;
        } else {
            break;
        }
        node = node.getParent();
    }
    if (oldInfixExpression == null)
        return false;
    if (resultingCollections == null) {
        return true;
    }
    LinkedCorrectionProposal stringBufferProposal = getConvertToStringBufferProposal(context, ast, oldInfixExpression);
    resultingCollections.add(stringBufferProposal);
    ASTRewriteCorrectionProposal messageFormatProposal = getConvertToMessageFormatProposal(context, ast, oldInfixExpression);
    if (messageFormatProposal != null)
        resultingCollections.add(messageFormatProposal);
    return true;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) AST(org.eclipse.jdt.core.dom.AST) ArrayInitializer(org.eclipse.jdt.core.dom.ArrayInitializer) Initializer(org.eclipse.jdt.core.dom.Initializer) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) LinkedCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration)

Example 39 with BodyDeclaration

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

the class ASTResolving method findParentBodyDeclaration.

public static BodyDeclaration findParentBodyDeclaration(ASTNode node, boolean treatModifiersOutside) {
    StructuralPropertyDescriptor lastLocation = null;
    while (node != null) {
        if (node instanceof BodyDeclaration) {
            BodyDeclaration decl = (BodyDeclaration) node;
            if (!treatModifiersOutside || lastLocation != decl.getModifiersProperty()) {
                return decl;
            }
            treatModifiersOutside = false;
        }
        lastLocation = node.getLocationInParent();
        node = node.getParent();
    }
    return (BodyDeclaration) node;
}
Also used : BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) StructuralPropertyDescriptor(org.eclipse.jdt.core.dom.StructuralPropertyDescriptor)

Example 40 with BodyDeclaration

use of org.eclipse.jdt.core.dom.BodyDeclaration in project flux 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, 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 JavaCore#getDefaultOptions()}.
 *        If set to <code>null</code>, then use the current settings from {@link 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)

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