xref: /aosp_15_r20/external/dagger2/java/dagger/internal/Preconditions.java (revision f585d8a307d0621d6060bd7e80091fdcbf94fe27)
1 /*
2  * Copyright (C) 2016 The Dagger Authors.
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 dagger.internal;
18 
19 
20 /**
21  * An adaptation of Guava's {@code com.google.common.base.Preconditions} that is specially tailored
22  * to support checks applied in Dagger's generated code.
23  */
24 public final class Preconditions {
25   /**
26    * Ensures that an object reference passed as a parameter to the calling method is not null.
27    *
28    * @param reference an object reference
29    * @return the non-null reference that was validated
30    * @throws NullPointerException if {@code reference} is null
31    */
checkNotNull(T reference)32   public static <T> T checkNotNull(T reference) {
33     if (reference == null) {
34       throw new NullPointerException();
35     }
36     return reference;
37   }
38 
39   /**
40    * Ensures that an object reference passed as a parameter to the calling method is not null.
41    *
42    * @param reference an object reference
43    * @param errorMessage the exception message to use if the check fails
44    * @return the non-null reference that was validated
45    * @throws NullPointerException if {@code reference} is null
46    */
checkNotNull(T reference, String errorMessage)47   public static <T> T checkNotNull(T reference, String errorMessage) {
48     if (reference == null) {
49       throw new NullPointerException(errorMessage);
50     }
51     return reference;
52   }
53 
54   /**
55    * Ensures that an object reference returned from a provides method is not null.
56    *
57    * @param reference an object reference
58    * @return the non-null reference that was validated
59    * @throws NullPointerException if {@code reference} is null
60    */
checkNotNullFromProvides(T reference)61   public static <T> T checkNotNullFromProvides(T reference) {
62     if (reference == null) {
63       throw new NullPointerException("Cannot return null from a non-@Nullable @Provides method");
64     }
65     return reference;
66   }
67 
68   /**
69    * Ensures that an object reference returned from a component method is not null.
70    *
71    * @param reference an object reference
72    * @return the non-null reference that was validated
73    * @throws NullPointerException if {@code reference} is null
74    */
checkNotNullFromComponent(T reference)75   public static <T> T checkNotNullFromComponent(T reference) {
76     if (reference == null) {
77       throw new NullPointerException("Cannot return null from a non-@Nullable component method");
78     }
79     return reference;
80   }
81 
82   /**
83    * Ensures that an object reference passed as a parameter to the calling method is not null.
84    *
85    * @param reference an object reference
86    * @param errorMessageTemplate a template for the exception message should the check fail. The
87    *     message is formed by replacing the single {@code %s} placeholder in the template with
88    *     {@code errorMessageArg}.
89    * @param errorMessageArg the argument to be substituted into the message template. Converted to a
90    *     string using {@link String#valueOf(Object)}, except for {@link Class} objects, which use
91    *     {@link Class#getCanonicalName()}.
92    * @return the non-null reference that was validated
93    * @throws NullPointerException if {@code reference} is null
94    * @throws IllegalArgumentException if {@code errorMessageTemplate} doesn't contain exactly one
95    *     "%s"
96    */
checkNotNull( T reference, String errorMessageTemplate, Object errorMessageArg)97   public static <T> T checkNotNull(
98       T reference, String errorMessageTemplate, Object errorMessageArg) {
99     if (reference == null) {
100       // Simple implementation of String.format, which is not GWT-compatible
101       if (!errorMessageTemplate.contains("%s")) {
102         throw new IllegalArgumentException("errorMessageTemplate has no format specifiers");
103       }
104       if (errorMessageTemplate.indexOf("%s") != errorMessageTemplate.lastIndexOf("%s")) {
105         throw new IllegalArgumentException(
106             "errorMessageTemplate has more than one format specifier");
107       }
108       String argString =
109           errorMessageArg instanceof Class
110               ? ((Class) errorMessageArg).getCanonicalName()
111               : String.valueOf(errorMessageArg);
112       throw new NullPointerException(errorMessageTemplate.replace("%s", argString));
113     }
114     return reference;
115   }
116 
117   /**
118    * Checks that the component builder field {@code requirement} has been initialized.
119    *
120    * @throws IllegalStateException if {@code requirement is null}
121    */
checkBuilderRequirement(T requirement, Class<T> clazz)122   public static <T> void checkBuilderRequirement(T requirement, Class<T> clazz) {
123     if (requirement == null) {
124       throw new IllegalStateException(clazz.getCanonicalName() + " must be set");
125     }
126   }
127 
Preconditions()128   private Preconditions() {}
129 }
130