Search in sources :

Example 6 with ISafeRunnable

use of org.eclipse.core.runtime.ISafeRunnable in project che by eclipse.

the class UndoManager2 method fireUndoStackChanged.

private void fireUndoStackChanged() {
    if (fListeners == null)
        return;
    Object[] listeners = fListeners.getListeners();
    for (int i = 0; i < listeners.length; i++) {
        final IUndoManagerListener listener = (IUndoManagerListener) listeners[i];
        SafeRunner.run(new ISafeRunnable() {

            public void run() throws Exception {
                listener.undoStackChanged(UndoManager2.this);
            }

            public void handleException(Throwable exception) {
                RefactoringCorePlugin.log(exception);
            }
        });
    }
}
Also used : IUndoManagerListener(org.eclipse.ltk.core.refactoring.IUndoManagerListener) ISafeRunnable(org.eclipse.core.runtime.ISafeRunnable) CoreException(org.eclipse.core.runtime.CoreException) ExecutionException(org.eclipse.core.commands.ExecutionException)

Example 7 with ISafeRunnable

use of org.eclipse.core.runtime.ISafeRunnable in project che by eclipse.

the class InferTypeArgumentsRefactoring method checkFinalConditions.

/*
	 * @see org.eclipse.ltk.core.refactoring.Refactoring#checkFinalConditions(org.eclipse.core.runtime.IProgressMonitor)
	 */
@Override
public RefactoringStatus checkFinalConditions(final IProgressMonitor pm) throws CoreException, OperationCanceledException {
    HashMap<IJavaProject, ArrayList<IJavaElement>> projectsToElements = getJavaElementsPerProject(fElements);
    //$NON-NLS-1$
    pm.beginTask("", projectsToElements.size() + 2);
    final RefactoringStatus result = new RefactoringStatus();
    try {
        fTCModel = new InferTypeArgumentsTCModel();
        final InferTypeArgumentsConstraintCreator unitCollector = new InferTypeArgumentsConstraintCreator(fTCModel, fAssumeCloneReturnsSameType);
        for (Iterator<Entry<IJavaProject, ArrayList<IJavaElement>>> iter = projectsToElements.entrySet().iterator(); iter.hasNext(); ) {
            Entry<IJavaProject, ArrayList<IJavaElement>> entry = iter.next();
            IJavaProject project = entry.getKey();
            ArrayList<IJavaElement> javaElementsList = entry.getValue();
            IJavaElement[] javaElements = javaElementsList.toArray(new IJavaElement[javaElementsList.size()]);
            List<ICompilationUnit> cus = Arrays.asList(JavaModelUtil.getAllCompilationUnits(javaElements));
            int batchSize = 150;
            int batches = ((cus.size() - 1) / batchSize) + 1;
            SubProgressMonitor projectMonitor = new SubProgressMonitor(pm, 1);
            //$NON-NLS-1$
            projectMonitor.beginTask("", batches);
            projectMonitor.setTaskName(RefactoringCoreMessages.InferTypeArgumentsRefactoring_building);
            for (int i = 0; i < batches; i++) {
                List<ICompilationUnit> batch = cus.subList(i * batchSize, Math.min(cus.size(), (i + 1) * batchSize));
                ICompilationUnit[] batchCus = batch.toArray(new ICompilationUnit[batch.size()]);
                final SubProgressMonitor batchMonitor = new SubProgressMonitor(projectMonitor, 1);
                batchMonitor.subTask(RefactoringCoreMessages.InferTypeArgumentsRefactoring_calculating_dependencies);
                ASTParser parser = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
                parser.setProject(project);
                parser.setCompilerOptions(RefactoringASTParser.getCompilerOptions(project));
                parser.setResolveBindings(true);
                parser.createASTs(batchCus, new String[0], new ASTRequestor() {

                    @Override
                    public void acceptAST(final ICompilationUnit source, final CompilationUnit ast) {
                        batchMonitor.subTask(BasicElementLabels.getFileName(source));
                        SafeRunner.run(new ISafeRunnable() {

                            public void run() throws Exception {
                                IProblem[] problems = ast.getProblems();
                                for (int p = 0; p < problems.length; p++) {
                                    if (problems[p].isError()) {
                                        String cuName = JavaElementLabels.getElementLabel(source, JavaElementLabels.CU_QUALIFIED);
                                        String msg = Messages.format(RefactoringCoreMessages.InferTypeArgumentsRefactoring_error_in_cu_skipped, new Object[] { cuName });
                                        result.addError(msg, JavaStatusContext.create(source, SourceRangeFactory.create(problems[p])));
                                        return;
                                    }
                                }
                                ast.accept(unitCollector);
                            }

                            public void handleException(Throwable exception) {
                                String cuName = JavaElementLabels.getElementLabel(source, JavaElementLabels.CU_QUALIFIED);
                                String msg = Messages.format(RefactoringCoreMessages.InferTypeArgumentsRefactoring_internal_error, new Object[] { cuName });
                                JavaPlugin.log(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IJavaStatusConstants.INTERNAL_ERROR, msg, null));
                                String msg2 = Messages.format(RefactoringCoreMessages.InferTypeArgumentsRefactoring_error_skipped, new Object[] { cuName });
                                result.addError(msg2, JavaStatusContext.create(source));
                            }
                        });
                        fTCModel.newCu();
                    }

                    @Override
                    public void acceptBinding(String bindingKey, IBinding binding) {
                    //do nothing
                    }
                }, batchMonitor);
            }
            projectMonitor.done();
            fTCModel.newCu();
        }
        //			Display.getDefault().syncExec(new Runnable() {
        //				public void run() {
        //					MessageDialog.openInformation(Display.getCurrent().getActiveShell(), "Debugging...", "after constraint gen");
        //				}
        //			});
        pm.setTaskName(RefactoringCoreMessages.InferTypeArgumentsRefactoring_solving);
        InferTypeArgumentsConstraintsSolver solver = new InferTypeArgumentsConstraintsSolver(fTCModel);
        InferTypeArgumentsUpdate updates = solver.solveConstraints(new SubProgressMonitor(pm, 1));
        //free caches
        solver = null;
        fChangeManager = new TextChangeManager();
        rewriteDeclarations(updates, new SubProgressMonitor(pm, 1));
        IFile[] filesToModify = ResourceUtil.getFiles(fChangeManager.getAllCompilationUnits());
        result.merge(Checks.validateModifiesFiles(filesToModify, getValidationContext()));
        return result;
    } finally {
        pm.done();
        clearGlobalState();
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) IBinding(org.eclipse.jdt.core.dom.IBinding) ArrayList(java.util.ArrayList) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) Entry(java.util.Map.Entry) ASTRequestor(org.eclipse.jdt.core.dom.ASTRequestor) ISafeRunnable(org.eclipse.core.runtime.ISafeRunnable) RefactoringASTParser(org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser) ASTParser(org.eclipse.jdt.core.dom.ASTParser) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IJavaElement(org.eclipse.jdt.core.IJavaElement) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IProblem(org.eclipse.jdt.core.compiler.IProblem) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) IJavaProject(org.eclipse.jdt.core.IJavaProject) TextChangeManager(org.eclipse.jdt.internal.corext.refactoring.util.TextChangeManager)

Example 8 with ISafeRunnable

use of org.eclipse.core.runtime.ISafeRunnable in project che by eclipse.

the class CompositeChange method dispose.

/**
	 * {@inheritDoc}
	 * <p>
	 * The composite change sends <code>dispose</code> to all its children. It is guaranteed
	 * that all children receive the <code>dispose</code> call.
	 * </p>
	 */
public void dispose() {
    for (Iterator iter = fChanges.iterator(); iter.hasNext(); ) {
        final Change change = (Change) iter.next();
        SafeRunner.run(new ISafeRunnable() {

            public void run() throws Exception {
                change.dispose();
            }

            public void handleException(Throwable exception) {
                RefactoringCorePlugin.log(exception);
            }
        });
    }
}
Also used : Iterator(java.util.Iterator) ISafeRunnable(org.eclipse.core.runtime.ISafeRunnable) CoreException(org.eclipse.core.runtime.CoreException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException)

Example 9 with ISafeRunnable

use of org.eclipse.core.runtime.ISafeRunnable in project che by eclipse.

the class DynamicValidationStateChange method workspaceChanged.

public void workspaceChanged() {
    long currentTime = System.currentTimeMillis();
    if (currentTime - fTimeStamp < LIFE_TIME)
        return;
    fValidationState = RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.DynamicValidationStateChange_workspace_changed);
    // remove listener from workspace tracker
    WorkspaceTracker.INSTANCE.removeListener(this);
    fListenerRegistered = false;
    // clear up the children to not hang onto too much memory
    Change[] children = clear();
    for (int i = 0; i < children.length; i++) {
        final Change change = children[i];
        SafeRunner.run(new ISafeRunnable() {

            public void run() throws Exception {
                change.dispose();
            }

            public void handleException(Throwable exception) {
                JavaPlugin.log(exception);
            }
        });
    }
}
Also used : ISafeRunnable(org.eclipse.core.runtime.ISafeRunnable) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange) Change(org.eclipse.ltk.core.refactoring.Change) CoreException(org.eclipse.core.runtime.CoreException)

Example 10 with ISafeRunnable

use of org.eclipse.core.runtime.ISafeRunnable in project flux by eclipse.

the class ASTProvider method createAST.

private static CompilationUnit createAST(final ITypeRoot input, final IProgressMonitor progressMonitor) {
    if (progressMonitor != null && progressMonitor.isCanceled())
        return null;
    final ASTParser parser = ASTParser.newParser(SHARED_AST_LEVEL);
    parser.setResolveBindings(true);
    parser.setStatementsRecovery(SHARED_AST_STATEMENT_RECOVERY);
    parser.setBindingsRecovery(SHARED_BINDING_RECOVERY);
    parser.setSource(input);
    if (progressMonitor != null && progressMonitor.isCanceled())
        return null;
    final CompilationUnit[] root = new CompilationUnit[1];
    SafeRunner.run(new ISafeRunnable() {

        public void run() {
            try {
                if (progressMonitor != null && progressMonitor.isCanceled())
                    return;
                root[0] = (CompilationUnit) parser.createAST(progressMonitor);
                //mark as unmodifiable
                ASTNodes.setFlagsToAST(root[0], ASTNode.PROTECT);
            } catch (OperationCanceledException ex) {
                return;
            }
        }

        public void handleException(Throwable ex) {
            //$NON-NLS-1$
            IStatus status = new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IStatus.OK, "Error in JDT Core during AST creation", ex);
            JavaPlugin.getDefault().getLog().log(status);
        }
    });
    return root[0];
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) IStatus(org.eclipse.core.runtime.IStatus) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) ISafeRunnable(org.eclipse.core.runtime.ISafeRunnable) ASTParser(org.eclipse.jdt.core.dom.ASTParser)

Aggregations

ISafeRunnable (org.eclipse.core.runtime.ISafeRunnable)27 CoreException (org.eclipse.core.runtime.CoreException)23 IConfigurationElement (org.eclipse.core.runtime.IConfigurationElement)11 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)5 ExecutionException (org.eclipse.core.commands.ExecutionException)4 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)4 IUndoManagerListener (org.eclipse.ltk.core.refactoring.IUndoManagerListener)4 ArrayList (java.util.ArrayList)3 IStatus (org.eclipse.core.runtime.IStatus)3 Status (org.eclipse.core.runtime.Status)3 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)3 ASTParser (org.eclipse.jdt.core.dom.ASTParser)3 IOException (java.io.IOException)2 Iterator (java.util.Iterator)2 IFile (org.eclipse.core.resources.IFile)2 IProject (org.eclipse.core.resources.IProject)2 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)2 PerformanceStats (org.eclipse.core.runtime.PerformanceStats)2 IElementChangedListener (org.eclipse.jdt.core.IElementChangedListener)2 IJavaElement (org.eclipse.jdt.core.IJavaElement)2