• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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.codegen.binding;
18 
19 import static androidx.room.compiler.processing.XElementKt.isMethodParameter;
20 import static androidx.room.compiler.processing.XElementKt.isTypeElement;
21 import static dagger.internal.codegen.base.DiagnosticFormatting.stripCommonTypePrefixes;
22 import static dagger.internal.codegen.base.ElementFormatter.elementToString;
23 import static dagger.internal.codegen.xprocessing.XElements.asExecutable;
24 import static dagger.internal.codegen.xprocessing.XElements.asTypeElement;
25 import static dagger.internal.codegen.xprocessing.XElements.isExecutable;
26 
27 import androidx.room.compiler.processing.XElement;
28 import androidx.room.compiler.processing.XTypeElement;
29 import com.google.common.collect.ImmutableList;
30 import dagger.internal.codegen.base.Formatter;
31 import javax.inject.Inject;
32 
33 /**
34  * Formats a {@link BindingDeclaration} into a {@link String} suitable for use in error messages.
35  */
36 public final class BindingDeclarationFormatter extends Formatter<BindingDeclaration> {
37   private final MethodSignatureFormatter methodSignatureFormatter;
38 
39   @Inject
BindingDeclarationFormatter(MethodSignatureFormatter methodSignatureFormatter)40   BindingDeclarationFormatter(MethodSignatureFormatter methodSignatureFormatter) {
41     this.methodSignatureFormatter = methodSignatureFormatter;
42   }
43 
44   /**
45    * Returns {@code true} for declarations that this formatter can format. Specifically bindings
46    * from subcomponent declarations or those with {@linkplain BindingDeclaration#bindingElement()
47    * binding elements} that are methods, constructors, or types.
48    */
canFormat(BindingDeclaration bindingDeclaration)49   public boolean canFormat(BindingDeclaration bindingDeclaration) {
50     if (bindingDeclaration instanceof SubcomponentDeclaration) {
51       return true;
52     }
53     if (bindingDeclaration.bindingElement().isPresent()) {
54       XElement bindingElement = bindingDeclaration.bindingElement().get();
55       return isMethodParameter(bindingElement)
56           || isTypeElement(bindingElement)
57           || isExecutable(bindingElement);
58     }
59     // TODO(dpb): validate whether what this is doing is correct
60     return false;
61   }
62 
63   @Override
format(BindingDeclaration bindingDeclaration)64   public String format(BindingDeclaration bindingDeclaration) {
65     if (bindingDeclaration instanceof SubcomponentDeclaration) {
66       return formatSubcomponentDeclaration((SubcomponentDeclaration) bindingDeclaration);
67     }
68 
69     if (bindingDeclaration.bindingElement().isPresent()) {
70       XElement bindingElement = bindingDeclaration.bindingElement().get();
71       if (isMethodParameter(bindingElement)) {
72         return elementToString(bindingElement);
73       } else if (isTypeElement(bindingElement)) {
74         return stripCommonTypePrefixes(asTypeElement(bindingElement).getType().toString());
75       } else if (isExecutable(bindingElement)) {
76         return methodSignatureFormatter.format(
77             asExecutable(bindingElement),
78             bindingDeclaration.contributingModule().map(XTypeElement::getType));
79       }
80       throw new IllegalArgumentException("Formatting unsupported for element: " + bindingElement);
81     }
82 
83     return String.format(
84         "Dagger-generated binding for %s",
85         stripCommonTypePrefixes(bindingDeclaration.key().toString()));
86   }
87 
formatSubcomponentDeclaration(SubcomponentDeclaration subcomponentDeclaration)88   private String formatSubcomponentDeclaration(SubcomponentDeclaration subcomponentDeclaration) {
89     ImmutableList<XTypeElement> moduleSubcomponents =
90         subcomponentDeclaration.moduleAnnotation().subcomponents();
91     int index = moduleSubcomponents.indexOf(subcomponentDeclaration.subcomponentType());
92     StringBuilder annotationValue = new StringBuilder();
93     if (moduleSubcomponents.size() != 1) {
94       annotationValue.append("{");
95     }
96     annotationValue.append(
97         formatArgumentInList(
98             index,
99             moduleSubcomponents.size(),
100             subcomponentDeclaration.subcomponentType().getQualifiedName() + ".class"));
101     if (moduleSubcomponents.size() != 1) {
102       annotationValue.append("}");
103     }
104 
105     return String.format(
106         "@%s(subcomponents = %s) for %s",
107         subcomponentDeclaration.moduleAnnotation().simpleName(),
108         annotationValue,
109         subcomponentDeclaration.contributingModule().get());
110   }
111 }
112