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

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

Introduction

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

Prototype

public final StructuralPropertyDescriptor getLocationInParent() 

Source Link

Document

Returns the location of this node within its parent, or null if this is a root node.

Usage

From source file:org.eclipse.wb.internal.core.utils.ast.AstEditor.java

License:Open Source License

/**
 * Replaces {@link Type} in {@link VariableDeclarationStatement} with new type.<br>
 * We use this method in morphing.//from  ww w  . j a va2 s.  c o  m
 * 
 * @param declaration
 *          the fragment from {@link VariableDeclarationStatement}. {@link FieldDeclaration}
 *          should have only one fragment.
 * @param newTypeName
 *          the fully qualified name of type to use.
 */
public void replaceVariableType(VariableDeclaration declaration, String newTypeName) throws Exception {
    Type type;
    VariableDeclarationStatement declarationStatement = null;
    FieldDeclaration fieldDeclaration = null;
    if (declaration.getLocationInParent() == VariableDeclarationStatement.FRAGMENTS_PROPERTY) {
        declarationStatement = (VariableDeclarationStatement) declaration.getParent();
        type = declarationStatement.getType();
    } else if (declaration.getLocationInParent() == FieldDeclaration.FRAGMENTS_PROPERTY) {
        fieldDeclaration = (FieldDeclaration) declaration.getParent();
        type = fieldDeclaration.getType();
    } else {
        throw new IllegalArgumentException("Unknown argument: " + declaration.getClass());
    }
    // do replace
    {
        Type newType = getParser().parseQualifiedType(type.getStartPosition(), newTypeName);
        // replace Type source
        replaceSubstring(type, newTypeName);
        // replace Type node
        if (declarationStatement != null) {
            declarationStatement.setType(newType);
        }
        if (fieldDeclaration != null) {
            fieldDeclaration.setType(newType);
        }
        resolveImports(newType);
    }
}