1 /*
2  * Copyright (C) 2023 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 com.android.net.module.util;
18 
19 import android.annotation.Nullable;
20 
21 /**
22  * Utilities to deal with multiple SDKs in a single mainline module.
23  * @hide
24  */
25 public class SdkUtil {
26     /**
27      * Holder class taking advantage of erasure to avoid reflection running into class not found
28      * exceptions.
29      *
30      * This is useful to store a reference to a class that might not be present at runtime when
31      * fields are examined through reflection. An example is the MessageUtils class, which tries
32      * to get all fields in a class and therefore will try to load any class for which there
33      * is a member. Another example would be arguments or return values of methods in tests,
34      * when the testing framework uses reflection to list methods and their arguments.
35      *
36      * In these cases, LateSdk<T> can be used to hide type T from reflection, since it's erased
37      * and it becomes a vanilla LateSdk in Java bytecode. The T still can't be instantiated at
38      * runtime of course, but runtime tests will avoid that.
39      *
40      * @param <T> The held type
41      * @hide
42      */
43     public static class LateSdk<T> {
44         @Nullable public final T value;
LateSdk(@ullable final T value)45         public LateSdk(@Nullable final T value) {
46             this.value = value;
47         }
48     }
49 }
50