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