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

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

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom VariableDeclarationFragment 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:at.bestsolution.fxide.jdt.corext.dom.ASTFlattener.java

License:Open Source License

@Override
public boolean visit(VariableDeclarationFragment node) {
    node.getName().accept(this);
    if (node.getAST().apiLevel() >= AST.JLS8) {
        List<Dimension> dimensions = node.extraDimensions();
        for (Iterator<Dimension> it = dimensions.iterator(); it.hasNext();) {
            Dimension e = it.next();
            e.accept(this);
        }/*w w w. j a  v  a2 s  . co  m*/
    } else {
        for (int i = 0; i < node.getExtraDimensions(); i++) {
            this.fBuffer.append("[]"); //$NON-NLS-1$
        }
    }
    if (node.getInitializer() != null) {
        this.fBuffer.append("=");//$NON-NLS-1$
        node.getInitializer().accept(this);
    }
    return false;
}

From source file:boa.datagen.util.Java7Visitor.java

License:Apache License

@Override
public boolean visit(FieldDeclaration node) {
    List<boa.types.Ast.Variable> list = fields.peek();
    for (Object o : node.fragments()) {
        VariableDeclarationFragment f = (VariableDeclarationFragment) o;
        Variable.Builder b = Variable.newBuilder();
        //         b.setPosition(pos.build()); // FIXME
        b.setName(f.getName().getFullyQualifiedName());
        for (Object m : node.modifiers()) {
            if (((IExtendedModifier) m).isAnnotation())
                ((Annotation) m).accept(this);
            else/*from   ww w . ja  v  a2 s  . c  o m*/
                ((org.eclipse.jdt.core.dom.Modifier) m).accept(this);
            b.addModifiers(modifiers.pop());
        }
        boa.types.Ast.Type.Builder tb = boa.types.Ast.Type.newBuilder();
        String name = typeName(node.getType());
        for (int i = 0; i < f.getExtraDimensions(); i++)
            name += "[]";
        tb.setName(getIndex(name));
        tb.setKind(boa.types.Ast.TypeKind.OTHER);
        b.setVariableType(tb.build());
        if (f.getInitializer() != null) {
            f.getInitializer().accept(this);
            b.setInitializer(expressions.pop());
        }
        list.add(b.build());
    }
    return false;
}

From source file:boa.datagen.util.Java7Visitor.java

License:Apache License

@Override
public boolean visit(VariableDeclarationStatement node) {
    boa.types.Ast.Statement.Builder b = boa.types.Ast.Statement.newBuilder();
    //      b.setPosition(pos.build());
    List<boa.types.Ast.Statement> list = statements.peek();
    b.setKind(boa.types.Ast.Statement.StatementKind.EXPRESSION);
    boa.types.Ast.Expression.Builder eb = boa.types.Ast.Expression.newBuilder();
    //      eb.setPosition(pos.build());//FIXME
    eb.setKind(boa.types.Ast.Expression.ExpressionKind.VARDECL);
    for (Object o : node.fragments()) {
        VariableDeclarationFragment f = (VariableDeclarationFragment) o;
        Variable.Builder vb = Variable.newBuilder();
        //         vb.setPosition(pos.build());//FIXME
        vb.setName(f.getName().getFullyQualifiedName());
        for (Object m : node.modifiers()) {
            if (((IExtendedModifier) m).isAnnotation())
                ((Annotation) m).accept(this);
            else// w w  w.  jav a2  s.  co  m
                ((org.eclipse.jdt.core.dom.Modifier) m).accept(this);
            vb.addModifiers(modifiers.pop());
        }
        boa.types.Ast.Type.Builder tb = boa.types.Ast.Type.newBuilder();
        String name = typeName(node.getType());
        for (int i = 0; i < f.getExtraDimensions(); i++)
            name += "[]";
        tb.setName(getIndex(name));
        tb.setKind(boa.types.Ast.TypeKind.OTHER);
        vb.setVariableType(tb.build());
        if (f.getInitializer() != null) {
            f.getInitializer().accept(this);
            vb.setInitializer(expressions.pop());
        }
        eb.addVariableDecls(vb.build());
    }
    b.setExpression(eb.build());
    list.add(b.build());
    return false;
}

From source file:boa.datagen.util.Java7Visitor.java

License:Apache License

@Override
public boolean visit(VariableDeclarationExpression node) {
    boa.types.Ast.Expression.Builder eb = boa.types.Ast.Expression.newBuilder();
    //      eb.setPosition(pos.build());
    eb.setKind(boa.types.Ast.Expression.ExpressionKind.VARDECL);
    for (Object o : node.fragments()) {
        VariableDeclarationFragment f = (VariableDeclarationFragment) o;
        Variable.Builder b = Variable.newBuilder();
        //         b.setPosition(pos.build());//FIXME
        b.setName(f.getName().getFullyQualifiedName());
        for (Object m : node.modifiers()) {
            if (((IExtendedModifier) m).isAnnotation())
                ((Annotation) m).accept(this);
            else//from  w w w .j a va2  s .c  o  m
                ((org.eclipse.jdt.core.dom.Modifier) m).accept(this);
            b.addModifiers(modifiers.pop());
        }
        boa.types.Ast.Type.Builder tb = boa.types.Ast.Type.newBuilder();
        String name = typeName(node.getType());
        for (int i = 0; i < f.getExtraDimensions(); i++)
            name += "[]";
        tb.setName(getIndex(name));
        tb.setKind(boa.types.Ast.TypeKind.OTHER);
        b.setVariableType(tb.build());
        if (f.getInitializer() != null) {
            f.getInitializer().accept(this);
            b.setInitializer(expressions.pop());
        }
        eb.addVariableDecls(b.build());
    }
    expressions.push(eb.build());
    return false;
}

From source file:ca.mcgill.cs.swevo.jayfx.ASTCrawler.java

License:Open Source License

@Override
public boolean visit(final FieldDeclaration pNode) {
    @SuppressWarnings("rawtypes")
    final List fragments = pNode.fragments();
    IElement lField;/*from w w w.  j  a  v  a  2  s .com*/

    this.aCurrMethodReminder.push(this.aCurrMethod);

    if (Modifier.isStatic(pNode.getModifiers())) {
        this.aCurrMethod = (MethodElement) FlyweightElementFactory.getElement(Category.METHOD,
                this.aCurrType.getId() + "." + ASTCrawler.aCLINIT_METHOD_NAME);
        if (!this.aDB.contains(this.aCurrMethod))
            // This <clinit>() method will be in any class that has a static
            // field with initialization
            // But the DECLARES relation will be linked only if this method
            // has at least one sub relations
            // The linkage is done at the end of traversing each
            // compilations unit while end visiting type Declaration
            this.aDB.addElement(this.aCurrMethod, pNode.getModifiers());
    } else
        this.aCurrMethod = this.aTempMethod;

    // Consider multiple declaration in one statment
    for (@SuppressWarnings("rawtypes")
    final Iterator itr = fragments.iterator(); itr.hasNext();) {
        final VariableDeclarationFragment fragment = (VariableDeclarationFragment) itr.next();
        final String lSimpleName = fragment.getName().getIdentifier();
        final Expression lInit = fragment.getInitializer();

        if (lSimpleName != null) {
            lField = FlyweightElementFactory.getElement(Category.FIELD,
                    this.aCurrType.getId() + "." + lSimpleName);
            this.aDB.addElement(lField, pNode.getModifiers());

            this.aDB.addRelation(this.aCurrType, Relation.DECLARES_FIELD, lField);

            // If there is any initialization to this field then we write
            // them as an access by <init> or <clinit>
            if (lInit != null) {
                this.aDB.addRelationAndTranspose(this.aCurrMethod, Relation.ACCESSES, lField);
                this.aDB.addRelationAndTranspose(this.aCurrMethod, Relation.SETS, lField);

                // Want to go into the right side of assignment operator
                lInit.accept(this);
            }
        }
    }

    // Because we have covered everything we need about field declaration,
    // we dont' have to go deeper into this node.
    // return false;
    return true;
}

From source file:ca.mcgill.cs.swevo.ppa.inference.VariableDeclarationInferenceStrategy.java

License:Open Source License

public void inferTypes(ASTNode node) {
    VariableDeclarationFragment fragment = (VariableDeclarationFragment) node;
    ITypeBinding newBinding = fragment.getName().resolveTypeBinding();
    Expression exp = fragment.getInitializer();
    if (!indexer.isSafe(exp)) {
        TypeFact tFact = new TypeFact(indexer.getMainIndex(exp), exp.resolveTypeBinding(), TypeFact.UNKNOWN,
                newBinding, TypeFact.SUBTYPE, TypeFact.ASSIGN_STRATEGY);
        ppaEngine.reportTypeFact(tFact);
    }/*from  w  w w.ja  v  a2 s  .c  o m*/
}

From source file:ca.mcgill.cs.swevo.ppa.inference.VariableDeclarationInferenceStrategy.java

License:Open Source License

public boolean isSafe(ASTNode node) {
    VariableDeclarationFragment fragment = (VariableDeclarationFragment) node;
    Expression exp = fragment.getInitializer();

    return indexer.isSafe(exp);
}

From source file:changetypes.ASTVisitorAtomicChange.java

License:Open Source License

public boolean visit(VariableDeclarationStatement vds) {
    if (this.mtbStack.isEmpty()) {
        return true;
    }/*from   w w  w.j  a  va  2s  . c  o m*/
    for (Object ovdf : vds.fragments()) {
        VariableDeclarationFragment vdf = (VariableDeclarationFragment) ovdf;
        try {
            this.facts.add(Fact.makeLocalVarFact(getQualifiedName((IMethodBinding) this.mtbStack.peek()),
                    getQualifiedName(vds.getType().resolveBinding()), vdf.getName().getIdentifier(),
                    vdf.getInitializer().toString()));
        } catch (Exception localException) {
            System.err.println("Cannot resolve variable declaration \"" + vdf.getName().toString() + "\"");
        }
    }
    return true;
}

From source file:coloredide.utils.CopiedNaiveASTFlattener.java

License:Open Source License

public boolean visit(VariableDeclarationFragment node) {
    node.getName().accept(this);
    for (int i = 0; i < node.getExtraDimensions(); i++) {
        this.buffer.append("[]");//$NON-NLS-1$
    }/*from   ww  w  .  j  av  a2  s .  c  o m*/
    if (node.getInitializer() != null) {
        this.buffer.append("=");//$NON-NLS-1$
        node.getInitializer().accept(this);
    }
    return false;
}

From source file:com.bsiag.eclipse.jdt.java.formatter.linewrap.FieldAligner.java

License:Open Source License

private void alignFields(ArrayList<FieldDeclaration> alignGroup) {
    if (alignGroup.size() < 2)
        return;//from w w  w  .j av a2 s  . co  m
    this.fieldAlignGroups.add(alignGroup);

    int maxNameAlign = 0;
    for (FieldDeclaration declaration : alignGroup) {
        List<VariableDeclarationFragment> fragments = declaration.fragments();
        SimpleName fieldName = fragments.get(0).getName();
        int nameIndex = this.tm.firstIndexIn(fieldName, TokenNameIdentifier);
        int positionInLine = this.tm.getPositionInLine(nameIndex);
        maxNameAlign = Math.max(maxNameAlign, positionInLine);
    }
    maxNameAlign = this.tm.toIndent(maxNameAlign, false);

    int maxAssignAlign = 0;
    for (FieldDeclaration declaration : alignGroup) {
        List<VariableDeclarationFragment> fragments = declaration.fragments();
        VariableDeclarationFragment fragment = fragments.get(0);
        int nameIndex = this.tm.firstIndexIn(fragment.getName(), TokenNameIdentifier);
        Token nameToken = this.tm.get(nameIndex);

        nameToken.setAlign(maxNameAlign);

        if (fragment.getInitializer() != null) {
            int equalIndex = this.tm.firstIndexAfter(fragment.getName(), TokenNameEQUAL);
            int positionInLine = this.tm.getPositionInLine(equalIndex);
            maxAssignAlign = Math.max(maxAssignAlign, positionInLine);
        }
    }
    maxAssignAlign = this.tm.toIndent(maxAssignAlign, false);

    for (FieldDeclaration declaration : alignGroup) {
        List<VariableDeclarationFragment> fragments = declaration.fragments();
        VariableDeclarationFragment fragment = fragments.get(0);
        if (fragment.getInitializer() != null) {
            int assingIndex = this.tm.firstIndexAfter(fragment.getName(), TokenNameEQUAL);
            Token assignToken = this.tm.get(assingIndex);
            assignToken.setAlign(maxAssignAlign);

            int baseIndent = this.tm.getPositionInLine(assingIndex + 1) - assignToken.getIndent();
            int lastIndex = this.tm.lastIndexIn(declaration, -1);
            for (int i = assingIndex + 1; i <= lastIndex; i++) {
                Token token = this.tm.get(i);
                token.setIndent(baseIndent + token.getIndent());
            }
        }
    }
}