1 /*
2  * Copyright (C) 2021 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 17
27  * language features are supported in libcore.
28  */
29 public class Java17LanguageFeatures {
30 
getTextBlock()31     public static String getTextBlock() {
32         // Language feature (Java 15): JEP-378 - Text blocks
33         return """
34             This is a
35             multiline
36             string.""";
37     }
38 
calculateApproximateArea(Shape s)39     public static int calculateApproximateArea(Shape s) {
40         // Language feature (Java 16): JEP-394 - Pattern matching instanceof
41         if (s instanceof Triangle t) {
42             return (t.base * t.height / 2);
43         } else if (s instanceof Rectangle r) {
44             return (r.length * r.width);
45         } else if (s instanceof Circle c) {
46             return (c.radius * c.radius * 3);
47         } else {
48             return 0;
49         }
50     }
51 
52     public static abstract class Shape {
53     }
54 
55     public static class Triangle extends Shape {
56         public final int base;
57         public final int height;
58 
Triangle(int base, int height)59         public Triangle(int base, int height) {
60             this.base = base;
61             this.height = height;
62         }
63     }
64 
65     public static class Rectangle extends Shape {
66         public final int length;
67         public final int width;
68 
Rectangle(int length, int width)69         public Rectangle(int length, int width) {
70             this.length = length;
71             this.width = width;
72         }
73     }
74 
75     public static class Circle extends Shape {
76         public final int radius;
77 
Circle(int radius)78         public Circle(int radius) {
79             this.radius = radius;
80         }
81     }
82 
buildPoint(int x, int y)83     public static Point buildPoint(int x, int y) {
84         // Language feature (Java 16): JEP-395 - Records
85         return new Point(x, y);
86     }
87 
Point(int x, int y)88     public record Point(int x, int y) { }
89 
getSealedClassId(BaseSealedClass obj)90     public static int getSealedClassId(BaseSealedClass obj) {
91         // Language feature (Java 17): JEP-409 - Sealed classes
92         return obj.getId();
93     }
94 
95     public static sealed class BaseSealedClass permits FinalDerivedClass, NonSealedDerivedClass {
getId()96         public int getId() {
97             return 0;
98         }
99     }
100 
101     public static final class FinalDerivedClass extends BaseSealedClass {
102         @Override
getId()103         public int getId() {
104             return 1;
105         }
106     }
107 
108     public static non-sealed class NonSealedDerivedClass extends BaseSealedClass {
109         @Override
getId()110         public int getId() {
111             return 2;
112         }
113     }
114 }
115