blob: f5e936417a264ccdc38f787835cbe13875c4c376 [file] [log] [blame]
dpbd8d950a2016-08-30 09:24:37 -07001/*
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
17package dagger.internal.codegen;
18
19import static com.google.auto.common.MoreElements.isAnnotationPresent;
20import static com.google.common.base.Preconditions.checkArgument;
21
22import com.google.auto.value.AutoValue;
dpbd8d950a2016-08-30 09:24:37 -070023import dagger.BindsOptionalOf;
ronshapiroa6f99ed2017-12-12 09:26:26 -080024import dagger.model.Key;
dpbffd98f62016-12-20 10:05:16 -080025import java.util.Optional;
ronshapiroed0d8522018-01-04 12:01:06 -080026import javax.inject.Inject;
dpbd8d950a2016-08-30 09:24:37 -070027import javax.lang.model.element.Element;
28import javax.lang.model.element.ExecutableElement;
29import javax.lang.model.element.TypeElement;
30
31/** A {@link BindsOptionalOf} declaration. */
32@AutoValue
33abstract class OptionalBindingDeclaration extends BindingDeclaration {
34
35 /**
36 * {@inheritDoc}
37 *
38 * <p>The key's type is the method's return type, even though the synthetic bindings will be for
39 * {@code Optional} of derived types.
40 */
41 @Override
42 public abstract Key key();
43
44 static class Factory {
ronshapiroa6416fc2017-11-17 10:08:34 -080045 private final KeyFactory keyFactory;
dpbd8d950a2016-08-30 09:24:37 -070046
ronshapiroed0d8522018-01-04 12:01:06 -080047 @Inject
ronshapiroa6416fc2017-11-17 10:08:34 -080048 Factory(KeyFactory keyFactory) {
dpbd8d950a2016-08-30 09:24:37 -070049 this.keyFactory = keyFactory;
50 }
51
52 OptionalBindingDeclaration forMethod(ExecutableElement method, TypeElement contributingModule) {
53 checkArgument(isAnnotationPresent(method, BindsOptionalOf.class));
54 return new AutoValue_OptionalBindingDeclaration(
55 Optional.<Element>of(method),
56 Optional.of(contributingModule),
57 keyFactory.forBindsOptionalOfMethod(method, contributingModule));
58 }
59 }
60}