Search in sources :

Example 1 with JavaRenameProcessor

use of org.eclipse.jdt.internal.corext.refactoring.rename.JavaRenameProcessor in project che by eclipse.

the class RenameMethodRefactoringContribution method createRefactoring.

/**
	 * {@inheritDoc}
	 */
@Override
public Refactoring createRefactoring(JavaRefactoringDescriptor descriptor, RefactoringStatus status) throws JavaModelException {
    JavaRefactoringArguments arguments = new JavaRefactoringArguments(descriptor.getProject(), retrieveArgumentMap(descriptor));
    String input = arguments.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT);
    IMethod method = (IMethod) JavaRefactoringDescriptorUtil.handleToElement(arguments.getProject(), input);
    if (method == null) {
        status.addFatalError(Messages.format(RefactoringCoreMessages.RenameMethodRefactoringContribution_could_not_create, new Object[] { BasicElementLabels.getResourceName(arguments.getProject()), input }));
        return null;
    }
    JavaRenameProcessor processor;
    if (MethodChecks.isVirtual(method)) {
        processor = new RenameVirtualMethodProcessor(method, arguments, status);
    } else {
        processor = new RenameNonVirtualMethodProcessor(method, arguments, status);
    }
    return new RenameRefactoring(processor);
}
Also used : JavaRefactoringArguments(org.eclipse.jdt.internal.corext.refactoring.JavaRefactoringArguments) RenameNonVirtualMethodProcessor(org.eclipse.jdt.internal.corext.refactoring.rename.RenameNonVirtualMethodProcessor) RenameVirtualMethodProcessor(org.eclipse.jdt.internal.corext.refactoring.rename.RenameVirtualMethodProcessor) RenameRefactoring(org.eclipse.ltk.core.refactoring.participants.RenameRefactoring) JavaRenameProcessor(org.eclipse.jdt.internal.corext.refactoring.rename.JavaRenameProcessor) IMethod(org.eclipse.jdt.core.IMethod)

Example 2 with JavaRenameProcessor

use of org.eclipse.jdt.internal.corext.refactoring.rename.JavaRenameProcessor in project tdi-studio-se by Talend.

the class EditPropertiesAction method processRename.

protected void processRename(IRepositoryNode node, String originalName) {
    try {
        IRunProcessService runProcessService = CorePlugin.getDefault().getRunProcessService();
        ITalendProcessJavaProject talendProcessJavaProject = runProcessService.getTalendProcessJavaProject();
        if (talendProcessJavaProject == null) {
            return;
        }
        IFolder srcFolder = talendProcessJavaProject.getSrcFolder();
        IPackageFragmentRoot root = talendProcessJavaProject.getJavaProject().getPackageFragmentRoot(srcFolder);
        // add for bug TDI-24379 on August 23, 2013.
        IFolder srcInterFolder = srcFolder.getFolder(JavaUtils.JAVA_INTERNAL_DIRECTORY);
        if (srcInterFolder.exists()) {
            File file = new File(srcInterFolder.getLocationURI());
            for (File f : file.listFiles()) {
                if (f.isFile()) {
                    f.delete();
                }
            }
        }
        // qli modified to fix the bug 5400 and 6185.
        // update for fix [TESB-6784]
        IPackageFragment routinesPkg = getPackageFragment(root, node);
        // ICompilationUnit unit = routinesPkg.getCompilationUnit(originalName +
        // SuffixConstants.SUFFIX_STRING_java);
        //$NON-NLS-1$
        ICompilationUnit unit = routinesPkg.getCompilationUnit(originalName + ".java");
        if (unit == null) {
            return;
        }
        String newName = node.getObject().getProperty().getLabel();
        JavaRenameProcessor processor = new RenameCompilationUnitProcessor(unit);
        // processor.setNewElementName(newName + SuffixConstants.SUFFIX_STRING_java);
        //$NON-NLS-1$
        processor.setNewElementName(newName + ".java");
        RenameRefactoring ref = new RenameRefactoring(processor);
        final PerformRefactoringOperation operation = new PerformRefactoringOperation(ref, CheckConditionsOperation.ALL_CONDITIONS);
        IRunnableWithProgress r = new IRunnableWithProgress() {

            @Override
            public void run(final IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                Display.getDefault().asyncExec(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            operation.run(monitor);
                        } catch (CoreException e) {
                            ExceptionHandler.process(e);
                        }
                    }
                });
            }
        };
        PlatformUI.getWorkbench().getProgressService().run(true, true, r);
        RefactoringStatus conditionStatus = operation.getConditionStatus();
        if (conditionStatus.hasError()) {
            //$NON-NLS-1$
            String errorMessage = Messages.getString("EditPropertiesAction.renameError", unit.getElementName(), newName);
            RefactoringStatusEntry[] entries = conditionStatus.getEntries();
            for (RefactoringStatusEntry entry : entries) {
                //$NON-NLS-1$
                errorMessage += "\n>>>" + entry.getMessage();
            }
            Shell shell = null;
            IRepositoryView viewPart = getViewPart();
            if (viewPart != null) {
                shell = viewPart.getViewSite().getShell();
            } else {
                shell = Display.getCurrent().getActiveShell();
            }
            //$NON-NLS-1$
            MessageDialog.openError(shell, Messages.getString("EditPropertiesAction.warning"), errorMessage);
            return;
        }
        // ICompilationUnit newUnit = routinesPkg.getCompilationUnit(newName + SuffixConstants.SUFFIX_STRING_java);
        //$NON-NLS-1$
        ICompilationUnit newUnit = routinesPkg.getCompilationUnit(newName + ".java");
        if (newUnit == null) {
            return;
        }
        RoutineItem item = (RoutineItem) node.getObject().getProperty().getItem();
        IFile javaFile = (IFile) newUnit.getAdapter(IResource.class);
        try {
            ByteArray byteArray = item.getContent();
            byteArray.setInnerContentFromFile(javaFile);
            IRepositoryService service = CorePlugin.getDefault().getRepositoryService();
            IProxyRepositoryFactory repFactory = service.getProxyRepositoryFactory();
            repFactory.save(item);
        } catch (Exception e) {
            // e.printStackTrace();
            ExceptionHandler.process(e);
        }
    } catch (Exception e) {
        // e.printStackTrace();
        ExceptionHandler.process(e);
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) IRunProcessService(org.talend.designer.runprocess.IRunProcessService) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) PerformRefactoringOperation(org.eclipse.ltk.core.refactoring.PerformRefactoringOperation) IRepositoryView(org.talend.repository.ui.views.IRepositoryView) ITalendProcessJavaProject(org.talend.core.runtime.process.ITalendProcessJavaProject) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) IRepositoryService(org.talend.repository.model.IRepositoryService) Shell(org.eclipse.swt.widgets.Shell) ByteArray(org.talend.core.model.properties.ByteArray) JavaRenameProcessor(org.eclipse.jdt.internal.corext.refactoring.rename.JavaRenameProcessor) IProxyRepositoryFactory(org.talend.repository.model.IProxyRepositoryFactory) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) RenameRefactoring(org.eclipse.ltk.core.refactoring.participants.RenameRefactoring) RefactoringStatusEntry(org.eclipse.ltk.core.refactoring.RefactoringStatusEntry) RoutineItem(org.talend.core.model.properties.RoutineItem) CoreException(org.eclipse.core.runtime.CoreException) PartInitException(org.eclipse.ui.PartInitException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) RenameCompilationUnitProcessor(org.eclipse.jdt.internal.corext.refactoring.rename.RenameCompilationUnitProcessor) CoreException(org.eclipse.core.runtime.CoreException) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IResource(org.eclipse.core.resources.IResource) IFolder(org.eclipse.core.resources.IFolder)

Example 3 with JavaRenameProcessor

use of org.eclipse.jdt.internal.corext.refactoring.rename.JavaRenameProcessor in project tdi-studio-se by Talend.

the class StandAloneTalendJavaEditor method refreshName.

/*
     * (non-Javadoc)
     * 
     * @see org.talend.core.ui.IUIRefresher#refreshName()
     */
@Override
public void refreshName() {
    ICompilationUnit unit = (ICompilationUnit) this.getInputJavaElement();
    String newName = item.getProperty().getLabel();
    try {
        boolean noError = true;
        // String newName2 = newName + SuffixConstants.SUFFIX_STRING_java;
        //$NON-NLS-1$
        String newName2 = newName + ".java";
        if (item instanceof RoutineItem && !unit.getElementName().equals(newName2)) {
            JavaRenameProcessor processor = new RenameCompilationUnitProcessor(unit);
            processor.setNewElementName(newName2);
            RenameRefactoring ref = new RenameRefactoring(processor);
            final PerformRefactoringOperation operation = new PerformRefactoringOperation(ref, CheckConditionsOperation.ALL_CONDITIONS);
            IRunnableWithProgress r = new IRunnableWithProgress() {

                @Override
                public void run(final IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                    Display.getDefault().asyncExec(new Runnable() {

                        @Override
                        public void run() {
                            try {
                                operation.run(monitor);
                            } catch (CoreException e) {
                                ExceptionHandler.process(e);
                            }
                        }
                    });
                }
            };
            PlatformUI.getWorkbench().getProgressService().run(true, true, r);
            RefactoringStatus conditionStatus = operation.getConditionStatus();
            if (conditionStatus != null && conditionStatus.hasError()) {
                //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                String errorMessage = "Rename " + unit.getElementName() + " to " + newName + " has errors!";
                RefactoringStatusEntry[] entries = conditionStatus.getEntries();
                for (RefactoringStatusEntry entry : entries) {
                    //$NON-NLS-1$
                    errorMessage += "\n>>>" + entry.getMessage();
                }
                //$NON-NLS-1$
                MessageDialog.openError(this.getSite().getShell(), "Warning", errorMessage);
                noError = false;
            }
        }
        if (noError) {
            doSave(null);
        }
        setName();
    } catch (Exception e) {
        ExceptionHandler.process(e);
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) RenameRefactoring(org.eclipse.ltk.core.refactoring.participants.RenameRefactoring) RefactoringStatusEntry(org.eclipse.ltk.core.refactoring.RefactoringStatusEntry) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) RoutineItem(org.talend.core.model.properties.RoutineItem) PerformRefactoringOperation(org.eclipse.ltk.core.refactoring.PerformRefactoringOperation) CoreException(org.eclipse.core.runtime.CoreException) PartInitException(org.eclipse.ui.PartInitException) LoginException(org.talend.commons.exception.LoginException) InvocationTargetException(java.lang.reflect.InvocationTargetException) PersistenceException(org.talend.commons.exception.PersistenceException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) RenameCompilationUnitProcessor(org.eclipse.jdt.internal.corext.refactoring.rename.RenameCompilationUnitProcessor) CoreException(org.eclipse.core.runtime.CoreException) IWorkspaceRunnable(org.eclipse.core.resources.IWorkspaceRunnable) JavaRenameProcessor(org.eclipse.jdt.internal.corext.refactoring.rename.JavaRenameProcessor)

Example 4 with JavaRenameProcessor

use of org.eclipse.jdt.internal.corext.refactoring.rename.JavaRenameProcessor in project xtext-eclipse by eclipse.

the class CombinedJvmJdtRenameProcessor method checkFinalConditions.

@Override
public RefactoringStatus checkFinalConditions(IProgressMonitor pm, CheckConditionsContext context) throws CoreException, OperationCanceledException {
    SubMonitor monitor = SubMonitor.convert(pm, jvmElements2jdtProcessors.size() + 1);
    RefactoringStatus status = super.checkFinalConditions(monitor.newChild(1), context);
    ResourceSet resourceSet = getResourceSet(getRenameElementContext());
    getRenameArguments().getRenameStrategy().applyDeclarationChange(getNewName(), resourceSet);
    for (Iterator<Map.Entry<URI, JavaRenameProcessor>> entryIterator = jvmElements2jdtProcessors.entrySet().iterator(); entryIterator.hasNext(); ) {
        Map.Entry<URI, JavaRenameProcessor> jvmElement2jdtRefactoring = entryIterator.next();
        if (monitor.isCanceled()) {
            throw new OperationCanceledException();
        }
        URI renamedJvmElementURI = getRenameArguments().getNewElementURI(jvmElement2jdtRefactoring.getKey());
        EObject renamedJvmElement = resourceSet.getEObject(renamedJvmElementURI, false);
        if (!(renamedJvmElement instanceof JvmIdentifiableElement) || renamedJvmElement.eIsProxy()) {
            status.addError("Cannot find inferred JVM element after refactoring.");
        } else {
            JavaRenameProcessor jdtRefactoring = jvmElement2jdtRefactoring.getValue();
            String newName = ((JvmIdentifiableElement) renamedJvmElement).getSimpleName();
            if (jdtRefactoring.getCurrentElementName().equals(newName)) {
                entryIterator.remove();
            } else {
                jdtRefactoring.setNewElementName(newName);
                status.merge(jdtRefactoring.checkFinalConditions(monitor.newChild(1), context));
            }
        }
    }
    getRenameArguments().getRenameStrategy().revertDeclarationChange(resourceSet);
    return status;
}
Also used : JvmIdentifiableElement(org.eclipse.xtext.common.types.JvmIdentifiableElement) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) SubMonitor(org.eclipse.core.runtime.SubMonitor) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) URI(org.eclipse.emf.common.util.URI) EObject(org.eclipse.emf.ecore.EObject) JavaRenameProcessor(org.eclipse.jdt.internal.corext.refactoring.rename.JavaRenameProcessor) Map(java.util.Map)

Example 5 with JavaRenameProcessor

use of org.eclipse.jdt.internal.corext.refactoring.rename.JavaRenameProcessor in project xtext-xtend by eclipse.

the class JavaRefactoringIntegrationTest method createXtendRenameRefactoring.

protected ProcessorBasedRefactoring createXtendRenameRefactoring(final XtextEditor editor, final int offset, String newName) {
    IRenameElementContext renameElementContext = createRenameElementContext(editor, offset);
    ProcessorBasedRefactoring renameRefactoring = renameRefactoringProvider.getRenameRefactoring(renameElementContext);
    RefactoringProcessor processor = renameRefactoring.getProcessor();
    if (processor instanceof AbstractRenameProcessor)
        ((AbstractRenameProcessor) processor).setNewName(newName);
    else if (processor instanceof JavaRenameProcessor)
        ((JavaRenameProcessor) processor).setNewElementName(newName);
    return renameRefactoring;
}
Also used : CompositeRefactoringProcessor(org.eclipse.xtext.common.types.ui.refactoring.participant.CompositeRefactoringProcessor) RefactoringProcessor(org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor) AbstractRenameProcessor(org.eclipse.xtext.ui.refactoring.impl.AbstractRenameProcessor) IRenameElementContext(org.eclipse.xtext.ui.refactoring.ui.IRenameElementContext) JavaRenameProcessor(org.eclipse.jdt.internal.corext.refactoring.rename.JavaRenameProcessor) ProcessorBasedRefactoring(org.eclipse.ltk.core.refactoring.participants.ProcessorBasedRefactoring)

Aggregations

JavaRenameProcessor (org.eclipse.jdt.internal.corext.refactoring.rename.JavaRenameProcessor)9 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)4 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)3 SubMonitor (org.eclipse.core.runtime.SubMonitor)3 RenameRefactoring (org.eclipse.ltk.core.refactoring.participants.RenameRefactoring)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Map (java.util.Map)2 CoreException (org.eclipse.core.runtime.CoreException)2 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)2 URI (org.eclipse.emf.common.util.URI)2 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)2 RenameCompilationUnitProcessor (org.eclipse.jdt.internal.corext.refactoring.rename.RenameCompilationUnitProcessor)2 IRunnableWithProgress (org.eclipse.jface.operation.IRunnableWithProgress)2 PerformRefactoringOperation (org.eclipse.ltk.core.refactoring.PerformRefactoringOperation)2 RefactoringStatusEntry (org.eclipse.ltk.core.refactoring.RefactoringStatusEntry)2 ProcessorBasedRefactoring (org.eclipse.ltk.core.refactoring.participants.ProcessorBasedRefactoring)2 RefactoringProcessor (org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor)2 PartInitException (org.eclipse.ui.PartInitException)2 CompositeRefactoringProcessor (org.eclipse.xtext.common.types.ui.refactoring.participant.CompositeRefactoringProcessor)2 AbstractRenameProcessor (org.eclipse.xtext.ui.refactoring.impl.AbstractRenameProcessor)2