1 /*
2  * Copyright 2019 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.kotlin;
18 
19 import kotlin.OptIn;
20 import sample.kotlin.foo.AnnotatedJavaPackage;
21 
22 /**
23  * Tests for calls made on classes within an experimental package.
24  */
25 @SuppressWarnings("unused")
26 class UseJavaPackageFromJava {
27 
28     /**
29      * Unsafe call into a method on a class within an experimental package.
30      */
unsafeMethodInExperimentalPackage()31     void unsafeMethodInExperimentalPackage() {
32         AnnotatedJavaPackage experimentalObject = new AnnotatedJavaPackage();
33         experimentalObject.method();
34     }
35 
36     /**
37      * Safe call due to propagation of experimental marker.
38      */
39     @ExperimentalJavaAnnotation
safePropagateMarker()40     void safePropagateMarker() {
41         AnnotatedJavaPackage experimentalObject = new AnnotatedJavaPackage();
42         experimentalObject.method();
43     }
44 
45     /**
46      * Safe call due to opt-in to experimental marker.
47      */
48     @OptIn(markerClass = ExperimentalJavaAnnotation.class)
safeOptInMarker()49     void safeOptInMarker() {
50         AnnotatedJavaPackage experimentalObject = new AnnotatedJavaPackage();
51         experimentalObject.method();
52     }
53 
54     /**
55      * Unsafe call into a method with an unsafe call. This should not be flagged, as the
56      * called method itself is not experimental.
57      */
unsafeSelfExperimental()58     void unsafeSelfExperimental() {
59         unsafeMethodInExperimentalPackage();
60     }
61 
62     /**
63      * Unsafe call into an experimental method within this class.
64      */
unsafeSelfPropagateMarker()65     void unsafeSelfPropagateMarker() {
66         safePropagateMarker();
67     }
68 
69     /**
70      * Safe call into an opted-in method within this class.
71      */
safeSelfOptInMarker()72     void safeSelfOptInMarker() {
73         safeOptInMarker();
74     }
75 }
76