From 0b3b582bd2a856a38ca4eb6d0a3f1102fffc78e2 Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Wed, 10 May 2017 12:16:49 +0200 Subject: [PATCH] Cleanup (minor optimization; warnings; docs; formatting) --- .../process/BaseOSProcessHandler.java | 17 ++++--- .../execution/process/ProcessWaitFor.java | 45 ++++++++++--------- .../com/intellij/util/io/BaseDataReader.java | 20 ++++----- 3 files changed, 43 insertions(+), 39 deletions(-) diff --git a/platform/util/src/com/intellij/execution/process/BaseOSProcessHandler.java b/platform/util/src/com/intellij/execution/process/BaseOSProcessHandler.java index 040d84e760f0..f979d03b0f0b 100644 --- a/platform/util/src/com/intellij/execution/process/BaseOSProcessHandler.java +++ b/platform/util/src/com/intellij/execution/process/BaseOSProcessHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2016 JetBrains s.r.o. + * Copyright 2000-2017 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. @@ -43,6 +43,7 @@ public class BaseOSProcessHandler extends ProcessHandler implements TaskExecutor private static final Options ADAPTIVE_NON_BLOCKING = new Options() { @Override + @SuppressWarnings("deprecation") public BaseDataReader.SleepingPolicy policy() { return new BaseDataReader.AdaptiveSleepingPolicy(); } @@ -89,12 +90,14 @@ public class BaseOSProcessHandler extends ProcessHandler implements TaskExecutor return myProcess; } - /** @deprecated use {@link #readerOptions()} (to be removed in IDEA 18) */ + /** @deprecated use {@link #readerOptions()} (to be removed in IDEA 2018) */ + @SuppressWarnings("DeprecatedIsStillUsed") protected boolean useAdaptiveSleepingPolicyWhenReadingOutput() { return false; } - /** @deprecated use {@link #readerOptions()} (to be removed in IDEA 18) */ + /** @deprecated use {@link #readerOptions()} (to be removed in IDEA 2018) */ + @SuppressWarnings("DeprecatedIsStillUsed") protected boolean useNonBlockingRead() { return !Registry.is("output.reader.blocking.mode", false); } @@ -163,12 +166,14 @@ public class BaseOSProcessHandler extends ProcessHandler implements TaskExecutor super.startNotify(); } - /** @deprecated override {@link #createOutputDataReader()} (to be removed in IDEA 18) */ + /** @deprecated override {@link #createOutputDataReader()} (to be removed in IDEA 2018) */ + @SuppressWarnings("DeprecatedIsStillUsed") protected BaseDataReader createErrorDataReader(@SuppressWarnings("UnusedParameters") BaseDataReader.SleepingPolicy policy) { return createErrorDataReader(); } - /** @deprecated override {@link #createOutputDataReader()} (to be removed in IDEA 18) */ + /** @deprecated override {@link #createOutputDataReader()} (to be removed in IDEA 2018) */ + @SuppressWarnings("DeprecatedIsStillUsed") protected BaseDataReader createOutputDataReader(@SuppressWarnings("UnusedParameters") BaseDataReader.SleepingPolicy policy) { return createOutputDataReader(); } @@ -262,8 +267,8 @@ public class BaseOSProcessHandler extends ProcessHandler implements TaskExecutor return myCharset; } + /** @deprecated use {@link BaseOSProcessHandler#executeTask(Runnable)} instead (to be removed in IDEA 2018) */ public static class ExecutorServiceHolder { - /** @deprecated use {@link BaseOSProcessHandler#executeTask(Runnable)} instead (to be removed in IDEA 17) */ public static Future submit(@NotNull Runnable task) { LOG.warn("Deprecated method. Please use com.intellij.execution.process.BaseOSProcessHandler.executeTask() instead", new Throwable()); return AppExecutorUtil.getAppExecutorService().submit(task); diff --git a/platform/util/src/com/intellij/execution/process/ProcessWaitFor.java b/platform/util/src/com/intellij/execution/process/ProcessWaitFor.java index 1fcd50b640fb..05a7731111f5 100644 --- a/platform/util/src/com/intellij/execution/process/ProcessWaitFor.java +++ b/platform/util/src/com/intellij/execution/process/ProcessWaitFor.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2016 JetBrains s.r.o. + * Copyright 2000-2017 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. @@ -29,7 +29,7 @@ public class ProcessWaitFor { private final Future myWaitForThreadFuture; private final BlockingQueue> myTerminationCallback = new ArrayBlockingQueue>(1); - /** @deprecated use {@link #ProcessWaitFor(Process, TaskExecutor, String)} instead (to be removed in IDEA 17) */ + /** @deprecated use {@link #ProcessWaitFor(Process, TaskExecutor, String)} instead (to be removed in IDEA 2018) */ @Deprecated public ProcessWaitFor(@NotNull final Process process, @NotNull TaskExecutor executor) { this(process, executor, ""); @@ -39,30 +39,35 @@ public class ProcessWaitFor { myWaitForThreadFuture = executor.executeTask(new Runnable() { @Override public void run() { - String oldThreadName = Thread.currentThread().getName(); + String oldThreadName = null; if (!StringUtil.isEmptyOrSpaces(presentableName)) { + oldThreadName = Thread.currentThread().getName(); Thread.currentThread().setName("ProcessWaitFor: " + presentableName); } - int exitCode = 0; try { - while (true) { + int exitCode = 0; + try { + while (true) { + try { + exitCode = process.waitFor(); + break; + } + catch (InterruptedException e) { + LOG.debug(e); + } + } + } + finally { try { - exitCode = process.waitFor(); - break; + myTerminationCallback.take().consume(exitCode); } catch (InterruptedException e) { - LOG.debug(e); + LOG.info(e); } } } finally { - try { - myTerminationCallback.take().consume(exitCode); - } - catch (InterruptedException e) { - LOG.info(e); - } - finally { + if (oldThreadName != null) { Thread.currentThread().setName(oldThreadName); } } @@ -82,11 +87,10 @@ public class ProcessWaitFor { try { myWaitForThreadFuture.get(); } + catch (CancellationException ignored) { } catch (ExecutionException e) { LOG.error(e); } - catch (CancellationException ignored) { - } } public boolean waitFor(long timeout, @NotNull TimeUnit unit) throws InterruptedException { @@ -96,12 +100,9 @@ public class ProcessWaitFor { catch (ExecutionException e) { LOG.error(e); } - catch (CancellationException ignored) { - } - catch (TimeoutException ignored) { - } + catch (CancellationException ignored) { } + catch (TimeoutException ignored) { } return myWaitForThreadFuture.isDone(); } - } \ No newline at end of file diff --git a/platform/util/src/com/intellij/util/io/BaseDataReader.java b/platform/util/src/com/intellij/util/io/BaseDataReader.java index 6d40463ed95f..e9a066edb059 100644 --- a/platform/util/src/com/intellij/util/io/BaseDataReader.java +++ b/platform/util/src/com/intellij/util/io/BaseDataReader.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2016 JetBrains s.r.o. + * Copyright 2000-2017 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. @@ -73,12 +73,11 @@ public abstract class BaseDataReader { } /** - * Please don't override this method as the BseOSProcessProcessHandler assumes that it can be two reading modes: blocking and non-blocking. + * Please don't override this method as the BaseOSProcessProcessHandler assumes that it can be two reading modes: blocking and non-blocking. * Implement {@link #readAvailableBlocking} and {@link #readAvailableNonBlocking} instead. - * - * + * * If the process handler assumes that reader handles the blocking mode, while it doesn't, it will result into premature stream close. - * + * * @return true in case any data was read * @throws IOException if an exception during IO happened */ @@ -87,9 +86,7 @@ public abstract class BaseDataReader { } /** - * * Non-blocking read returns the control back to the process handler when there is no data to read. - * */ protected boolean readAvailableNonBlocking() throws IOException { throw new UnsupportedOperationException(); @@ -129,6 +126,8 @@ public abstract class BaseDataReader { int getTimeToSleep(boolean wasActive); } + /** @deprecated use one of default policies (recommended) or implement your own (to be removed in IDEA 2018) */ + @SuppressWarnings("DeprecatedIsStillUsed") public static class AdaptiveSleepingPolicy implements SleepingPolicy { private static final int maxSleepTimeWhenIdle = 200; private static final int maxIterationsWithCurrentSleepTime = 50; @@ -170,9 +169,8 @@ public abstract class BaseDataReader { stopSignalled = isStopped; if (!stopSignalled) { - // if process stopped, there is no sense to sleep, + // if process stopped, there is no sense to sleep, // just check if there is unread output in the stream - synchronized (mySleepMonitor) { mySleepMonitor.wait(mySleepingPolicy.getTimeToSleep(read)); } @@ -194,7 +192,7 @@ public abstract class BaseDataReader { } } } - + private void resumeReading() { synchronized (mySleepMonitor) { mySleepMonitor.notifyAll(); @@ -225,4 +223,4 @@ public abstract class BaseDataReader { LOG.error(e); } } -} +} \ No newline at end of file