Example usage for org.eclipse.jdt.core.dom VariableDeclarationFragment INITIALIZER_PROPERTY

List of usage examples for org.eclipse.jdt.core.dom VariableDeclarationFragment INITIALIZER_PROPERTY

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom VariableDeclarationFragment INITIALIZER_PROPERTY.

Prototype

ChildPropertyDescriptor INITIALIZER_PROPERTY

To view the source code for org.eclipse.jdt.core.dom VariableDeclarationFragment INITIALIZER_PROPERTY.

Click Source Link

Document

The "initializer" structural property of this node type (child type: Expression ).

Usage

From source file:org.autorefactor.refactoring.rules.UseDiamondOperatorRefactoring.java

License:Open Source License

private boolean parentAllowsDiamondOperator(ClassInstanceCreation node) {
    final ASTNode parentInfo = getFirstParentOfType(node, ParenthesizedExpression.class);
    final StructuralPropertyDescriptor locationInParent = parentInfo.getLocationInParent();

    switch (parentInfo.getParent().getNodeType()) {
    case ASSIGNMENT:
        return Assignment.RIGHT_HAND_SIDE_PROPERTY.equals(locationInParent);
    case METHOD_INVOCATION:
        return false; // FIXME some of them can be refactored
    case RETURN_STATEMENT:
        return ReturnStatement.EXPRESSION_PROPERTY.equals(locationInParent);
    case VARIABLE_DECLARATION_FRAGMENT:
        return VariableDeclarationFragment.INITIALIZER_PROPERTY.equals(locationInParent);
    default://from   w  ww .j  a  v a  2  s .c  o m
        return false;
    }
}

From source file:org.eclipse.emf.codegen.merge.java.facade.ast.ASTJField.java

License:Open Source License

/**
 * If required, separates the variable declaration fragment into a new {@link FieldDeclaration}
 * object. If this declaration does not need to be split, reverts the changes made by {@link #prepareSplit()}.
 * <p>/*from w  ww  .j  a  v  a  2 s.com*/
 * New field declaration will have only one variable declaration fragment. 
 * New declaration is added to the {@link ASTRewrite}.
 * The attributes of this ASTJField are updated to reference elements of the new declaration.
 * Only the javadoc, variable initializer, and annotations are copied as String, all other attributes are copied
 * using {@link ASTNode#copySubtree(org.eclipse.jdt.core.dom.AST, ASTNode)}. All
 * formatting except for Javadoc, initializer, and annotations is lost.
 * If field declaration wrapped by ASTJField has only one variable declaration
 * fragment left, no changes are made.
 * <p>
 * Note that this method must be called after {@link #prepareSplit()} and before any
 * changes are made to the nodes.
 * 
 * @see #prepareSplit()
 */
protected void performSplit() {
    if (!splitPerformed && getASTNode() != originalFieldDeclaration) {
        ListRewrite listRewrite = rewriter.getListRewrite(originalFieldDeclaration,
                FieldDeclaration.FRAGMENTS_PROPERTY);
        List<?> fragments = listRewrite.getRewrittenList();

        // perform split if there is more than 1 fragment
        if (fragments.size() > 1) {
            FieldDeclaration newDeclaration = getASTNode();

            // set javadoc
            if (originalFieldDeclaration.getJavadoc() != null) {
                String javadocString = getFacadeHelper()
                        .applyFormatRules(getFacadeHelper().toString(originalFieldDeclaration.getJavadoc()));
                setTrackedNodeProperty(newDeclaration, javadocString, newDeclaration.getJavadocProperty(),
                        ASTNode.JAVADOC);
            }

            // set initializer
            if (variableDeclarationFragment.getInitializer() != null) {
                if (initializer == UNITIALIZED_STRING) {
                    initializer = getFacadeHelper().applyFormatRules(
                            getFacadeHelper().toString(variableDeclarationFragment.getInitializer()));
                }
                setTrackedNodeProperty(variableDeclarationFragment, initializer,
                        VariableDeclarationFragment.INITIALIZER_PROPERTY, ASTNode.JAVADOC);
            }

            // set annotations contents
            @SuppressWarnings("unchecked")
            Iterator<IExtendedModifier> newModifiersIterator = newDeclaration.modifiers().iterator();
            @SuppressWarnings("unchecked")
            Iterator<IExtendedModifier> originalModifiersIterator = originalFieldDeclaration.modifiers()
                    .iterator();

            for (; newModifiersIterator.hasNext() && originalModifiersIterator.hasNext();) {
                IExtendedModifier modifier = newModifiersIterator.next();
                IExtendedModifier originalModifier = originalModifiersIterator.next();
                if (originalModifier.isAnnotation()) {
                    ASTJAnnotation astjAnnotation = (ASTJAnnotation) getFacadeHelper().convertToNode(modifier);
                    astjAnnotation.trackAndReplace(astjAnnotation.getRewrittenASTNode(),
                            getFacadeHelper().applyFormatRules(getFacadeHelper().toString(originalModifier)));
                }
            }

            // insert new declaration before this one
            listRewrite = rewriter.getListRewrite(originalFieldDeclaration.getParent(),
                    (ChildListPropertyDescriptor) originalFieldDeclaration.getLocationInParent());
            listRewrite.insertBefore(newDeclaration, originalFieldDeclaration, null);

            // update the wrapped object
            setWrappedObject(newDeclaration);

            // delete variable fragment from old declaration
            removeNodeFromListProperty(originalFieldDeclaration, variableDeclarationFragment,
                    FieldDeclaration.FRAGMENTS_PROPERTY);

            // add variable fragment to new declaration
            ListRewrite newListRewrite = rewriter.getListRewrite(newDeclaration,
                    FieldDeclaration.FRAGMENTS_PROPERTY);
            newListRewrite.insertFirst(variableDeclarationFragment, null);
        } else {
            // only 1 fragment left - revert the changes
            revertPrepareSplit();
        }
    }
    // split is performed
    splitPerformed = true;
}

From source file:org.eclipse.wb.internal.swing.model.property.editor.models.tree.TreeModelEvaluator.java

License:Open Source License

public Object evaluate(EvaluationContext context, Expression expression, ITypeBinding typeBinding,
        String typeQualifiedName) throws Exception {
    AnonymousClassDeclaration rootDeclaration = findRootNodeDeclaration(expression);
    if (rootDeclaration != null) {
        // create root node
        final DefaultMutableTreeNode rootNode;
        {/*w  w  w. j a v a 2 s.  c  om*/
            ClassInstanceCreation rootNodeCreation = (ClassInstanceCreation) rootDeclaration.getParent();
            StringLiteral rootTextLiteral = (StringLiteral) rootNodeCreation.arguments().get(0);
            rootNode = new DefaultMutableTreeNode(rootTextLiteral.getLiteralValue());
        }
        // create nodes
        final Map<String, DefaultMutableTreeNode> nameToNode = Maps.newTreeMap();
        rootDeclaration.accept(new ASTVisitor() {
            private DefaultMutableTreeNode m_lastNode;

            @Override
            public void endVisit(ClassInstanceCreation creation) {
                if (AstNodeUtils.getFullyQualifiedName(creation, false)
                        .equals("javax.swing.tree.DefaultMutableTreeNode") && creation.arguments().size() == 1
                        && creation.arguments().get(0) instanceof StringLiteral) {
                    StringLiteral stringLiteral = (StringLiteral) creation.arguments().get(0);
                    DefaultMutableTreeNode node = new DefaultMutableTreeNode(stringLiteral.getLiteralValue());
                    if (creation.getLocationInParent() == VariableDeclarationFragment.INITIALIZER_PROPERTY) {
                        String name = ((VariableDeclarationFragment) creation.getParent()).getName()
                                .getIdentifier();
                        nameToNode.put(name, node);
                    } else if (creation.getLocationInParent() == Assignment.RIGHT_HAND_SIDE_PROPERTY
                            && ((Assignment) creation.getParent()).getLeftHandSide() instanceof SimpleName) {
                        Assignment assignment = (Assignment) creation.getParent();
                        SimpleName variable = (SimpleName) assignment.getLeftHandSide();
                        String name = variable.getIdentifier();
                        nameToNode.put(name, node);
                    } else {
                        m_lastNode = node;
                    }
                }
            }

            @Override
            public void endVisit(MethodInvocation invocation) {
                if (AstNodeUtils.getMethodSignature(invocation)
                        .equals("add(javax.swing.tree.MutableTreeNode)")) {
                    // prepare node
                    DefaultMutableTreeNode node = null;
                    {
                        Object argument = invocation.arguments().get(0);
                        if (argument instanceof SimpleName) {
                            SimpleName variable = (SimpleName) argument;
                            node = nameToNode.get(variable.getIdentifier());
                        } else if (argument instanceof ClassInstanceCreation) {
                            node = m_lastNode;
                        }
                    }
                    // prepare parent
                    DefaultMutableTreeNode parentNode = null;
                    if (invocation.getExpression() instanceof SimpleName) {
                        SimpleName variable = (SimpleName) invocation.getExpression();
                        parentNode = nameToNode.get(variable.getIdentifier());
                    } else if (invocation.getExpression() == null) {
                        parentNode = rootNode;
                    }
                    // add node to parent
                    if (parentNode != null && node != null) {
                        parentNode.add(node);
                    }
                    // clear last node
                    m_lastNode = null;
                }
            }
        });
        // OK, return model
        return new DefaultTreeModel(rootNode);
    }
    // we don't understand given expression
    return AstEvaluationEngine.UNKNOWN;
}

From source file:org.moe.natjgen.FieldEditor.java

License:Apache License

public void setInitializer(Type type, long constant) throws NotEditableException {
    editLock();//from  www.java2  s. c o m

    Expression v = getExpressionForConstantValue(type.getKind(), constant);

    getRewrite().set(getFirstFragment(), VariableDeclarationFragment.INITIALIZER_PROPERTY, v, getEditGroup());
}

From source file:org.moe.natjgen.FieldEditor.java

License:Apache License

public void setInitializer(Type type, long const64, long const32) throws NotEditableException {
    editLock();// w w  w.  j  av a 2s .  c  o m

    Expression v64 = getExpressionForConstantValue(type.getKind(), const64);
    Expression v32;
    if (type.getDowngradeAnnotation() == null) {
        v32 = getExpressionForConstantValue(type.getKind(), const32);
    } else if (Constants.NIntAnnotationFQ.equals(type.getDowngradeAnnotation())) {
        v32 = getExpressionForConstantValue(type.getKind(), const32);
    } else if (Constants.NUIntAnnotationFQ.equals(type.getDowngradeAnnotation())) {
        v32 = getExpressionForConstantValue(type.getKind(), const32);
    } else if (Constants.NFloatAnnotationFQ.equals(type.getDowngradeAnnotation())) {
        v32 = getExpressionForConstantValue(Type.Float, const32);
    } else {
        throw new IllegalStateException();
    }

    /**
     * <NatJFQ>
     */
    Name objc_runtime = getAST().newName(Constants.NatJFQ);

    /**
     * <NatJFQ>.is64Bit()
     */
    MethodInvocation assoc_inv = getAST().newMethodInvocation();
    getRewrite().set(assoc_inv, MethodInvocation.EXPRESSION_PROPERTY, objc_runtime, getEditGroup());
    getRewrite().set(assoc_inv, MethodInvocation.NAME_PROPERTY, getAST().newName("is64Bit"), getEditGroup());

    /**
     * <NatJFQ>.is64Bit() ? <v64> : <v32>
     */
    ConditionalExpression cond = getAST().newConditionalExpression();
    getRewrite().set(cond, ConditionalExpression.EXPRESSION_PROPERTY, assoc_inv, getEditGroup());
    getRewrite().set(cond, ConditionalExpression.THEN_EXPRESSION_PROPERTY, v64, getEditGroup());
    getRewrite().set(cond, ConditionalExpression.ELSE_EXPRESSION_PROPERTY, v32, getEditGroup());

    /**
     * Add additional casting required by byte, short and char
     */
    if (type.getKind() == Type.Byte || type.getKind() == Type.Short || type.getKind() == Type.Char) {
        CastExpression cast = getAST().newCastExpression();
        if (type.getKind() == Type.Byte) {
            cast.setType(getAST().newPrimitiveType(PrimitiveType.BYTE));
        } else if (type.getKind() == Type.Short) {
            cast.setType(getAST().newPrimitiveType(PrimitiveType.SHORT));
        } else if (type.getKind() == Type.Char) {
            cast.setType(getAST().newPrimitiveType(PrimitiveType.CHAR));
        } else {
            throw new IllegalStateException();
        }
        ParenthesizedExpression parenthesized = getAST().newParenthesizedExpression();
        parenthesized.setExpression(cond);
        cast.setExpression(parenthesized);
        getRewrite().set(getFirstFragment(), VariableDeclarationFragment.INITIALIZER_PROPERTY, cast,
                getEditGroup());
    } else {
        getRewrite().set(getFirstFragment(), VariableDeclarationFragment.INITIALIZER_PROPERTY, cond,
                getEditGroup());
    }
}

From source file:org.moe.natjgen.MethodEditor.java

License:Apache License

@SuppressWarnings("unchecked")
public void updateSafePropertyBinding(String getter, String setter, CalleeArgument calleeArgument) {
    Block block = (Block) getRewrite().get(methodDecl, MethodDeclaration.BODY_PROPERTY);
    if (block == null) {
        block = getAST().newBlock();//from   w  ww  .  j a  va  2  s.c  om
        getRewrite().set(methodDecl, MethodDeclaration.BODY_PROPERTY, block, getEditGroup());
    }
    ListRewrite block_stmts = getRewrite().getListRewrite(block, Block.STATEMENTS_PROPERTY);
    for (ASTNode object : (List<ASTNode>) block_stmts.getRewrittenList()) {
        block_stmts.remove(object, getEditGroup());
    }

    /**
     * <ObjCRuntimeFQ>
     */
    Name objc_runtime = getAST().newName(Constants.ObjCRuntimeFQ);

    /**
     * Object __old = <getter>()
     */
    SimpleType objcobj_type = getAST().newSimpleType(getAST().newName("Object"));
    MethodInvocation getter_inv = getAST().newMethodInvocation();
    getRewrite().set(getter_inv, MethodInvocation.NAME_PROPERTY, getAST().newName(getter), getEditGroup());

    Name old_obj = getAST().newName("__old");
    VariableDeclarationFragment vdf = getAST().newVariableDeclarationFragment();
    getRewrite().set(vdf, VariableDeclarationFragment.NAME_PROPERTY, old_obj, getEditGroup());
    getRewrite().set(vdf, VariableDeclarationFragment.INITIALIZER_PROPERTY, getter_inv, getEditGroup());
    VariableDeclarationExpression vde = getAST().newVariableDeclarationExpression(vdf);
    getRewrite().set(vde, VariableDeclarationExpression.TYPE_PROPERTY,
            ASTNode.copySubtree(getAST(), objcobj_type), getEditGroup());
    insertStatement(block_stmts, vde);

    /**
     * <ObjCRuntimeFQ>.associateObjCObject(this, <argument>)
     */
    MethodInvocation assoc_inv = getAST().newMethodInvocation();
    getRewrite().set(assoc_inv, MethodInvocation.EXPRESSION_PROPERTY, objc_runtime, getEditGroup());
    getRewrite().set(assoc_inv, MethodInvocation.NAME_PROPERTY, getAST().newName("associateObjCObject"),
            getEditGroup());
    ListRewrite assoc_inv_args = getRewrite().getListRewrite(assoc_inv, MethodInvocation.ARGUMENTS_PROPERTY);
    assoc_inv_args.insertLast(getAST().newThisExpression(), getEditGroup());
    assoc_inv_args.insertLast(getAST().newSimpleName(calleeArgument.getName()), getEditGroup());

    /**
     * if (<arg> != null) { ObjCRuntime.associateObjCObject(this, <arg>); }
     */
    {
        Block con_block = getAST().newBlock();
        ListRewrite con_block_stmt = getRewrite().getListRewrite(con_block, Block.STATEMENTS_PROPERTY);
        insertStatement(con_block_stmt, assoc_inv);

        InfixExpression con_obj_nonnull = getAST().newInfixExpression();
        getRewrite().set(con_obj_nonnull, InfixExpression.OPERATOR_PROPERTY,
                InfixExpression.Operator.NOT_EQUALS, getEditGroup());
        getRewrite().set(con_obj_nonnull, InfixExpression.LEFT_OPERAND_PROPERTY,
                getAST().newSimpleName(calleeArgument.getName()), getEditGroup());
        getRewrite().set(con_obj_nonnull, InfixExpression.RIGHT_OPERAND_PROPERTY, getAST().newNullLiteral(),
                getEditGroup());

        IfStatement if_obj_nonnull = getAST().newIfStatement();
        getRewrite().set(if_obj_nonnull, IfStatement.EXPRESSION_PROPERTY, con_obj_nonnull, getEditGroup());
        getRewrite().set(if_obj_nonnull, IfStatement.THEN_STATEMENT_PROPERTY, con_block, getEditGroup());
        block_stmts.insertLast(if_obj_nonnull, getEditGroup());
    }

    /**
     * <setter>(...)
     */
    MethodInvocation setter_inv = getAST().newMethodInvocation();
    getRewrite().set(setter_inv, MethodInvocation.NAME_PROPERTY, getAST().newName(setter), getEditGroup());
    ListRewrite setter_inv_args = getRewrite().getListRewrite(setter_inv, MethodInvocation.ARGUMENTS_PROPERTY);
    setter_inv_args.insertLast(getAST().newSimpleName(calleeArgument.getName()), getEditGroup());
    insertStatement(block_stmts, setter_inv);

    /**
     * <ObjCRuntimeFQ>.dissociateObjCObject(this, old)
     */
    MethodInvocation unassoc_inv = getAST().newMethodInvocation();
    getRewrite().set(unassoc_inv, MethodInvocation.EXPRESSION_PROPERTY,
            ASTNode.copySubtree(getAST(), objc_runtime), getEditGroup());
    getRewrite().set(unassoc_inv, MethodInvocation.NAME_PROPERTY, getAST().newName("dissociateObjCObject"),
            getEditGroup());
    ListRewrite unassoc_inv_args = getRewrite().getListRewrite(unassoc_inv,
            MethodInvocation.ARGUMENTS_PROPERTY);
    unassoc_inv_args.insertLast(getAST().newThisExpression(), getEditGroup());
    unassoc_inv_args.insertLast(ASTNode.copySubtree(getAST(), old_obj), getEditGroup());

    /**
     * if (__old != null) { ObjCRuntime.dissociateObjCObject(this, __old); }
     */
    {
        Block con_block = getAST().newBlock();
        ListRewrite con_block_stmt = getRewrite().getListRewrite(con_block, Block.STATEMENTS_PROPERTY);
        insertStatement(con_block_stmt, unassoc_inv);

        InfixExpression con_obj_nonnull = getAST().newInfixExpression();
        getRewrite().set(con_obj_nonnull, InfixExpression.OPERATOR_PROPERTY,
                InfixExpression.Operator.NOT_EQUALS, getEditGroup());
        getRewrite().set(con_obj_nonnull, InfixExpression.LEFT_OPERAND_PROPERTY,
                ASTNode.copySubtree(getAST(), old_obj), getEditGroup());
        getRewrite().set(con_obj_nonnull, InfixExpression.RIGHT_OPERAND_PROPERTY, getAST().newNullLiteral(),
                getEditGroup());

        IfStatement if_obj_nonnull = getAST().newIfStatement();
        getRewrite().set(if_obj_nonnull, IfStatement.EXPRESSION_PROPERTY, con_obj_nonnull, getEditGroup());
        getRewrite().set(if_obj_nonnull, IfStatement.THEN_STATEMENT_PROPERTY, con_block, getEditGroup());
        block_stmts.insertLast(if_obj_nonnull, getEditGroup());
    }
}