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 11
27  * language features are supported in libcore.
28  */
29 public class Java11LanguageFeatures {
30 
collectUpperCaseStrings(List<String> list)31     public static String collectUpperCaseStrings(List<String> list) {
32         // Language feature: local-variable syntax for lambda
33         return list.stream()
34             .map((var item) -> item.toUpperCase())
35             .collect(Collectors.joining(" "));
36     }
37 
guessTheString(String guess)38     public static String guessTheString(String guess) {
39         // Language feature (Java 10): local-variable type inference
40         var answer = "The answer to the universe, life and everything";
41         if (!(answer instanceof String)) {
42             return null;
43         }
44         if (!guess.equals(answer)) {
45             return "";
46         }
47         return answer;
48     }
49 
50     public static class Person {
51         // Language feature: Reflection on private fields/methods of nest-mates
52         // Note: This at the moment is not supported yet in Android and calling these methods will
53         // result in exceptions (b/210843415).
54         private final String name;
55         private final Vocabulary vocabulary = new Vocabulary();
56 
Person(String name)57         public Person(String name) {
58             this.name = name;
59         }
60 
greet()61         public String greet() throws NoSuchMethodException, InvocationTargetException,
62                NoSuchFieldException, IllegalAccessException {
63             Method greetFn = Vocabulary.class.getDeclaredMethod("greet");
64             return (String) greetFn.invoke(vocabulary);
65         }
66 
67         private class Vocabulary {
68 
greet()69             private String greet() throws NoSuchFieldException, IllegalAccessException {
70                 Field nameField = Person.class.getDeclaredField("name");
71                 String nameValue = (String) nameField.get(Person.this);
72                 return "Hello " + nameValue;
73             }
74         }
75     }
76 }
77