Skip to content

Commit 9221a68

Browse files
deepanshu-The Android Automerger
authored and
The Android Automerger
committed
Layoutlib Create: Remove references to java package class Objects.
Remove references to Java 7 class java.util.Objects and replace it with a new class that can be loaded on Java 6. Change-Id: Ibbd9b20b8bc89e247f1d0c48d743d06d1a4f0704
1 parent 9921d9b commit 9221a68

File tree

3 files changed

+138
-3
lines changed

3 files changed

+138
-3
lines changed

tools/layoutlib/create/README.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,9 @@ This is the easiest: we currently inject the following classes:
169169
their return value.
170170
- CreateInfo class, which configured the generator. Not used yet, but could
171171
in theory help us track what the generator changed.
172-
- AutoCloseable is part of Java 7. To enable us to still run on Java 6, a new class is
173-
injected. The implementation for the class has been taken from Android's libcore
174-
(platform/libcore/luni/src/main/java/java/lang/AutoCloseable.java).
172+
- AutoCloseable and Objects are part of Java 7. To enable us to still run on Java 6, new
173+
classes are injected. The implementation for these classes has been taken from
174+
Android's libcore (platform/libcore/luni/src/main/java/java/...).
175175
- Charsets, IntegralToString and UnsafeByteSequence are not part of the standard JAVA VM.
176176
They are added to the Dalvik VM for performance reasons. An implementation that is very
177177
close to the original (which is at platform/libcore/luni/src/main/java/...) is injected.

tools/layoutlib/create/src/com/android/tools/layoutlib/create/CreateInfo.java

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.android.tools.layoutlib.java.AutoCloseable;
2121
import com.android.tools.layoutlib.java.Charsets;
2222
import com.android.tools.layoutlib.java.IntegralToString;
23+
import com.android.tools.layoutlib.java.Objects;
2324
import com.android.tools.layoutlib.java.UnsafeByteSequence;
2425

2526
/**
@@ -111,6 +112,7 @@ public String[] getJavaPkgClasses() {
111112
LayoutlibDelegate.class,
112113
/* Java package classes */
113114
AutoCloseable.class,
115+
Objects.class,
114116
IntegralToString.class,
115117
UnsafeByteSequence.class,
116118
Charsets.class,
@@ -221,6 +223,7 @@ public String[] getJavaPkgClasses() {
221223
private final static String[] JAVA_PKG_CLASSES =
222224
new String[] {
223225
"java.lang.AutoCloseable", "com.android.tools.layoutlib.java.AutoCloseable",
226+
"java.util.Objects", "com.android.tools.layoutlib.java.Objects",
224227
"java.nio.charset.Charsets", "com.android.tools.layoutlib.java.Charsets",
225228
"java.lang.IntegralToString", "com.android.tools.layoutlib.java.IntegralToString",
226229
"java.lang.UnsafeByteSequence", "com.android.tools.layoutlib.java.UnsafeByteSequence",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
* Copyright (C) 2013 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.android.tools.layoutlib.java;
18+
19+
import java.util.Arrays;
20+
import java.util.Comparator;
21+
22+
/**
23+
* Defines the same class as the java.util.Objects which is added in Java 7.
24+
* This hack makes it possible to run the Android code which uses Java 7 features
25+
* (API 18 and beyond) to run on Java 6.
26+
* <p/>
27+
* Extracted from API level 19, file:
28+
* platform/libcore/luni/src/main/java/java/util/Objects.java
29+
*/
30+
public final class Objects {
31+
private Objects() {}
32+
33+
/**
34+
* Returns 0 if {@code a == b}, or {@code c.compare(a, b)} otherwise.
35+
* That is, this makes {@code c} null-safe.
36+
*/
37+
public static <T> int compare(T a, T b, Comparator<? super T> c) {
38+
if (a == b) {
39+
return 0;
40+
}
41+
return c.compare(a, b);
42+
}
43+
44+
/**
45+
* Returns true if both arguments are null,
46+
* the result of {@link Arrays#equals} if both arguments are primitive arrays,
47+
* the result of {@link Arrays#deepEquals} if both arguments are arrays of reference types,
48+
* and the result of {@link #equals} otherwise.
49+
*/
50+
public static boolean deepEquals(Object a, Object b) {
51+
if (a == null || b == null) {
52+
return a == b;
53+
} else if (a instanceof Object[] && b instanceof Object[]) {
54+
return Arrays.deepEquals((Object[]) a, (Object[]) b);
55+
} else if (a instanceof boolean[] && b instanceof boolean[]) {
56+
return Arrays.equals((boolean[]) a, (boolean[]) b);
57+
} else if (a instanceof byte[] && b instanceof byte[]) {
58+
return Arrays.equals((byte[]) a, (byte[]) b);
59+
} else if (a instanceof char[] && b instanceof char[]) {
60+
return Arrays.equals((char[]) a, (char[]) b);
61+
} else if (a instanceof double[] && b instanceof double[]) {
62+
return Arrays.equals((double[]) a, (double[]) b);
63+
} else if (a instanceof float[] && b instanceof float[]) {
64+
return Arrays.equals((float[]) a, (float[]) b);
65+
} else if (a instanceof int[] && b instanceof int[]) {
66+
return Arrays.equals((int[]) a, (int[]) b);
67+
} else if (a instanceof long[] && b instanceof long[]) {
68+
return Arrays.equals((long[]) a, (long[]) b);
69+
} else if (a instanceof short[] && b instanceof short[]) {
70+
return Arrays.equals((short[]) a, (short[]) b);
71+
}
72+
return a.equals(b);
73+
}
74+
75+
/**
76+
* Null-safe equivalent of {@code a.equals(b)}.
77+
*/
78+
public static boolean equals(Object a, Object b) {
79+
return (a == null) ? (b == null) : a.equals(b);
80+
}
81+
82+
/**
83+
* Convenience wrapper for {@link Arrays#hashCode}, adding varargs.
84+
* This can be used to compute a hash code for an object's fields as follows:
85+
* {@code Objects.hash(a, b, c)}.
86+
*/
87+
public static int hash(Object... values) {
88+
return Arrays.hashCode(values);
89+
}
90+
91+
/**
92+
* Returns 0 for null or {@code o.hashCode()}.
93+
*/
94+
public static int hashCode(Object o) {
95+
return (o == null) ? 0 : o.hashCode();
96+
}
97+
98+
/**
99+
* Returns {@code o} if non-null, or throws {@code NullPointerException}.
100+
*/
101+
public static <T> T requireNonNull(T o) {
102+
if (o == null) {
103+
throw new NullPointerException();
104+
}
105+
return o;
106+
}
107+
108+
/**
109+
* Returns {@code o} if non-null, or throws {@code NullPointerException}
110+
* with the given detail message.
111+
*/
112+
public static <T> T requireNonNull(T o, String message) {
113+
if (o == null) {
114+
throw new NullPointerException(message);
115+
}
116+
return o;
117+
}
118+
119+
/**
120+
* Returns "null" for null or {@code o.toString()}.
121+
*/
122+
public static String toString(Object o) {
123+
return (o == null) ? "null" : o.toString();
124+
}
125+
126+
/**
127+
* Returns {@code nullString} for null or {@code o.toString()}.
128+
*/
129+
public static String toString(Object o, String nullString) {
130+
return (o == null) ? nullString : o.toString();
131+
}
132+
}

0 commit comments

Comments
 (0)