1 /*
2  * Copyright 2020 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 sample.experimental;
18 
19 @SuppressWarnings({"unused", "WeakerAccess"})
20 class UseKtExperimentalFromJava {
21     /**
22      * Unsafe call into an experimental class.
23      */
getDateUnsafe()24     int getDateUnsafe() {
25         sample.experimental.DateProviderKt dateProvider = new sample.experimental.DateProviderKt();
26         return dateProvider.getDate();
27     }
28 
29     @ExperimentalDateTimeKt
getDateExperimental()30     int getDateExperimental() {
31         sample.experimental.DateProviderKt dateProvider = new sample.experimental.DateProviderKt();
32         return dateProvider.getDate();
33     }
34 
35     @SuppressWarnings("deprecation")
36     @androidx.annotation.experimental.UseExperimental(markerClass = ExperimentalDateTimeKt.class)
getDateUseExperimental()37     int getDateUseExperimental() {
38         sample.experimental.DateProviderKt dateProvider = new sample.experimental.DateProviderKt();
39         return dateProvider.getDate();
40     }
41 
displayDate()42     void displayDate() {
43         System.out.println("" + getDateUnsafe());
44     }
45 
46     // Tests involving multiple experimental markers.
47 
48     /**
49      * Unsafe call into an experimental class.
50      */
51     @ExperimentalDateTimeKt
getDateExperimentalLocationUnsafe()52     int getDateExperimentalLocationUnsafe() {
53         sample.experimental.DateProviderKt dateProvider = new sample.experimental.DateProviderKt();
54         LocationProviderKt locationProvider = new LocationProviderKt();
55         return dateProvider.getDate() + locationProvider.getLocation();
56     }
57 
58     @ExperimentalDateTimeKt
59     @ExperimentalLocationKt
getDateAndLocationExperimental()60     int getDateAndLocationExperimental() {
61         sample.experimental.DateProviderKt dateProvider = new sample.experimental.DateProviderKt();
62         LocationProviderKt locationProvider = new LocationProviderKt();
63         return dateProvider.getDate() + locationProvider.getLocation();
64     }
65 
66     @SuppressWarnings("deprecation")
67     @androidx.annotation.experimental.UseExperimental(markerClass = ExperimentalDateTimeKt.class)
68     @ExperimentalLocationKt
getDateUseExperimentalLocationExperimental()69     int getDateUseExperimentalLocationExperimental() {
70         sample.experimental.DateProviderKt dateProvider = new sample.experimental.DateProviderKt();
71         LocationProviderKt locationProvider = new LocationProviderKt();
72         return dateProvider.getDate() + locationProvider.getLocation();
73     }
74 
75     @SuppressWarnings("deprecation")
76     @androidx.annotation.experimental.UseExperimental(markerClass = {ExperimentalDateTimeKt.class,
77             ExperimentalLocationKt.class})
getDateAndLocationUseExperimental()78     int getDateAndLocationUseExperimental() {
79         sample.experimental.DateProviderKt dateProvider = new DateProviderKt();
80         LocationProviderKt locationProvider = new LocationProviderKt();
81         return dateProvider.getDate() + locationProvider.getLocation();
82     }
83 
84     /**
85      * Regression test for issue reported in b/140637106, which passes here but fails in Studio.
86      */
regressionTestStaticUsage()87     void regressionTestStaticUsage() {
88         TimeProviderKt.getTimeStatically();
89         TimeProviderKt.Companion.getTimeStatically();
90     }
91 
92     /**
93      * Regression test for issue reported in b/140637106, which passes here but fails in Studio.
94      */
regressionTestInlineUsage()95     void regressionTestInlineUsage() {
96         new TimeProviderKt().getTime();
97         new TimeProviderKt().getTimeJava();
98     }
99 
100     @SuppressWarnings("deprecation")
101     @androidx.annotation.experimental.UseExperimental(markerClass = ExperimentalDateTimeKt.class)
102     static class FancyDateProvider extends DateProviderKt {}
103 }
104