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.*;
* 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 {
}
try {
- return future.get(200, TimeUnit.MILLISECONDS);
+ T result = future.get(200, TimeUnit.MILLISECONDS);
+ ExceptionUtil.rethrowAll(error.get());
+ return result;
}
catch (TimeoutException ignored) { }
}
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;
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) {
}
}));
}
- 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 {
/*
- * 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.
}
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;
+ }
+ }
}