Example usage for org.eclipse.jdt.core ILocalVariable getAncestor

List of usage examples for org.eclipse.jdt.core ILocalVariable getAncestor

Introduction

In this page you can find the example usage for org.eclipse.jdt.core ILocalVariable getAncestor.

Prototype

IJavaElement getAncestor(int ancestorType);

Source Link

Document

Returns this Java element or the first ancestor of this element that has the given type.

Usage

From source file:org.codehaus.groovy.eclipse.refactoring.core.rename.renameLocal.GroovyRenameLocalVariableProcessor.java

License:Apache License

/**
 * @param localVariable/*from ww  w . j ava2  s  . c  o  m*/
 * @param newName
 * @param status
 */
private void initialize(ILocalVariable localVariable, String newName, RefactoringStatus status) {
    this.localVariable = localVariable;
    ICompilationUnit unit = (ICompilationUnit) localVariable.getAncestor(IJavaElement.COMPILATION_UNIT);
    if (unit instanceof GroovyCompilationUnit) {
        this.unit = (GroovyCompilationUnit) unit;
    } else {
        status.merge(RefactoringStatus.createErrorStatus(
                "Expecting a Groovy compilation unit, but instead found " + unit.getElementName()));
    }
    if (newName != null && newName.length() > 0) {
        if (newName.equals(localVariable.getElementName())) {
            status.merge(RefactoringStatus.createErrorStatus("New name is the same as the old name"));
        }
        setNewElementName(newName);
    } else {
        status.merge(RefactoringStatus.createErrorStatus("Invalid new name"));
    }
}

From source file:org.springframework.ide.eclipse.beans.ui.refactoring.jdt.ConstructorArgumentRenameRefactoringParticipant.java

License:Open Source License

@Override
protected boolean initialize(Object element) {
    if (element instanceof ILocalVariable) {
        ILocalVariable variable = (ILocalVariable) element;
        IJavaProject javaProject = (IJavaProject) variable.getAncestor(IJavaElement.JAVA_PROJECT);
        IProject project = javaProject.getProject();
        if (SpringCoreUtils.isSpringProject(project) && variable.getParent() instanceof IMethod) {
            IMethod method = (IMethod) variable.getParent();
            try {
                if (method.isConstructor()) {
                    refactoredVariable = variable;
                    newName = getArguments().getNewName();
                    return true;
                }//ww w .j a v  a2s .com
            } catch (JavaModelException e) {
            }
        }
    }
    return false;
}