TaskAttributeFactory: Use taskConfigCache.masquerade()

Remove the `masquerade(PatchSetArgument)` methods from TaskTree and
Preloader and directly inject a TaskConfigCache in TaskAttributeFactory.

Replace usage of PerThreadCache with assisted injection to ensure
the same instance of TaskConfigCache is used by TaskTree, Preloader, and
TaskAttributeFactory during the lifetime of each TaskAttributeFactory.

Change-Id: I93647b2a9df4cfa383b9e657831613852ea4eced
diff --git a/src/main/java/com/googlesource/gerrit/plugins/task/Modules.java b/src/main/java/com/googlesource/gerrit/plugins/task/Modules.java
index 73ed927..3451836 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/task/Modules.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/task/Modules.java
@@ -35,13 +35,14 @@
   public static class Module extends FactoryModule {
     @Override
     protected void configure() {
-      install(new TaskConfigCache.Module());
       bind(CapabilityDefinition.class)
           .annotatedWith(Exports.named(ViewPathsCapability.VIEW_PATHS))
           .to(ViewPathsCapability.class);
       factory(TaskPath.Factory.class);
       factory(TaskReference.Factory.class);
       factory(TaskExpression.Factory.class);
+      factory(TaskTree.Factory.class);
+      factory(Preloader.Factory.class);
 
       bind(ChangePluginDefinedInfoFactory.class)
           .annotatedWith(Exports.named("task"))
diff --git a/src/main/java/com/googlesource/gerrit/plugins/task/Preloader.java b/src/main/java/com/googlesource/gerrit/plugins/task/Preloader.java
index e1ce7ad..7b325cf 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/task/Preloader.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/task/Preloader.java
@@ -15,9 +15,8 @@
 package com.googlesource.gerrit.plugins.task;
 
 import com.google.inject.Inject;
-import com.google.inject.Provider;
+import com.google.inject.assistedinject.Assisted;
 import com.googlesource.gerrit.plugins.task.TaskConfig.Task;
-import com.googlesource.gerrit.plugins.task.cli.PatchSetArgument;
 import com.googlesource.gerrit.plugins.task.statistics.HitHashMap;
 import com.googlesource.gerrit.plugins.task.statistics.StatisticsMap;
 import java.io.IOException;
@@ -32,6 +31,10 @@
 
 /** Use to pre-load a task definition with values from its preload-task definition. */
 public class Preloader {
+  public interface Factory {
+    Preloader create(@Assisted TaskConfigCache taskConfigCache);
+  }
+
   public static class Statistics {
     protected Object optionalTaskByExpressionCache;
     protected long loaded;
@@ -48,9 +51,8 @@
 
   @Inject
   public Preloader(
-      Provider<TaskConfigCache> taskConfigCacheProvider,
-      TaskExpression.Factory taskExpressionFactory) {
-    this.taskConfigCache = taskConfigCacheProvider.get();
+      TaskExpression.Factory taskExpressionFactory, @Assisted TaskConfigCache taskConfigCache) {
+    this.taskConfigCache = taskConfigCache;
     this.taskExpressionFactory = taskExpressionFactory;
   }
 
@@ -165,10 +167,6 @@
     return taskConfigCache.getTaskConfig(key.subSection().file()).getOptionalTask(key.task());
   }
 
-  public void masquerade(PatchSetArgument psa) {
-    taskConfigCache.masquerade(psa);
-  }
-
   protected static <S, K, V> void preloadField(
       Field field, Task definition, Task preloadFrom, Task preloadTo)
       throws IllegalArgumentException, IllegalAccessException {
diff --git a/src/main/java/com/googlesource/gerrit/plugins/task/TaskAttributeFactory.java b/src/main/java/com/googlesource/gerrit/plugins/task/TaskAttributeFactory.java
index c7ed499..2f1c358 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/task/TaskAttributeFactory.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/task/TaskAttributeFactory.java
@@ -104,6 +104,7 @@
   protected final PredicateCache predicateCache;
   protected final boolean hasViewPathsCapability;
   protected final TaskPath.Factory taskPathFactory;
+  protected final TaskConfigCache taskConfigCache;
 
   protected Modules.MyOptions options;
   protected TaskPluginAttribute lastTaskPluginAttribute;
@@ -112,18 +113,20 @@
   @Inject
   public TaskAttributeFactory(
       String pluginName,
-      TaskTree definitions,
+      TaskTree.Factory taskTreeFactory,
       PredicateCache predicateCache,
       PermissionBackend permissionBackend,
-      TaskPath.Factory taskPathFactory) {
+      TaskPath.Factory taskPathFactory,
+      TaskConfigCache taskConfigCache) {
     this.pluginName = pluginName;
-    this.definitions = definitions;
+    this.definitions = taskTreeFactory.create(taskConfigCache);
     this.predicateCache = predicateCache;
     this.hasViewPathsCapability =
         permissionBackend
             .currentUser()
             .testOrFalse(new PluginPermission(this.pluginName, ViewPathsCapability.VIEW_PATHS));
     this.taskPathFactory = taskPathFactory;
+    this.taskConfigCache = taskConfigCache;
   }
 
   @Override
@@ -134,7 +137,7 @@
     if (options.all || options.onlyApplicable || options.onlyInvalid) {
       initStatistics();
       for (PatchSetArgument psa : options.patchSetArguments) {
-        definitions.masquerade(psa);
+        taskConfigCache.masquerade(psa);
       }
       cds.forEach(cd -> pluginInfosByChange.put(cd.getId(), createWithExceptions(cd)));
       if (lastTaskPluginAttribute != null) {
diff --git a/src/main/java/com/googlesource/gerrit/plugins/task/TaskConfigCache.java b/src/main/java/com/googlesource/gerrit/plugins/task/TaskConfigCache.java
index 1f73a1d..648c729 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/task/TaskConfigCache.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/task/TaskConfigCache.java
@@ -18,10 +18,8 @@
 import com.google.gerrit.entities.BranchNameKey;
 import com.google.gerrit.entities.Project;
 import com.google.gerrit.entities.RefNames;
-import com.google.gerrit.extensions.config.FactoryModule;
 import com.google.gerrit.extensions.restapi.AuthException;
 import com.google.gerrit.server.CurrentUser;
-import com.google.gerrit.server.cache.PerThreadCache;
 import com.google.gerrit.server.config.AllProjectsName;
 import com.google.gerrit.server.config.AllProjectsNameProvider;
 import com.google.gerrit.server.git.GitRepositoryManager;
@@ -37,39 +35,6 @@
 import org.eclipse.jgit.lib.Repository;
 
 public class TaskConfigCache {
-  public static class Module extends FactoryModule {
-    @Override
-    protected void configure() {
-      factory(TaskConfigCache.Factory.class);
-      bind(TaskConfigCache.class).toProvider(TaskConfigCache.Provider.class);
-    }
-  }
-
-  public static class Provider implements com.google.inject.Provider<TaskConfigCache> {
-    protected static final PerThreadCache.Key<TaskConfigCache> TASK_CONFIG_CACHE_KEY =
-        PerThreadCache.Key.create(TaskConfigCache.class);
-
-    protected final TaskConfigCache.Factory taskConfigCacheFactory;
-
-    @Inject
-    Provider(TaskConfigCache.Factory taskConfigCacheFactory) {
-      this.taskConfigCacheFactory = taskConfigCacheFactory;
-    }
-
-    @Override
-    public TaskConfigCache get() {
-      PerThreadCache perThreadCache = PerThreadCache.get();
-      if (perThreadCache != null) {
-        return perThreadCache.get(TASK_CONFIG_CACHE_KEY, () -> taskConfigCacheFactory.create());
-      }
-      return taskConfigCacheFactory.create();
-    }
-  }
-
-  private interface Factory {
-    TaskConfigCache create();
-  }
-
   private static final FluentLogger log = FluentLogger.forEnclosingClass();
 
   protected final GitRepositoryManager gitMgr;
diff --git a/src/main/java/com/googlesource/gerrit/plugins/task/TaskTree.java b/src/main/java/com/googlesource/gerrit/plugins/task/TaskTree.java
index 73bd127..5d4d773 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/task/TaskTree.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/task/TaskTree.java
@@ -32,12 +32,12 @@
 import com.google.gerrit.server.query.change.ChangeQueryProcessor;
 import com.google.inject.Inject;
 import com.google.inject.Provider;
+import com.google.inject.assistedinject.Assisted;
 import com.googlesource.gerrit.plugins.task.TaskConfig.External;
 import com.googlesource.gerrit.plugins.task.TaskConfig.NamesFactory;
 import com.googlesource.gerrit.plugins.task.TaskConfig.NamesFactoryType;
 import com.googlesource.gerrit.plugins.task.TaskConfig.Task;
 import com.googlesource.gerrit.plugins.task.TaskConfig.TasksFactory;
-import com.googlesource.gerrit.plugins.task.cli.PatchSetArgument;
 import com.googlesource.gerrit.plugins.task.properties.Properties;
 import com.googlesource.gerrit.plugins.task.statistics.HitHashMap;
 import com.googlesource.gerrit.plugins.task.statistics.HitHashMapOfCollection;
@@ -69,6 +69,10 @@
 public class TaskTree {
   private static final FluentLogger log = FluentLogger.forEnclosingClass();
 
+  public interface Factory {
+    TaskTree create(@Assisted TaskConfigCache taskConfigCache);
+  }
+
   @FunctionalInterface
   public interface NodeFactory {
     Node create(NodeList parent, Task definition) throws Exception;
@@ -114,7 +118,8 @@
       Provider<ChangeQueryProcessor> changeQueryProcessorProvider,
       PredicateCache predicateCache,
       TaskExpression.Factory taskExpressionFactory,
-      Preloader preloader) {
+      Preloader.Factory preloaderFactory,
+      @Assisted TaskConfigCache taskConfigCache) {
     this.accountResolver = accountResolver;
     this.allUsers = allUsers;
     this.user = user != null ? user : anonymousUser;
@@ -123,11 +128,7 @@
     this.predicateCache = predicateCache;
     this.matchCache = new MatchCache(predicateCache);
     this.taskExpressionFactory = taskExpressionFactory;
-    this.preloader = preloader;
-  }
-
-  public void masquerade(PatchSetArgument psa) {
-    preloader.masquerade(psa);
+    this.preloader = preloaderFactory.create(taskConfigCache);
   }
 
   public List<Node> getRootNodes(ChangeData changeData)