aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJerome Gaillard <jgaillard@google.com>2022-01-27 16:01:21 +0000
committerAndroid (Google) Code Review <android-gerrit@google.com>2022-01-27 16:01:21 +0000
commitc3b67cac286b54adf4e33516143e1d400e243118 (patch)
tree9ef5c666e271f80260d5145ac96d91f860043e89
parentc4696b7ac17d30a0063ac9f27f4e477164d1c8f6 (diff)
parent4f5ba306870ee3194750d484a92e4f6dcbd7de80 (diff)
downloadlayoutlib-c3b67cac286b54adf4e33516143e1d400e243118.tar.gz
Merge "Implement Reference.refersTo for layoutlib" into master-layoutlib-native
-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;
+ }
+}