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

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

Introduction

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

Prototype

public final int getNodeType() 

Source Link

Document

Returns an integer value identifying the type of this concrete AST node.

Usage

From source file:edu.uci.ics.sourcerer.tools.java.extractor.eclipse.ReferenceExtractorVisitor.java

License:Open Source License

/**
 * This method writes:/*  ww w  .j a v  a  2 s  . c  o m*/
 * <ul>
 *   <li>Field entity to <code>IEntityWriter</code>.
 *   <ul>
 *     <li>Inside relation to <code>IRelationWriter</code>.</li>
 *     <li>Holds relation to <code>IRelationWriter</code>.</li>
 *   </ul></li>
 *   <li>Local variable to <code>ILocalVariableWriter</code>.
 *   <ul>
 *     <li>Uses relation to <code>IRelationWriter</codE>.</li>
 *   </ul></li>
 * </ul>
 * 
 * Field fully qualified names (FQNs) adhere to the following format:<br> 
 * parent fqn + . + simple name
 */
@Override
public boolean visit(VariableDeclarationFragment node) {
    if (node.getParent().getNodeType() == ASTNode.FIELD_DECLARATION) {
        FieldDeclaration parent = (FieldDeclaration) node.getParent();

        // Get the fqn
        String fqn = fqnStack.peek(EnclosingDeclaredType.class).getFieldFqn(node.getName().getIdentifier());

        // Write the entity
        entityWriter.writeEntity(Entity.FIELD, fqn, parent.getModifiers(), createMetrics(node),
                createLocation(node));

        // Write the contains relation
        relationWriter.writeRelation(Relation.CONTAINS, fqnStack.getFqn(), fqn, createUnknownLocation());

        Type type = parent.getType();
        String typeFqn = getTypeFqn(type);

        // Write the holds relation
        relationWriter.writeRelation(Relation.HOLDS, fqn, typeFqn, createLocation(type));

        // Add the field to the fqnstack
        fqnStack.push(fqn, Entity.FIELD);

        // Write the uses relation
        accept(parent.getType());

        // Write the javadoc comment
        accept(parent.getJavadoc());
    } else if (node.getParent().getNodeType() == ASTNode.VARIABLE_DECLARATION_STATEMENT) {
        VariableDeclarationStatement parent = (VariableDeclarationStatement) node.getParent();

        Type type = parent.getType();
        String typeFqn = getTypeFqn(type);

        // Write the local variable
        localVariableWriter.writeLocalVariable(LocalVariable.LOCAL, node.getName().getIdentifier(),
                parent.getModifiers(), typeFqn, createLocation(type), fqnStack.getFqn(), null,
                createLocation(node));
    } else if (node.getParent().getNodeType() == ASTNode.VARIABLE_DECLARATION_EXPRESSION) {
        VariableDeclarationExpression parent = (VariableDeclarationExpression) node.getParent();

        Type type = parent.getType();
        String typeFqn = getTypeFqn(type);

        // Write the local variable
        localVariableWriter.writeLocalVariable(LocalVariable.LOCAL, node.getName().getIdentifier(),
                parent.getModifiers(), typeFqn, createLocation(type), fqnStack.getFqn(), null,
                createLocation(node));
    } else if (node.getParent().getNodeType() == ASTNode.LAMBDA_EXPRESSION) {
        ; //TODO: This captures variables that are arguments of lambdas. It can later be a relation of the type lambda contains variable.
    } else {
        throw new IllegalStateException(
                "Unknown parent for variable declaration fragment (code " + node.getNodeType() + ").");
    }

    if (node.getInitializer() != null) {
        inLhsAssignment = true;
        accept(node.getName());
        inLhsAssignment = false;
        accept(node.getInitializer());
    }

    return false;
}