diff --git a/platform/core-api/src/com/intellij/openapi/application/BaseActionRunnable.java b/platform/core-api/src/com/intellij/openapi/application/BaseActionRunnable.java index 6bd8f05eb0c7..d29a19677dbf 100644 --- a/platform/core-api/src/com/intellij/openapi/application/BaseActionRunnable.java +++ b/platform/core-api/src/com/intellij/openapi/application/BaseActionRunnable.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2016 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. @@ -18,7 +18,6 @@ package com.intellij.openapi.application; import org.jetbrains.annotations.NotNull; public abstract class BaseActionRunnable { - private boolean mySilentExecution; public boolean isSilentExecution() { @@ -30,23 +29,19 @@ public abstract class BaseActionRunnable { @NotNull public abstract RunResult execute(); - protected boolean canWriteNow() { - return getApplication().isWriteAccessAllowed(); - } - - protected boolean canReadNow() { - return getApplication().isReadAccessAllowed(); - } - - protected Application getApplication() { - return ApplicationManager.getApplication(); - } - - /** Same as execute() but do not log error if exception occurred. */ + /** + * Same as {@link #execute()}, but does not log an error if an exception occurs. + */ @NotNull public final RunResult executeSilently() { mySilentExecution = true; return execute(); } + // + /** @deprecated use {@link ApplicationManager#getApplication()} (to be removed in IDEA 2018) */ + protected Application getApplication() { + return ApplicationManager.getApplication(); + } + // } \ No newline at end of file diff --git a/platform/core-api/src/com/intellij/openapi/application/ReadAction.java b/platform/core-api/src/com/intellij/openapi/application/ReadAction.java index c46c5567f38c..1738cd4c48cc 100644 --- a/platform/core-api/src/com/intellij/openapi/application/ReadAction.java +++ b/platform/core-api/src/com/intellij/openapi/application/ReadAction.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2012 JetBrains s.r.o. + * Copyright 2000-2016 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. @@ -41,7 +41,8 @@ public abstract class ReadAction extends BaseActionRunnable { AccessToken token = start(); try { action.run(); - } finally { + } + finally { token.finish(); } } @@ -50,9 +51,9 @@ public abstract class ReadAction extends BaseActionRunnable { AccessToken token = start(); try { return action.compute(); - } finally { + } + finally { token.finish(); } } - -} +} \ No newline at end of file diff --git a/platform/core-api/src/com/intellij/openapi/application/WriteAction.java b/platform/core-api/src/com/intellij/openapi/application/WriteAction.java index 3751b2313c74..bbe4f76da772 100644 --- a/platform/core-api/src/com/intellij/openapi/application/WriteAction.java +++ b/platform/core-api/src/com/intellij/openapi/application/WriteAction.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2015 JetBrains s.r.o. + * Copyright 2000-2016 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. @@ -30,9 +30,8 @@ public abstract class WriteAction extends BaseActionRunnable { public RunResult execute() { final RunResult result = new RunResult(this); - final Application application = ApplicationManager.getApplication(); - boolean dispatchThread = application.isDispatchThread(); - if (dispatchThread) { + Application application = ApplicationManager.getApplication(); + if (application.isDispatchThread()) { AccessToken token = start(getClass()); try { result.run(); @@ -67,8 +66,7 @@ public abstract class WriteAction extends BaseActionRunnable { @NotNull public static AccessToken start() { // get useful information about the write action - Class aClass = ObjectUtils.notNull(ReflectionUtil.getGrandCallerClass(), WriteAction.class); - return start(aClass); + return start(ObjectUtils.notNull(ReflectionUtil.getGrandCallerClass(), WriteAction.class)); } @NotNull @@ -95,4 +93,4 @@ public abstract class WriteAction extends BaseActionRunnable { token.finish(); } } -} +} \ No newline at end of file diff --git a/platform/core-api/src/com/intellij/openapi/command/WriteCommandAction.java b/platform/core-api/src/com/intellij/openapi/command/WriteCommandAction.java index 87cd2037c868..0113f34a0845 100644 --- a/platform/core-api/src/com/intellij/openapi/command/WriteCommandAction.java +++ b/platform/core-api/src/com/intellij/openapi/command/WriteCommandAction.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2015 JetBrains s.r.o. + * Copyright 2000-2016 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. @@ -24,7 +24,6 @@ import com.intellij.openapi.util.Computable; import com.intellij.openapi.util.Ref; import com.intellij.openapi.util.ThrowableComputable; import com.intellij.psi.PsiFile; -import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -33,6 +32,7 @@ import java.util.Collection; public abstract class WriteCommandAction extends BaseActionRunnable { private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.command.WriteCommandAction"); + private final String myCommandName; private final String myGroupID; private final Project myProject; @@ -42,17 +42,15 @@ public abstract class WriteCommandAction extends BaseActionRunnable { this(project, "Undefined", files); } - protected WriteCommandAction(@Nullable Project project, @Nullable @NonNls String commandName, /*@NotNull*/ PsiFile... files) { + protected WriteCommandAction(@Nullable Project project, @Nullable String commandName, /*@NotNull*/ PsiFile... files) { this(project, commandName, null, files); } - protected WriteCommandAction(@Nullable final Project project, @Nullable final String commandName, @Nullable final String groupID, /*@NotNull*/ PsiFile... files) { + protected WriteCommandAction(@Nullable Project project, @Nullable String commandName, @Nullable String groupID, /*@NotNull*/ PsiFile... files) { myCommandName = commandName; myGroupID = groupID; myProject = project; - if (files == null) { - LOG.warn("'files' parameter must not be null", new Throwable()); - } + if (files == null) LOG.warn("'files' parameter must not be null", new Throwable()); myPsiFiles = files == null || files.length == 0 ? PsiFile.EMPTY_ARRAY : files; } @@ -72,16 +70,18 @@ public abstract class WriteCommandAction extends BaseActionRunnable { @Override public RunResult execute() { Application application = ApplicationManager.getApplication(); - final boolean dispatchThread = application.isDispatchThread(); + boolean dispatchThread = application.isDispatchThread(); + if (!dispatchThread && application.isReadAccessAllowed()) { LOG.error("Must not start write action from within read action in the other thread - deadlock is coming"); throw new IllegalStateException(); } - final RunResult result = new RunResult(this); + final RunResult result = new RunResult(this); if (dispatchThread) { performWriteCommandAction(result); - } else { + } + else { try { TransactionGuard.getInstance().submitTransactionAndWait(new Runnable() { @Override @@ -95,10 +95,6 @@ public abstract class WriteCommandAction extends BaseActionRunnable { return result; } - public static boolean ensureFilesWritable(@NotNull Project project, @NotNull Collection psiFiles) { - return FileModificationService.getInstance().preparePsiElementsForWrite(psiFiles); - } - private void performWriteCommandAction(@NotNull RunResult result) { if (!FileModificationService.getInstance().preparePsiElementsForWrite(Arrays.asList(myPsiFiles))) return; @@ -108,7 +104,7 @@ public abstract class WriteCommandAction extends BaseActionRunnable { doExecuteCommand(new Runnable() { @Override public void run() { - AccessToken token = getApplication().acquireWriteActionLock(WriteCommandAction.this.getClass()); + AccessToken token = ApplicationManager.getApplication().acquireWriteActionLock(WriteCommandAction.this.getClass()); try { results[0].run(); results[0] = null; @@ -129,8 +125,7 @@ public abstract class WriteCommandAction extends BaseActionRunnable { } /** - * See {@link CommandProcessor#executeCommand(com.intellij.openapi.project.Project, java.lang.Runnable, java.lang.String, java.lang.Object, com.intellij.openapi.command.UndoConfirmationPolicy, boolean)} for details. - * + * See {@link CommandProcessor#executeCommand(Project, Runnable, String, Object, UndoConfirmationPolicy, boolean)} for details. */ protected boolean shouldRecordActionForActiveDocument() { return true; @@ -162,7 +157,7 @@ public abstract class WriteCommandAction extends BaseActionRunnable { runnable.run(); } }; - CommandProcessor.getInstance().executeCommand(getProject(), wrappedRunnable, getCommandName(), getGroupID(), + CommandProcessor.getInstance().executeCommand(getProject(), wrappedRunnable, getCommandName(), getGroupID(), getUndoConfirmationPolicy(), shouldRecordActionForActiveDocument()); } @@ -170,26 +165,27 @@ public abstract class WriteCommandAction extends BaseActionRunnable { * WriteCommandAction without result */ public abstract static class Simple extends WriteCommandAction { - protected Simple(final Project project, /*@NotNull*/ PsiFile... files) { + protected Simple(Project project, /*@NotNull*/ PsiFile... files) { super(project, files); } - protected Simple(final Project project, final String commandName, /*@NotNull*/ PsiFile... files) { + + protected Simple(Project project, String commandName, /*@NotNull*/ PsiFile... files) { super(project, commandName, files); } - protected Simple(final Project project, final String name, final String groupID, /*@NotNull*/ PsiFile... files) { + protected Simple(Project project, String name, String groupID, /*@NotNull*/ PsiFile... files) { super(project, name, groupID, files); } @Override - protected void run(@NotNull final Result result) throws Throwable { + protected void run(@NotNull Result result) throws Throwable { run(); } protected abstract void run() throws Throwable; } - public static void runWriteCommandAction(Project project, @NotNull final Runnable runnable) { + public static void runWriteCommandAction(Project project, @NotNull Runnable runnable) { runWriteCommandAction(project, "Undefined", null, runnable); } @@ -206,6 +202,7 @@ public abstract class WriteCommandAction extends BaseActionRunnable { }.execute(); } + @SuppressWarnings("LambdaUnfriendlyMethodOverload") public static T runWriteCommandAction(Project project, @NotNull final Computable computable) { return new WriteCommandAction(project) { @Override @@ -215,15 +212,24 @@ public abstract class WriteCommandAction extends BaseActionRunnable { }.execute().getResultObject(); } + @SuppressWarnings("LambdaUnfriendlyMethodOverload") public static T runWriteCommandAction(Project project, @NotNull final ThrowableComputable computable) throws E { - RunResult result = new WriteCommandAction(project,"") { + RunResult result = new WriteCommandAction(project, "") { @Override protected void run(@NotNull Result result) throws Throwable { result.setResult(computable.compute()); } }.execute(); - if (result.getThrowable() != null) throw (E)result.getThrowable(); + Throwable t = result.getThrowable(); + if (t != null) { @SuppressWarnings("unchecked") E e = (E)t; throw e; } return result.throwException().getResultObject(); } -} + // + /** @deprecated use {@link FileModificationService#preparePsiElementsForWrite(Collection)} (to be removed in IDEA 2018) */ + @SuppressWarnings("unused") + public static boolean ensureFilesWritable(@NotNull Project project, @NotNull Collection psiFiles) { + return FileModificationService.getInstance().preparePsiElementsForWrite(psiFiles); + } + // +} \ No newline at end of file