remove TransactionKind, step 3 and last

This commit is contained in:
peter
2016-04-11 18:32:16 +02:00
parent 12efba4780
commit ff1b55aab3
3 changed files with 9 additions and 135 deletions
@@ -107,7 +107,8 @@ public abstract class TransactionGuard {
* @param transaction code to execute inside a transaction.
*/
public static void submitTransaction(@NotNull Disposable parentDisposable, @NotNull Runnable transaction) {
getInstance().submitMergeableTransaction(parentDisposable, TransactionKind.ANY_CHANGE, transaction);
TransactionGuard guard = getInstance();
guard.submitMergeableTransaction(parentDisposable, guard.getContextTransaction(), transaction);
}
/**
@@ -119,28 +120,11 @@ public abstract class TransactionGuard {
/**
* Schedules a transaction and waits for it to be completed. Fails if invoked on UI thread inside an incompatible transaction,
* or inside a read action on non-UI thread.
* @see #submitMergeableTransaction(Disposable, TransactionKind, Runnable)
* @see #submitMergeableTransaction(Disposable, TransactionId, Runnable)
* @throws ProcessCanceledException if current thread is interrupted
*/
public abstract void submitTransactionAndWait(@NotNull Runnable transaction) throws ProcessCanceledException;
/**
* A synchronous version of {@link #submitMergeableTransaction(Disposable, TransactionKind, Runnable)}.
* @return a token object for this transaction. Call {@link AccessToken#finish()} (inside finally) when the transaction is complete.
*/
@NotNull
public abstract AccessToken startSynchronousTransaction(@NotNull TransactionKind kind);
/**
* Same as {@link #submitMergeableTransaction(Disposable, TransactionKind, Runnable)} with no parent disposable.
*/
public void submitMergeableTransaction(@NotNull TransactionKind kind, @NotNull Runnable transaction) {
submitMergeableTransaction(ApplicationManager.getApplication(), kind, transaction);
}
@Deprecated
public abstract void submitMergeableTransaction(@NotNull Disposable parentDisposable, @NotNull TransactionKind kind, @NotNull Runnable transaction);
/**
* Executes the given runnable inside a transaction as soon as possible on the UI thread. The runnable is executed either when there's
* no active transaction running, or when the running transaction has the same (or compatible) id as {@code mergeInto}. If the id of
@@ -154,13 +138,6 @@ public abstract class TransactionGuard {
*/
public abstract void submitMergeableTransaction(@NotNull Disposable parentDisposable, @Nullable TransactionId mergeInto, @NotNull Runnable transaction);
/**
* Asserts that a transaction is currently running, or not. Callable only on Swing thread.
* @param transactionRequired whether the assertion should check that the application is inside transaction or not
* @param errorMessage the message that will be logged if current transaction status differs from the expected one
*/
public abstract void assertInsideTransaction(boolean transactionRequired, @NotNull String errorMessage);
/**
* @return the id of the currently running transaction for using in {@link #submitMergeableTransaction(Disposable, TransactionId, Runnable)},
* or null if there's no transaction running or merging is not allowed in the callee context (e.g. from invokeLater).
@@ -1,57 +0,0 @@
/*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.openapi.application;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.editor.Document;
/**
* A kind of transaction used in {@link TransactionGuard#submitMergeableTransaction(Disposable, TransactionKind, Runnable)}.
*/
public interface TransactionKind {
/**
* Same as {@link Common#TEXT_EDITING}
*/
TransactionKind TEXT_EDITING = Common.TEXT_EDITING;
/**
* Same as {@link Common#ANY_CHANGE}
*/
TransactionKind ANY_CHANGE = Common.ANY_CHANGE;
/**
* An auxiliary enum to make it possible to use transaction kinds in annotations
*/
enum Common implements TransactionKind {
/**
* This kind represents document modifications via editor actions, code completion and document->PSI commit.
* @see com.intellij.psi.PsiDocumentManager#commitDocument(Document)
*/
TEXT_EDITING,
/**
* This kind represents any model modifications:
* <li>PSI or document changes
* <li>Virtual file system changes, e.g. files created/deleted/renamed/content-changed,
* caused by refresh process or explicit operations.
* <li>Project root set change
* <li>Dumb mode (reindexing) start/finish, (see {@link com.intellij.openapi.project.DumbService}).
*/
ANY_CHANGE
}
}
@@ -16,7 +16,6 @@
package com.intellij.openapi.application;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressIndicatorProvider;
@@ -38,7 +37,6 @@ import java.util.concurrent.atomic.AtomicLong;
* @author peter
*/
public class TransactionGuardImpl extends TransactionGuard {
private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.application.TransactionGuardImpl");
private final Queue<Transaction> myQueue = new LinkedBlockingQueue<Transaction>();
private final Map<ModalityState, TransactionIdImpl> myModality2Transaction = ContainerUtil.createConcurrentWeakMap();
private final Set<ModalityState> myWriteSafeModalities = Collections.newSetFromMap(ContainerUtil.<ModalityState, Boolean>createConcurrentWeakMap());
@@ -49,22 +47,6 @@ public class TransactionGuardImpl extends TransactionGuard {
myWriteSafeModalities.add(ModalityState.NON_MODAL);
}
@Override
@NotNull
public AccessToken startSynchronousTransaction(@NotNull TransactionKind kind) throws IllegalStateException {
if (isInsideTransaction() && !myWritingAllowed) {
// please assign exceptions that occur here to Peter
/*
LOG.error("Synchronous transactions are allowed only from user actions. " +
"Please use submit*Transaction instead of invokeLater. " +
"See FAQ in TransactionGuard class javadoc.");
*/
return AccessToken.EMPTY_ACCESS_TOKEN;
}
return startTransactionUnchecked();
}
@NotNull
private AccessToken startTransactionUnchecked() {
final TransactionIdImpl prevTransaction = myCurrentTransaction;
@@ -127,26 +109,10 @@ public class TransactionGuardImpl extends TransactionGuard {
}
}
private boolean isInsideTransaction() {
ApplicationManager.getApplication().assertIsDispatchThread();
return myCurrentTransaction != null;
}
@Override
public void submitMergeableTransaction(@NotNull final Disposable parentDisposable, @NotNull final TransactionKind kind, @NotNull final Runnable _transaction) {
submitMergeableTransaction(parentDisposable, kind, getContextTransaction(), _transaction);
}
@Override
public void submitMergeableTransaction(@NotNull Disposable parentDisposable, @Nullable TransactionId mergeInto, @NotNull Runnable transaction) {
submitMergeableTransaction(parentDisposable, TransactionKind.ANY_CHANGE, (TransactionIdImpl)mergeInto, transaction);
}
private void submitMergeableTransaction(@NotNull final Disposable parentDisposable,
@NotNull final TransactionKind kind,
@Nullable final TransactionIdImpl expectedId,
@NotNull final Runnable _transaction) {
@NotNull final Transaction transaction = new Transaction(_transaction, expectedId, kind, parentDisposable);
public void submitMergeableTransaction(@NotNull Disposable parentDisposable, @Nullable TransactionId mergeInto, @NotNull Runnable _transaction) {
final TransactionIdImpl expectedId = (TransactionIdImpl)mergeInto;
final Transaction transaction = new Transaction(_transaction, expectedId, parentDisposable);
final Application app = ApplicationManager.getApplication();
final boolean isDispatchThread = app.isDispatchThread();
Runnable runnable = new Runnable() {
@@ -183,18 +149,11 @@ public class TransactionGuardImpl extends TransactionGuard {
return transaction.mergeInto != null && currentId.myStartCounter <= transaction.mergeInto.myStartCounter;
}
@Override
public void assertInsideTransaction(boolean transactionRequired, @NotNull String errorMessage) {
if (transactionRequired != isInsideTransaction()) {
LOG.error(errorMessage);
}
}
@Override
public void submitTransactionAndWait(@NotNull final Runnable runnable) throws ProcessCanceledException {
Application app = ApplicationManager.getApplication();
if (app.isDispatchThread()) {
Transaction transaction = new Transaction(runnable, getContextTransaction(), TransactionKind.ANY_CHANGE, app);
Transaction transaction = new Transaction(runnable, getContextTransaction(), app);
if (!canRunTransactionNow(transaction, true)) {
throw new AssertionError("Cannot run synchronous submitTransactionAndWait from invokeLater. " +
"Please use asynchronous submit*Transaction. " +
@@ -208,7 +167,7 @@ public class TransactionGuardImpl extends TransactionGuard {
final Semaphore semaphore = new Semaphore();
semaphore.down();
final Throwable[] exception = {null};
submitMergeableTransaction(Disposer.newDisposable("never disposed"), TransactionKind.ANY_CHANGE, new Runnable() {
submitMergeableTransaction(Disposer.newDisposable("never disposed"), getContextTransaction(), new Runnable() {
@Override
public void run() {
try {
@@ -333,16 +292,11 @@ public class TransactionGuardImpl extends TransactionGuard {
private static class Transaction {
@NotNull final Runnable runnable;
@Nullable final TransactionIdImpl mergeInto;
@NotNull final TransactionKind kind;
@NotNull final Disposable parentDisposable;
Transaction(@NotNull Runnable runnable,
@Nullable TransactionIdImpl mergeInto,
@NotNull TransactionKind kind,
@NotNull Disposable parentDisposable) {
Transaction(@NotNull Runnable runnable, @Nullable TransactionIdImpl mergeInto, @NotNull Disposable parentDisposable) {
this.runnable = runnable;
this.mergeInto = mergeInto;
this.kind = kind;
this.parentDisposable = parentDisposable;
}
}