1 /*
2  * Copyright 2022 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 
21 /**
22  * Regression test for b/192562469 where the lint check does not handle annotation usage in lambdas.
23  */
24 @SuppressWarnings("unused")
25 public class RegressionTestJava192562469 {
26     @ExperimentalJavaAnnotation
27     interface ExperimentalInterface {
experimentalMethod()28         void experimentalMethod();
29     }
30 
31     /**
32      * Unsafe usage due to implementation of an experimental interface.
33      */
34     static class ConcreteExperimentalInterface implements ExperimentalInterface { // unsafe
35         @Override
experimentalMethod()36         public void experimentalMethod() {} // unsafe override
37     }
38 
39     /**
40      * Safe usage due to opt-in.
41      */
42     @OptIn(markerClass = ExperimentalJavaAnnotation.class)
43     static class ConcreteExperimentalInterfaceOptIn implements ExperimentalInterface {
44         @Override
experimentalMethod()45         public void experimentalMethod() {} // safe
46     }
47 
48     /**
49      * Safe usage due to propagation.
50      */
51     @ExperimentalJavaAnnotation
52     static class ConcreteExperimentalInterfacePropagate implements ExperimentalInterface {
53         @Override
experimentalMethod()54         public void experimentalMethod() {} // safe
55     }
56 
57     /**
58      * Unsafe implementations of an experimental interface.
59      */
regressionTestOverrides()60     void regressionTestOverrides() {
61         @SuppressWarnings("Convert2Lambda")
62         ExperimentalInterface anonymous = new ExperimentalInterface() { // unsafe
63             @Override
64             public void experimentalMethod() {} // unsafe override
65         };
66 
67         ExperimentalInterface lambda = () -> {}; // unsafe
68     }
69 
70     /**
71      * Safe implementations of an experimental interface due to opt-in.
72      */
73     @OptIn(markerClass = ExperimentalJavaAnnotation.class)
regressionTestOverridesOptIn()74     void regressionTestOverridesOptIn() {
75         @SuppressWarnings("Convert2Lambda")
76         ExperimentalInterface anonymous = new ExperimentalInterface() { // safe
77             @Override
78             public void experimentalMethod() {} // safe
79         };
80 
81         ExperimentalInterface lambda = () -> {}; // safe
82     }
83 
84     /**
85      * Safe implementations of an experimental interface due to propagation.
86      */
87     @ExperimentalJavaAnnotation
regressionTestOverridesPropagate()88     void regressionTestOverridesPropagate() {
89         @SuppressWarnings("Convert2Lambda")
90         ExperimentalInterface anonymous = new ExperimentalInterface() { // safe
91             @Override
92             public void experimentalMethod() {} // safe
93         };
94 
95         ExperimentalInterface lambda = () -> {}; // safe
96     }
97 }
98