xref: /aosp_15_r20/tools/metalava/metalava/src/test/java/com/android/tools/metalava/Java9LanguageFeaturesTest.kt (revision 115816f9299ab6ddd6b9673b81f34e707f6bacab)
1 /*
2  * Copyright (C) 2018 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 @file:Suppress("ALL")
18 
19 package com.android.tools.metalava
20 
21 import com.android.tools.metalava.model.text.FileFormat
22 import com.android.tools.metalava.testing.java
23 import org.junit.Test
24 
25 class Java9LanguageFeaturesTest : DriverTest() {
26     @Test
Private Interface Methodnull27     fun `Private Interface Method`() {
28         // Basic class; also checks that default constructor is made explicit
29         check(
30             format = FileFormat.V2,
31             checkCompilation = false, // Not compiling with JDK 9 yet
32             sourceFiles =
33                 arrayOf(
34                     java(
35                         """
36                     package test.pkg;
37 
38                     public interface Person {
39                         String name();
40                         private String reverse(String s) {
41                             return new StringBuilder(s).reverse().toString();
42                         }
43                     }
44                     """
45                     )
46                 ),
47             api =
48                 """
49                 package test.pkg {
50                   public interface Person {
51                     method public String name();
52                   }
53                 }
54                 """,
55             extraArguments = arrayOf(ARG_JAVA_SOURCE, "1.9")
56         )
57     }
58 
59     @Test
Basic class signature extractionnull60     fun `Basic class signature extraction`() {
61         // Basic class; also checks that default constructor is made explicit
62         check(
63             format = FileFormat.V2,
64             checkCompilation = false, // Not compiling with JDK 9 yet
65             sourceFiles =
66                 arrayOf(
67                     java(
68                         """
69                     package libcore.internal;
70 
71                     import java.io.ByteArrayInputStream;
72                     import java.io.ByteArrayOutputStream;
73                     import java.io.IOException;
74                     import java.io.InputStream;
75                     import java.util.ArrayList;
76                     import java.util.Arrays;
77                     import java.util.List;
78                     import java.util.Objects;
79                     import java.util.concurrent.atomic.AtomicReference;
80 
81                     public class Java9LanguageFeatures {
82 
83                         public interface Person {
84                             String name();
85 
86                             default boolean isPalindrome() {
87                                 return name().equals(reverse(name()));
88                             }
89 
90                             default boolean isPalindromeIgnoreCase() {
91                                 return name().equalsIgnoreCase(reverse(name()));
92                             }
93 
94                             // Language feature: private interface method
95                             private String reverse(String s) {
96                                 return new StringBuilder(s).reverse().toString();
97                             }
98                         }
99 
100                         @SafeVarargs
101                         public static<T> String toListString(T... values) {
102                             return toString(values).toString();
103                         }
104 
105                         // Language feature: @SafeVarargs on private methods
106                         @SafeVarargs
107                         private static<T> List<String> toString(T... values) {
108                             List<String> result = new ArrayList<>();
109                             for (T value : values) {
110                                 result.add(value.toString());
111                             }
112                             return result;
113                         }
114 
115                         public <T> AtomicReference<T> createReference(T content) {
116                             // Language feature: <> on anonymous class
117                             //noinspection unchecked
118                             return new AtomicReference<>(content) { };
119                         }
120 
121                         public static byte[] copy(byte[] bytes) throws IOException {
122                             ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
123                             InputStream inputStream = new ByteArrayInputStream(bytes);
124                             try (inputStream) { // Language feature: try on effectively-final variable
125                                 int value;
126                                 while ((value = inputStream.read()) != -1) {
127                                     byteArrayOutputStream.write(value);
128                                 }
129                             }
130                             return byteArrayOutputStream.toByteArray();
131                         }
132                     }
133                     """
134                     )
135                 ),
136             api =
137                 """
138                 package libcore.internal {
139                   public class Java9LanguageFeatures {
140                     ctor public Java9LanguageFeatures();
141                     method public static byte[] copy(byte[]) throws java.io.IOException;
142                     method public <T> java.util.concurrent.atomic.AtomicReference<T> createReference(T);
143                     method @java.lang.SafeVarargs public static <T> String toListString(T...);
144                   }
145                   public static interface Java9LanguageFeatures.Person {
146                     method public default boolean isPalindrome();
147                     method public default boolean isPalindromeIgnoreCase();
148                     method public String name();
149                   }
150                 }
151                 """,
152             extraArguments = arrayOf(ARG_JAVA_SOURCE, "1.9")
153         )
154     }
155 
156     @Test
Using JDK APIsnull157     fun `Using JDK APIs`() {
158         // Non-Android example
159         val jdk = System.getProperty("java.home") ?: error("Expected java.home to be set")
160         check(
161             format = FileFormat.V2,
162             sourceFiles =
163                 arrayOf(
164                     java(
165                         """
166                     package test.pkg;
167                     import javax.swing.JButton;
168                     public class SwingTest extends JButton {
169                         public JButton button;
170                     }
171                     """
172                     )
173                 ),
174             api =
175                 """
176                 package test.pkg {
177                   public class SwingTest extends javax.swing.JButton {
178                     ctor public SwingTest();
179                     field public javax.swing.JButton button;
180                   }
181                 }
182                 """,
183             extraArguments = arrayOf(ARG_JDK_HOME, jdk)
184         )
185     }
186 }
187