Example usage for org.eclipse.jdt.core.dom VariableDeclaration getInitializer

List of usage examples for org.eclipse.jdt.core.dom VariableDeclaration getInitializer

Introduction

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

Prototype

public Expression getInitializer() 

Source Link

Document

Returns the initializer of this variable declaration, or null if there is none.

Usage

From source file:com.crispico.flower.mp.codesync.code.java.adapter.JavaAttributeModelAdapter.java

License:Open Source License

@Override
public Object getValueFeatureValue(Object element, Object feature, Object correspondingValue) {
    if (CodeSyncPackage.eINSTANCE.getCodeSyncElement_Name().equals(feature)) {
        return getLabel(element);
    }//from  www . ja  va 2 s .c o  m
    if (CodeSyncPackage.eINSTANCE.getCodeSyncElement_Type().equals(feature)) {
        return ATTRIBUTE;
    }
    if (AstCacheCodePackage.eINSTANCE.getTypedElement_Type().equals(feature)) {
        return getStringFromType(getFieldDeclaration(element).getType());
    }
    if (AstCacheCodePackage.eINSTANCE.getAttribute_Initializer().equals(feature)) {
        VariableDeclaration var = (VariableDeclaration) getFieldDeclaration(element).fragments().get(0);
        return getStringFromExpression(var.getInitializer());
    }
    return super.getValueFeatureValue(element, feature, correspondingValue);
}

From source file:com.google.gdt.eclipse.designer.gwtext.model.widgets.GridPanelInfo.java

License:Open Source License

/**
 * @return the {@link ArrayCreation} of columns in "ColumnModel".
 */// www.java  2  s . c  om
private ArrayCreation getColumnsArray(boolean ensure) throws Exception {
    ClassInstanceCreation columnModelCreation = getColumnModelCreation(ensure);
    if (columnModelCreation == null) {
        return null;
    }
    // "columns" Array usage in "ColumnModel" creation
    Expression columnArray = DomGenerics.arguments(columnModelCreation).get(0);
    ExecutionFlowDescription flowDescription = JavaInfoUtils.getState(this).getFlowDescription();
    while (true) {
        if (columnArray instanceof ArrayCreation) {
            return (ArrayCreation) columnArray;
        }
        if (AstNodeUtils.isVariable(columnArray)) {
            ASTNode lastAssignment = ExecutionFlowUtils.getLastAssignment(flowDescription, columnArray);
            if (lastAssignment instanceof VariableDeclaration) {
                VariableDeclaration variableDeclaration = (VariableDeclaration) lastAssignment;
                columnArray = variableDeclaration.getInitializer();
                continue;
            }
        }
        if (ensure) {
            return (ArrayCreation) getEditor().replaceExpression(columnArray,
                    "new com.gwtext.client.widgets.grid.ColumnConfig[] {}");
        }
        return null;
    }
}

From source file:com.google.gdt.eclipse.designer.gwtext.model.widgets.GridPanelInfo.java

License:Open Source License

private ClassInstanceCreation getColumnModelCreation0(ASTNode node, boolean ensure) throws Exception {
    List<Expression> arguments = DomGenerics.arguments(node);
    for (Expression argument : arguments) {
        if (AstNodeUtils.isSuccessorOf(argument, "com.gwtext.client.widgets.grid.ColumnModel")) {
            ExecutionFlowDescription flowDescription = JavaInfoUtils.getState(this).getFlowDescription();
            while (true) {
                if (argument instanceof ClassInstanceCreation) {
                    return (ClassInstanceCreation) argument;
                }// w ww.ja  v  a2s  .c o m
                if (AstNodeUtils.isVariable(argument)) {
                    ASTNode lastAssignment = ExecutionFlowUtils.getLastAssignment(flowDescription, argument);
                    if (lastAssignment instanceof VariableDeclaration) {
                        VariableDeclaration variableDeclaration = (VariableDeclaration) lastAssignment;
                        argument = variableDeclaration.getInitializer();
                        continue;
                    }
                }
                if (ensure) {
                    return (ClassInstanceCreation) getEditor().replaceExpression(argument,
                            "new com.gwtext.client.widgets.grid.ColumnModel(new com.gwtext.client.widgets.grid.ColumnConfig[] {})");
                }
                return null;
            }
        }
    }
    return null;
}

From source file:com.google.gdt.eclipse.designer.gxt.model.widgets.GridInfo.java

License:Open Source License

/**
 * @return the {@link ClassInstanceCreation} for "ColumnModel" of this grid.
 *///w w  w  .  j  a  v a  2 s.c  om
private ClassInstanceCreation getColumnModelCreation(boolean ensure) throws Exception {
    if (!(getCreationSupport() instanceof ConstructorCreationSupport)) {
        return null;
    }
    ASTNode gridCreation = getCreationSupport().getNode();
    List<Expression> arguments = DomGenerics.arguments(gridCreation);
    for (Expression argument : arguments) {
        if (AstNodeUtils.isSuccessorOf(argument, "com.extjs.gxt.ui.client.widget.grid.ColumnModel")) {
            ExecutionFlowDescription flowDescription = JavaInfoUtils.getState(this).getFlowDescription();
            while (true) {
                if (argument instanceof ClassInstanceCreation) {
                    return (ClassInstanceCreation) argument;
                }
                if (AstNodeUtils.isVariable(argument)) {
                    ASTNode lastAssignment = ExecutionFlowUtils.getLastAssignment(flowDescription, argument);
                    if (lastAssignment instanceof VariableDeclaration) {
                        VariableDeclaration variableDeclaration = (VariableDeclaration) lastAssignment;
                        argument = variableDeclaration.getInitializer();
                        continue;
                    }
                }
                if (ensure) {
                    return (ClassInstanceCreation) getEditor().replaceExpression(argument,
                            "new com.extjs.gxt.ui.client.widget.grid.ColumnModel(null)");
                }
                return null;
            }
        }
    }
    return null;
}

From source file:com.google.gdt.eclipse.designer.uibinder.model.util.NameSupport.java

License:Open Source License

/**
 * @return <code>true</code> if has initializer <code>GWT.create(UiBinder+.class)</code>.
 *//*from  w  w w.  j  a v  a 2 s.  c  o  m*/
public static boolean isBinderCreate(FieldDeclaration fieldDeclaration) {
    List<VariableDeclarationFragment> fragments = DomGenerics.fragments(fieldDeclaration);
    for (VariableDeclaration fragment : fragments) {
        if (fragment.getInitializer() instanceof MethodInvocation) {
            MethodInvocation invocation = (MethodInvocation) fragment.getInitializer();
            if (invocation.getName().getIdentifier().equals("create") && AstNodeUtils
                    .isSuccessorOf(invocation.getExpression(), "com.google.gwt.core.client.GWT")) {
                List<Expression> arguments = DomGenerics.arguments(invocation);
                if (arguments.size() == 1 && arguments.get(0) instanceof TypeLiteral) {
                    TypeLiteral typeLiteral = (TypeLiteral) arguments.get(0);
                    if (AstNodeUtils.isSuccessorOf(typeLiteral.getType(),
                            "com.google.gwt.uibinder.client.UiBinder")) {
                        return true;
                    }
                }
            }
        }
    }
    return false;
}

From source file:edu.cmu.cs.crystal.cfg.eclipse.EclipseCFG.java

License:Open Source License

private EclipseCFGNode handleVariableDecl(VariableDeclaration node, EclipseCFGNode startPoint) {
    EclipseCFGNode decl = nodeMap.get(node);
    EclipseCFGNode name = nodeMap.get(node.getName());
    EclipseCFGNode current = null;//from w  ww  .j a  v  a 2  s  .co  m

    if (startPoint != null) {
        current = startPoint.getEnd();
    }

    if (node.getInitializer() != null) {
        EclipseCFGNode init = nodeMap.get(node.getInitializer());
        if (current != null)
            createEdge(current, init.getStart());
        else
            startPoint = init;
        current = init.getEnd();
    }

    if (current != null)
        createEdge(current, name.getStart());
    else
        startPoint = name;

    createEdge(name.getEnd(), decl);

    decl.setStart(startPoint.getStart());

    return decl;
}

From source file:edu.cmu.cs.crystal.tac.eclipse.EclipseTACInstructionFactory.java

License:Open Source License

public TACInstruction create(VariableDeclaration node, IEclipseVariableQuery eclipseVariableQuery) {
    if (node.getParent() instanceof FieldDeclaration) {
        // this can happen in the constructor, where the CFG inlines field initializers
        // need to create a store iff this declaration has an initializer
        if (node.getInitializer() == null)
            return null;
        return new StoreFieldInstructionImpl(node, eclipseVariableQuery.variable(node.getInitializer()),
                new EclipseFieldDeclaration(node, eclipseVariableQuery), eclipseVariableQuery);

    } else {/*from  w  w  w  .  j  a v  a2s.  c  o m*/
        // local variable
        SourceVariableDeclarationImpl decl = new SourceVariableDeclarationImpl(node, eclipseVariableQuery);
        if (node.getInitializer() == null)
            return decl;
        // copy result of previous initializer into declared variable
        CopyInstructionImpl init = new CopyInstructionImpl(node,
                eclipseVariableQuery.variable(node.getInitializer()), true,
                eclipseVariableQuery.sourceVariable(decl.resolveBinding()), eclipseVariableQuery);
        return new EclipseInstructionSequence(node, new TACInstruction[] { decl, init }, eclipseVariableQuery);
    }
}

From source file:edu.cmu.cs.crystal.tac.eclipse.EclipseTACTargetSelectionTest.java

License:Open Source License

@Test
public void testInitializers() throws Exception {
    CompilationUnit simple = EclipseTACSimpleTestDriver.parseCode("Initializers", INITIALIZERS);
    MethodDeclaration m = EclipseTACSimpleTestDriver.getFirstMethod(simple);
    EclipseTAC tac = new EclipseTAC(m.resolveBinding());

    List<Statement> stmts = m.getBody().statements();
    Assert.assertTrue(stmts.size() == 5);

    TACInstruction decl, init;/*  w w  w. j  ava 2  s.co m*/
    for (int i = 0; i < 4; i++) {
        VariableDeclarationStatement s = (VariableDeclarationStatement) stmts.get(i);
        Assert.assertTrue("Statement: " + s, s.fragments().size() == 1);
        VariableDeclaration d = (VariableDeclaration) s.fragments().get(0);
        decl = tac.instruction(d);
        Assert.assertNotNull("Statement: " + s, decl);
        Assert.assertNotNull("Statement: " + s, d.getInitializer());
        if (d.getInitializer() instanceof ParenthesizedExpression)
            init = tac.instruction(((ParenthesizedExpression) d.getInitializer()).getExpression());
        else
            init = tac.instruction(d.getInitializer());
        Assert.assertNotNull("Statement: " + s, init);

        Assert.assertTrue("Statement: " + s, init instanceof AssignmentInstruction);
        Variable t = ((AssignmentInstruction) init).getTarget();

        Variable declared = null;
        //         if(decl instanceof SourceVariableDeclaration) {
        //            declared = ((SourceVariableDeclaration) decl).getDeclaredVariable();
        //            Assert.assertEquals("Statement: " + s, declared, t);
        //         }
        //         else 
        if (decl instanceof EclipseInstructionSequence) {
            TACInstruction[] seq = ((EclipseInstructionSequence) decl).getInstructions();
            Assert.assertTrue("Statement: " + s, seq.length == 2);
            Assert.assertTrue("Statement: " + s, seq[0] instanceof SourceVariableDeclaration);
            declared = ((SourceVariableDeclaration) seq[0]).getDeclaredVariable();
            Assert.assertTrue("Statement: " + s, seq[1] instanceof CopyInstruction);
            Assert.assertEquals("Statement: " + s, declared, ((CopyInstruction) seq[1]).getTarget());
            Assert.assertEquals("Statement: " + s, t, ((CopyInstruction) seq[1]).getOperand());
        } else
            Assert.fail("Statement has unexpected translation: " + s);

        Assert.assertEquals("Statement: " + s, tac.sourceVariable(d.resolveBinding()), declared);
    }

}

From source file:egovframework.mgt.fit.library.parser.visitor.ClassParsingVisitor.java

License:Apache License

/**
 *   ./*from   ww  w  .  jav a 2s. c  om*/
 * @return   
 */
@Override
public boolean visit(FieldDeclaration node) {
    if (node.getNodeType() == ASTNode.FIELD_DECLARATION) {
        for (Object obj : node.fragments()) {
            if (obj instanceof VariableDeclaration) {
                VariableDeclaration vd = (VariableDeclaration) obj;
                Variable field = new Variable();
                field.setModifier(node.getModifiers());
                field.setType(project.resolveClass(ParsingHelper.getFullNameWithSimpleName(
                        node.getType().toString(), javaClass.getPackage(), javaClass.getImports())));
                field.setName(vd.getName().getFullyQualifiedName());
                field.setValue(StringHelper.safeToString(vd.getInitializer()));
                node.accept(new AnnotationParsingVisitor(field, ASTNode.FIELD_DECLARATION));
                field.setNode(vd);
                javaClass.addField(field);
            }
        }

    }
    return super.visit(node);
}

From source file:egovframework.mgt.fit.library.parser.visitor.LocalVariableParsingVisitor.java

License:Apache License

/**
 *    ?  ?? ?.//from  www.  ja  v a  2s .  com
 * @return   
 */
@Override
public boolean visit(VariableDeclarationStatement node) {
    if (node instanceof VariableDeclarationStatement) {
        for (Object obj : node.fragments()) {
            if (obj instanceof VariableDeclaration) {
                VariableDeclaration vd = (VariableDeclaration) obj;
                Variable lv = new Variable();
                lv.setModifier(node.getModifiers());
                lv.setType(
                        project.resolveClass(javaClass.getFullnameWithSimpleName(node.getType().toString())));
                lv.setName(vd.getName().getFullyQualifiedName());
                lv.setValue(StringHelper.safeToString(vd.getInitializer()));
                node.accept(new AnnotationParsingVisitor(lv, ASTNode.VARIABLE_DECLARATION_STATEMENT));
                lv.setNode(vd);
                method.addLocalVariable(lv);
            }
        }
    }
    return super.visit(node);
}