Search in sources :

Example 81 with RenameJavaElementDescriptor

use of org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor in project che by eclipse.

the class RenameLocalVariableProcessor method createChange.

@Override
public Change createChange(IProgressMonitor monitor) throws CoreException {
    try {
        monitor.beginTask(RefactoringCoreMessages.RenameTypeProcessor_creating_changes, 1);
        RenameJavaElementDescriptor descriptor = createRefactoringDescriptor();
        fChange.setDescriptor(new RefactoringChangeDescriptor(descriptor));
        return fChange;
    } finally {
        monitor.done();
    }
}
Also used : RenameJavaElementDescriptor(org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor) RefactoringChangeDescriptor(org.eclipse.ltk.core.refactoring.RefactoringChangeDescriptor)

Example 82 with RenameJavaElementDescriptor

use of org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor in project che by eclipse.

the class RenamePackageProcessor method createChange.

@Override
public Change createChange(IProgressMonitor monitor) throws CoreException {
    try {
        monitor.beginTask(RefactoringCoreMessages.RenamePackageRefactoring_creating_change, 1);
        final RenameJavaElementDescriptor descriptor = createRefactoringDescriptor();
        final DynamicValidationRefactoringChange result = new DynamicValidationRefactoringChange(descriptor, RefactoringCoreMessages.RenamePackageRefactoring_change_name);
        result.addAll(fChangeManager.getAllChanges());
        fRenamePackageChange = new RenamePackageChange(fPackage, getNewElementName(), fRenameSubpackages);
        result.add(fRenamePackageChange);
        monitor.worked(1);
        return result;
    } finally {
        fChangeManager = null;
        fImportsManager = null;
        monitor.done();
    }
}
Also used : DynamicValidationRefactoringChange(org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationRefactoringChange) RenamePackageChange(org.eclipse.jdt.internal.corext.refactoring.changes.RenamePackageChange) RenameJavaElementDescriptor(org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor)

Example 83 with RenameJavaElementDescriptor

use of org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor in project che by eclipse.

the class RenameTypeParameterProcessor method createChange.

@Override
public Change createChange(IProgressMonitor monitor) throws CoreException, OperationCanceledException {
    Assert.isNotNull(monitor);
    try {
        Change change = fChange;
        if (change != null) {
            String project = null;
            IJavaProject javaProject = fTypeParameter.getJavaProject();
            if (javaProject != null)
                project = javaProject.getElementName();
            String description = Messages.format(RefactoringCoreMessages.RenameTypeParameterProcessor_descriptor_description_short, BasicElementLabels.getJavaElementName(fTypeParameter.getElementName()));
            String header = Messages.format(RefactoringCoreMessages.RenameTypeParameterProcessor_descriptor_description, new String[] { BasicElementLabels.getJavaElementName(fTypeParameter.getElementName()), JavaElementLabels.getElementLabel(fTypeParameter.getDeclaringMember(), JavaElementLabels.ALL_FULLY_QUALIFIED), BasicElementLabels.getJavaElementName(getNewElementName()) });
            String comment = new JDTRefactoringDescriptorComment(project, this, header).asString();
            RenameJavaElementDescriptor descriptor = RefactoringSignatureDescriptorFactory.createRenameJavaElementDescriptor(IJavaRefactorings.RENAME_TYPE_PARAMETER);
            descriptor.setProject(project);
            descriptor.setDescription(description);
            descriptor.setComment(comment);
            descriptor.setFlags(RefactoringDescriptor.NONE);
            descriptor.setJavaElement(fTypeParameter);
            descriptor.setNewName(getNewElementName());
            descriptor.setUpdateReferences(fUpdateReferences);
            change = new DynamicValidationRefactoringChange(descriptor, RefactoringCoreMessages.RenameTypeParameterProcessor_change_name, new Change[] { change });
        }
        return change;
    } finally {
        fChange = null;
        monitor.done();
    }
}
Also used : IJavaProject(org.eclipse.jdt.core.IJavaProject) DynamicValidationRefactoringChange(org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationRefactoringChange) Change(org.eclipse.ltk.core.refactoring.Change) DynamicValidationRefactoringChange(org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationRefactoringChange) RenameJavaElementDescriptor(org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor) JDTRefactoringDescriptorComment(org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment)

Example 84 with RenameJavaElementDescriptor

use of org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor in project che by eclipse.

the class RenameLinkedModeRefactoringSession method createRenameDescriptor.

/**
     * Creates a rename descriptor.
     *
     * @param javaElement element to rename
     * @param newName new name
     * @return a rename descriptor with current settings as used in the refactoring dialogs
     * @throws JavaModelException if an error occurs while accessing the element
     */
private RenameJavaElementDescriptor createRenameDescriptor(IJavaElement javaElement, String newName) throws JavaModelException {
    String contributionId;
    // see RefactoringExecutionStarter#createRenameSupport(..):
    int elementType = javaElement.getElementType();
    switch(elementType) {
        case IJavaElement.JAVA_PROJECT:
            contributionId = IJavaRefactorings.RENAME_JAVA_PROJECT;
            break;
        case IJavaElement.PACKAGE_FRAGMENT_ROOT:
            contributionId = IJavaRefactorings.RENAME_SOURCE_FOLDER;
            break;
        case IJavaElement.PACKAGE_FRAGMENT:
            contributionId = IJavaRefactorings.RENAME_PACKAGE;
            break;
        case IJavaElement.COMPILATION_UNIT:
            contributionId = IJavaRefactorings.RENAME_COMPILATION_UNIT;
            break;
        case IJavaElement.TYPE:
            contributionId = IJavaRefactorings.RENAME_TYPE;
            break;
        case IJavaElement.METHOD:
            final IMethod method = (IMethod) javaElement;
            if (method.isConstructor())
                return createRenameDescriptor(method.getDeclaringType(), newName);
            else
                contributionId = IJavaRefactorings.RENAME_METHOD;
            break;
        case IJavaElement.FIELD:
            IField field = (IField) javaElement;
            if (field.isEnumConstant())
                contributionId = IJavaRefactorings.RENAME_ENUM_CONSTANT;
            else
                contributionId = IJavaRefactorings.RENAME_FIELD;
            break;
        case IJavaElement.TYPE_PARAMETER:
            contributionId = IJavaRefactorings.RENAME_TYPE_PARAMETER;
            break;
        case IJavaElement.LOCAL_VARIABLE:
            contributionId = IJavaRefactorings.RENAME_LOCAL_VARIABLE;
            break;
        default:
            return null;
    }
    RenameJavaElementDescriptor descriptor = (RenameJavaElementDescriptor) RefactoringCore.getRefactoringContribution(contributionId).createDescriptor();
    descriptor.setJavaElement(javaElement);
    descriptor.setNewName(newName);
    if (elementType != IJavaElement.PACKAGE_FRAGMENT_ROOT)
        descriptor.setUpdateReferences(true);
    switch(elementType) {
        case IJavaElement.METHOD:
        case IJavaElement.FIELD:
            descriptor.setDeprecateDelegate(/*refactoringSettings.getBoolean(DelegateUIHelper.DELEGATE_DEPRECATION)*/
            false);
            descriptor.setKeepOriginal(/*refactoringSettings.getBoolean(DelegateUIHelper.DELEGATE_UPDATING)*/
            false);
    }
    switch(elementType) {
        case IJavaElement.TYPE:
            //			case IJavaElement.COMPILATION_UNIT: // TODO
            descriptor.setUpdateSimilarDeclarations(/*refactoringSettings.getBoolean(RenameRefactoringWizard.TYPE_UPDATE_SIMILAR_ELEMENTS)*/
            false);
            int strategy;
            try {
                //refactoringSettings.getInt(RenameRefactoringWizard.TYPE_SIMILAR_MATCH_STRATEGY);
                strategy = 1;
            } catch (NumberFormatException e) {
                strategy = RenamingNameSuggestor.STRATEGY_EXACT;
            }
            descriptor.setMatchStrategy(strategy);
    }
    switch(elementType) {
        case IJavaElement.PACKAGE_FRAGMENT:
            descriptor.setUpdateHierarchy(/*refactoringSettings.getBoolean(RenameRefactoringWizard.PACKAGE_RENAME_SUBPACKAGES)*/
            true);
    }
    switch(elementType) {
        case IJavaElement.PACKAGE_FRAGMENT:
        case IJavaElement.TYPE:
            String fileNamePatterns = /*refactoringSettings.get(RenameRefactoringWizard.QUALIFIED_NAMES_PATTERNS)*/
            "*";
            if (fileNamePatterns != null && fileNamePatterns.length() != 0) {
                descriptor.setFileNamePatterns(fileNamePatterns);
                boolean updateQualifiedNames = /*refactoringSettings.getBoolean(RenameRefactoringWizard.UPDATE_QUALIFIED_NAMES)*/
                false;
                descriptor.setUpdateQualifiedNames(updateQualifiedNames);
            //                    fShowPreview|= updateQualifiedNames;
            }
    }
    switch(elementType) {
        case IJavaElement.PACKAGE_FRAGMENT:
        case IJavaElement.TYPE:
        case IJavaElement.FIELD:
            //refactoringSettings.getBoolean(RenameRefactoringWizard.UPDATE_TEXTUAL_MATCHES);
            boolean updateTextualOccurrences = false;
            descriptor.setUpdateTextualOccurrences(updateTextualOccurrences);
    }
    switch(elementType) {
        case IJavaElement.FIELD:
            descriptor.setRenameGetters(/*refactoringSettings.getBoolean(RenameRefactoringWizard.FIELD_RENAME_GETTER)*/
            false);
            descriptor.setRenameSetters(/*refactoringSettings.getBoolean(RenameRefactoringWizard.FIELD_RENAME_SETTER)*/
            false);
    }
    return descriptor;
}
Also used : IMethod(org.eclipse.jdt.core.IMethod) IField(org.eclipse.jdt.core.IField) RenameJavaElementDescriptor(org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor)

Example 85 with RenameJavaElementDescriptor

use of org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor in project evosuite by EvoSuite.

the class TestGenerationAction method renameSuite.

/**
 * If there already exists a test suite for the chosen target class, then
 * renaming the old test suite is one of the options the user can choose
 *
 * @param suite
 * @param packageName
 * @param suitePath
 */
public void renameSuite(final IResource suite, String packageName, String suitePath) {
    System.out.println("Renaming " + suitePath + " in package " + packageName);
    IPackageFragment[] packages;
    try {
        packages = JavaCore.create(suite.getProject()).getPackageFragments();
        System.out.println("Packages found: " + packages.length);
        for (IPackageFragment f : packages) {
            if ((f.getKind() == IPackageFragmentRoot.K_SOURCE) && (f.getElementName().equals(packageName))) {
                for (ICompilationUnit cu : f.getCompilationUnits()) {
                    String cuName = cu.getElementName();
                    System.out.println("targetPath = " + suitePath);
                    System.out.println("cuName = " + cuName);
                    if (cuName.equals(suitePath)) {
                        RefactoringContribution contribution = RefactoringCore.getRefactoringContribution(IJavaRefactorings.RENAME_COMPILATION_UNIT);
                        RenameJavaElementDescriptor descriptor = (RenameJavaElementDescriptor) contribution.createDescriptor();
                        descriptor.setProject(cu.getResource().getProject().getName());
                        String newName = getNewName(cuName, f.getCompilationUnits());
                        System.out.println("New name for Test Suite: " + newName);
                        // new name for a Class
                        descriptor.setNewName(newName);
                        descriptor.setJavaElement(cu);
                        RefactoringStatus status = new RefactoringStatus();
                        try {
                            Refactoring refactoring = descriptor.createRefactoring(status);
                            IProgressMonitor monitor = new NullProgressMonitor();
                            refactoring.checkInitialConditions(monitor);
                            refactoring.checkFinalConditions(monitor);
                            Change change = refactoring.createChange(monitor);
                            change.perform(monitor);
                        } catch (CoreException e) {
                            e.printStackTrace();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    } catch (JavaModelException e1) {
        e1.printStackTrace();
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) JavaModelException(org.eclipse.jdt.core.JavaModelException) RefactoringContribution(org.eclipse.ltk.core.refactoring.RefactoringContribution) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) Change(org.eclipse.ltk.core.refactoring.Change) JavaModelException(org.eclipse.jdt.core.JavaModelException) CoreException(org.eclipse.core.runtime.CoreException) IOException(java.io.IOException) ExecutionException(org.eclipse.core.commands.ExecutionException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) RenameJavaElementDescriptor(org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor) Refactoring(org.eclipse.ltk.core.refactoring.Refactoring)

Aggregations

RenameJavaElementDescriptor (org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor)120 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)56 IType (org.eclipse.jdt.core.IType)55 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)55 Test (org.junit.Test)33 RenameRefactoring (org.eclipse.ltk.core.refactoring.participants.RenameRefactoring)29 IMethod (org.eclipse.jdt.core.IMethod)28 JDTRefactoringDescriptorComment (org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment)26 IJavaProject (org.eclipse.jdt.core.IJavaProject)25 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)25 IField (org.eclipse.jdt.core.IField)24 Refactoring (org.eclipse.ltk.core.refactoring.Refactoring)23 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)22 DynamicValidationRefactoringChange (org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationRefactoringChange)21 ArrayList (java.util.ArrayList)19 RenameArguments (org.eclipse.ltk.core.refactoring.participants.RenameArguments)19 JavaModelException (org.eclipse.jdt.core.JavaModelException)15 IPath (org.eclipse.core.runtime.IPath)12 TextChange (org.eclipse.ltk.core.refactoring.TextChange)10 IFile (org.eclipse.core.resources.IFile)9