forked from square/dagger
-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathMethodSignatureFormatter.java
More file actions
189 lines (173 loc) · 7.83 KB
/
MethodSignatureFormatter.java
File metadata and controls
189 lines (173 loc) · 7.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* Copyright (C) 2014 The Dagger Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dagger.internal.codegen.binding;
import static androidx.room3.compiler.processing.compat.XConverters.getProcessingEnv;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static dagger.internal.codegen.base.DiagnosticFormatting.stripCommonTypePrefixes;
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableList;
import static dagger.internal.codegen.xprocessing.XAnnotations.asClassName;
import static dagger.internal.codegen.xprocessing.XElements.closestEnclosingTypeElement;
import static dagger.internal.codegen.xprocessing.XElements.getSimpleName;
import static dagger.internal.codegen.xprocessing.XTypes.isDeclared;
import androidx.room3.compiler.codegen.XClassName;
import androidx.room3.compiler.processing.XAnnotation;
import androidx.room3.compiler.processing.XExecutableElement;
import androidx.room3.compiler.processing.XExecutableParameterElement;
import androidx.room3.compiler.processing.XExecutableType;
import androidx.room3.compiler.processing.XMethodElement;
import androidx.room3.compiler.processing.XMethodType;
import androidx.room3.compiler.processing.XType;
import androidx.room3.compiler.processing.XTypeElement;
import androidx.room3.compiler.processing.XVariableElement;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Streams;
import dagger.internal.codegen.base.Formatter;
import dagger.internal.codegen.xprocessing.Nullability;
import dagger.internal.codegen.xprocessing.XAnnotations;
import dagger.internal.codegen.xprocessing.XTypes;
import java.util.Iterator;
import java.util.Optional;
import javax.inject.Inject;
/** Formats the signature of an {@link XExecutableElement} suitable for use in error messages. */
public final class MethodSignatureFormatter extends Formatter<XExecutableElement> {
private static final XClassName JET_BRAINS_NOT_NULL =
XClassName.get("org.jetbrains.annotations", "NotNull");
private static final XClassName JET_BRAINS_NULLABLE =
XClassName.get("org.jetbrains.annotations", "Nullable");
private final InjectionAnnotations injectionAnnotations;
@Inject
MethodSignatureFormatter(InjectionAnnotations injectionAnnotations) {
this.injectionAnnotations = injectionAnnotations;
}
/**
* A formatter that uses the type where the method is declared for the annotations and name of the
* method, but the method's resolved type as a member of {@code type} for the key.
*/
public Formatter<XMethodElement> typedFormatter(XType type) {
checkArgument(isDeclared(type));
return new Formatter<XMethodElement>() {
@Override
public String format(XMethodElement method) {
return MethodSignatureFormatter.this.format(
method,
method.asMemberOf(type),
closestEnclosingTypeElement(method),
/* includeReturnType= */ true);
}
};
}
@Override
public String format(XExecutableElement method) {
return format(method, Optional.empty());
}
/**
* Formats an ExecutableElement as if it were contained within the container, if the container is
* present.
*/
public String format(XExecutableElement method, Optional<XType> container) {
return format(method, container, /* includeReturnType= */ true);
}
private String format(
XExecutableElement method, Optional<XType> container, boolean includeReturnType) {
return container.isPresent() && !getSimpleName(method).contentEquals("<init>")
? format(
method,
method.asMemberOf(container.get()),
container.get().getTypeElement(),
includeReturnType)
: format(
method,
method.getExecutableType(),
closestEnclosingTypeElement(method),
includeReturnType);
}
private String format(
XExecutableElement method,
XExecutableType methodType,
XTypeElement container,
boolean includeReturnType) {
StringBuilder builder = new StringBuilder();
ImmutableList<String> formattedAnnotations = formatedAnnotations(method);
if (!formattedAnnotations.isEmpty()) {
builder.append(String.join(" ", formattedAnnotations)).append(" ");
}
if (getSimpleName(method).contentEquals("<init>")) {
builder.append(container.getQualifiedName());
} else {
if (includeReturnType) {
builder.append(nameOfType(((XMethodType) methodType).getReturnType())).append(' ');
}
builder.append(container.getQualifiedName()).append('.').append(getSimpleName(method));
}
builder.append('(');
checkState(method.getParameters().size() == methodType.getParameterTypes().size());
Iterator<XExecutableParameterElement> parameters = method.getParameters().iterator();
Iterator<XType> parameterTypes = methodType.getParameterTypes().iterator();
for (int i = 0; parameters.hasNext(); i++) {
if (i > 0) {
builder.append(", ");
}
appendParameter(builder, parameters.next(), parameterTypes.next());
}
builder.append(')');
return builder.toString();
}
public String formatWithoutReturnType(XExecutableElement method) {
return format(method, Optional.empty(), /* includeReturnType= */ false);
}
private void appendParameter(
StringBuilder builder, XVariableElement parameter, XType parameterType) {
injectionAnnotations
.getQualifier(parameter)
.ifPresent(qualifier -> builder.append(formatAnnotation(qualifier)).append(' '));
builder.append(nameOfType(parameterType));
}
private static String nameOfType(XType type) {
return stripCommonTypePrefixes(XTypes.toStableString(type));
}
private static ImmutableList<String> formatedAnnotations(XExecutableElement executableElement) {
Nullability nullability = Nullability.of(executableElement);
ImmutableList<String> formattedAnnotations =
Streams.concat(
executableElement.getAllAnnotations().stream()
// Filter out @NotNull annotations added by KAPT to make error messages
// consistent
.filter(annotation -> !asClassName(annotation).equals(JET_BRAINS_NOT_NULL))
.map(MethodSignatureFormatter::formatAnnotation),
nullability.nullableAnnotations().stream()
// Filter out @NotNull annotations added by KAPT to make error messages
// consistent
.filter(annotation -> !annotation.equals(JET_BRAINS_NOT_NULL))
.map(annotation -> String.format("@%s", annotation.getCanonicalName())))
.distinct()
.collect(toImmutableList());
if (nullability.isKotlinTypeNullable()
&& nullability.nullableAnnotations().stream()
.noneMatch(JET_BRAINS_NULLABLE::equals)
&& getProcessingEnv(executableElement).findTypeElement(JET_BRAINS_NULLABLE) != null) {
formattedAnnotations =
ImmutableList.<String>builder()
.addAll(formattedAnnotations)
.add(String.format("@%s", JET_BRAINS_NULLABLE.getCanonicalName()))
.build();
}
return formattedAnnotations;
}
private static String formatAnnotation(XAnnotation annotation) {
return stripCommonTypePrefixes(XAnnotations.toString(annotation));
}
}