aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJerome Gaillard <jgaillard@google.com>2022-01-27 13:37:19 +0000
committerJerome Gaillard <jgaillard@google.com>2022-01-27 13:37:19 +0000
commit4f5ba306870ee3194750d484a92e4f6dcbd7de80 (patch)
treee8365f8b26229fe025df7369a24fcbcd3609544c
parent282562780266ed1589dc7360f71b199af49ed2a9 (diff)
downloadlayoutlib-4f5ba306870ee3194750d484a92e4f6dcbd7de80.tar.gz
Implement Reference.refersTo for layoutlib
The ART version of java.lang.ref.Reference implemented the method refersTo, which only exists in OpenJDK 16. Since layoutlib currently uses JDK 11, this method needs to be implemented. Bug: 74062470 Test: layoutlib tests Change-Id: Ie7825605cc2b16e1f6de633646f43514c626fff4
-rw-r--r--create/src/com/android/tools/layoutlib/create/CreateInfo.java21
-rw-r--r--create/src/com/android/tools/layoutlib/java/Reference_Delegate.java29
2 files changed, 50 insertions, 0 deletions
diff --git a/create/src/com/android/tools/layoutlib/create/CreateInfo.java b/create/src/com/android/tools/layoutlib/create/CreateInfo.java
index 8c90a4c3f3..66a22d1fa5 100644
--- a/create/src/com/android/tools/layoutlib/create/CreateInfo.java
+++ b/create/src/com/android/tools/layoutlib/create/CreateInfo.java
@@ -19,10 +19,13 @@ package com.android.tools.layoutlib.create;
import com.android.tools.layoutlib.annotations.LayoutlibDelegate;
import com.android.tools.layoutlib.java.LinkedHashMap_Delegate;
import com.android.tools.layoutlib.java.NioUtils_Delegate;
+import com.android.tools.layoutlib.java.Reference_Delegate;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
+import java.lang.ref.Reference;
+import java.lang.ref.WeakReference;
import java.text.DateFormat;
import java.util.Arrays;
import java.util.HashMap;
@@ -147,6 +150,7 @@ public final class CreateInfo implements ICreateInfo {
new ProcessInitializerInitSchedReplacer(),
new NativeInitPathReplacer(),
new ActivityThreadInAnimationReplacer(),
+ new ReferenceRefersToReplacer(),
};
/**
@@ -164,6 +168,7 @@ public final class CreateInfo implements ICreateInfo {
/* Java package classes */
LinkedHashMap_Delegate.class,
NioUtils_Delegate.class,
+ Reference_Delegate.class,
};
/**
@@ -650,4 +655,20 @@ public final class CreateInfo implements ICreateInfo {
mi.desc = "()Landroid/content/Context;";
}
}
+
+ public static class ReferenceRefersToReplacer implements MethodReplacer {
+ @Override
+ public boolean isNeeded(String owner, String name, String desc, String sourceClass) {
+ return Type.getInternalName(WeakReference.class).equals(owner) &&
+ "refersTo".equals(name);
+ }
+
+ @Override
+ public void replace(MethodInformation mi) {
+ mi.opcode = Opcodes.INVOKESTATIC;
+ mi.owner = Type.getInternalName(Reference_Delegate.class);
+ mi.desc = Type.getMethodDescriptor(Type.BOOLEAN_TYPE, Type.getType(Reference.class),
+ Type.getType(Object.class));
+ }
+ }
}
diff --git a/create/src/com/android/tools/layoutlib/java/Reference_Delegate.java b/create/src/com/android/tools/layoutlib/java/Reference_Delegate.java
new file mode 100644
index 0000000000..a774275051
--- /dev/null
+++ b/create/src/com/android/tools/layoutlib/java/Reference_Delegate.java
@@ -0,0 +1,29 @@
+/*
+ * Copyright (C) 2022 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.android.tools.layoutlib.java;
+
+import java.lang.ref.Reference;
+
+/**
+ * Provides an alternative implementation for java.lang.ref.Reference#refersTo, as it exists
+ * in ART but not in the host JDK.
+ */
+public class Reference_Delegate {
+ public static <T> boolean refersTo(Reference<T> thisRef, T obj) {
+ return thisRef.get() == obj;
+ }
+}