Example usage for org.eclipse.jdt.core.dom VariableDeclarationStatement FRAGMENTS_PROPERTY

List of usage examples for org.eclipse.jdt.core.dom VariableDeclarationStatement FRAGMENTS_PROPERTY

Introduction

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

Prototype

ChildListPropertyDescriptor FRAGMENTS_PROPERTY

To view the source code for org.eclipse.jdt.core.dom VariableDeclarationStatement FRAGMENTS_PROPERTY.

Click Source Link

Document

The "fragments" structural property of this node type (element type: VariableDeclarationFragment ).

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 . ja va 2s  .  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);
    }
}