Skip to content

Commit 507e786

Browse files
committed
Reland "Use Java 21 in libcore"
This reverts commit 72142d5. Reason for revert: Reland the feature once issues from postsubmit at resolved. Bug: 335612268 Change-Id: Iece5ec194320610f9df39fb200a7401080803473
1 parent 1b0d821 commit 507e786

File tree

9 files changed

+493
-1
lines changed

9 files changed

+493
-1
lines changed

JavaLibrary.bp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,8 @@ java_library {
243243
libs: [
244244
"conscrypt.module.intra.core.api",
245245
],
246+
247+
java_version: "21",
246248
}
247249

248250
platform_compat_config {
@@ -405,6 +407,8 @@ java_library {
405407
"-Xep:NullableOnContainingClass:WARN",
406408
],
407409
},
410+
411+
java_version: "21",
408412
}
409413

410414
// Java library for use on host, e.g. by robolectric or layoutlib.
@@ -706,6 +710,14 @@ filegroup {
706710
visibility: ["//libcore/luni/src/test/java17language"],
707711
}
708712

713+
// A filegroup that provides access to a source file for a toolchain test that
714+
// checks Java 21 language features are handled properly by JarJar.
715+
filegroup {
716+
name: "core-java-21-language-features-source",
717+
srcs: ["luni/src/main/java/libcore/internal/Java21LanguageFeatures.java"],
718+
visibility: ["//libcore/luni/src/test/java21language"],
719+
}
720+
709721
genrule {
710722
name: "core-tests-smali-dex",
711723
srcs: ["luni/src/test/java/**/*.smali"],
@@ -794,6 +806,7 @@ java_test {
794806
"core-java-9-language-tests",
795807
"core-java-11-language-tests",
796808
"core-java-17-language-tests",
809+
"core-java-21-language-tests",
797810
"core-test-rules",
798811
"core-tests-support",
799812
"junit-params",
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
/*
2+
* Copyright (C) 2024 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 libcore.internal;
18+
19+
import java.lang.reflect.Field;
20+
import java.lang.reflect.InvocationTargetException;
21+
import java.lang.reflect.Method;
22+
import java.util.List;
23+
import java.util.stream.Collectors;
24+
25+
/**
26+
* Proof of concept / fake code whose only purpose is to demonstrate that Java 21
27+
* language features are supported in libcore.
28+
*/
29+
public class Java21LanguageFeatures {
30+
31+
public static int calculateApproximateArea(Shape s) {
32+
// Language feature (Java 21): JEP-441 - Pattern Matching for switch
33+
// Matching by type
34+
return switch (s) {
35+
case Triangle t -> (t.base * t.height / 2);
36+
case Rectangle r -> (r.length * r.width);
37+
case Circle c -> (c.radius * c.radius * 3);
38+
default -> 0;
39+
};
40+
}
41+
42+
public static abstract class Shape {
43+
}
44+
45+
public static class Triangle extends Shape {
46+
public final int base;
47+
public final int height;
48+
49+
public Triangle(int base, int height) {
50+
this.base = base;
51+
this.height = height;
52+
}
53+
}
54+
55+
public static class Rectangle extends Shape {
56+
public final int length;
57+
public final int width;
58+
59+
public Rectangle(int length, int width) {
60+
this.length = length;
61+
this.width = width;
62+
}
63+
}
64+
65+
public static class Circle extends Shape {
66+
public final int radius;
67+
68+
public Circle(int radius) {
69+
this.radius = radius;
70+
}
71+
}
72+
73+
public static String isDroid(String s) {
74+
// Language feature (Java 21): JEP-441 - Pattern Matching for switch
75+
// Matching with null check
76+
return switch (s) {
77+
case null -> "404";
78+
case "Android", "Marvin" -> "Yes";
79+
default -> "No";
80+
};
81+
}
82+
83+
public static String isDroidIgnoreCase(String s) {
84+
// Language feature (Java 21): JEP-441 - Pattern Matching for switch
85+
// Matching with case refinement
86+
return switch (s) {
87+
case null -> "404";
88+
case String str when str.equalsIgnoreCase("android") -> "Yes";
89+
case String str when str.equalsIgnoreCase("marvin") -> "Yes";
90+
default -> "No";
91+
};
92+
}
93+
94+
public static boolean hasManySides(ShapeType t) {
95+
// Language feature (Java 21): JEP-441 - Pattern Matching for switch
96+
// Matching enum constants
97+
return switch (t) {
98+
case PointyShapeType.TRIANGLE -> false;
99+
case PointyShapeType.RECTANGLE -> true;
100+
case RoundedShapeType.CIRCLE -> false;
101+
};
102+
}
103+
104+
public sealed interface ShapeType permits PointyShapeType, RoundedShapeType {}
105+
public enum PointyShapeType implements ShapeType { TRIANGLE, RECTANGLE }
106+
public enum RoundedShapeType implements ShapeType { CIRCLE }
107+
108+
public static int getX(Object obj) {
109+
// Language feature (Java 21): JEP-440 - Record Patterns
110+
if (obj instanceof Point(int x, int y)) {
111+
return x;
112+
} else {
113+
return 0;
114+
}
115+
}
116+
117+
public static int getY(Object obj) {
118+
// Language feature (Java 21): JEP-440 - Record Patterns
119+
// Pattern match with type inference for components
120+
if (obj instanceof Point(var x, var y)) {
121+
return y;
122+
} else {
123+
return 0;
124+
}
125+
}
126+
127+
public static boolean isLineVertical(Object obj) {
128+
// Language feature (Java 21): JEP-440 - Record Patterns
129+
// Nested record patterns
130+
if (obj instanceof Line(Point(int xa, int ya),
131+
Point(int xb, int yb))) {
132+
return xa == xb;
133+
} else {
134+
return false;
135+
}
136+
}
137+
138+
public record Point(int x, int y) { }
139+
140+
public record Line(Point a, Point b) { }
141+
142+
public static boolean isFirstItemLarger(Pair pair) {
143+
// Language feature (Java 21): JEP-440 - Record Patterns
144+
// Switch and record patterns
145+
return switch (pair) {
146+
case Pair(PairableInt(int xa), PairableInt(int xb)) -> xa > xb;
147+
case Pair(PairableString a, Pairable b) -> false;
148+
case Pair(PairableInt a, PairableString b) -> false;
149+
};
150+
}
151+
152+
public sealed interface Pairable permits PairableInt, PairableString { }
153+
154+
public record PairableInt(int x) implements Pairable { }
155+
156+
public record PairableString(String s) implements Pairable { }
157+
158+
public record Pair(Pairable a, Pairable b) { }
159+
160+
public static int sumOfMembers(AnyPair<Integer, Integer> pair) {
161+
// Language feature (Java 21): JEP-440 - Record Patterns
162+
// Pattern match with type inference type arguments
163+
return switch (pair) {
164+
case AnyPair(var a, var b) -> a.intValue() + b.intValue();
165+
};
166+
}
167+
168+
public record AnyPair<S, T>(S a, T b) { }
169+
170+
171+
public sealed interface Blob permits SquishyBlob, ComplexBlob { }
172+
173+
public enum SquishyBlob implements Blob { SMALL, MEDIUM, LARGE }
174+
175+
public sealed interface ComplexBlob extends Blob permits MultiBlob, GeometricBlob { }
176+
177+
public enum Color { RED, GREEN, BLUE }
178+
179+
public record MultiBlob(ShapeType type, Color color, Integer count) implements ComplexBlob { }
180+
181+
public record GeometricBlob(int sides) implements ComplexBlob { }
182+
183+
public static Object getMainCharacteristic(Blob blob) {
184+
return switch (blob) {
185+
case SquishyBlob squishy -> squishy;
186+
case MultiBlob (RoundedShapeType type, var color, var count)
187+
when type == RoundedShapeType.CIRCLE -> RoundedShapeType.CIRCLE;
188+
case MultiBlob (var type, Color color, var count)
189+
when color == Color.RED -> Color.RED;
190+
case MultiBlob (var type, var color, var count) -> count;
191+
case GeometricBlob (int sides) when sides > 1000 -> Integer.valueOf(1000);
192+
case GeometricBlob (int sides) -> Integer.valueOf(sides);
193+
};
194+
}
195+
}

luni/src/test/java17language/Android.bp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ java_library {
4141

4242
// Generate a clone of Java17LanguageFeaturesTest which uses a version of
4343
// Java17LanguageFeatures repackaged by jarjar. This ensures that jarjar is able
44-
// to handle a class file which must be at least v55 and includes bytecode
44+
// to handle a class file which must be at least v61 and includes bytecode
4545
// compiled from Java 17 language features.
4646
filegroup {
4747
name: "core-rewrite-java-17-test-for-jarjar-sed-script",
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Copyright (C) 2024 The Android Open Source Project
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// Android tests related to Java 21 language features.
16+
17+
// Use jarjar to repackage Java21LanguageFeatures, to be used in tests below.
18+
package {
19+
default_team: "trendy_team_java_core_libraries",
20+
// http://go/android-license-faq
21+
// A large-scale-change added 'default_applicable_licenses' to import
22+
// the below license kinds from "libcore_luni_license":
23+
// SPDX-license-identifier-Apache-2.0
24+
default_applicable_licenses: ["libcore_luni_license"],
25+
}
26+
27+
java_library {
28+
name: "core-java-21-language-features-repackaged-for-test",
29+
hostdex: true,
30+
31+
srcs: [":core-java-21-language-features-source"],
32+
jarjar_rules: "jarjar_rules_java21_language_features.txt",
33+
java_version: "21",
34+
35+
sdk_version: "none",
36+
system_modules: "core-all-system-modules",
37+
patch_module: "java.base",
38+
39+
visibility: ["//visibility:private"],
40+
}
41+
42+
// Generate a clone of Java21LanguageFeaturesTest which uses a version of
43+
// Java21LanguageFeatures repackaged by jarjar. This ensures that jarjar is able
44+
// to handle a class file which must be at least v65 and includes bytecode
45+
// compiled from Java 21 language features.
46+
filegroup {
47+
name: "core-rewrite-java-21-test-for-jarjar-sed-script",
48+
srcs: ["rewrite-test-for-jarjar.sed"],
49+
visibility: ["//visibility:private"],
50+
}
51+
52+
filegroup {
53+
name: "core-java-21-language-features-test-src",
54+
srcs: ["java/libcore/libcore/internal/Java21LanguageFeaturesTest.java"],
55+
visibility: ["//visibility:private"],
56+
}
57+
58+
genrule {
59+
name: "core-gen-test-repackaged-java-21-language-features",
60+
srcs: [
61+
":core-rewrite-java-21-test-for-jarjar-sed-script",
62+
":core-java-21-language-features-test-src",
63+
],
64+
out: ["libcore/libcore/internal/Java21LanguageFeaturesJarjarTest.java"],
65+
cmd: "sed -r -f $(location :core-rewrite-java-21-test-for-jarjar-sed-script) $(location :core-java-21-language-features-test-src) > $(out)",
66+
visibility: ["//visibility:private"],
67+
}
68+
69+
java_library {
70+
name: "core-java-21-language-tests",
71+
hostdex: true,
72+
srcs: [
73+
"java/**/*.java",
74+
":core-gen-test-repackaged-java-21-language-features",
75+
],
76+
sdk_version: "none",
77+
system_modules: "core-all-system-modules",
78+
static_libs: [
79+
"core-java-21-language-features-repackaged-for-test",
80+
"junit",
81+
],
82+
visibility: ["//libcore"],
83+
java_version: "21",
84+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rule libcore.internal.** libcore.internal.repackaged.@1

0 commit comments

Comments
 (0)