IDEA-130959 (exceptions passing)

This commit is contained in:
Roman Shevchenko
2014-11-11 18:10:53 +01:00
parent 3f40aa3cd8
commit 01c823a6d8
3 changed files with 41 additions and 15 deletions
@@ -20,6 +20,7 @@ import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.util.Ref;
import com.intellij.util.ExceptionUtil;
import org.jetbrains.annotations.NotNull;
import java.util.concurrent.*;
@@ -49,8 +50,21 @@ public class ApplicationUtil {
* Allows to interrupt a process which does not performs checkCancelled() calls by itself.
* Note that the process may continue to run in background indefinitely - so <b>avoid using this method unless absolutely needed</b>.
*/
public static <T> T runWithCheckCanceled(@NotNull Callable<T> callable) throws ExecutionException, InterruptedException {
Future<T> future = ApplicationManager.getApplication().executeOnPooledThread(callable);
public static <T> T runWithCheckCanceled(@NotNull final Callable<T> callable) throws Exception {
final Ref<Throwable> error = Ref.create();
Future<T> future = ApplicationManager.getApplication().executeOnPooledThread(new Callable<T>() {
@Override
public T call() throws Exception {
try {
return callable.call();
}
catch (Throwable t) {
error.set(t);
return null;
}
};
});
while (true) {
try {
@@ -62,7 +76,9 @@ public class ApplicationUtil {
}
try {
return future.get(200, TimeUnit.MILLISECONDS);
T result = future.get(200, TimeUnit.MILLISECONDS);
ExceptionUtil.rethrowAll(error.get());
return result;
}
catch (TimeoutException ignored) { }
}
@@ -37,6 +37,7 @@ import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.encoding.EncodingRegistry;
import com.intellij.testFramework.LightVirtualFile;
import com.intellij.util.ArrayUtil;
import com.intellij.util.ExceptionUtil;
import com.intellij.util.text.CharArrayUtil;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
@@ -374,11 +375,10 @@ public final class LoadTextUtil {
if (decompiler != null) {
CharSequence text;
final Application app = ApplicationManager.getApplication();
Application app = ApplicationManager.getApplication();
if (app != null && app.isDispatchThread() && !app.isWriteAccessAllowed()) {
final Ref<CharSequence> result = Ref.create(ArrayUtil.EMPTY_CHAR_SEQUENCE);
final Ref<RuntimeException> exception = Ref.create();
final Ref<Throwable> error = Ref.create();
ProgressManager.getInstance().run(new Task.Modal(null, "Decompiling " + file.getName(), true) {
@Override
public void run(@NotNull ProgressIndicator indicator) {
@@ -391,17 +391,12 @@ public final class LoadTextUtil {
}
}));
}
catch (RuntimeException e) {
exception.set(e);
catch (Throwable t) {
error.set(t);
}
catch (Exception ignored) { }
}
});
if (!exception.isNull()) {
throw exception.get();
}
ExceptionUtil.rethrowUnchecked(error.get());
text = result.get();
}
else {
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
* Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -146,4 +146,19 @@ public class ExceptionUtil {
}
return result;
}
public static void rethrowUnchecked(@Nullable Throwable t) {
if (t != null) {
if (t instanceof Error) throw (Error)t;
if (t instanceof RuntimeException) throw (RuntimeException)t;
}
}
public static void rethrowAll(@Nullable Throwable t) throws Exception {
if (t != null) {
if (t instanceof Error) throw (Error)t;
if (t instanceof RuntimeException) throw (RuntimeException)t;
throw (Exception)t;
}
}
}