1 /* 2 * Copyright 2021 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 /** 22 * Tests for calls made to members on an experimental class. 23 */ 24 @SuppressWarnings({"unused", "WeakerAccess"}) 25 class UseJavaExperimentalClassFromJava { 26 27 /** 28 * Unsafe call into a field on an experimental class. 29 */ unsafeExperimentalClassField()30 int unsafeExperimentalClassField() { 31 AnnotatedJavaClass experimentalObject = new AnnotatedJavaClass(); 32 return experimentalObject.field; 33 } 34 35 /** 36 * Unsafe call into a method on an experimental class. 37 */ unsafeExperimentalClassMethod()38 int unsafeExperimentalClassMethod() { 39 AnnotatedJavaClass experimentalObject = new AnnotatedJavaClass(); 40 return experimentalObject.method(); 41 } 42 43 /** 44 * Unsafe call into a static field on an experimental class. 45 */ unsafeExperimentalClassStaticField()46 int unsafeExperimentalClassStaticField() { 47 return AnnotatedJavaClass.FIELD_STATIC; 48 } 49 50 /** 51 * Unsafe call into a static method on an experimental class. 52 */ unsafeExperimentalClassStaticMethod()53 int unsafeExperimentalClassStaticMethod() { 54 return AnnotatedJavaClass.methodStatic(); 55 } 56 57 /** 58 * Safe call due to propagation of experimental annotation. 59 */ 60 @ExperimentalJavaAnnotation safePropagateMarker()61 int safePropagateMarker() { 62 AnnotatedJavaClass experimentalObject = new AnnotatedJavaClass(); 63 return experimentalObject.method(); 64 } 65 66 /** 67 * Safe call due to opting in to experimental annotation. 68 */ 69 @OptIn(markerClass = ExperimentalJavaAnnotation.class) safeOptInMarker()70 int safeOptInMarker() { 71 AnnotatedJavaClass experimentalObject = new AnnotatedJavaClass(); 72 return experimentalObject.method(); 73 } 74 } 75