From 0350ddfde93ecc754e3ebc874d430355957a83f3 Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Mon, 8 May 2017 17:46:01 +0200 Subject: [PATCH] =?UTF-8?q?deprecate=20addCompilationStatusListener=20?= =?UTF-8?q?=E2=80=94=20message=20bus=20should=20be=20used=20instead?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../compiler/CompilerManagerImpl.java | 3 +- .../CompilerReferenceServiceImpl.java | 15 ++-- .../compiler/BaseCompilerTestCase.java | 4 +- .../compiler/CompilationStatusListener.java | 4 +- .../openapi/compiler/CompilerManager.java | 17 +++-- .../testDiscovery/JavaAutoRunManager.java | 68 ++++++++++--------- 6 files changed, 58 insertions(+), 53 deletions(-) diff --git a/java/compiler/impl/src/com/intellij/compiler/CompilerManagerImpl.java b/java/compiler/impl/src/com/intellij/compiler/CompilerManagerImpl.java index 1f4862cf463a..c80a3be30c00 100644 --- a/java/compiler/impl/src/com/intellij/compiler/CompilerManagerImpl.java +++ b/java/compiler/impl/src/com/intellij/compiler/CompilerManagerImpl.java @@ -55,8 +55,7 @@ import org.jetbrains.jps.javac.ExternalJavacManager; import org.jetbrains.jps.javac.OutputFileConsumer; import org.jetbrains.jps.javac.OutputFileObject; -import javax.tools.Diagnostic; -import javax.tools.JavaFileObject; +import javax.tools.*; import java.io.File; import java.io.IOException; import java.lang.reflect.Array; diff --git a/java/compiler/impl/src/com/intellij/compiler/backwardRefs/CompilerReferenceServiceImpl.java b/java/compiler/impl/src/com/intellij/compiler/backwardRefs/CompilerReferenceServiceImpl.java index 3ce2edfec32d..788206919e2f 100644 --- a/java/compiler/impl/src/com/intellij/compiler/backwardRefs/CompilerReferenceServiceImpl.java +++ b/java/compiler/impl/src/com/intellij/compiler/backwardRefs/CompilerReferenceServiceImpl.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. @@ -27,10 +27,7 @@ import com.intellij.compiler.server.BuildManagerListener; import com.intellij.lang.injection.InjectedLanguageManager; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.application.ReadAction; -import com.intellij.openapi.compiler.CompilationStatusListener; -import com.intellij.openapi.compiler.CompileContext; -import com.intellij.openapi.compiler.CompileScope; -import com.intellij.openapi.compiler.CompilerManager; +import com.intellij.openapi.compiler.*; import com.intellij.openapi.diagnostic.ControlFlowException; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.fileEditor.FileDocumentManager; @@ -58,6 +55,7 @@ import com.intellij.util.containers.ConcurrentFactoryMap; import com.intellij.util.indexing.FileBasedIndex; import com.intellij.util.indexing.StorageException; import com.intellij.util.io.PersistentEnumeratorBase; +import com.intellij.util.messages.MessageBusConnection; import gnu.trove.THashSet; import gnu.trove.TIntHashSet; import org.jetbrains.annotations.NotNull; @@ -103,7 +101,8 @@ public class CompilerReferenceServiceImpl extends CompilerReferenceServiceEx imp @Override public void projectOpened() { if (isEnabled()) { - myProject.getMessageBus().connect(myProject).subscribe(BuildManagerListener.TOPIC, new BuildManagerListener() { + MessageBusConnection connection = myProject.getMessageBus().connect(myProject); + connection.subscribe(BuildManagerListener.TOPIC, new BuildManagerListener() { @Override public void buildStarted(Project project, UUID sessionId, boolean isAutomake) { if (project == myProject) { @@ -112,8 +111,7 @@ public class CompilerReferenceServiceImpl extends CompilerReferenceServiceEx imp } }); - CompilerManager compilerManager = CompilerManager.getInstance(myProject); - compilerManager.addCompilationStatusListener(new CompilationStatusListener() { + connection.subscribe(CompilerTopics.COMPILATION_STATUS, new CompilationStatusListener() { @Override public void compilationFinished(boolean aborted, int errors, int warnings, CompileContext compileContext) { compilationFinished(compileContext); @@ -142,6 +140,7 @@ public class CompilerReferenceServiceImpl extends CompilerReferenceServiceEx imp myDirtyScopeHolder.installVFSListener(); if (!ApplicationManager.getApplication().isUnitTestMode()) { + CompilerManager compilerManager = CompilerManager.getInstance(myProject); ApplicationManager.getApplication().executeOnPooledThread(() -> { boolean isUpToDate; boolean indexExist = CompilerReferenceReader.exists(myProject); diff --git a/java/compiler/impl/testSrc/com/intellij/compiler/BaseCompilerTestCase.java b/java/compiler/impl/testSrc/com/intellij/compiler/BaseCompilerTestCase.java index 6464086e1205..1a2b9f339188 100644 --- a/java/compiler/impl/testSrc/com/intellij/compiler/BaseCompilerTestCase.java +++ b/java/compiler/impl/testSrc/com/intellij/compiler/BaseCompilerTestCase.java @@ -244,12 +244,12 @@ public abstract class BaseCompilerTestCase extends ModuleTestCase { final Semaphore semaphore = new Semaphore(); semaphore.down(); final List generatedFilePaths = new ArrayList<>(); - getCompilerManager().addCompilationStatusListener(new CompilationStatusAdapter() { + myProject.getMessageBus().connect(getTestRootDisposable()).subscribe(CompilerTopics.COMPILATION_STATUS, new CompilationStatusAdapter() { @Override public void fileGenerated(String outputRoot, String relativePath) { generatedFilePaths.add(relativePath); } - }, getTestRootDisposable()); + }); UIUtil.invokeAndWaitIfNeeded((Runnable)() -> { final CompileStatusNotification callback = new CompileStatusNotification() { @Override diff --git a/java/compiler/openapi/src/com/intellij/openapi/compiler/CompilationStatusListener.java b/java/compiler/openapi/src/com/intellij/openapi/compiler/CompilationStatusListener.java index 05f9241fec83..6200ad99fb8e 100644 --- a/java/compiler/openapi/src/com/intellij/openapi/compiler/CompilationStatusListener.java +++ b/java/compiler/openapi/src/com/intellij/openapi/compiler/CompilationStatusListener.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 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. @@ -20,7 +20,7 @@ import java.util.EventListener; /** * A listener for compiler events. * - * @see CompilerManager#addCompilationStatusListener(CompilationStatusListener) + * @see CompilerTopics.COMPILATION_STATUS */ public interface CompilationStatusListener extends EventListener { /** diff --git a/java/compiler/openapi/src/com/intellij/openapi/compiler/CompilerManager.java b/java/compiler/openapi/src/com/intellij/openapi/compiler/CompilerManager.java index f254462499ce..947d3c3bf29e 100644 --- a/java/compiler/openapi/src/com/intellij/openapi/compiler/CompilerManager.java +++ b/java/compiler/openapi/src/com/intellij/openapi/compiler/CompilerManager.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 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. @@ -256,18 +256,21 @@ public abstract class CompilerManager { @Nullable Runnable onTaskFinished); /** - * Register a listener to track compilation events. - * - * @param listener the listener to be registered. + * @deprecated Use CompilerTopics.COMPILATION_STATUS */ + @Deprecated public abstract void addCompilationStatusListener(@NotNull CompilationStatusListener listener); + + /** + * @deprecated Use CompilerTopics.COMPILATION_STATUS + */ + @Deprecated public abstract void addCompilationStatusListener(@NotNull CompilationStatusListener listener, @NotNull Disposable parentDisposable); /** - * Unregister a compilation listener. - * - * @param listener the listener to be unregistered. + * @deprecated Use CompilerTopics.COMPILATION_STATUS */ + @Deprecated public abstract void removeCompilationStatusListener(@NotNull CompilationStatusListener listener); /** diff --git a/java/execution/impl/src/com/intellij/execution/testDiscovery/JavaAutoRunManager.java b/java/execution/impl/src/com/intellij/execution/testDiscovery/JavaAutoRunManager.java index c224cf46e66c..6d731e84db8f 100644 --- a/java/execution/impl/src/com/intellij/execution/testDiscovery/JavaAutoRunManager.java +++ b/java/execution/impl/src/com/intellij/execution/testDiscovery/JavaAutoRunManager.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. @@ -17,14 +17,16 @@ package com.intellij.execution.testDiscovery; import com.intellij.execution.testframework.autotest.AbstractAutoTestManager; import com.intellij.execution.testframework.autotest.AutoTestWatcher; +import com.intellij.openapi.Disposable; import com.intellij.openapi.compiler.CompilationStatusListener; import com.intellij.openapi.compiler.CompileContext; -import com.intellij.openapi.compiler.CompilerManager; +import com.intellij.openapi.compiler.CompilerTopics; import com.intellij.openapi.components.ServiceManager; import com.intellij.openapi.components.State; import com.intellij.openapi.components.Storage; import com.intellij.openapi.components.StoragePathMacros; import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.Disposer; import org.jetbrains.annotations.NotNull; @State( @@ -46,44 +48,46 @@ public class JavaAutoRunManager extends AbstractAutoTestManager { protected AutoTestWatcher createWatcher(Project project) { return new AutoTestWatcher() { private boolean myHasErrors = false; - - private CompilationStatusListener myStatusListener; - + private Disposable myEventDisposable; @Override public void activate() { - if (myStatusListener == null) { - myStatusListener = new CompilationStatusListener() { - private boolean myFoundFilesToMake = false; - @Override - public void compilationFinished(boolean aborted, int errors, int warnings, CompileContext compileContext) { - if (!myFoundFilesToMake) return; - if (errors == 0) { - restartAllAutoTests(0); - } - myHasErrors = errors == 0; - myFoundFilesToMake = false; - } - - @Override - public void automakeCompilationFinished(int errors, int warnings, CompileContext compileContext) { - compilationFinished(false, errors, warnings, compileContext); - } - - @Override - public void fileGenerated(String outputRoot, String relativePath) { - myFoundFilesToMake = true; - } - }; - - CompilerManager.getInstance(project).addCompilationStatusListener(myStatusListener, project); + if (myEventDisposable != null) { + return; } + + myEventDisposable = Disposer.newDisposable(); + project.getMessageBus().connect(myEventDisposable).subscribe(CompilerTopics.COMPILATION_STATUS, new CompilationStatusListener() { + private boolean myFoundFilesToMake = false; + + @Override + public void compilationFinished(boolean aborted, int errors, int warnings, CompileContext compileContext) { + if (!myFoundFilesToMake) return; + if (errors == 0) { + restartAllAutoTests(0); + } + myHasErrors = errors == 0; + myFoundFilesToMake = false; + } + + @Override + public void automakeCompilationFinished(int errors, int warnings, CompileContext compileContext) { + compilationFinished(false, errors, warnings, compileContext); + } + + @Override + public void fileGenerated(String outputRoot, String relativePath) { + myFoundFilesToMake = true; + } + }); } @Override public void deactivate() { - if (myStatusListener != null) { - CompilerManager.getInstance(project).removeCompilationStatusListener(myStatusListener); + Disposable eventDisposable = myEventDisposable; + if (eventDisposable != null) { + myEventDisposable = null; + Disposer.dispose(eventDisposable); } }