Copy task Namesfactories before altering them
Copying before altering allows future modifications to make configs have
immutable Lists and Maps which will help avoid unintended modifications
of shared configs. Although this extra copying results in a bit of extra
copying work, safer sharing enables the way for config caches which can
lead to a lot less work being done.
Change-Id: Id04de8a459d504a9ee1c114c8eab0a3005b08390
diff --git a/src/main/java/com/googlesource/gerrit/plugins/task/Copier.java b/src/main/java/com/googlesource/gerrit/plugins/task/Copier.java
new file mode 100644
index 0000000..ff86a47
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/task/Copier.java
@@ -0,0 +1,62 @@
+// Copyright (C) 2021 The Android Open Source Project
+//
+// 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.googlesource.gerrit.plugins.task;
+
+import com.google.common.primitives.Primitives;
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class Copier {
+ protected static <T> void deepCopyDeclaredFields(
+ Class<T> cls, T from, T to, boolean includeInaccessible) {
+ for (Field field : cls.getDeclaredFields()) {
+ try {
+ if (includeInaccessible) {
+ field.setAccessible(true);
+ }
+ Class<?> fieldCls = field.getType();
+ Object val = field.get(from);
+ if (field.getType().isPrimitive()
+ || Primitives.isWrapperType(fieldCls)
+ || (val instanceof String)
+ || val == null) {
+ field.set(to, val);
+ } else if (val instanceof List) {
+ List<?> list = List.class.cast(val);
+ field.set(to, new ArrayList<>(list));
+ } else if (val instanceof Map) {
+ Map<?, ?> map = Map.class.cast(val);
+ field.set(to, new HashMap<>(map));
+ } else if (field.getName().equals("this$0")) { // Can't copy internal final field
+ } else {
+ throw new RuntimeException(
+ "Don't know how to deep copy " + fieldValueToString(field, val));
+ }
+ } catch (IllegalAccessException e) {
+ if (includeInaccessible) {
+ throw new RuntimeException(
+ "Cannot access field to copy it " + fieldValueToString(field, "unknown"));
+ }
+ }
+ }
+ }
+
+ protected static String fieldValueToString(Field field, Object val) {
+ return "field:" + field.getName() + " value:" + val + " type:" + field.getType();
+ }
+}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/task/TaskConfig.java b/src/main/java/com/googlesource/gerrit/plugins/task/TaskConfig.java
index 2adb8c2..97fefb7 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/task/TaskConfig.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/task/TaskConfig.java
@@ -14,11 +14,9 @@
package com.googlesource.gerrit.plugins.task;
-import com.google.common.primitives.Primitives;
import com.google.gerrit.common.Container;
import com.google.gerrit.reviewdb.client.Branch;
import com.google.gerrit.server.git.meta.AbstractVersionedMetaData;
-import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
@@ -97,40 +95,7 @@
protected TaskBase(TaskBase base) {
super(base.subSection);
- copyDeclaredFields(TaskBase.class, base);
- }
-
- protected <T> void copyDeclaredFields(Class<T> cls, T from) {
- for (Field field : cls.getDeclaredFields()) {
- try {
- field.setAccessible(true);
- Class<?> fieldCls = field.getType();
- Object val = field.get(from);
- if (field.getType().isPrimitive()
- || Primitives.isWrapperType(fieldCls)
- || (val instanceof String)
- || val == null) {
- field.set(this, val);
- } else if (val instanceof List) {
- List<?> list = List.class.cast(val);
- field.set(this, new ArrayList<>(list));
- } else if (val instanceof Map) {
- Map<?, ?> map = Map.class.cast(val);
- field.set(this, new HashMap<>(map));
- } else if (field.getName().equals("this$0")) { // Don't copy internal final field
- } else {
- throw new RuntimeException(
- "Don't know how to deep copy " + fieldValueToString(field, val));
- }
- } catch (IllegalAccessException e) {
- throw new RuntimeException(
- "Cannot access field to copy it " + fieldValueToString(field, "unknown"));
- }
- }
- }
-
- protected String fieldValueToString(Field field, Object val) {
- return "field:" + field.getName() + " value:" + val + " type:" + field.getType();
+ Copier.deepCopyDeclaredFields(TaskBase.class, base, this, false);
}
}
@@ -144,7 +109,7 @@
public Task(Task task) {
super(task);
- copyDeclaredFields(Task.class, task);
+ Copier.deepCopyDeclaredFields(Task.class, task, this, false);
}
public Task(TasksFactory tasks, String name) {
@@ -192,6 +157,11 @@
names = getStringList(s, KEY_NAME);
type = getString(s, KEY_TYPE, null);
}
+
+ public NamesFactory(NamesFactory n) {
+ super(n.subSection);
+ Copier.deepCopyDeclaredFields(NamesFactory.class, n, this, false);
+ }
}
public class External extends Section {
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 73c153e..c940ae0 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/task/TaskTree.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/task/TaskTree.java
@@ -218,7 +218,8 @@
for (String tasksFactoryName : task.subTasksFactories) {
TasksFactory tasksFactory = task.config.getTasksFactory(tasksFactoryName);
if (tasksFactory != null) {
- NamesFactory namesFactory = task.config.getNamesFactory(tasksFactory.namesFactory);
+ NamesFactory namesFactory =
+ task.config.new NamesFactory(task.config.getNamesFactory(tasksFactory.namesFactory));
if (namesFactory != null && namesFactory.type != null) {
new Properties.NamesFactory(namesFactory, getProperties());
switch (NamesFactoryType.getNamesFactoryType(namesFactory.type)) {